1 2 23 package org.enhydra.tool.common; 24 25 import java.io.File ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 import java.io.FileNotFoundException ; 30 import java.net.JarURLConnection ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.util.jar.JarEntry ; 34 import java.util.jar.JarFile ; 35 import java.util.Arrays ; 36 import java.util.ArrayList ; 37 import java.util.Enumeration ; 38 39 public class Template { 41 private File output = null; 42 private URL url = null; 43 private String base = new String (); 44 45 public Template(URL u, String b) { 47 url = u; 48 base = b.replace('\\', '/'); 49 } 50 51 public Template(File f, String b) { 52 try { 53 url = f.toURL(); 54 base = b.replace('\\', '/'); 55 } catch (MalformedURLException e) { 56 e.printStackTrace(System.err); 57 } 58 } 59 60 public InputStream getInputStream() throws IOException { 61 return url.openStream(); 62 } 63 64 public Template[] listTemplates() { 65 Template[] children = new Template[0]; 66 67 children = listTemplates(null); 68 return children; 69 } 70 71 public Template[] listTemplates(TemplateFilter filter) { 72 Template[] children = new Template[0]; 73 ArrayList list = null; 74 Template[] jarTemplates = new Template[0]; 75 Template[] fileTemplates = new Template[0]; 76 77 jarTemplates = listJarTemplates(filter); 78 fileTemplates = listFileTemplates(filter); 79 list = new ArrayList (Arrays.asList(fileTemplates)); 80 for (int j = 0; j < jarTemplates.length; j++) { 81 boolean override = false; 82 83 for (int f = 0; f < fileTemplates.length; f++) { 84 if (jarTemplates[j].getRelativePath().equals(fileTemplates[f].getRelativePath())) { 85 override = true; 86 break; 87 } 88 } 89 if (!override) { 90 list.add(jarTemplates[j]); 91 } 92 } 93 jarTemplates = new Template[0]; 94 fileTemplates = new Template[0]; 95 list.trimToSize(); 96 if (list.size() > 0) { 97 children = new Template[list.size()]; 98 children = (Template[]) list.toArray(children); 99 } 100 list.clear(); 101 return children; 102 } 103 104 private Template[] listJarTemplates(TemplateFilter filter) { 105 Template[] children = new Template[0]; 106 ArrayList list = new ArrayList (); 107 JarURLConnection connect = null; 108 JarFile jar = null; 109 JarEntry entry = null; 110 Enumeration entries = null; 111 URL baseUrl = null; 112 int index = -1; 113 114 if (url.getProtocol().equals("jar")) { 115 baseUrl = url; 116 } else { 117 StringBuffer buf = new StringBuffer (); 118 119 buf.append(getClass().getName().replace('.', '/')); 120 buf.append(".class"); 121 baseUrl = ClassLoader.getSystemResource(buf.toString()); 122 buf.setLength(0); 123 index = baseUrl.toString().indexOf('!'); 124 if (index > -1) { 125 buf.append(baseUrl.toString().substring(0, index + 1)); 126 buf.append('/'); 127 buf.append(getBasePath()); 128 if (!buf.toString().endsWith("/")) { 129 buf.append('/'); 130 } 131 try { 132 baseUrl = new URL (buf.toString()); 133 } catch (MalformedURLException e) { 134 baseUrl = null; 135 e.printStackTrace(System.err); 136 } 137 } else { 138 baseUrl = null; 139 } 140 } 141 if (baseUrl == null) { 142 entries = null; 143 } else { 144 try { 145 connect = (JarURLConnection ) baseUrl.openConnection(); 146 jar = connect.getJarFile(); 147 entries = jar.entries(); 148 } catch (FileNotFoundException e) { 149 System.err.println(e.getMessage()); 150 entries = null; 151 } catch (IOException e) { 152 e.printStackTrace(System.err); 153 entries = null; 154 } 155 } 156 if (entries == null) { 157 158 } else { 160 while (entries.hasMoreElements()) { 161 String entryName = new String (); 162 String parentPath = new String (); 163 164 entry = (JarEntry ) entries.nextElement(); 165 entryName = entry.getName(); 166 167 if (entryName.equals(getBasePath())) { 169 continue; 170 } 171 index = entryName.lastIndexOf('/'); 172 if (index > -1 && entry.isDirectory()) { 173 parentPath = entryName.substring(0, index); 174 } else { 175 parentPath = entryName; 176 } 177 index = parentPath.lastIndexOf('/'); 178 if (index > -1) { 179 parentPath = parentPath.substring(0, index + 1); 180 } 181 if (!parentPath.equals(getBasePath())) { 182 continue; 183 } 184 Template temp = null; 185 URL newUrl = null; 186 StringBuffer buf = new StringBuffer (); 187 188 buf.append(extractJarPath(baseUrl)); 189 buf.append(entryName); 190 try { 191 newUrl = new URL (buf.toString()); 192 temp = new Template(newUrl, base); 193 if (filter == null) { 194 list.add(temp); 195 } else if (filter.accept(temp)) { 196 list.add(temp); 197 } 198 } catch (MalformedURLException e) { 199 e.printStackTrace(System.err); 200 } 201 } 202 } 203 list.trimToSize(); 204 if (list.size() > 0) { 205 children = new Template[list.size()]; 206 children = (Template[]) list.toArray(children); 207 } 208 list.clear(); 209 return children; 210 } 211 212 private Template[] listFileTemplates(TemplateFilter filter) { 213 Template[] children = new Template[0]; 214 ArrayList list = new ArrayList (); 215 URL baseUrl = null; 216 File dir = null; 217 File [] files = new File [0]; 218 219 if (url.getProtocol().equalsIgnoreCase("file")) { 220 baseUrl = url; 221 } else if (url.toString().startsWith("jar:file:") 222 && url.toString().indexOf('!') > -1) { 223 StringBuffer buf = new StringBuffer (); 224 String path = url.toString(); 225 226 path = path.substring(4, url.toString().indexOf('!')); 227 path = path.substring(0, path.lastIndexOf('/')); 228 path = path.substring(0, path.lastIndexOf('/') + 1); 229 buf.append(path); 230 buf.append(getBasePath()); 231 try { 232 baseUrl = new URL (buf.toString()); 233 } catch (MalformedURLException e) { 234 e.printStackTrace(System.err); 235 baseUrl = null; 236 } 237 } 238 if (baseUrl == null) { 239 list.clear(); 240 } else { 241 dir = new File (baseUrl.getFile()); 242 if (dir.isDirectory()) { 243 files = dir.listFiles(); 244 for (int i = 0; i < files.length; i++) { 245 Template temp = new Template(files[i], base); 246 247 if (filter == null) { 248 list.add(temp); 249 } else if (filter.accept(temp)) { 250 list.add(temp); 251 } 252 } 253 } 254 } 255 list.trimToSize(); 256 if (list.size() > 0) { 257 children = new Template[list.size()]; 258 children = (Template[]) list.toArray(children); 259 } 260 list.clear(); 261 return children; 262 } 263 264 public int endsWith(String [] list) { 265 int ew = -1; 266 267 for (int i = 0; i < list.length; i++) { 268 if (endsWith(list[i])) { 269 ew = i; 270 break; 271 } 272 } 273 return ew; 274 } 275 276 public boolean endsWith(String in) { 277 boolean ew = false; 278 279 if (in == null || url == null) { 280 ew = false; 281 } else { 282 String compEnd = new String (in); 283 String compUrl = new String (url.toString()); 284 285 compEnd = compEnd.replace('\\', '/'); 286 if (compUrl.endsWith("/") && (!compEnd.endsWith("/"))) { 287 compEnd = compEnd + '/'; 288 } 289 compEnd = compEnd.toLowerCase(); 290 compUrl = compUrl.toLowerCase(); 291 ew = compUrl.endsWith(compEnd); 292 } 293 return ew; 294 } 295 296 public String getExtension() { 297 String ext = new String (); 298 int index = -1; 299 300 if (url == null) { 301 ext = new String (); 302 } else { 303 ext = new String (url.toString()); 304 ext = ext.replace('\\', '/'); 305 index = ext.lastIndexOf('/'); 306 if (index > -1) { 307 ext = ext.substring(index); 308 } 309 index = ext.lastIndexOf('.'); 310 if (index > -1 && (ext.length() > index)) { 311 ext = ext.substring(index + 1); 312 } else { 313 ext = new String (); 314 } 315 } 316 return ext; 317 } 318 319 public String getName() { 320 String n = new String (); 321 int index = -1; 322 323 if (url == null) { 324 n = new String (); 325 } else { 326 n = url.getFile(); 327 index = n.lastIndexOf('/'); 328 if (index == n.length()) { 329 n = n.substring(0, n.length() - 1); 330 } 331 index = n.lastIndexOf('/'); 332 n = n.substring(index); 333 } 334 return n; 335 } 336 337 private String getBasePath() { 338 StringBuffer buf = new StringBuffer (); 339 340 buf.append(base); 341 buf.append(getRelativePath()); 342 return buf.toString(); 343 } 344 345 private String extractJarPath(URL jarUrl) { 346 StringBuffer buf = new StringBuffer (); 347 int index = -1; 348 349 if (jarUrl.getProtocol().equalsIgnoreCase("jar")) { 350 index = jarUrl.toString().indexOf('!'); 351 if (index > -1) { 352 buf.append(jarUrl.toString().substring(0, index + 1)); 353 buf.append('/'); 354 } 355 } 356 return buf.toString(); 357 } 358 359 public String getRelativePath() { 360 String path = new String (); 361 String file = new String (); 362 int index = -1; 363 364 if (base == null || url == null) { 365 path = new String (); 366 } else { 367 file = url.getFile(); 368 index = file.indexOf(base); 369 if (index > -1) { 370 path = file.substring(index + base.length()); 371 } else { 372 index = file.lastIndexOf('/'); 373 if (index > -1) { 374 path = file.substring(index); 375 } else { 376 path = file; 377 } 378 } 379 } 380 if (path.startsWith("/")) { 381 if (path.length() == 1) { 382 path = "."; 383 } else { 384 path = path.substring(1, path.length()); 385 } 386 } 387 return path; 388 } 389 390 public boolean isDirectory() { 391 boolean isd = false; 392 393 if (url == null) { 394 isd = false; 395 } else if (url.getProtocol().equals("file")) { 396 File file = new File (url.getFile()); 397 398 isd = file.isDirectory(); 399 } else if (url.getProtocol().equals("jar")) { 400 JarURLConnection connect; 401 JarEntry entry = null; 402 403 try { 404 connect = (JarURLConnection ) url.openConnection(); 405 entry = connect.getJarEntry(); 406 isd = (entry.getSize() == 0) || entry.isDirectory() 407 || entry.getName().endsWith("/"); 408 } catch (IOException e) { 409 e.printStackTrace(System.err); 410 isd = false; 411 } 412 } 413 return isd; 414 } 415 416 public boolean isFile() { 417 boolean isf = false; 418 419 if (url == null) { 420 isf = false; 421 } else if (url.getProtocol().equals("file")) { 422 File file = new File (url.getFile()); 423 424 isf = file.isFile(); 425 } else if (url.getProtocol().equals("jar")) { 426 JarURLConnection connect; 427 428 try { 429 connect = (JarURLConnection ) url.openConnection(); 430 isf = (connect.getJarEntry().getSize() > 0); 431 } catch (IOException e) { 432 e.printStackTrace(System.err); 433 isf = false; 434 } 435 } 436 return isf; 437 } 438 439 public File getOutput() { 440 return output; 441 } 442 443 public void setOutput(File f) { 444 output = f; 445 } 446 447 public String toString() { 448 return url.toString(); 449 } 450 451 } 452 | Popular Tags |