1 9 package org.jboss.portal.common.util; 10 11 import java.util.Iterator ; 12 import java.util.Comparator ; 13 import java.util.List ; 14 import java.util.ArrayList ; 15 import java.util.SortedSet ; 16 import java.util.TreeSet ; 17 import java.util.Collections ; 18 import java.util.Enumeration ; 19 import java.util.jar.JarInputStream ; 20 import java.util.jar.JarEntry ; 21 import java.util.jar.JarFile ; 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.net.MalformedURLException ; 25 26 30 public class Jar 31 { 32 33 36 private static final Comparator comparator = new Comparator () 37 { 38 public int compare(Object o1, Object o2) 39 { 40 EntryInfo e1 = (EntryInfo)o1; 41 EntryInfo e2 = (EntryInfo)o2; 42 Iterator i1 = e1.iterator(); 43 Iterator i2 = e2.iterator(); 44 while (true) 45 { 46 if (i1.hasNext()) 47 { 48 Object o = i1.next(); 49 String s1 = (String )o; 50 if (i2.hasNext()) 51 { 52 String s2 = (String )i2.next(); 53 int res = s1.compareTo(s2); 54 if (res != 0) 55 { 56 return res; 57 } 58 } 59 else 60 { 61 return 1; 62 } 63 } 64 else 65 { 66 if (i2.hasNext()) 67 { 68 return -1; 69 } 70 else 71 { 72 return 0; 73 } 74 } 75 } 76 } 77 }; 78 79 public static Iterator iterator(JarFile file) throws IOException , IllegalArgumentException 80 { 81 if (file == null) 82 { 83 throw new IllegalArgumentException (); 84 } 85 SortedSet set = new TreeSet (comparator); 86 for (Enumeration e = file.entries();e.hasMoreElements();) 87 { 88 JarEntry entry = (JarEntry )e.nextElement(); 89 EntryInfo info = new EntryInfo(entry); 90 set.add(info); 91 } 92 return set.iterator(); 93 } 94 95 public static Iterator iterator(JarInputStream in) throws IOException , IllegalArgumentException 96 { 97 if (in == null) 98 { 99 throw new IllegalArgumentException (); 100 } 101 SortedSet set = new TreeSet (comparator); 102 for (JarEntry entry = in.getNextJarEntry();entry !=null;entry = in.getNextJarEntry()) 103 { 104 Jar.EntryInfo info = new EntryInfo(entry); 105 set.add(info); 106 } 107 return set.iterator(); 108 } 109 110 113 public static class EntryInfo 114 { 115 116 private final JarEntry entry; 117 private final List atoms; 118 119 public EntryInfo(JarEntry entry) 120 { 121 if (entry == null) 122 { 123 throw new IllegalArgumentException (); 124 } 125 this.entry = entry; 126 String name = entry.getName(); 127 ArrayList atoms = new ArrayList (); 128 int previous = -1; 129 while (true) 130 { 131 int current = name.indexOf('/', previous + 1); 132 if (current == -1) 133 { 134 current = name.length(); 135 } 136 if (current >= name.length() - 1) 137 { 138 if (current - previous > 1) 139 { 140 atoms.add(name.substring(previous + 1, current)); 141 } 142 break; 143 } 144 if (current - previous > 1) 145 { 146 atoms.add(name.substring(previous + 1, current)); 147 } 148 previous = current; 149 } 150 this.atoms = Collections.unmodifiableList(atoms); 151 } 152 153 public Iterator iterator() 154 { 155 return atoms.iterator(); 156 } 157 158 public JarEntry getEntry() 159 { 160 return entry; 161 } 162 163 public boolean isDirectory() 164 { 165 return entry.isDirectory(); 166 } 167 168 public int size() 169 { 170 return atoms.size(); 171 } 172 173 public List getAtoms() 174 { 175 return atoms; 176 } 177 178 public String get(int index) 179 { 180 return (String )atoms.get(index); 181 } 182 183 public boolean isChildOf(EntryInfo parent) throws IllegalArgumentException 184 { 185 if (parent == null) 186 { 187 throw new IllegalArgumentException (); 188 } 189 if (!parent.isDirectory()) 190 { 191 return false; 192 } 193 if (parent.size() + 1 != atoms.size()) 194 { 195 return false; 196 } 197 return parent.atoms.equals(parent.atoms.subList(0, atoms.size() - 1)); 198 } 199 200 public boolean isDescendantOf(EntryInfo ancestor) throws IllegalArgumentException 201 { 202 if (ancestor == null) 203 { 204 throw new IllegalArgumentException (); 205 } 206 if (!ancestor.isDirectory()) 207 { 208 return false; 209 } 210 if (ancestor.size() >= atoms.size()) 211 { 212 return false; 213 } 214 return ancestor.atoms.equals(atoms.subList(0, ancestor.size())); 215 } 216 217 public URL toURL(URL jarURL) throws IllegalArgumentException , IllegalStateException , MalformedURLException 218 { 219 if (jarURL == null) 220 { 221 throw new IllegalArgumentException ("No null jarURL"); 222 } 223 if (isDirectory()) 224 { 225 throw new IllegalStateException ("Cannot create dir URL"); 226 } 227 StringBuffer tmp = new StringBuffer (jarURL.toString()).append("!/"); 228 for (int i = 0; i < atoms.size(); i++) 229 { 230 String atom = (String )atoms.get(i); 231 tmp.append(i > 0 ? "/" : "").append(atom); 232 } 233 return new URL ("jar", "", tmp.toString()); 234 } 235 236 public String toString() 237 { 238 StringBuffer tmp = new StringBuffer (); 239 for (int i = 0; i < atoms.size(); i++) 240 { 241 String atom = (String )atoms.get(i); 242 tmp.append(i > 0 ? "/" : "").append(atom); 243 } 244 if (entry.isDirectory()) 245 { 246 tmp.append("/"); 247 } 248 return tmp.toString(); 249 } 250 } 251 252 253 254 } 255 | Popular Tags |