1 16 19 package org.apache.taglibs.string.util; 20 21 import org.apache.commons.lang.StringUtils; 22 23 33 final public class StringW { 34 35 40 static public String quoteRegularExpression(String str) { 41 char[] chrs = str.toCharArray(); 44 int sz = chrs.length; 45 StringBuffer buffer = new StringBuffer (2*sz); 46 for(int i=0; i<sz; i++) { 47 switch(chrs[i]) { 48 case '[' : 49 case ']' : 50 case '?' : 51 case '+' : 52 case '*' : 53 case '/' : 54 case '.' : 55 case '^' : 56 case '$' : 57 buffer.append("\\"); 58 default : 59 buffer.append(chrs[i]); 60 } 61 } 62 return buffer.toString(); 63 } 64 65 70 static public String wordWrap(String str) { 71 return wordWrap(str, 80, "\n", "-", true); 72 } 73 78 static public String wordWrap(String str, int width) { 79 return wordWrap(str, width, "\n", "-", true); 80 } 81 91 static public String wordWrap(String str, int width, String delim, String split ) { 92 return wordWrap(str, width, delim, split, true); 93 } 94 95 106 static public String wordWrap(String str, int width, String delim, 107 String split, boolean delimInside) { 108 int sz = str.length(); 109 110 112 width++; 114 115 StringBuffer buffer = new StringBuffer (sz/width*delim.length()+sz); 117 118 if ( delimInside ) { 121 width = width - delim.length(); 122 } else { 123 width --; 124 } 125 127 int idx = -1; 128 String substr = null; 129 130 for(int i=0; i<sz; i+=width) { 132 133 if(i > sz - width) { 135 buffer.append(str.substring(i)); 136 break; 138 } 139 140 substr = str.substring(i, i+width); 143 145 idx = substr.indexOf(delim); 147 if(idx != -1) { 149 buffer.append(substr.substring(0,idx)); 150 buffer.append(delim); 152 i -= width-idx-delim.length(); 153 154 if(substr.length() > idx+1) { 158 if(substr.charAt(idx+1) != '\n') { 159 if(Character.isWhitespace(substr.charAt(idx+1))) { 160 i++; 161 } 162 } 163 } 164 continue; 166 } 167 168 idx = -1; 169 170 char[] chrs = substr.toCharArray(); 172 for(int j=width; j>0; j--) { 173 if(Character.isWhitespace(chrs[j-1])) { 174 idx = j; 175 break; 177 } 178 } 179 180 if(idx == -1) { 183 for(int j=width; j>0; j--) { 184 if(chrs[j-1] == '-') { 185 idx = j; 186 break; 188 } 189 } 190 if(idx == -1) { 191 buffer.append(substr); 192 buffer.append(delim); 193 } else { 196 if(idx != width) { 197 idx++; 198 } 199 buffer.append(substr.substring(0,idx)); 200 buffer.append(delim); 201 i -= width-idx; 204 } 205 } else { 206 221 buffer.append(substr.substring(0,idx)); 223 buffer.append(StringUtils.repeat(" ",width-idx)); 224 buffer.append(delim); 227 i -= width-idx; 230 } 232 } 233 return buffer.toString(); 235 } 236 237 260 public static String truncateNicely(String str, int lower, int upper, String appendToEnd) 261 { 262 str = XmlW.removeXml(str); 264 265 if(upper < lower) { 267 upper = lower; 268 } 269 270 if(str.length() > upper) { 273 int loc; 275 276 loc = str.lastIndexOf(' ', upper); 278 279 if(loc >= lower) { 281 str = str.substring(0, loc); 283 } else { 284 str = str.substring(0, upper); 286 } 287 288 str = str + appendToEnd; 290 } 291 292 return str; 293 } 294 } 295 | Popular Tags |