1 57 58 package org.apache.soap.util; 59 60 import java.io.*; 61 import java.util.*; 62 import java.net.URL ; 63 import java.net.MalformedURLException ; 64 import java.beans.Introspector ; 65 66 71 public class StringUtils 72 { 73 public static final String lineSeparator = 74 System.getProperty("line.separator", "\n"); 75 public static String URI_SEPARATION_CHAR = "@"; 76 77 88 public static String getClassName(Class targetClass) 89 { 90 String className = targetClass.getName(); 91 92 return targetClass.isArray() ? parseDescriptor(className) : className; 93 } 94 95 98 private static String parseDescriptor(String className) 99 { 100 char[] classNameChars = className.toCharArray(); 101 int arrayDim = 0; 102 int i = 0; 103 104 while (classNameChars[i] == '[') 105 { 106 arrayDim++; 107 i++; 108 } 109 110 StringBuffer classNameBuf = new StringBuffer (); 111 112 switch (classNameChars[i++]) 113 { 114 case 'B' : classNameBuf.append("byte"); 115 break; 116 case 'C' : classNameBuf.append("char"); 117 break; 118 case 'D' : classNameBuf.append("double"); 119 break; 120 case 'F' : classNameBuf.append("float"); 121 break; 122 case 'I' : classNameBuf.append("int"); 123 break; 124 case 'J' : classNameBuf.append("long"); 125 break; 126 case 'S' : classNameBuf.append("short"); 127 break; 128 case 'Z' : classNameBuf.append("boolean"); 129 break; 130 case 'L' : classNameBuf.append(classNameChars, 131 i, classNameChars.length - i - 1); 132 break; 133 } 134 135 for (i = 0; i < arrayDim; i++) 136 classNameBuf.append("[]"); 137 138 return classNameBuf.toString(); 139 } 140 141 145 private static URL getURL(URL contextURL, String spec, int recursiveDepth) 146 throws MalformedURLException 147 { 148 URL url = null; 149 150 try 151 { 152 url = new URL (contextURL, spec); 153 154 try 155 { 156 url.openStream(); 157 } 158 catch (IOException ioe1) 159 { 160 throw new MalformedURLException ("This file was not found: " + url); 161 } 162 } 163 catch (MalformedURLException e1) 164 { 165 url = new URL ("file", "", spec); 166 167 try 168 { 169 url.openStream(); 170 } 171 catch (IOException ioe2) 172 { 173 if (contextURL != null) 174 { 175 String contextFileName = contextURL.getFile(); 176 String parentName = new File(contextFileName).getParent(); 177 178 if (parentName != null && recursiveDepth < 3) 179 { 180 return getURL(new URL ("file", "", parentName + '/'), 181 spec, 182 recursiveDepth + 1); 183 } 184 } 185 186 throw new MalformedURLException ("This file was not found: " + url); 187 } 188 } 189 190 return url; 191 } 192 193 195 public static URL getURL(URL contextURL, String spec) throws MalformedURLException 196 { 197 return getURL(contextURL, spec, 1); 198 } 199 200 204 public static Reader getContentAsReader(URL url) throws SecurityException , 205 IllegalArgumentException , 206 IOException 207 { 208 if (url == null) 209 { 210 throw new IllegalArgumentException ("URL cannot be null."); 211 } 212 213 try 214 { 215 Object content = url.getContent(); 216 217 if (content == null) 218 { 219 throw new IllegalArgumentException ("No content."); 220 } 221 222 if (content instanceof InputStream) 223 { 224 Reader in = new InputStreamReader((InputStream)content); 225 226 if (in.ready()) 227 { 228 return in; 229 } 230 else 231 { 232 throw new FileNotFoundException(); 233 } 234 } 235 else 236 { 237 throw new IllegalArgumentException ((content instanceof String ) 238 ? (String )content 239 : "This URL points to a: " + 240 StringUtils.getClassName(content.getClass())); 241 } 242 } 243 catch (SecurityException e) 244 { 245 throw new SecurityException ("Your JVM's SecurityManager has disallowed this."); 246 } 247 catch (FileNotFoundException e) 248 { 249 throw new FileNotFoundException("This file was not found: " + url); 250 } 251 } 252 253 256 public static String getContentAsString(URL url) throws SecurityException , 257 IllegalArgumentException , 258 IOException 259 { 260 return IOUtils.getStringFromReader(getContentAsReader(url)); 261 } 262 263 273 public static String parseFullTargetObjectURI(String fullTargetObjectURI) { 274 if ( fullTargetObjectURI == null ) return null ; 275 int delimIndex = fullTargetObjectURI.indexOf(URI_SEPARATION_CHAR); 276 if ( (fullTargetObjectURI != null) && (delimIndex != -1) ) 277 return fullTargetObjectURI.substring(0,delimIndex); 278 else 279 return fullTargetObjectURI; 280 281 } 282 283 284 } 285 | Popular Tags |