1 22 package org.jboss.deployers.plugins.deployers.helpers; 23 24 import java.io.InputStream ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 29 import org.jboss.deployers.spi.deployer.DeploymentUnit; 30 import org.jboss.util.xml.JBossEntityResolver; 31 import org.jboss.virtual.VirtualFile; 32 import org.w3c.dom.Document ; 33 import org.xml.sax.InputSource ; 34 35 42 public abstract class JAXPDeployer<T> extends AbstractParsingDeployer<T> 43 { 44 45 private boolean useNamespaceAwareParser = true; 46 47 48 private boolean validateDTDs; 49 50 51 private DocumentBuilderFactory documentBuilderFactory; 52 53 59 public JAXPDeployer(Class <T> deploymentType) 60 { 61 super(deploymentType); 62 } 63 64 69 public boolean isUseNamespaceAwareParser() 70 { 71 return useNamespaceAwareParser; 72 } 73 74 79 public void setUseNamespaceAwareParser(boolean useNamespaceAwareParser) 80 { 81 this.useNamespaceAwareParser = useNamespaceAwareParser; 82 } 83 84 89 public boolean isValidateDTDs() 90 { 91 return validateDTDs; 92 } 93 94 99 public void setValidateDTDs(boolean validateDTDs) 100 { 101 this.validateDTDs = validateDTDs; 102 } 103 104 110 protected DocumentBuilderFactory getDocumentBuilderFactory() 111 { 112 if (documentBuilderFactory == null) 113 throw new IllegalStateException ("Document builder factory has not been constructed"); 114 return documentBuilderFactory; 115 } 116 117 122 public void create() throws Exception 123 { 124 documentBuilderFactory = DocumentBuilderFactory.newInstance(); 125 documentBuilderFactory.setNamespaceAware(useNamespaceAwareParser); 126 documentBuilderFactory.setValidating(validateDTDs); 127 } 128 129 132 public void destroy() 133 { 134 documentBuilderFactory = null; 135 } 136 137 146 protected T parse(DeploymentUnit unit, VirtualFile file, T root) throws Exception 147 { 148 Document document = doParse(unit, file); 149 return parse(unit, file, document); 150 } 151 152 160 protected Document doParse(DeploymentUnit unit, VirtualFile file) throws Exception 161 { 162 if (file == null) 163 throw new IllegalArgumentException ("Null file"); 164 165 InputStream is = file.openStream(); 166 try 167 { 168 DocumentBuilder parser = getDocumentBuilderFactory().newDocumentBuilder(); 169 InputSource source = new InputSource (is); 170 source.setSystemId(file.toURI().toString()); 171 parser.setEntityResolver(new JBossEntityResolver()); 172 return parser.parse(is); 173 } 174 finally 175 { 176 try 177 { 178 is.close(); 179 } 180 catch (Exception ignored) 181 { 182 } 183 } 184 } 185 186 195 protected abstract T parse(DeploymentUnit unit, VirtualFile file, Document document) throws Exception ; 196 } 197 | Popular Tags |