View Javadoc

1   /*
2    * Copyright (c) 2003, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Genjava-Core nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  package com.generationjava.lang;
33  
34  import org.apache.commons.lang.StringUtils;
35  import org.apache.commons.lang.NumberUtils;
36  
37  /***
38   * A set of String library static methods. While extending String or 
39   * StringBuffer would have been the nicest solution, that is not 
40   * possible, so a simple set of static methods seems the most workable.
41   *
42   * Most methods have now gone to Commons Lang StringUtils.
43   */
44  final public class StringW {
45  
46  	static public String join(Object[] objs, String sep, String pre, String post) {
47  		String ret = StringUtils.join(objs, sep);
48  		if( (ret != null) && (ret != "")) {
49  			return pre + ret + post;
50  		} else {
51  			return ret;
52  		}
53  	}
54  
55      /***
56       * Create a word-wrapped version of a String. Wrap at 80 characters and 
57       * use newlines as the delimiter. If a word is over 80 characters long 
58       * use a - sign to split it.
59       */
60      static public String wordWrap(String str) {
61          return wordWrap(str, 80, "\n", "-");
62      }
63      /***
64       * Create a word-wrapped version of a String. Wrap at a specified width and 
65       * use newlines as the delimiter. If a word is over the width in lenght 
66       * use a - sign to split it.
67       */
68      static public String wordWrap(String str, int width) {
69          return wordWrap(str, width, "\n", "-");
70      }
71      static public String wordWrap(String str, String width, String delim, String split) {
72          return wordWrap(str, NumberUtils.stringToInt(width), delim, split);
73      }
74      /***
75       * Word-wrap a string.
76       *
77       * @param str   String to word-wrap
78       * @param width int to wrap at
79       * @param delim String to use to separate lines
80       * @param split String to use to split a word greater than width long
81       *
82       * @return String that has been word wrapped
83       */
84      static public String wordWrap(String str, int width, String delim, String split) {
85          int sz = str.length();
86  
87          /// shift width up one. mainly as it makes the logic easier
88          width++;
89  
90          // our best guess as to an initial size
91          StringBuffer buffer = new StringBuffer(sz/width*delim.length()+sz);
92  
93          // every line will include a delim on the end
94          width = width - delim.length();
95  
96          int idx = -1;
97          String substr = null;
98  
99          // beware: i is rolled-back inside the loop
100         for(int i=0; i<sz; i+=width) {
101 
102             // on the last line
103             if(i > sz - width) {
104                 buffer.append(str.substring(i));
105                 break;
106             }
107 
108             // the current line
109             substr = str.substring(i, i+width);
110 
111             // is the delim already on the line
112             idx = substr.indexOf(delim);
113             if(idx != -1) {
114                 buffer.append(substr.substring(0,idx));
115                 buffer.append(delim);
116                 i -= width-idx-delim.length();
117                 
118                 // Erase a space after a delim. Is this too obscure?
119                 if(substr.charAt(idx+1) != '\n') {
120                     if(Character.isWhitespace(substr.charAt(idx+1))) {
121                         i++;
122                     }
123                 }
124                 continue;
125             }
126 
127             idx = -1;
128 
129             // figure out where the last space is
130             char[] chrs = substr.toCharArray();
131             for(int j=width; j>0; j--) {
132                 if(Character.isWhitespace(chrs[j-1])) {
133                     idx = j;
134                     break;
135                 }
136             }
137 
138             // idx is the last whitespace on the line.
139             if(idx == -1) {
140                 for(int j=width; j>0; j--) {
141                     if(chrs[j-1] == '-') {
142                         idx = j;
143                         break;
144                     }
145                 }
146                 if(idx == -1) {
147                     buffer.append(substr);
148                     buffer.append(delim);
149                 } else {
150                     if(idx != width) {
151                         idx++;
152                     }
153                     buffer.append(substr.substring(0,idx));
154                     buffer.append(delim);
155                     i -= width-idx;
156                 }
157             } else {
158                 /*
159                 if(force) {
160                     if(idx == width-1) {
161                         buffer.append(substr);
162                         buffer.append(delim);
163                     } else {
164                         // stick a split in.
165                         int splitsz = split.length();
166                         buffer.append(substr.substring(0,width-splitsz));
167                         buffer.append(split);
168                         buffer.append(delim);
169                         i -= splitsz;
170                     }
171                 } else {
172                 */
173                     // insert spaces
174                     buffer.append(substr.substring(0,idx));
175                     buffer.append(StringUtils.repeat(" ",width-idx));
176                     buffer.append(delim);
177                     i -= width-idx;
178 //                }
179             }
180         }
181         return buffer.toString();
182     }
183 
184 }