KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > js > JsFiles


1 package net.sf.uitags.js;
2
3 import java.io.IOException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import net.sf.uitags.util.ArrayUtils;
13
14 final class JsFiles {
15   private static final Log log = LogFactory.getLog(JsFiles.class);
16
17   private static final String JavaDoc DEBUG_FILE_NAME = "util/excludes/initDebug.js";
18   private static final List JavaDoc EMPTY_LIST = new ArrayList JavaDoc();
19
20   private Suites suites;
21   private FileFinder fileFinder;
22   private List JavaDoc customFileNames = EMPTY_LIST;
23   private boolean inDebugMode = false;
24
25   /**
26    * Creates an instance which is <b>not</b> in debug mode by default.
27    */

28   JsFiles(Suites suites, FileFinder fileFinder) {
29     this.suites = suites;
30     this.fileFinder = fileFinder;
31   }
32
33   void setCustomFileNames(String JavaDoc customFileNames) {
34     if (customFileNames == null) {
35       this.customFileNames = EMPTY_LIST;
36     }
37     else {
38       String JavaDoc[] fileNamesAsArray = ArrayUtils.toArrayOfTrimmed(customFileNames);
39       this.customFileNames = Arrays.asList(fileNamesAsArray);
40     }
41   }
42
43   void setInDebugMode(boolean inDebugMode) {
44     this.inDebugMode = inDebugMode;
45   }
46
47   String JavaDoc getContents() throws IOException JavaDoc {
48     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
49
50     addDebugOnlyContentsIfNecessary(buffer);
51     addContentsFromSuites(buffer);
52     addCustomContents(buffer);
53
54     return buffer.toString();
55   }
56
57   /**
58    * Returns the latest of all files' last modified times. To maintain
59    * consistency with J2SE API, 0 is returned if the time is unknown (Servlet
60    * API returns -1 if unknown).
61    */

62   long getLastModified() {
63     return
64         Math.max(getTimestampFromDebugFile(),
65             Math.max(getLatestTimestampFromFilesInSuites(),
66                 getLatestTimestampFromCustomJsFiles()));
67   }
68
69   private long getTimestampFromDebugFile() {
70     if (this.fileFinder.supportsFilesNotFromSuites() && this.inDebugMode) {
71       try {
72         return this.fileFinder.getLastModified(DEBUG_FILE_NAME);
73       }
74       catch (IOException JavaDoc e) {
75         return 0;
76       }
77     }
78
79     return 0;
80   }
81
82   private long getLatestTimestampFromFilesInSuites() {
83     long latestTimestamp = 0;
84
85     for (Iterator JavaDoc i = this.suites.getFileNames().iterator(); i.hasNext(); ) {
86       String JavaDoc fileName = (String JavaDoc) i.next();
87       try {
88         latestTimestamp = Math.max(
89             this.fileFinder.getLastModified(fileName), latestTimestamp);
90       }
91       catch (IOException JavaDoc e) {
92         // Ignore error while trying to get the timestamp.
93
if (log.isWarnEnabled()) {
94           log.warn("Error trying to get last modified of " + fileName + ".");
95         }
96       }
97     }
98
99     return latestTimestamp;
100   }
101
102   private long getLatestTimestampFromCustomJsFiles() {
103     if (this.fileFinder.supportsFilesNotFromSuites()) {
104       long latestTimestamp = 0;
105
106       for (Iterator JavaDoc i = this.customFileNames.iterator(); i.hasNext(); ) {
107         String JavaDoc fileName = (String JavaDoc) i.next();
108         try {
109           latestTimestamp = Math.max(
110               this.fileFinder.getLastModified(fileName), latestTimestamp);
111         }
112         catch (IOException JavaDoc e) {
113           // Ignore error while trying to get the timestamp.
114
if (log.isWarnEnabled()) {
115             log.warn("Error trying to get last modified of " + fileName + ".");
116           }
117         }
118       }
119
120       return latestTimestamp;
121     }
122
123     return 0;
124   }
125
126   private void addDebugOnlyContentsIfNecessary(StringBuffer JavaDoc buffer)
127       throws IOException JavaDoc {
128     if (this.fileFinder.supportsFilesNotFromSuites() && this.inDebugMode) {
129       buffer.append(this.fileFinder.readContents(DEBUG_FILE_NAME));
130     }
131   }
132
133   private void addContentsFromSuites(StringBuffer JavaDoc buffer)
134       throws IOException JavaDoc {
135     for (Iterator JavaDoc i = this.suites.getFileNames().iterator(); i.hasNext(); ) {
136       String JavaDoc fileName = (String JavaDoc) i.next();
137       buffer.append(this.fileFinder.readContents(fileName));
138     }
139   }
140
141   private void addCustomContents(StringBuffer JavaDoc buffer)
142       throws IOException JavaDoc {
143     if (this.fileFinder.supportsFilesNotFromSuites()) {
144       for (Iterator JavaDoc i = this.customFileNames.iterator(); i.hasNext(); ) {
145         String JavaDoc fileName = (String JavaDoc) i.next();
146         buffer.append(this.fileFinder.readContents(fileName));
147       }
148     }
149   }
150 }
151
Popular Tags