KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webman > stager > SiteDocumentsListing


1 package webman.stager;
2
3 import java.io.*;
4 import java.util.*;
5
6 /**
7  * holds list of site documents
8  * can read data from and write data to a config file
9  * @author $Author: torsten $
10  * @version $Revision: 1.11 $
11  */

12 public class SiteDocumentsListing implements java.io.Serializable JavaDoc
13 {
14     private final static String JavaDoc NEWLINE = System.getProperty("line.separator");
15     Hashtable directories = new Hashtable();
16     String JavaDoc fuckJTest = "";
17
18     /**
19      * create a new listing
20      */

21     public SiteDocumentsListing(){}
22
23     /**
24      * loads an old listing
25      */

26     public SiteDocumentsListing(String JavaDoc configFile) throws IOException
27     {
28         this();
29         initFrom(configFile);
30     }
31     
32     /**
33      * adds a file to the listing
34      */

35     public void addDocument(String JavaDoc directory, String JavaDoc documentName, long date, long size, String JavaDoc targetdir)
36     {
37     // save all paths in unix form
38
String JavaDoc dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory );
39         String JavaDoc target = ( File.separatorChar != '/' ? getUnixPath(targetdir) : targetdir );
40         SiteDocument sd = new SiteDocument(documentName, date, size, target);
41         // get directory map or create new one
42
Hashtable d = (Hashtable)directories.get(dir);
43         if ( d == null )
44         {
45             d = new Hashtable();
46             directories.put(dir, d);
47         }
48         // set new entry or overwrite old one
49
d.put(sd.getName(), sd);
50     }
51
52     public String JavaDoc[] getDocumentNames(String JavaDoc directory)
53     {
54     // all paths are in unix form
55
String JavaDoc dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory );
56         Hashtable d = (Hashtable)directories.get(dir);
57         return (String JavaDoc[])d.keySet().toArray(new String JavaDoc[]{});
58     }
59
60     public String JavaDoc getTargetDir(String JavaDoc directory, String JavaDoc file)
61     {
62         // all paths are in unix form
63
String JavaDoc 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         // convert path to platform form
75
return ( File.separatorChar != '/' ? getPlatformPath(sd.target) : sd.target );
76     }
77
78     public String JavaDoc[] getDirectories()
79     {
80         String JavaDoc[] res = (String JavaDoc[])directories.keySet().toArray(new String JavaDoc[]{});
81         // convert path to platform form
82
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     /**
90      * used to determine which files to transfer (from editoral server)
91      * call method only on new listing !!!
92      */

93     public String JavaDoc[] getChangedAndNewDocumentNames(String JavaDoc directory, SiteDocumentsListing oldListing)
94     {
95         // all paths are in unix form
96
String JavaDoc dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory );
97         Collection res = new Vector(((Hashtable)directories.get(dir)).values());
98         Object JavaDoc 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 JavaDoc[] neue = res.toArray();
104             for ( int i = 0; i < neue.length; i++ )
105             {
106                 // remove any document that is in old and new listing the same
107
if ( alte.contains(neue[i]) )
108                 {
109                     res.remove(neue[i]);
110                 }
111             }
112         }
113         // build list of names
114
String JavaDoc[] result = new String JavaDoc[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     /**
125      * used to determine which documents to delete (on online server)
126      * call method only on old listing !!!
127      */

128     public String JavaDoc[] getNoMoreExistingDocuments(String JavaDoc directory, SiteDocumentsListing newListing)
129     {
130         // all paths are in unix form
131
String JavaDoc dir = ( File.separatorChar != '/' ? getUnixPath(directory) : directory );
132         Set res = ((Hashtable)directories.get(dir)).keySet();
133         Object JavaDoc tneue = newListing.directories.get(dir);
134         // if directory doesn't exist in new listing, delete all docs of old one
135
if ( tneue != null )
136         {
137             Set neue = ((Hashtable)tneue).keySet();
138             Object JavaDoc[] alte = res.toArray();
139             for ( int i = 0; i < alte.length; i++ )
140             {
141                 // remove any document name that is in old and in new listing the same
142
if ( neue.contains(alte[i]) )
143                 {
144                     res.remove(alte[i]);
145                 }
146             }
147         }
148         return (String JavaDoc[])res.toArray(new String JavaDoc[]{});
149     }
150
151     /**
152      * used to determine which directories to delete
153      * call method only on old listing !!!
154      * returns directory names as list with subdirectories before their
155      * parent directories
156      */

157     public String JavaDoc[] getNoMoreExistingDirectories(SiteDocumentsListing newListing)
158     {
159         Set res = new HashSet(directories.keySet());
160         Object JavaDoc[] neue = newListing.directories.keySet().toArray();
161         Object JavaDoc[] alte = res.toArray();
162         for ( int i = 0; i < alte.length; i++ )
163         {
164             // dont delete directories that are also in new listing
165
// or have a subdir in newlisting
166
//System.out.println("old dir: " + alte[i] + "<br>");
167
boolean found = false;
168             for ( int k = 0; k < neue.length && !found; k++ )
169             {
170                 //System.out.println("new dir: " + neue[k] + "<br>");
171
if ( neue[k].equals(alte[i]) || ((String JavaDoc)neue[k]).startsWith((String JavaDoc)alte[i]) )
172                 {
173                     found = true;
174                 }
175             }
176             // remove found directories from result
177
if ( found )
178             {
179                 res.remove(alte[i]);
180             }
181         }
182         String JavaDoc[] result = (String JavaDoc[])res.toArray(new String JavaDoc[]{});
183         // bring longer names to front positions
184
Arrays.sort(result, new Comparator()
185                             { public int compare(Object JavaDoc o1, Object JavaDoc o2)
186                                 {
187                                     int l1 = ((String JavaDoc)o1).length();
188                                     int l2 = ((String JavaDoc)o2).length();
189                                     return ( l1 > l2 ? -1 : ( l2 > l1 ? 1 : 0) );
190                                 }
191                             });
192         // convert path to platform form
193
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 JavaDoc 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 JavaDoc e)
212         {
213             // is in this case a io exception
214
throw new IOException(e.toString());
215         }
216         istream.close();
217     }
218
219     public void saveToFile(String JavaDoc 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         { //Muss dies so umständlich machen, da JTest "oos.close()" in einem finally-Block haben will!
229
throw e;
230         }
231         finally
232         {
233             if (oos != null )
234             {
235                 try
236                 {
237                     oos.close(); //ich denke, man sollte den oos nach gebrauch wieder schließen
238
}
239                 catch (IOException e)
240                 {
241                     fuckJTest = "Wat soll ickn hier noch machen??";
242                 }
243             }
244         }
245     }
246
247     /**
248      * performs a substitution of \ to /
249      */

