Source Code Cross Referenced for DirectoryFileListing.java in  » Content-Management-System » webman » de » webman » generator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » webman » de.webman.generator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.