1 package webman.stager; 2 3 import java.io.*; 4 import java.util.*; 5 6 12 public class SiteDocumentsListing implements java.io.Serializable 13 { 14 private final static String NEWLINE = System.getProperty("line.separator"); 15 Hashtable directories = new Hashtable(); 16 String fuckJTest = ""; 17 18 21 public SiteDocumentsListing(){} 22 23 26 public SiteDocumentsListing(String configFile) throws IOException 27 { 28 this(); 29 initFrom(configFile); 30 } 31 32 35 public void addDocument(String directory, String documentName, long date, long size, String targetdir) 36 { 37 String dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory ); 39 String target = ( File.separatorChar != '/' ? getUnixPath(targetdir) : targetdir ); 40 SiteDocument sd = new SiteDocument(documentName, date, size, target); 41 Hashtable d = (Hashtable)directories.get(dir); 43 if ( d == null ) 44 { 45 d = new Hashtable(); 46 directories.put(dir, d); 47 } 48 d.put(sd.getName(), sd); 50 } 51 52 public String [] getDocumentNames(String directory) 53 { 54 String dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory ); 56 Hashtable d = (Hashtable)directories.get(dir); 57 return (String [])d.keySet().toArray(new String []{}); 58 } 59 60 public String getTargetDir(String directory, String file) 61 { 62 String dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory ); 64 Hashtable d = (Hashtable)directories.get(dir); 65 if ( d == null ) 66 { 67 return null; 68 } 69 SiteDocument sd = (SiteDocument)d.get(file); 70 if ( sd == null ) 71 { 72 return null; 73 } 74 return ( File.separatorChar != '/' ? getPlatformPath(sd.target) : sd.target ); 76 } 77 78 public String [] getDirectories() 79 { 80 String [] res = (String [])directories.keySet().toArray(new String []{}); 81 if ( File.separatorChar != '/' ) 83 { 84 for ( int i = 0; i < res.length; i++ ) res[i] = getPlatformPath(res[i]); 85 } 86 return res; 87 } 88 89 93 public String [] getChangedAndNewDocumentNames(String directory, SiteDocumentsListing oldListing) 94 { 95 String dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory ); 97 Collection res = new Vector(((Hashtable)directories.get(dir)).values()); 98 Object alteH = null; 99 if ( oldListing != null && oldListing.directories.get(dir) != null ) 100 { 101 alteH = oldListing.directories.get(dir); 102 Collection alte = ((Hashtable)alteH).values(); 103 Object [] neue = res.toArray(); 104 for ( int i = 0; i < neue.length; i++ ) 105 { 106 if ( alte.contains(neue[i]) ) 108 { 109 res.remove(neue[i]); 110 } 111 } 112 } 113 String [] result = new String [res.size()]; 115 Iterator it = res.iterator(); 116 int i = 0; 117 while ( it.hasNext() ) 118 { 119 result[i++] = ((SiteDocument)it.next()).getName(); 120 } 121 return result; 122 } 123 124 128 public String [] getNoMoreExistingDocuments(String directory, SiteDocumentsListing newListing) 129 { 130 String dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory ); 132 Set res = ((Hashtable)directories.get(dir)).keySet(); 133 Object tneue = newListing.directories.get(dir); 134 if ( tneue != null ) 136 { 137 Set neue = ((Hashtable)tneue).keySet(); 138 Object [] alte = res.toArray(); 139 for ( int i = 0; i < alte.length; i++ ) 140 { 141 if ( neue.contains(alte[i]) ) 143 { 144 res.remove(alte[i]); 145 } 146 } 147 } 148 return (String [])res.toArray(new String []{}); 149 } 150 151 157 public String [] getNoMoreExistingDirectories(SiteDocumentsListing newListing) 158 { 159 Set res = new HashSet(directories.keySet()); 160 Object [] neue = newListing.directories.keySet().toArray(); 161 Object [] alte = res.toArray(); 162 for ( int i = 0; i < alte.length; i++ ) 163 { 164 boolean found = false; 168 for ( int k = 0; k < neue.length && !found; k++ ) 169 { 170 if ( neue[k].equals(alte[i]) || ((String )neue[k]).startsWith((String )alte[i]) ) 172 { 173 found = true; 174 } 175 } 176 if ( found ) 178 { 179 res.remove(alte[i]); 180 } 181 } 182 String [] result = (String [])res.toArray(new String []{}); 183 Arrays.sort(result, new Comparator() 185 { public int compare(Object o1, Object o2) 186 { 187 int l1 = ((String )o1).length(); 188 int l2 = ((String )o2).length(); 189 return ( l1 > l2 ? -1 : ( l2 > l1 ? 1 : 0) ); 190 } 191 }); 192 if ( File.separatorChar != '/' ) 194 { 195 for ( int i = 0; i < result.length; i++ ) 196 { 197 result[i] = getPlatformPath(result[i]); 198 } 199 } 200 return result; 201 } 202 203 void initFrom(String file) throws IOException 204 { 205 FileInputStream istream = new FileInputStream(file); 206 ObjectInputStream ois = new ObjectInputStream(istream); 207 try 208 { 209 directories = (Hashtable)ois.readObject(); 210 } 211 catch(ClassNotFoundException e) 212 { 213 throw new IOException(e.toString()); 215 } 216 istream.close(); 217 } 218 219 public void saveToFile(String file) throws IOException 220 { 221 ObjectOutputStream oos = null; 222 try 223 { 224 oos = new ObjectOutputStream(new FileOutputStream(file)); 225 oos.writeObject(directories); 226 } 227 catch (IOException e) 228 { throw e; 230 } 231 finally 232 { 233 if (oos != null ) 234 { 235 try 236 { 237 oos.close(); } 239 catch (IOException e) 240 { 241 fuckJTest = "Wat soll ickn hier noch machen??"; 242 } 243 } 244 } 245 } 246 247 250 String getUnixPath(String path) 251 { 252 return ( path == null ? null : path.replace('\\', '/') ); 253 } 254 255 258 String getPlatformPath(String path) 259 { 260 return path.replace('/', File.separatorChar); 261 } 262 263 public String toString() 264 { 265 String res = "SiteDocumentsListing:" + NEWLINE; 266 String [] dirs = getDirectories(); 267 for ( int i = 0; i < dirs.length; i++ ) 268 { 269 res += dirs[i] + NEWLINE; 270 String [] docs = getDocumentNames(dirs[i]); 271 for ( int k = 0; k < docs.length; k++ ) 272 { 273 res += " " + docs[k] + NEWLINE; 274 } 275 } 276 return res; 277 } 278 } 279 280 class SiteDocument implements Serializable 281 { 282 String name; 283 long date; 284 long size; 285 String target = null; 286 287 SiteDocument(String name, long date, long size, String target) 288 { 289 this.name = name; 290 this.date = date; 291 this.size = size; 292 this.target = target; 293 } 294 295 SiteDocument(String name, long date, long size) 296 { 297 this.name = name; 298 this.date = date; 299 this.size = size; 300 } 301 302 public String getName() 303 { 304 return name; 305 } 306 307 public boolean equals(Object other) 308 { 309 if ( other == null || other.getClass() != getClass() ) 310 { 311 return false; 312 } 313 SiteDocument o = (SiteDocument)other; 314 return name.equals(o.name) 315 && date == o.date 316 && size == o.size 317 && ( (target == null && o.target == null) 318 || (target != null && target.equals(o.target)) 319 ); 320 } 321 322 public int hashCode() 323 { 324 return super.hashCode(); 325 } 326 327 public String toString() 328 { 329 return "SiteDocument(name=" + name 330 + ", date=" + date 331 + ", size=" + size 332 + ", target=" + target + ")"; 333 334 } 335 } | Popular Tags |