scolebourne 2002/11/22 17:07:11
Modified: util/src/java/org/apache/commons/util BitField.java
StopWatch.java Interpolator.java XmlUtils.java
Added: util/src/java/org/apache/commons/util WordWrapUtils.java
Log:
Fixed licences and formatting
Revision Changes Path
1.2 +38 -70
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/BitField.java
Index: BitField.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/BitField.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BitField.java 30 Jan 2002 05:52:44 -0000 1.1
+++ BitField.java 23 Nov 2002 01:07:10 -0000 1.2
@@ -1,10 +1,4 @@
-/*
- * $Header$
- * $Revision$
- * $Date$
- *
- * ====================================================================
- *
+/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
@@ -36,7 +30,7 @@
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
- * permission of the Apache Group.
+ * permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -56,24 +50,21 @@
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
- *
*/
-
package org.apache.commons.util;
/**
* Manage operations dealing with bit-mapped fields.
- *
+ * <p>
* Code originated from the POI project.
*
* @author Scott Sanders (sanders at apache dot org)
* @author Marc Johnson (mjohnson at apache dot org)
* @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Stephen Colebourne
* @version $Id$
*/
-
-public class BitField
-{
+public class BitField {
private final int _mask;
private final int _shift_count;
@@ -85,16 +76,13 @@
* bits that this BitField operates on
*/
- public BitField(final int mask)
- {
+ public BitField(final int mask) {
_mask = mask;
- int count = 0;
+ int count = 0;
int bit_pattern = mask;
- if (bit_pattern != 0)
- {
- while ((bit_pattern & 1) == 0)
- {
+ if (bit_pattern != 0) {
+ while ((bit_pattern & 1) == 0) {
count++;
bit_pattern >>= 1;
}
@@ -115,8 +103,7 @@
* @return the selected bits, shifted right appropriately
*/
- public int getValue(final int holder)
- {
+ public int getValue(final int holder) {
return getRawValue(holder) >> _shift_count;
}
@@ -133,9 +120,8 @@
* @return the selected bits, shifted right appropriately
*/
- public short getShortValue(final short holder)
- {
- return ( short ) getValue(holder);
+ public short getShortValue(final short holder) {
+ return (short) getValue(holder);
}
/**
@@ -147,8 +133,7 @@
* @return the selected bits
*/
- public int getRawValue(final int holder)
- {
+ public int getRawValue(final int holder) {
return (holder & _mask);
}
@@ -161,9 +146,8 @@
* @return the selected bits
*/
- public short getShortRawValue(final short holder)
- {
- return ( short ) getRawValue(holder);
+ public short getShortRawValue(final short holder) {
+ return (short) getRawValue(holder);
}
/**
@@ -178,8 +162,7 @@
* @return true if any of the bits are set, else false
*/
- public boolean isSet(final int holder)
- {
+ public boolean isSet(final int holder) {
return (holder & _mask) != 0;
}
@@ -194,8 +177,7 @@
* @return true if all of the bits are set, else false
*/
- public boolean isAllSet(final int holder)
- {
+ public boolean isAllSet(final int holder) {
return (holder & _mask) == _mask;
}
@@ -210,8 +192,7 @@
* parameter replacing the old bits
*/
- public int setValue(final int holder, final int value)
- {
+ public int setValue(final int holder, final int value) {
return (holder & ~_mask) | ((value << _shift_count) & _mask);
}
@@ -226,9 +207,8 @@
* parameter replacing the old bits
*/
- public short setShortValue(final short holder, final short value)
- {
- return ( short ) setValue(holder, value);
+ public short setShortValue(final short holder, final short value) {
+ return (short) setValue(holder, value);
}
/**
@@ -241,8 +221,7 @@
* (set to 0)
*/
- public int clear(final int holder)
- {
+ public int clear(final int holder) {
return holder & ~_mask;
}
@@ -256,9 +235,8 @@
* (set to 0)
*/
- public short clearShort(final short holder)
- {
- return ( short ) clear(holder);
+ public short clearShort(final short holder) {
+ return (short) clear(holder);
}
/**
@@ -271,9 +249,8 @@
* (set to 0)
*/
- public byte clearByte(final byte holder)
- {
- return ( byte ) clear(holder);
+ public byte clearByte(final byte holder) {
+ return (byte) clear(holder);
}
/**
@@ -285,8 +262,7 @@
* @return the value of holder with the specified bits set to 1
*/
- public int set(final int holder)
- {
+ public int set(final int holder) {
return holder | _mask;
}
@@ -299,9 +275,8 @@
* @return the value of holder with the specified bits set to 1
*/
- public short setShort(final short holder)
- {
- return ( short ) set(holder);
+ public short setShort(final short holder) {
+ return (short) set(holder);
}
/**
@@ -313,9 +288,8 @@
* @return the value of holder with the specified bits set to 1
*/
- public byte setByte(final byte holder)
- {
- return ( byte ) set(holder);
+ public byte setByte(final byte holder) {
+ return (byte) set(holder);
}
/**
@@ -329,10 +303,8 @@
* cleared
*/
- public int setBoolean(final int holder, final boolean flag)
- {
- return flag ? set(holder)
- : clear(holder);
+ public int setBoolean(final int holder, final boolean flag) {
+ return flag ? set(holder) : clear(holder);
}
/**
@@ -346,10 +318,8 @@
* cleared
*/
- public short setShortBoolean(final short holder, final boolean flag)
- {
- return flag ? setShort(holder)
- : clearShort(holder);
+ public short setShortBoolean(final short holder, final boolean flag) {
+ return flag ? setShort(holder) : clearShort(holder);
}
/**
@@ -363,10 +333,8 @@
* cleared
*/
- public byte setByteBoolean(final byte holder, final boolean flag)
- {
- return flag ? setByte(holder)
- : clearByte(holder);
+ public byte setByteBoolean(final byte holder, final boolean flag) {
+ return flag ? setByte(holder) : clearByte(holder);
}
-} // end public class BitField
+}
1.2 +171 -162
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StopWatch.java
Index: StopWatch.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StopWatch.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StopWatch.java 29 Jul 2002 08:03:30 -0000 1.1
+++ StopWatch.java 23 Nov 2002 01:07:10 -0000 1.2
@@ -1,162 +1,171 @@
-package org.apache.commons.util;
-
-/*
- * ====================================================================
- *
- * The Apache Software License, Version 1.1
- *
- * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
- * reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
- * Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowlegement may appear in the software itself,
- * if and wherever such third-party acknowlegements normally appear.
- *
- * 4. The names "The Jakarta Project", "Commons", and "Apache Software
- * Foundation" must not be used to endorse or promote products derived
- * from this software without prior written permission. For written
- * permission, please contact [EMAIL PROTECTED]
- *
- * 5. Products derived from this software may not be called "Apache"
- * nor may "Apache" appear in their names without prior written
- * permission of the Apache Group.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
- * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-
-
-
-
-
-
-/**
- * Useful when doing timings in a test.
- *
- * $Header$
- * $Revision$
- * $Date$
- */
-public class StopWatch {
-
- // some test code
- static public void main(String[] strs) {
- StopWatch obj = new StopWatch();
- obj.start();
- try { Thread.currentThread().sleep(1500); } catch(InterruptedException ie)
{;}
- obj.stop();
- System.out.println(obj);
- }
-
- private long startTime = -1;
- private long stopTime = -1;
-
- public StopWatch() {
- }
-
- /**
- * Start the stopwatch.
- */
- public void start() {
- this.startTime = System.currentTimeMillis();
- }
-
- /**
- * Stop the stopwatch.
- */
- public void stop() {
- this.stopTime = System.currentTimeMillis();
- }
-
- /**
- * Reset the stopwatch.
- */
- public void reset() {
- this.startTime = -1;
- this.stopTime = -1;
- }
-
- /**
- * Split the time.
- */
- public void split() {
- this.stopTime = System.currentTimeMillis();
- }
-
- /**
- * Remove a split.
- */
- public void unsplit() {
- this.stopTime = -1;
- }
-
- /**
- * Get the time on the stopwatch. This is either the
- * time between start and latest split, between start and stop,
- * or the time between the start and the moment this method is called.
- */
- public long getTime() {
- if(stopTime == -1) {
- return (System.currentTimeMillis() - this.startTime);
- } else {
- return this.stopTime - this.startTime;
- }
- }
-
- public String toString() {
- return getTimeString();
- }
-
- /**
- * Get the time gap as a String.
- * In hours, minutes, seconds and milliseconds.
- */
- public String getTimeString() {
- int HIM = 60 * 60 * 1000;
- int MIM = 60 * 1000;
- int hours, minutes, seconds, milliseconds;
- long time = getTime();
- hours = (int) (time / HIM);
- time = time - (hours * HIM);
- minutes = (int) (time / MIM);
- time = time - (minutes * MIM);
- seconds = (int) (time / 1000);
- time = time - (seconds * 1000);
- milliseconds = (int) time;
-
- return hours + "h:" + minutes + "m:" + seconds + "s:" + milliseconds +
"ms";
- }
-
-}
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
+ * Apache Software Foundation (http://www.apache.org/)."
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
+ *
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+package org.apache.commons.util;
+
+/**
+ * <code>StopWatch</code> provides a convenient API for timings.
+ *
+ * @author Henri Yandell
+ * @author Stephen Colebourne
+ * @version $Id$
+ */
+public class StopWatch {
+
+ // some test code - TODO, write JUnit test
+ static public void main(String[] strs) {
+ StopWatch obj = new StopWatch();
+ obj.start();
+ try {
+ Thread.currentThread().sleep(1500);
+ } catch (InterruptedException ie) {
+ ;
+ }
+ obj.stop();
+ System.out.println(obj);
+ }
+
+ /** The start time */
+ private long startTime = -1;
+ /** The stop time */
+ private long stopTime = -1;
+
+ /**
+ * Constructor.
+ */
+ public StopWatch() {
+ }
+
+ /**
+ * Start the stopwatch.
+ */
+ public void start() {
+ this.startTime = System.currentTimeMillis();
+ }
+
+ /**
+ * Stop the stopwatch.
+ */
+ public void stop() {
+ this.stopTime = System.currentTimeMillis();
+ }
+
+ /**
+ * Reset the stopwatch.
+ */
+ public void reset() {
+ this.startTime = -1;
+ this.stopTime = -1;
+ }
+
+ /**
+ * Split the time.
+ */
+ public void split() {
+ this.stopTime = System.currentTimeMillis();
+ }
+
+ /**
+ * Remove a split.
+ */
+ public void unsplit() {
+ this.stopTime = -1;
+ }
+
+ /**
+ * Get the time on the stopwatch. This is either the
+ * time between start and latest split, between start and stop,
+ * or the time between the start and the moment this method is called.
+ *
+ * @return the time in milliseconds
+ */
+ public long getTime() {
+ if (stopTime == -1) {
+ return (System.currentTimeMillis() - this.startTime);
+ } else {
+ return (this.stopTime - this.startTime);
+ }
+ }
+
+ /**
+ * Gets a summary of the object as a String.
+ *
+ * @return the time as a String
+ */
+ public String toString() {
+ return getTimeString();
+ }
+
+ /**
+ * Get the time gap as a String.
+ * In hours, minutes, seconds and milliseconds.
+ *
+ * @return the time as a String
+ */
+ protected String getTimeString() {
+ int HIM = 60 * 60 * 1000;
+ int MIM = 60 * 1000;
+ int hours, minutes, seconds, milliseconds;
+ long time = getTime();
+ hours = (int) (time / HIM);
+ time = time - (hours * HIM);
+ minutes = (int) (time / MIM);
+ time = time - (minutes * MIM);
+ seconds = (int) (time / 1000);
+ time = time - (seconds * 1000);
+ milliseconds = (int) time;
+
+ return hours + "h:" + minutes + "m:" + seconds + "s:" + milliseconds + "ms";
+ }
+
+}
1.2 +13 -19
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/Interpolator.java
Index: Interpolator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/Interpolator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Interpolator.java 29 Jul 2002 08:03:30 -0000 1.1
+++ Interpolator.java 23 Nov 2002 01:07:10 -0000 1.2
@@ -1,11 +1,7 @@
-package org.apache.commons.util;
-
-/*
- * ====================================================================
- *
+/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -13,7 +9,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -21,20 +17,20 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
- * from this software without prior written permission. For written
+ * from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
- * permission of the Apache Group.
+ * permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -54,19 +50,17 @@
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
- *
- */
+ */
+package org.apache.commons.util;
import java.util.Map;
import java.util.Iterator;
import org.apache.commons.lang.StringUtils;
-
/**
* Interpolates text with embedded ${var} variables.
*
- * $Header$
- * $Revision$
- * $Date$
+ * @author Henri Yandell
+ * @version $Id$
*/
public class Interpolator {
@@ -82,9 +76,9 @@
while (keys.hasNext()) {
String key = keys.next().toString();
String value = map.get(key).toString();
- text = StringUtils.replace(text, "${"+key+"}", value);
+ text = StringUtils.replace(text, "${" + key + "}", value);
if (key.indexOf(" ") == -1) {
- text = StringUtils.replace(text, "$"+key, value);
+ text = StringUtils.replace(text, "$" + key, value);
}
}
return text;
1.6 +90 -77
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/XmlUtils.java
Index: XmlUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/XmlUtils.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XmlUtils.java 17 Aug 2002 23:31:00 -0000 1.5
+++ XmlUtils.java 23 Nov 2002 01:07:10 -0000 1.6
@@ -1,9 +1,7 @@
-package org.apache.commons.util;
-
/* ====================================================================
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2001 The Apache Software Foundation. All rights
+ * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -18,21 +16,21 @@
* the documentation and/or other materials provided with the
* distribution.
*
- * 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * 3. The end-user documentation included with the redistribution, if
+ * any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
- * Alternately, this acknowledgment may appear in the software itself,
- * if and wherever such third-party acknowledgments normally appear.
+ * Alternately, this acknowlegement may appear in the software itself,
+ * if and wherever such third-party acknowlegements normally appear.
*
- * 4. The names "Apache" and "Apache Software Foundation" and
- * "Apache Commons" must not be used to endorse or promote products
- * derived from this software without prior written permission. For
- * written permission, please contact [EMAIL PROTECTED]
- *
- * 5. Products derived from this software may not be called "Apache",
- * "Apache Turbine", nor may "Apache" appear in their name, without
- * prior written permission of the Apache Software Foundation.
+ * 4. The names "The Jakarta Project", "Commons", and "Apache Software
+ * Foundation" must not be used to endorse or promote products derived
+ * from this software without prior written permission. For written
+ * permission, please contact [EMAIL PROTECTED]
+ *
+ * 5. Products derived from this software may not be called "Apache"
+ * nor may "Apache" appear in their names without prior written
+ * permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
@@ -53,32 +51,45 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
+package org.apache.commons.util;
import org.apache.commons.lang.StringUtils;
-
/**
* XML helping static methods.
*
- * @author [EMAIL PROTECTED]
+ * @author Henri Yandell
+ * @author Stephen Colebourne
* @version $Id$
*/
-final public class XmlUtils {
+public class XmlUtils {
+
+ /**
+ * Constructor. This class is not intended to be instantiated.
+ */
+ public XmlUtils() {
+ }
- static public String escapeXml(String str) {
- str = StringUtils.replace(str,"&","&");
- str = StringUtils.replace(str,"<","<");
- str = StringUtils.replace(str,">",">");
- str = StringUtils.replace(str,"\"",""");
- str = StringUtils.replace(str,"'","'");
+ /**
+ * Escape restricted XML characters to entities.
+ */
+ public static String escapeXml(String str) {
+ str = StringUtils.replace(str, "&", "&");
+ str = StringUtils.replace(str, "<", "<");
+ str = StringUtils.replace(str, ">", ">");
+ str = StringUtils.replace(str, "\"", """);
+ str = StringUtils.replace(str, "'", "'");
return str;
}
- static public String unescapeXml(String str) {
- str = StringUtils.replace(str,"&","&");
- str = StringUtils.replace(str,"<","<");
- str = StringUtils.replace(str,">",">");
- str = StringUtils.replace(str,""","\"");
- str = StringUtils.replace(str,"'","'");
+ /**
+ * Unescape entities to a 'normal' string
+ */
+ public static String unescapeXml(String str) {
+ str = StringUtils.replace(str, "&", "&");
+ str = StringUtils.replace(str, "<", "<");
+ str = StringUtils.replace(str, ">", ">");
+ str = StringUtils.replace(str, """, "\"");
+ str = StringUtils.replace(str, "'", "'");
return str;
}
@@ -86,84 +97,85 @@
* Remove any xml tags from a String.
* Same as HtmlW's method.
*/
- static public String removeXml(String str) {
+ public static String removeXml(String str) {
int sz = str.length();
StringBuffer buffer = new StringBuffer(sz);
boolean inString = false;
boolean inTag = false;
- for(int i=0; i<sz; i++) {
+ for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
- if(ch == '<') {
+ if (ch == '<') {
inTag = true;
- } else
- if(ch == '>') {
+ } else if (ch == '>') {
inTag = false;
continue;
}
- if(!inTag) {
+ if (!inTag) {
buffer.append(ch);
}
}
return buffer.toString();
}
- static public String getContent(String tag, String text) {
+ public static String getContent(String tag, String text) {
int idx = XmlUtils.getIndexOpeningTag(tag, text);
- if(idx == -1) {
+ if (idx == -1) {
return "";
}
text = text.substring(idx);
int end = XmlUtils.getIndexClosingTag(tag, text);
idx = text.indexOf('>');
- if(idx == -1) {
+ if (idx == -1) {
return "";
}
- return text.substring(idx+1, end);
+ return text.substring(idx + 1, end);
}
- static public int getIndexOpeningTag(String tag, String text) {
+ public static int getIndexOpeningTag(String tag, String text) {
return getIndexOpeningTag(tag, text, 0);
}
+
static private int getIndexOpeningTag(String tag, String text, int start) {
// consider whitespace?
- int idx = text.indexOf("<"+tag, start);
- if(idx == -1) {
+ int idx = text.indexOf("<" + tag, start);
+ if (idx == -1) {
return -1;
}
- char next = text.charAt(idx+1+tag.length());
- if( (next == '>') || Character.isWhitespace(next) ) {
+ char next = text.charAt(idx + 1 + tag.length());
+ if ((next == '>') || Character.isWhitespace(next)) {
return idx;
} else {
- return getIndexOpeningTag(tag, text, idx+1);
+ return getIndexOpeningTag(tag, text, idx + 1);
}
}
// Pass in "para" and a string that starts with
// <para> and it will return the index of the matching </para>
// It assumes well-formed xml. Or well enough.
- static public int getIndexClosingTag(String tag, String text) {
+ public static int getIndexClosingTag(String tag, String text) {
return getIndexClosingTag(tag, text, 0);
}
- static public int getIndexClosingTag(String tag, String text, int start) {
- String open = "<"+tag;
- String close = "</"+tag+">";
-// System.err.println("OPEN: "+open);
-// System.err.println("CLOSE: "+close);
+
+ public static int getIndexClosingTag(String tag, String text, int start) {
+ String open = "<" + tag;
+ String close = "</" + tag + ">";
+ // System.err.println("OPEN: "+open);
+ // System.err.println("CLOSE: "+close);
int closeSz = close.length();
int nextCloseIdx = text.indexOf(close, start);
-// System.err.println("first close: "+nextCloseIdx);
- if(nextCloseIdx == -1) {
+ // System.err.println("first close: "+nextCloseIdx);
+ if (nextCloseIdx == -1) {
return -1;
}
int count = StringUtils.countMatches(text.substring(start, nextCloseIdx),
open);
-// System.err.println("count: "+count);
- if(count == 0) {
- return -1; // tag is never opened
+ // System.err.println("count: "+count);
+ if (count == 0) {
+ return -1; // tag is never opened
}
int expected = 1;
- while(count != expected) {
- nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz);
- if(nextCloseIdx == -1) {
+ while (count != expected) {
+ nextCloseIdx = text.indexOf(close, nextCloseIdx + closeSz);
+ if (nextCloseIdx == -1) {
return -1;
}
count = StringUtils.countMatches(text.substring(start, nextCloseIdx),
open);
@@ -172,24 +184,25 @@
return nextCloseIdx;
}
- static public String getAttribute(String attribute, String text) {
+ public static String getAttribute(String attribute, String text) {
return getAttribute(attribute, text, 0);
}
- static public String getAttribute(String attribute, String text, int idx) {
- int close = text.indexOf(">", idx);
- int attrIdx = text.indexOf(attribute+"=\"", idx);
- if(attrIdx == -1) {
- return null;
- }
- if(attrIdx > close) {
- return null;
- }
- int attrStartIdx = attrIdx + attribute.length() + 2;
- int attrCloseIdx = text.indexOf("\"", attrStartIdx);
- if(attrCloseIdx > close) {
- return null;
- }
- return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
+
+ public static String getAttribute(String attribute, String text, int idx) {
+ int close = text.indexOf(">", idx);
+ int attrIdx = text.indexOf(attribute + "=\"", idx);
+ if (attrIdx == -1) {
+ return null;
+ }
+ if (attrIdx > close) {
+ return null;
+ }
+ int attrStartIdx = attrIdx + attribute.length() + 2;
+ int attrCloseIdx = text.indexOf("\"", attrStartIdx);
+ if (attrCloseIdx > close) {
+ return null;
+ }
+ return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
}
}
1.1
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/WordWrapUtils.java
Index: WordWrapUtils.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.util;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import org.apache.commons.lang.StringUtils;
/**
* <code>WordWrapUtils</code> is a utility class to assist with word wrapping.
*
* @author Henri Yandell
* @author Stephen Colebourne
* @version $Id: WordWrapUtils.java,v 1.1 2002/11/23 01:07:10 scolebourne Exp $
*/
public class WordWrapUtils {
// Wrapping
//--------------------------------------------------------------------------
/**
* Wraps a block of text to a specified line length.
* <p>
* This method takes a block of text, which might have long lines in it
* and wraps the long lines based on the supplied wrapColumn parameter.
* It was initially implemented for use by VelocityEmail. If there are tabs
* in inString, you are going to get results that are a bit strange,
* since tabs are a single character but are displayed as 4 or 8
* spaces. Remove the tabs.
*
* @param str text which is in need of word-wrapping
* @param newline the characters that define a newline
* @param wrapColumn the column to wrap the words at
* @return the text with all the long lines word-wrapped
*/
public static String wrapText(String str, String newline, int wrapColumn) {
StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true);
StringBuffer stringBuffer = new StringBuffer();
while (lineTokenizer.hasMoreTokens()) {
try {
String nextLine = lineTokenizer.nextToken();
if (nextLine.length() > wrapColumn) {
// This line is long enough to be wrapped.
nextLine = wrapLine(nextLine, newline, wrapColumn);
}
stringBuffer.append(nextLine);
} catch (NoSuchElementException nsee) {
// thrown by nextToken(), but I don't know why it would
break;
}
}
return (stringBuffer.toString());
}
/**
* Wraps a single line of text.
* Called by wrapText() to do the real work of wrapping.
*
* @param line a line which is in need of word-wrapping
* @param newline the characters that define a newline
* @param wrapColumn the column to wrap the words at
* @return a line with newlines inserted
*/
private static String wrapLine(String line, String newline, int wrapColumn) {
StringBuffer wrappedLine = new StringBuffer();
while (line.length() > wrapColumn) {
int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
if (spaceToWrapAt >= 0) {
wrappedLine.append(line.substring(0, spaceToWrapAt));
wrappedLine.append(newline);
line = line.substring(spaceToWrapAt + 1);
}
// This must be a really long word or URL. Pass it
// through unchanged even though it's longer than the
// wrapColumn would allow. This behavior could be
// dependent on a parameter for those situations when
// someone wants long words broken at line length.
else {
spaceToWrapAt = line.indexOf(' ', wrapColumn);
if (spaceToWrapAt >= 0) {
wrappedLine.append(line.substring(0, spaceToWrapAt));
wrappedLine.append(newline);
line = line.substring(spaceToWrapAt + 1);
} else {
wrappedLine.append(line);
line = "";
}
}
}
// Whatever is left in line is short enough to just pass through
wrappedLine.append(line);
return (wrappedLine.toString());
}
// Word wrapping
//--------------------------------------------------------------------------
/**
* Create a word-wrapped version of a String. Wrap at 80 characters and
* use newlines as the delimiter. If a word is over 80 characters long
* use a - sign to split it.
*/
public static String wordWrap(String str) {
return wordWrap(str, 80, "\n", "-");
}
/**
* Create a word-wrapped version of a String. Wrap at a specified width and
* use newlines as the delimiter. If a word is over the width in lenght
* use a - sign to split it.
*/
public static String wordWrap(String str, int width) {
return wordWrap(str, width, "\n", "-");
}
/**
* Word-wrap a string.
*
* @param str String to word-wrap
* @param width int to wrap at
* @param delim String to use to separate lines
* @param split String to use to split a word greater than width long
*
* @return String that has been word wrapped
*/
public static String wordWrap(String str, int width, String delim, String split)
{
int sz = str.length();
/// shift width up one. mainly as it makes the logic easier
width++;
// our best guess as to an initial size
StringBuffer buffer = new StringBuffer(sz / width * delim.length() + sz);
// every line will include a delim on the end
width = width - delim.length();
int idx = -1;
String substr = null;
// beware: i is rolled-back inside the loop
for (int i = 0; i < sz; i += width) {
// on the last line
if (i > sz - width) {
buffer.append(str.substring(i));
break;
}
// the current line
substr = str.substring(i, i + width);
// is the delim already on the line
idx = substr.indexOf(delim);
if (idx != -1) {
buffer.append(substr.substring(0, idx));
buffer.append(delim);
i -= width - idx - delim.length();
// Erase a space after a delim. Is this too obscure?
if (substr.charAt(idx + 1) != '\n') {
if (Character.isWhitespace(substr.charAt(idx + 1))) {
i++;
}
}
continue;
}
idx = -1;
// figure out where the last space is
char[] chrs = substr.toCharArray();
for (int j = width; j > 0; j--) {
if (Character.isWhitespace(chrs[j - 1])) {
idx = j;
break;
}
}
// idx is the last whitespace on the line.
if (idx == -1) {
for (int j = width; j > 0; j--) {
if (chrs[j - 1] == '-') {
idx = j;
break;
}
}
if (idx == -1) {
buffer.append(substr);
buffer.append(delim);
} else {
if (idx != width) {
idx++;
}
buffer.append(substr.substring(0, idx));
buffer.append(delim);
i -= width - idx;
}
} else {
// insert spaces
buffer.append(substr.substring(0, idx));
buffer.append(StringUtils.repeat(" ", width - idx));
buffer.append(delim);
i -= width - idx;
}
}
return buffer.toString();
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>