1 18 package org.apache.tools.ant.util; 19 20 import java.io.File ; 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.parsers.FactoryConfigurationError ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 import javax.xml.parsers.SAXParser ; 26 import javax.xml.parsers.SAXParserFactory ; 27 import org.apache.tools.ant.BuildException; 28 import org.xml.sax.Parser ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.XMLReader ; 31 32 34 42 public class JAXPUtils { 43 44 49 private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); 50 51 57 private static SAXParserFactory parserFactory = null; 58 59 64 private static SAXParserFactory nsParserFactory = null; 65 66 71 private static DocumentBuilderFactory builderFactory = null; 72 73 82 public static synchronized SAXParserFactory getParserFactory() 83 throws BuildException { 84 85 if (parserFactory == null) { 86 parserFactory = newParserFactory(); 87 } 88 return parserFactory; 89 } 90 91 100 public static synchronized SAXParserFactory getNSParserFactory() 101 throws BuildException { 102 103 if (nsParserFactory == null) { 104 nsParserFactory = newParserFactory(); 105 nsParserFactory.setNamespaceAware(true); 106 } 107 return nsParserFactory; 108 } 109 110 117 public static SAXParserFactory newParserFactory() throws BuildException { 118 119 try { 120 return SAXParserFactory.newInstance(); 121 } catch (FactoryConfigurationError e) { 122 throw new BuildException("XML parser factory has not been " 123 + "configured correctly: " 124 + e.getMessage(), e); 125 } 126 } 127 128 137 public static Parser getParser() throws BuildException { 138 try { 139 return newSAXParser(getParserFactory()).getParser(); 140 } catch (SAXException e) { 141 throw convertToBuildException(e); 142 } 143 } 144 145 154 public static XMLReader getXMLReader() throws BuildException { 155 try { 156 return newSAXParser(getParserFactory()).getXMLReader(); 157 } catch (SAXException e) { 158 throw convertToBuildException(e); 159 } 160 } 161 162 170 public static XMLReader getNamespaceXMLReader() throws BuildException { 171 try { 172 return newSAXParser(getNSParserFactory()).getXMLReader(); 173 } catch (SAXException e) { 174 throw convertToBuildException(e); 175 } 176 } 177 178 187 public static String getSystemId(File file) { 188 return FILE_UTILS.toURI(file.getAbsolutePath()); 189 } 190 191 198 public static DocumentBuilder getDocumentBuilder() throws BuildException { 199 try { 200 return getDocumentBuilderFactory().newDocumentBuilder(); 201 } catch (ParserConfigurationException e) { 202 throw new BuildException(e); 203 } 204 } 205 206 212 private static SAXParser newSAXParser(SAXParserFactory factory) 213 throws BuildException { 214 try { 215 return factory.newSAXParser(); 216 } catch (ParserConfigurationException e) { 217 throw new BuildException("Cannot create parser for the given " 218 + "configuration: " + e.getMessage(), e); 219 } catch (SAXException e) { 220 throw convertToBuildException(e); 221 } 222 } 223 224 229 private static BuildException convertToBuildException(SAXException e) { 230 Exception nested = e.getException(); 231 if (nested != null) { 232 return new BuildException(nested); 233 } else { 234 return new BuildException(e); 235 } 236 } 237 238 243 private static synchronized 244 DocumentBuilderFactory getDocumentBuilderFactory() 245 throws BuildException { 246 if (builderFactory == null) { 247 try { 248 builderFactory = DocumentBuilderFactory.newInstance(); 249 } catch (FactoryConfigurationError e) { 250 throw new BuildException("Document builder factory has not " 251 + "been configured correctly: " 252 + e.getMessage(), e); 253 } 254 } 255 return builderFactory; 256 } 257 258 } 259 | Popular Tags |