1 16 17 import java.io.File ; 18 import java.io.FileNotFoundException ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.StringReader ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 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 ; 40 import org.w3c.dom.DocumentType ; 41 import org.xml.sax.InputSource ; 42 import org.xml.sax.SAXException ; 43 44 48 public final class DocumentCache { 49 50 51 protected final static Map fileCache = new HashMap (); 52 53 54 private static DocumentBuilder builder; 55 private static Transformer transformer; 56 57 60 static { 61 try { 62 DocumentBuilderFactory 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 e) { 72 throw new BuildException("TransformerException: "+e); 73 } catch (ParserConfigurationException e) { 74 throw new BuildException("ParserConfigurationException: "+e); 75 } 76 } 77 78 public static Document getDocument(File file, Task task) 79 throws SAXException , IOException { 80 final String fileName = file.toURL().toExternalForm(); 81 Document document = (Document )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 if ( task != null ) { 91 task.log("Reading: " + fileName, Project.MSG_DEBUG); 92 } 93 document = builder.parse(fileName); 94 } catch (IOException e) { 95 throw new BuildException("IOException: "+e); 96 } 97 } 98 return document; 99 } 100 101 public static Document getDocument(String string, String systemURI) { 102 try { 103 final InputSource is = new InputSource (new StringReader (string)); 104 if ( systemURI != null ) { 105 is.setSystemId(systemURI); 106 } 107 return builder.parse(is); 108 } catch (Exception e) { 109 throw new BuildException("Unable to parse string.", e); 110 } 111 } 112 113 public static void storeDocument(File file, Document document, Task task) 114 throws IOException { 115 task.log("Storing file in cache: " + file, Project.MSG_DEBUG); 116 final String fileName = file.toURL().toExternalForm(); 117 fileCache.put(fileName, document); 118 } 119 120 public static void writeDocument(File file, Document document, Task task) { 121 if ( task != null ) { 122 task.log("Writing: " + file); 123 } 124 final DocumentType doctype = document.getDoctype(); 127 Properties props = new Properties (); 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 s = new StreamResult (file); 140 s.setOutputStream(new FileOutputStream (file)); 144 transformer.transform(new DOMSource (document), 145 s); 146 } catch (FileNotFoundException e) { 147 throw new BuildException("FileNotFoundException: "+e); 148 } catch (TransformerException e) { 149 throw new BuildException("TransformerException: "+e); 150 } 151 } 152 } 153 | Popular Tags |