KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DocumentCache


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.File JavaDoc;
18 import java.io.FileNotFoundException JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.ParserConfigurationException JavaDoc;
29 import javax.xml.transform.OutputKeys JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.Project;
38 import org.apache.tools.ant.Task;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.DocumentType JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /**
45  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
46  * @version CVS $Id: DocumentCache.java 156529 2005-03-08 14:45:48Z cziegeler $
47  */

48 public final class DocumentCache {
49
50     /** Cache the read configuration files (Documents) */
51     protected final static Map JavaDoc fileCache = new HashMap JavaDoc();
52     
53     /** The document builder */
54     private static DocumentBuilder JavaDoc builder;
55     private static Transformer JavaDoc transformer;
56
57     /**
58      * Initialize internal instance of XMLCatalog
59      */

60     static {
61         try {
62             DocumentBuilderFactory JavaDoc builderFactory = DocumentBuilderFactory.newInstance();
63             builderFactory.setValidating(false);
64             builderFactory.setExpandEntityReferences(false);
65             builderFactory.setNamespaceAware(false);
66             builderFactory.setAttribute(
67                "http://apache.org/xml/features/nonvalidating/load-external-dtd",
68                Boolean.FALSE);
69             builder = builderFactory.newDocumentBuilder();
70             transformer = TransformerFactory.newInstance().newTransformer();
71         } catch (TransformerException JavaDoc e) {
72             throw new BuildException("TransformerException: "+e);
73         } catch (ParserConfigurationException JavaDoc e) {
74             throw new BuildException("ParserConfigurationException: "+e);
75         }
76     }
77
78     public static Document JavaDoc getDocument(File JavaDoc file, Task task)
79     throws SAXException JavaDoc, IOException JavaDoc {
80         final String JavaDoc fileName = file.toURL().toExternalForm();
81         Document JavaDoc document = (Document JavaDoc)fileCache.get(fileName);
82         if ( document != null ) {
83             if ( task != null ) {
84                 task.log("Using file from cache: " + fileName, Project.MSG_DEBUG);
85             }
86             fileCache.remove(fileName);
87         } else {
88             try {
89                 // load xml
90
if ( task != null ) {
91                     task.log("Reading: " + fileName, Project.MSG_DEBUG);
92                 }
93                 document = builder.parse(fileName);
94             } catch (IOException JavaDoc e) {
95                 throw new BuildException("IOException: "+e);
96             }
97         }
98         return document;
99     }
100     
101     public static Document JavaDoc getDocument(String JavaDoc string, String JavaDoc systemURI) {
102         try {
103             final InputSource JavaDoc is = new InputSource JavaDoc(new StringReader JavaDoc(string));
104             if ( systemURI != null ) {
105                 is.setSystemId(systemURI);
106             }
107             return builder.parse(is);
108         } catch (Exception JavaDoc e) {
109             throw new BuildException("Unable to parse string.", e);
110         }
111     }
112     
113     public static void storeDocument(File JavaDoc file, Document JavaDoc document, Task task)
114     throws IOException JavaDoc {
115         task.log("Storing file in cache: " + file, Project.MSG_DEBUG);
116         final String JavaDoc fileName = file.toURL().toExternalForm();
117         fileCache.put(fileName, document);
118     }
119
120     public static void writeDocument(File JavaDoc file, Document JavaDoc document, Task task) {
121         if ( task != null ) {
122             task.log("Writing: " + file);
123         }
124         // Set the DOCTYPE output option on the transformer
125
// if we have any DOCTYPE declaration in the input xml document
126
final DocumentType JavaDoc doctype = document.getDoctype();
127         Properties JavaDoc props = new Properties JavaDoc();
128         if (null != doctype) {
129             if (null != doctype.getPublicId()) {
130                 props.put(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
131             }
132             if (null != doctype.getSystemId()) {
133                 props.put(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
134             }
135         }
136         transformer.setOutputProperties(props);
137         
138         try {
139             StreamResult JavaDoc s = new StreamResult JavaDoc(file);
140             // for JDK 5.0 we explicitly have to set the output stream
141
// otherwise we get FileNotFoundExceptions (at least on
142
// windows)
143
s.setOutputStream(new FileOutputStream JavaDoc(file));
144             transformer.transform(new DOMSource JavaDoc(document),
145                                   s);
146         } catch (FileNotFoundException JavaDoc e) {
147             throw new BuildException("FileNotFoundException: "+e);
148         } catch (TransformerException JavaDoc e) {
149             throw new BuildException("TransformerException: "+e);
150         }
151     }
152 }
153
Popular Tags