1 28 29 33 package net.sf.jasperreports.engine.util; 34 35 36 37 41 public class JRStringUtil 42 { 43 44 45 48 public static String xmlEncode(String text) 49 { 50 int length = text.length(); 51 if (text != null && length > 0) 52 { 53 StringBuffer ret = new StringBuffer (length * 12 / 10); 54 55 int last = 0; 56 for (int i = 0; i < length; i ++) 57 { 58 char c = text.charAt(i); 59 switch (c) 60 { 61 case '&' : 63 if (last < i) 64 { 65 ret.append(text.substring(last, i)); 66 } 67 last = i + 1; 68 69 ret.append("&"); 70 break; 71 case '>' : 72 if (last < i) 73 { 74 ret.append(text.substring(last, i)); 75 } 76 last = i + 1; 77 78 ret.append(">"); 79 break; 80 case '<' : 81 if (last < i) 82 { 83 ret.append(text.substring(last, i)); 84 } 85 last = i + 1; 86 87 ret.append("<"); 88 break; 89 case '\"' : 90 if (last < i) 91 { 92 ret.append(text.substring(last, i)); 93 } 94 last = i + 1; 95 96 ret.append("""); 97 break; 98 99 default : 100 break; 101 } 102 } 103 104 if (last < length) 105 { 106 ret.append(text.substring(last)); 107 } 108 109 return ret.toString(); 110 } 111 112 return text; 113 } 114 115 116 119 public static String htmlEncode(String text) 120 { 121 int length = text.length(); 122 if (text != null && length > 0) 123 { 124 StringBuffer ret = new StringBuffer (length * 12 / 10); 125 126 boolean isEncodeSpace = true; 127 int last = 0; 128 for (int i = 0; i < length; i ++) 129 { 130 char c = text.charAt(i); 131 switch (c) 132 { 133 case ' ' : 134 if (isEncodeSpace) 135 { 136 if (last < i) 137 { 138 ret.append(text.substring(last, i)); 139 } 140 last = i + 1; 141 142 ret.append(" "); 143 isEncodeSpace = false; 144 } 145 else 146 { 147 isEncodeSpace = true; 148 } 149 break; 150 case '&' : 151 if (last < i) 152 { 153 ret.append(text.substring(last, i)); 154 } 155 last = i + 1; 156 157 ret.append("&"); 158 isEncodeSpace = false; 159 break; 160 case '>' : 161 if (last < i) 162 { 163 ret.append(text.substring(last, i)); 164 } 165 last = i + 1; 166 167 ret.append(">"); 168 isEncodeSpace = false; 169 break; 170 case '<' : 171 if (last < i) 172 { 173 ret.append(text.substring(last, i)); 174 } 175 last = i + 1; 176 177 ret.append("<"); 178 isEncodeSpace = false; 179 break; 180 case '\"' : 181 if (last < i) 182 { 183 ret.append(text.substring(last, i)); 184 } 185 last = i + 1; 186 187 ret.append("""); 188 isEncodeSpace = false; 189 break; 190 case '\n' : 191 if (last < i) 192 { 193 ret.append(text.substring(last, i)); 194 } 195 last = i + 1; 196 197 ret.append("<br/>"); 198 isEncodeSpace = false; 199 break; 200 201 default : 202 isEncodeSpace = false; 203 break; 204 } 205 } 206 207 if (last < length) 208 { 209 ret.append(text.substring(last)); 210 } 211 212 return ret.toString(); 213 } 214 215 return text; 216 } 217 218 219 225 public static String getLiteral(String name) 226 { 227 if (isValidLiteral(name)) 228 { 229 return name; 230 } 231 232 StringBuffer buffer = new StringBuffer (name.length() + 5); 233 234 char[] literalChars = new char[name.length()]; 235 name.getChars(0, literalChars.length, literalChars, 0); 236 237 for (int i = 0; i < literalChars.length; i++) 238 { 239 if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) 240 { 241 buffer.append((int)literalChars[i]); 242 } 243 else if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) 244 { 245 buffer.append((int)literalChars[i]); 246 } 247 else 248 { 249 buffer.append(literalChars[i]); 250 } 251 } 252 253 return buffer.toString(); 254 } 255 256 257 262 private static boolean isValidLiteral(String literal) 263 { 264 boolean result = true; 265 266 char[] literalChars = new char[literal.length()]; 267 literal.getChars(0, literalChars.length, literalChars, 0); 268 269 for (int i = 0; i < literalChars.length; i++) 270 { 271 if (i == 0 && !Character.isJavaIdentifierStart(literalChars[i])) 272 { 273 result = false; 274 break; 275 } 276 277 if (i != 0 && !Character.isJavaIdentifierPart(literalChars[i])) 278 { 279 result = false; 280 break; 281 } 282 } 283 284 return result; 285 } 286 287 288 294 public static String replaceDosEOL(String text) 295 { 296 if (text == null || text.length() < 2) 297 { 298 return text; 299 } 300 301 int length = text.length(); 302 char[] chars = text.toCharArray(); 303 int r = 0; 304 for (int i = 0; i < length; ++i) 305 { 306 char ch = chars[i]; 307 if (!(ch == '\r' && i + 1 < length && chars[i + 1] == '\n')) 308 { 309 if (r > 0) 310 { 311 chars[i - r] = ch; 312 } 313 } 314 else 315 { 316 ++r; 317 } 318 } 319 320 return r > 0 ? new String (chars, 0, length - r) : text; 321 } 322 } 323 | Popular Tags |