commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons-sandbox/util/src/java/org/apache/commons/util WordWrapper.java
Date Sun, 14 Jul 2002 02:43:30 GMT
bayard      2002/07/13 19:43:30

  Modified:    util/src/java/org/apache/commons/util WordWrapper.java
  Log:
  Added the wrapLine and wrapText methods from Strings.
  
  Revision  Changes    Path
  1.2       +93 -0     jakarta-commons-sandbox/util/src/java/org/apache/commons/util/WordWrapper.java
  
  Index: WordWrapper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/WordWrapper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WordWrapper.java	14 Jul 2002 02:36:49 -0000	1.1
  +++ WordWrapper.java	14 Jul 2002 02:43:30 -0000	1.2
  @@ -1,5 +1,8 @@
   package org.apache.commons.util;
   
  +import java.util.StringTokenizer;
  +import java.util.NoSuchElementException;
  +
   import org.apache.commons.lang.Strings;
   
   /* ====================================================================
  @@ -57,6 +60,96 @@
    */
   
   public class WordWrapper {
  +
  +    // 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
       //--------------------------------------------------------------------------
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message