1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package net.sf.jolene.factories; |
8 | |
|
9 | |
import net.sf.jolene.constants.Prefs; |
10 | |
import net.sf.jolene.dom.Document; |
11 | |
import org.apache.log4j.LogManager; |
12 | |
import org.apache.log4j.Logger; |
13 | |
|
14 | |
import java.io.IOException; |
15 | |
import java.io.File; |
16 | |
import java.io.FileNotFoundException; |
17 | |
import java.util.HashMap; |
18 | |
import java.util.Map; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class DocumentFactory { |
28 | |
|
29 | 1 | private static final Logger log = LogManager.getLogger(DocumentFactory.class); |
30 | |
|
31 | |
private static final int DEFAULT_CACHE_SIZE = 32; |
32 | |
|
33 | 1 | private static DocumentFactory instance = new DocumentFactory(); |
34 | |
|
35 | |
private Map<String, Document> documents; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public static DocumentFactory getInstance() { |
43 | 1 | return instance; |
44 | |
} |
45 | |
|
46 | 1 | private DocumentFactory() { |
47 | |
int cacheSize; |
48 | |
try { |
49 | 1 | cacheSize = Integer.parseInt(Prefs.CacheSize.getValue()); |
50 | 0 | } catch (NumberFormatException e) { |
51 | 0 | cacheSize = DEFAULT_CACHE_SIZE; |
52 | 0 | log.warn("NumberFormatException : " + e.getMessage() + " cache size set to 32"); |
53 | 1 | } |
54 | 1 | documents = new HashMap<String, Document>(cacheSize); |
55 | 1 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public Document getDocument(String docFileName) throws IOException { |
65 | 1 | log.debug("getDocument: " + docFileName); |
66 | |
|
67 | 1 | boolean cacheEnabled = Boolean.parseBoolean(Prefs.DocumentCaching.getValue()); |
68 | |
|
69 | 1 | log.debug("getDocument: filename: Cache enabled? " + cacheEnabled); |
70 | |
|
71 | 1 | if (cacheEnabled) { |
72 | 1 | File file = new File(docFileName); |
73 | 1 | if (! file.exists()) { |
74 | 0 | throw new FileNotFoundException(docFileName + " not found!"); |
75 | |
} |
76 | 1 | String key = docFileName + " - " + file.lastModified(); |
77 | 1 | if (!documents.containsKey(key)) { |
78 | 1 | documents.put(key, new Document(docFileName)); |
79 | |
} |
80 | 1 | return documents.get(key).clone(); |
81 | |
} |
82 | |
|
83 | 0 | return new Document(docFileName); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public Document getDocument(String docFileName, String contextPath, String uri) throws IOException { |
96 | 0 | log.debug("getDocument (with cp): " + docFileName + " cp: " + contextPath); |
97 | |
|
98 | 0 | boolean cacheEnabled = Boolean.parseBoolean(Prefs.DocumentCaching.getValue()); |
99 | 0 | log.debug("getDocument: filename, contextPath, uri: Cache enabled? " + cacheEnabled); |
100 | |
|
101 | 0 | if (cacheEnabled) { |
102 | 0 | File file = new File(docFileName); |
103 | 0 | if (! file.exists()) { |
104 | 0 | throw new FileNotFoundException(docFileName + " not found!"); |
105 | |
} |
106 | 0 | String key = docFileName + " - " + file.lastModified(); |
107 | 0 | if (!documents.containsKey(key)) { |
108 | 0 | documents.put(key, new Document(docFileName, contextPath, uri)); |
109 | |
} |
110 | 0 | return documents.get(key).clone(); |
111 | |
|
112 | |
} |
113 | 0 | return new Document(docFileName, contextPath, uri); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
} |