1 18 19 package org.apache.jorphan.util; 20 21 import java.io.UnsupportedEncodingException ; 22 import java.lang.reflect.Method ; 23 import java.net.URLDecoder ; 24 import java.net.URLEncoder ; 25 import java.util.Vector ; 26 27 import junit.framework.TestCase; 28 29 36 public final class JOrphanUtils 37 { 38 41 private JOrphanUtils() 42 { 43 } 44 45 58 public static String [] split(String splittee, String splitChar) 59 { 60 if (splittee == null || splitChar == null) 61 { 62 return new String [0]; 63 } 64 int spot; 65 while ((spot = splittee.indexOf(splitChar + splitChar)) != -1) 66 { 67 splittee = 68 splittee.substring(0, spot + splitChar.length()) 69 + splittee.substring( 70 spot + 2 * splitChar.length(), 71 splittee.length()); 72 } 73 Vector returns = new Vector (); 74 int start = 0; 75 int length = splittee.length(); 76 spot = 0; 77 while (start < length 78 && (spot = splittee.indexOf(splitChar, start)) > -1) 79 { 80 if (spot > 0) 81 { 82 returns.addElement(splittee.substring(start, spot)); 83 } 84 start = spot + splitChar.length(); 85 } 86 if (start < length) 87 { 88 returns.add(splittee.substring(start)); 89 } 90 String [] values = new String [returns.size()]; 91 returns.copyInto(values); 92 return values; 93 } 94 95 private static final String SPACES = " "; 96 private static final int SPACES_LEN = SPACES.length(); 97 98 106 public static StringBuffer rightAlign(StringBuffer in, int len){ 107 int pfx = len - in.length(); 108 if (pfx <= 0 ) return in; 109 if (pfx > SPACES_LEN) pfx = SPACES_LEN; 110 in.insert(0,SPACES.substring(0,pfx)); 111 return in; 112 } 113 114 122 public static StringBuffer leftAlign(StringBuffer in, int len){ 123 int sfx = len - in.length(); 124 if (sfx <= 0 ) return in; 125 if (sfx > SPACES_LEN) sfx = SPACES_LEN; 126 in.append(SPACES.substring(0,sfx)); 127 return in; 128 } 129 130 138 public static String booleanToString(boolean value){ 139 return value ? "true" : "false"; 140 } 141 142 150 public static String booleanToSTRING(boolean value){ 151 return value ? "TRUE" : "FALSE"; 152 } 153 154 160 public static Boolean valueOf(boolean value) 161 { 162 return value ? Boolean.TRUE : Boolean.FALSE; 163 } 164 165 private static Method decodeMethod = null; 166 private static Method encodeMethod = null; 167 168 static { 169 Class URLEncoder = URLEncoder .class; 170 Class URLDecoder = URLDecoder .class; 171 Class [] argTypes = { String .class, String .class }; 172 try 173 { 174 decodeMethod = URLDecoder.getMethod("decode",argTypes); 175 encodeMethod = URLEncoder.getMethod("encode",argTypes); 176 } 178 catch (Exception e) 179 { 180 } 182 } 184 185 193 public static String encode(String string, String encoding) 194 throws UnsupportedEncodingException 195 { 196 if (encodeMethod != null) { 197 Object args [] = {string,encoding}; 199 try 200 { 201 return (String ) encodeMethod.invoke(null, args ); 202 } 203 catch (Exception e) 204 { 205 e.printStackTrace(); 206 return string; 207 } 208 } else { 209 return URLEncoder.encode(string); 210 } 211 212 } 213 214 222 public static String decode(String string, String encoding) 223 throws UnsupportedEncodingException 224 { 225 if (decodeMethod != null) { 226 Object args [] = {string,encoding}; 228 try { 229 return (String ) decodeMethod.invoke(null, args ); 230 } 231 catch (Exception e) 232 { 233 e.printStackTrace(); 234 return string; 235 } 236 } else { 237 return URLDecoder.decode(string); 238 } 239 } 240 241 250 public static String replaceFirst(String source, String search,String replace) 251 { 252 int start=source.indexOf(search); 253 int len=search.length(); 254 if (start == -1) return source; 255 if (start == 0) return replace+source.substring(len); 256 return source.substring(0,start)+replace+source.substring(start+len); 257 } 258 259 public static class Test extends TestCase 260 { 261 public void testReplace1() 262 { 263 assertEquals("xyzdef",replaceFirst("abcdef","abc","xyz")); 264 } 265 public void testReplace2() 266 { 267 assertEquals("axyzdef",replaceFirst("abcdef","bc","xyz")); 268 } 269 public void testReplace3() 270 { 271 assertEquals("abcxyz",replaceFirst("abcdef","def","xyz")); 272 } 273 public void testReplace4() 274 { 275 assertEquals("abcdef",replaceFirst("abcdef","bce","xyz")); 276 } 277 public void testReplace5() 278 { 279 assertEquals("abcdef",replaceFirst("abcdef","alt=\"\" ","")); 280 } 281 public void testReplace6() 282 { 283 assertEquals("abcdef",replaceFirst("abcdef","alt=\"\" ","")); 284 } 285 public void testReplace7() 286 { 287 assertEquals("alt=\"\"",replaceFirst("alt=\"\"","alt=\"\" ","")); 288 } 289 public void testReplace8() 290 { 291 assertEquals("img SRC=xyz ",replaceFirst("img SRC=xyz alt=\"\" ","alt=\"\" ","")); 292 } 293 public void testSplit1() 294 { 295 String in="a,bc,,"; String out[]=split(in,","); 297 assertEquals(2,out.length); 298 assertEquals("a",out[0]); 299 assertEquals("bc",out[1]); 300 } 301 public void testSplit2() 302 { 303 String in=",,a,bc"; String out[]=split(in,","); 305 assertEquals("Should detect the leading split chars; ",2,out.length-2); 306 assertEquals("",out[0]); 307 assertEquals("",out[1]); 308 assertEquals("a",out[2]); 309 assertEquals("bc",out[3]); 310 } 311 } 312 } 313 | Popular Tags |