001: package de.webman.generator;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: /**
007: * Class to read from and write to files that contain a directory listing
008: **/
009: public class DirectoryFileListing {
010:
011: /**
012: * the name of the file that contains the listing
013: */
014: protected static String DEFAULT_LISTING_NAME = "generated_files.list";
015:
016: /**
017: * a tag marking the beginning of a list of real files
018: **/
019: protected static String FILES_TAG = "[ files ]";
020:
021: /**
022: * a tag marking the beginning of a list of directories
023: **/
024: protected static String DIRECTORIES_TAG = "[ directories ]";
025:
026: /**
027: * a tag marking the end of the listing
028: **/
029: protected static String END_TAG = "[ end ]";
030:
031: /**
032: * the file itself
033: */
034: protected File file;
035:
036: /**
037: * list of file names read from the listing
038: **/
039: protected Vector oldFiles = new Vector();
040:
041: /**
042: * list of directory names read from the listing
043: **/
044: protected Vector oldDirectories = new Vector();
045:
046: /**
047: * list of file names added through <code>generatedFile(String)</code>
048: **/
049: protected Vector newFiles = new Vector();
050:
051: /**
052: * list of directory names added through <code>generatedFile(String)</code>
053: **/
054: protected Vector newDirectories = new Vector();
055:
056: /**
057: * Creates a listing for the directory with the given name
058: * @param dirname the directory to create or update the listing in
059: **/
060: public DirectoryFileListing(String dirname) throws IOException {
061: this (dirname, DEFAULT_LISTING_NAME);
062: }
063:
064: /**
065: * Creates a listing for the directory with the given name
066: * @param dirname the directory to create or update the listing in
067: **/
068: public DirectoryFileListing(String dirname, String listingName)
069: throws IOException {
070: // try to open file
071: file = new File(dirname, listingName);
072: // read listing
073: init();
074: }
075:
076: /**
077: * Creates a listing in the given file
078: * @param dirname the directory to create or update the listing in
079: **/
080: public DirectoryFileListing(File f) throws IOException {
081: file = f;
082: // read listing
083: init();
084: }
085:
086: /**
087: * @return all directory names that were read from the listing
088: * is empty if the listing don't exist
089: **/
090: public Enumeration getDirectoryEntries() {
091: return oldDirectories.elements();
092: }
093:
094: public String[] getDirectories() {
095: return (String[]) oldDirectories.toArray(new String[] {});
096: }
097:
098: /**
099: * @return all file names that were read from the listing
100: * is empty if the listing don't exist
101: **/
102: public Enumeration getFileEntries() {
103: return oldFiles.elements();
104: }
105:
106: public String[] getFiles() {
107: return (String[]) oldFiles.toArray(new String[] {});
108: }
109:
110: /**
111: * Adds a directory to the listing
112: * @param name the directory name to add
113: * depending on the constructor call the listing will be immediately saved
114: * @see FileListHandler#FileListHandler(String, boolean)
115: **/
116: public void generatedDirectory(String name) {
117: newDirectories.addElement(name);
118: }
119:
120: /**
121: * Adds a file to the listing
122: *@param name the file name to add
123: **/
124: public void generatedFile(String name) {
125: newFiles.addElement(name);
126: }
127:
128: /**
129: * @return all names of directories that were previously in the listing
130: * but not added through <code>generatedDirectory()</code>
131: **/
132: public Enumeration getDirectoriesToDelete() {
133: return vectorDifference(oldDirectories, newDirectories)
134: .elements();
135: }
136:
137: /**
138: * @return all names of files that were previously in the listing
139: * but not added through <code>generatedFile()</code>
140: **/
141: public Enumeration getFilesToDelete() {
142: return vectorDifference(oldFiles, newFiles).elements();
143: }
144:
145: /**
146: * @param addonly if set to false, the new listing will consist
147: * of all file and directory names added through
148: * <code>generatedFile(String)</code> or <code>generatedDirectory(String)</code>
149: * if set to true, the new listing will consist of all old and new entries
150: **/
151: public void save(boolean addonly) throws java.io.IOException{
152: if ( addonly ){
153: newDirectories = vectorUnion(newDirectories, oldDirectories);
154: newFiles = vectorUnion(newFiles, oldFiles);
155: }
156: if ( newDirectories.size() + newFiles.size() > 0 ){
157: PrintWriter out = new PrintWriter(new FileWriter(file));
158: out.println(FILES_TAG);
159: Enumeration enum = newFiles.elements();
160: while ( enum.hasMoreElements() )
161: out.println((String)enum.nextElement());
162: out.println(DIRECTORIES_TAG);
163: enum = newDirectories.elements();
164: while ( enum.hasMoreElements() )
165: out.println((String)enum.nextElement());
166: out.println(END_TAG);
167: out.flush();
168: out.close();
169: }
170: else if ( file.exists() )
171: file.delete();
172: }
173:
174: /**
175: * returns all objects that are in a but not in b
176: **/
177: protected Vector vectorDifference(Vector a, Vector b){
178: Vector result = new Vector();
179: Enumeration enum = a.elements();
180: while ( enum.hasMoreElements() ){
181: Object o = enum.nextElement();
182: if ( !b.contains(o) ){
183: result.addElement(o);
184: }
185: }
186: return result;
187: }
188:
189: /**
190: * returns the union of 2 vectors
191: * the method Vector.addAll() does not check for duplicates
192: **/
193: protected Vector vectorUnion(Vector a, Vector b){
194: Vector result = new Vector(a.size());
195: Enumeration enum = a.elements();
196: while ( enum.hasMoreElements() ){
197: result.addElement(enum.nextElement());
198: }
199: enum = b.elements();
200: while ( enum.hasMoreElements() ){
201: Object o = enum.nextElement();
202: if ( !result.contains(o) ){
203: result.addElement(o);
204: }
205: }
206: return result;
207: }
208:
209: /**
210: * returns if there was a listing in the directory
211: **/
212: public boolean listingExists() {
213: return file.exists();
214: }
215:
216: /**
217: * reads the listing from the newly opened file
218: **/
219: protected void init() throws IOException {
220: if (!file.exists())
221: return;
222: BufferedReader in = new BufferedReader(new FileReader(file));
223: boolean read_files = true;
224: String line;
225: while ((line = in.readLine()) != null) {
226: if (line.equals(FILES_TAG))
227: read_files = true;
228: else if (line.equals(DIRECTORIES_TAG))
229: read_files = false;
230: else if (line.equals(END_TAG))
231: break;
232: else {
233: if (read_files)
234: oldFiles.addElement(line);
235: else
236: oldDirectories.addElement(line);
237: }
238: }
239: in.close();
240: }
241: }
|