1 20 package org.apache.cactus.integration.ant.deployment.application; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 31 import org.xml.sax.EntityResolver ; 32 import org.xml.sax.InputSource ; 33 import org.xml.sax.SAXException ; 34 35 42 public class ApplicationXmlIo 43 { 44 45 47 51 private static class ApplicationXmlEntityResolver implements EntityResolver 52 { 53 54 57 public InputSource resolveEntity(String thePublicId, String theSystemId) 58 throws SAXException , IOException 59 { 60 ApplicationXmlVersion version = 61 ApplicationXmlVersion.valueOf(thePublicId); 62 if (version != null) 63 { 64 String fileName = version.getSystemId().substring( 65 version.getSystemId().lastIndexOf('/')); 66 InputStream in = this.getClass().getResourceAsStream( 67 "/org/apache/cactus/integration/ant/deployment/resources" 68 + fileName); 69 if (in != null) 70 { 71 return new InputSource (in); 72 } 73 } 74 return null; 75 } 76 77 } 78 79 81 93 public static ApplicationXml parseApplicationXmlFromFile(File theFile, 94 EntityResolver theEntityResolver) 95 throws SAXException , ParserConfigurationException , IOException 96 { 97 InputStream in = null; 98 try 99 { 100 in = new FileInputStream (theFile); 101 return parseApplicationXml(in, theEntityResolver); 102 } 103 finally 104 { 105 if (in != null) 106 { 107 try 108 { 109 in.close(); 110 } 111 catch (IOException ioe) 112 { 113 } 115 } 116 } 117 } 118 119 131 public static ApplicationXml parseApplicationXml(InputStream theInput, 132 EntityResolver theEntityResolver) 133 throws SAXException , ParserConfigurationException , IOException 134 { 135 DocumentBuilderFactory factory = 136 DocumentBuilderFactory.newInstance(); 137 factory.setValidating(false); 138 factory.setNamespaceAware(false); 139 DocumentBuilder builder = factory.newDocumentBuilder(); 140 if (theEntityResolver != null) 141 { 142 builder.setEntityResolver(theEntityResolver); 143 } 144 else 145 { 146 builder.setEntityResolver(new ApplicationXmlEntityResolver()); 147 } 148 return new DefaultApplicationXml(builder.parse(theInput)); 149 } 150 151 } 152 | Popular Tags |