From 98e05ab7d903fbbef967871b80c1de7979094d67 Mon Sep 17 00:00:00 2001
From: matthew_swift <matthew_swift@localhost>
Date: Thu, 23 Aug 2007 16:04:17 +0000
Subject: [PATCH] Modify wrapText() to support optional indentation. The method now supports a third argument which is the number of spaces to indent each wrapped line. There are also delegate wrapText() methods which simulate the old behavior by passing an indentation of 0.
---
opends/src/server/org/opends/server/util/StaticUtils.java | 93 +++++++++++++++++++++++++++++++++++++++-------
1 files changed, 78 insertions(+), 15 deletions(-)
diff --git a/opends/src/server/org/opends/server/util/StaticUtils.java b/opends/src/server/org/opends/server/util/StaticUtils.java
index f708d58..e4db103 100644
--- a/opends/src/server/org/opends/server/util/StaticUtils.java
+++ b/opends/src/server/org/opends/server/util/StaticUtils.java
@@ -25,16 +25,17 @@
* Portions Copyright 2006-2007 Sun Microsystems, Inc.
*/
package org.opends.server.util;
-import org.opends.messages.Message;
-
+import static org.opends.messages.UtilityMessages.*;
+import static org.opends.server.loggers.debug.DebugLogger.*;
+import static org.opends.server.util.ServerConstants.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.InputStreamReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.text.ParseException;
@@ -46,25 +47,21 @@
import java.util.RandomAccess;
import java.util.StringTokenizer;
+import org.opends.messages.Message;
+import org.opends.messages.MessageBuilder;
+import org.opends.messages.MessageDescriptor;
import org.opends.server.core.DirectoryServer;
+import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.types.Attribute;
import org.opends.server.types.AttributeType;
import org.opends.server.types.AttributeValue;
import org.opends.server.types.DN;
+import org.opends.server.types.DebugLogLevel;
import org.opends.server.types.Entry;
import org.opends.server.types.IdentifiedException;
import org.opends.server.types.ObjectClass;
import org.opends.server.types.RDN;
-import static org.opends.server.loggers.debug.DebugLogger.*;
-import org.opends.server.loggers.debug.DebugTracer;
-import org.opends.server.types.DebugLogLevel;
-import static org.opends.messages.UtilityMessages.*;
-
-import org.opends.messages.MessageBuilder;
-import org.opends.messages.MessageDescriptor;
-import static org.opends.server.util.ServerConstants.*;
-
/**
@@ -1374,7 +1371,7 @@
* @return <CODE>true</CODE> if the two array lists are equal, or
* <CODE>false</CODE> if they are not.
*/
- public static boolean listsAreEqual(List list1, List list2)
+ public static boolean listsAreEqual(List<?> list1, List<?> list2)
{
if (list1 == null)
{
@@ -3695,9 +3692,11 @@
*/
public static String wrapText(Message message, int width)
{
- return wrapText(Message.toString(message), width);
+ return wrapText(Message.toString(message), width, 0);
}
+
+
/**
* Inserts line breaks into the provided buffer to wrap text at no more than
* the specified column width. Wrapping will only be done at space boundaries
@@ -3710,8 +3709,67 @@
*
* @return The wrapped text.
*/
- public static String wrapText(String text, int width)
+ public static String wrapText(String text, int width) {
+ return wrapText(text, width, 0);
+ }
+
+
+
+ /**
+ * Inserts line breaks into the provided buffer to wrap text at no
+ * more than the specified column width. Wrapping will only be done
+ * at space boundaries and if there are no spaces within the
+ * specified width, then wrapping will be performed at the first
+ * space after the specified column. In addition each line will be
+ * indented by the specified amount.
+ *
+ * @param message
+ * The message to be wrapped.
+ * @param width
+ * The maximum number of characters to allow on a line if
+ * there is a suitable breaking point (including any
+ * indentation).
+ * @param indent
+ * The number of columns to indent each line.
+ * @return The wrapped text.
+ */
+ public static String wrapText(Message message, int width, int indent)
{
+ return wrapText(Message.toString(message), width, indent);
+ }
+
+
+
+ /**
+ * Inserts line breaks into the provided buffer to wrap text at no
+ * more than the specified column width. Wrapping will only be done
+ * at space boundaries and if there are no spaces within the
+ * specified width, then wrapping will be performed at the first
+ * space after the specified column. In addition each line will be
+ * indented by the specified amount.
+ *
+ * @param text
+ * The text to be wrapped.
+ * @param width
+ * The maximum number of characters to allow on a line if
+ * there is a suitable breaking point (including any
+ * indentation).
+ * @param indent
+ * The number of columns to indent each line.
+ * @return The wrapped text.
+ */
+ public static String wrapText(String text, int width, int indent)
+ {
+ Validator.ensureTrue(indent >= 0 && indent < width);
+
+ // Calculate the real width and indentation padding.
+ width -= indent;
+ StringBuilder pb = new StringBuilder();
+ for (int i = 0; i < indent; i++) {
+ pb.append(' ');
+ }
+ String padding = pb.toString();
+
StringBuilder buffer = new StringBuilder();
StringTokenizer lineTokenizer = new StringTokenizer(text, "\r\n", true);
while (lineTokenizer.hasMoreTokens())
@@ -3725,6 +3783,7 @@
else if (line.length() < width)
{
// The line fits in the specified width, so append it as-is.
+ buffer.append(padding);
buffer.append(line);
}
else
@@ -3752,10 +3811,12 @@
// make do.
if (lineBuffer.length() > 0)
{
+ buffer.append(padding);
buffer.append(lineBuffer);
buffer.append(EOL);
lineBuffer = new StringBuilder();
}
+ buffer.append(padding);
buffer.append(word);
if (wordTokenizer.hasMoreTokens())
@@ -3793,6 +3854,7 @@
{
// It doesn't fit on the line, so end the current line and start
// a new one.
+ buffer.append(padding);
buffer.append(lineBuffer);
buffer.append(EOL);
@@ -3809,6 +3871,7 @@
// If there's anything left in the line buffer, then add it to the
// final buffer.
+ buffer.append(padding);
buffer.append(lineBuffer);
}
}
--
Gitblit v1.10.0