250     String JavaDoc getUnixPath(String JavaDoc path)
251     {
252         return ( path == null ? null : path.replace('\\', '/') );
253     }
254     
255     /**
256      * performs a substitution of / to file separator character
257      */

258     String JavaDoc getPlatformPath(String JavaDoc path)
259     {
260         return path.replace('/', File.separatorChar);
261     }
262
263     public String JavaDoc toString()
264     {
265         String JavaDoc res = "SiteDocumentsListing:" + NEWLINE;
266         String JavaDoc[] dirs = getDirectories();
267         for ( int i = 0; i < dirs.length; i++ )
268         {
269             res += dirs[i] + NEWLINE;
270             String JavaDoc[] 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 JavaDoc name;
283     long date;
284     long size;
285     String JavaDoc target = null;
286
287     SiteDocument(String JavaDoc name, long date, long size, String JavaDoc target)
288     {
289         this.name = name;
290         this.date = date;
291         this.size = size;
292         this.target = target;
293     }
294
295     SiteDocument(String JavaDoc name, long date, long size)
296     {
297         this.name = name;
298         this.date = date;
299         this.size = size;
300     }
301
302     public String JavaDoc getName()
303     {
304         return name;
305     }
306
307     public boolean equals(Object JavaDoc 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 JavaDoc toString()
328     {
329         return "SiteDocument(name=" + name
330             + ", date=" + date
331             + ", size=" + size
332             + ", target=" + target + ")";
333         
334     }
335 }
Popular Tags