1 14 package org.compiere.util; 15 16 import java.awt.Color ; 17 import java.awt.font.TextAttribute ; 18 import java.text.AttributedCharacterIterator ; 19 import java.text.AttributedString ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 31 public class Util 32 { 33 40 public static String replace (String value, String oldPart, String newPart) 41 { 42 if (value == null || value.length() == 0 43 || oldPart == null || oldPart.length() == 0) 44 return value; 45 int oldPartLength = oldPart.length(); 47 String oldValue = value; 48 StringBuffer retValue = new StringBuffer (); 49 int pos = oldValue.indexOf(oldPart); 50 while (pos != -1) 51 { 52 retValue.append(oldValue.substring(0, pos)); 53 if (newPart != null && newPart.length() > 0) 54 retValue.append(newPart); 55 oldValue = oldValue.substring(pos+oldPartLength); 56 pos = oldValue.indexOf(oldPart); 57 } 58 retValue.append(oldValue); 59 return retValue.toString(); 61 } 63 68 public static String removeCRLF (String in) 69 { 70 char[] inArray = in.toCharArray(); 71 StringBuffer out = new StringBuffer (inArray.length); 72 for (int i = 0; i < inArray.length; i++) 73 { 74 char c = inArray[i]; 75 if (c == '\n' || c == '\r') 76 ; 77 else 78 out.append(c); 79 } 80 return out.toString(); 81 } 83 84 90 public static String maskHTML (String content) 91 { 92 if (content == null || content.length() == 0 || content.equals(" ")) 93 return " "; 94 String temp = replace (content, "<", "<"); 96 temp = replace (temp, ">", ">"); 97 temp = replace (temp, "\"", """); 98 temp = replace (temp, "&", "&"); 99 return temp; 100 } 102 108 public static int getCount (String string, char countChar) 109 { 110 if (string == null || string.length() == 0) 111 return 0; 112 int counter = 0; 113 char[] array = string.toCharArray(); 114 for (int i = 0; i < array.length; i++) 115 { 116 if (array[i] == countChar) 117 counter++; 118 } 119 return counter; 120 } 122 123 124 131 public static int findIndexOf (String str, char search) 132 { 133 return findIndexOf(str, search, search); 134 } 136 144 public static int findIndexOf (String str, char search1, char search2) 145 { 146 if (str == null) 147 return -1; 148 int endIndex = -1; 150 int parCount = 0; 151 boolean ignoringText = false; 152 int size = str.length(); 153 while (++endIndex < size) 154 { 155 char c = str.charAt(endIndex); 156 if (c == '\'') 157 ignoringText = !ignoringText; 158 else if (!ignoringText) 159 { 160 if (parCount == 0 && (c == search1 || c == search2)) 161 return endIndex; 162 else if (c == ')') 163 parCount--; 164 else if (c == '(') 165 parCount++; 166 } 167 } 168 return -1; 169 } 171 178 public static int findIndexOf (String str, String search) 179 { 180 if (str == null || search == null || search.length() == 0) 181 return -1; 182 int endIndex = -1; 184 int parCount = 0; 185 boolean ignoringText = false; 186 int size = str.length(); 187 while (++endIndex < size) 188 { 189 char c = str.charAt(endIndex); 190 if (c == '\'') 191 ignoringText = !ignoringText; 192 else if (!ignoringText) 193 { 194 if (parCount == 0 && c == search.charAt(0)) 195 { 196 if (str.substring(endIndex).startsWith(search)) 197 return endIndex; 198 } 199 else if (c == ')') 200 parCount--; 201 else if (c == '(') 202 parCount++; 203 } 204 } 205 return -1; 206 } 208 209 210 215 static public String toHex (byte b) 216 { 217 char hexDigit[] = { 218 '0', '1', '2', '3', '4', '5', '6', '7', 219 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 220 }; 221 char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] }; 222 return new String (array); 223 } 224 225 230 static public String toHex (char c) 231 { 232 byte hi = (byte) (c >>> 8); 233 byte lo = (byte) (c & 0xff); 234 return toHex(hi) + toHex(lo); 235 } 237 238 239 244 public static String initCap (String in) 245 { 246 if (in == null || in.length() == 0) 247 return in; 248 boolean capitalize = true; 250 char[] data = in.toCharArray(); 251 for (int i = 0; i < data.length; i++) 252 { 253 if (data[i] == ' ' || Character.isWhitespace(data[i])) 254 capitalize = true; 255 else if (capitalize) 256 { 257 data[i] = Character.toUpperCase (data[i]); 258 capitalize = false; 259 } 260 else 261 data[i] = Character.toLowerCase (data[i]); 262 } 263 return new String (data); 264 } 266 267 268 275 static public AttributedCharacterIterator getIterator (AttributedString aString, AttributedCharacterIterator.Attribute [] relevantAttributes) 276 { 277 AttributedCharacterIterator iter = aString.getIterator(); 278 Set set = iter.getAllAttributeKeys(); 279 if (set.size() == 0) 281 return iter; 282 Set unwanted = new HashSet (iter.getAllAttributeKeys()); 284 for (int i = 0; i < relevantAttributes.length; i++) 285 unwanted.remove(relevantAttributes[i]); 286 if (unwanted.size() == 0) 287 return iter; 288 289 StringBuffer sb = new StringBuffer (); 291 for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) 292 sb.append(c); 293 aString = new AttributedString (sb.toString()); 294 295 Iterator it = iter.getAllAttributeKeys().iterator(); 297 while (it.hasNext()) 298 { 299 AttributedCharacterIterator.Attribute att = (AttributedCharacterIterator.Attribute )it.next(); 300 if (!unwanted.contains(att)) 301 { 302 for (char c = iter.first(); c != AttributedCharacterIterator.DONE; c = iter.next()) 303 { 304 Object value = iter.getAttribute(att); 305 if (value != null) 306 { 307 int start = iter.getRunStart(att); 308 int limit = iter.getRunLimit(att); 309 aString.addAttribute(att, value, start, limit); 311 iter.setIndex(limit); 312 } 313 } 314 } 315 } 318 return aString.getIterator(); 319 } 321 322 326 static public void dump (Map map) 327 { 328 System.out.println("Dump Map - size=" + map.size()); 329 Iterator it = map.keySet().iterator(); 330 while (it.hasNext()) 331 { 332 Object key = it.next(); 333 Object value = map.get(key); 334 System.out.println(key + "=" + value); 335 } 336 } 338 339 340 344 public static void main (String [] args) 345 { 346 AttributedString aString = new AttributedString ("test test"); 347 aString.addAttribute(TextAttribute.FOREGROUND, Color.blue); 348 aString.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, 2, 4); 349 getIterator (aString, new AttributedCharacterIterator.Attribute [] {TextAttribute.UNDERLINE}); 350 } 352 } | Popular Tags |