1 20 package org.apache.cactus.integration.ant.deployment.webapp; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import org.apache.xml.serialize.OutputFormat; 34 import org.apache.xml.serialize.XMLSerializer; 35 import org.w3c.dom.Document ; 36 import org.w3c.dom.DocumentType ; 37 import org.xml.sax.EntityResolver ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 41 48 public class WebXmlIo 49 { 50 51 53 57 private static class WebXmlEntityResolver implements EntityResolver 58 { 59 60 63 public InputSource resolveEntity(String thePublicId, String theSystemId) 64 throws SAXException , IOException 65 { 66 WebXmlVersion version = WebXmlVersion.valueOf(thePublicId); 67 if (version != null) 68 { 69 String fileName = version.getSystemId().substring( 70 version.getSystemId().lastIndexOf('/')); 71 InputStream in = this.getClass().getResourceAsStream( 72 "/org/apache/cactus/integration/ant/deployment/resources" 73 + fileName); 74 if (in != null) 75 { 76 return new InputSource (in); 77 } 78 } 79 return null; 80 } 81 82 } 83 84 86 94 public static WebXml newWebXml(WebXmlVersion theVersion) 95 throws ParserConfigurationException 96 { 97 DocumentBuilderFactory factory = 98 DocumentBuilderFactory.newInstance(); 99 factory.setValidating(false); 100 factory.setNamespaceAware(false); 101 DocumentBuilder builder = factory.newDocumentBuilder(); 102 DocumentType docType = null; 103 if (theVersion != null) 104 { 105 docType = 106 builder.getDOMImplementation().createDocumentType("web-app", 107 theVersion.getPublicId(), theVersion.getSystemId()); 108 } 109 Document doc = builder.getDOMImplementation().createDocument( 110 "", "web-app", docType); 111 return new WebXml(doc); 112 } 113 114 126 public static WebXml parseWebXmlFromFile(File theFile, 127 EntityResolver theEntityResolver) 128 throws SAXException , ParserConfigurationException , IOException 129 { 130 InputStream in = null; 131 try 132 { 133 in = new FileInputStream (theFile); 134 return parseWebXml(in, theEntityResolver); 135 } 136 finally 137 { 138 if (in != null) 139 { 140 try 141 { 142 in.close(); 143 } 144 catch (IOException ioe) 145 { 146 } 148 } 149 } 150 } 151 152 164 public static WebXml parseWebXml(InputStream theInput, 165 EntityResolver theEntityResolver) 166 throws SAXException , ParserConfigurationException , IOException 167 { 168 DocumentBuilderFactory factory = 169 DocumentBuilderFactory.newInstance(); 170 factory.setValidating(false); 171 factory.setNamespaceAware(false); 172 DocumentBuilder builder = factory.newDocumentBuilder(); 173 if (theEntityResolver != null) 174 { 175 builder.setEntityResolver(theEntityResolver); 176 } 177 else 178 { 179 builder.setEntityResolver(new WebXmlEntityResolver()); 180 } 181 return new WebXml(builder.parse(theInput)); 182 } 183 184 191 public static void writeWebXml(WebXml theWebXml, File theFile) 192 throws IOException 193 { 194 writeWebXml(theWebXml, theFile, null, false); 195 } 196 197 205 public static void writeWebXml(WebXml theWebXml, File theFile, 206 String theEncoding) 207 throws IOException 208 { 209 writeWebXml(theWebXml, theFile, theEncoding, false); 210 } 211 212 221 public static void writeWebXml(WebXml theWebXml, File theFile, 222 String theEncoding, boolean isIndent) 223 throws IOException 224 { 225 OutputStream out = null; 226 try 227 { 228 out = new FileOutputStream (theFile); 229 writeWebXml(theWebXml, out, theEncoding, isIndent); 230 } 231 finally 232 { 233 if (out != null) 234 { 235 try 236 { 237 out.close(); 238 } 239 catch (IOException ioe) 240 { 241 } 243 } 244 } 245 } 246 247 256 public static void writeWebXml(WebXml theWebXml, OutputStream theOutput, 257 String theEncoding, boolean isIndent) 258 throws IOException 259 { 260 OutputFormat outputFormat = 261 new OutputFormat(theWebXml.getDocument()); 262 if (theEncoding != null) 263 { 264 outputFormat.setEncoding(theEncoding); 265 } 266 outputFormat.setIndenting(isIndent); 267 outputFormat.setPreserveSpace(false); 268 XMLSerializer serializer = new XMLSerializer(theOutput, outputFormat); 269 serializer.serialize(theWebXml.getDocument()); 270 } 271 272 } 273 | Popular Tags |