1 58 package org.oddjob.arooa.xml; 59 60 import javax.xml.parsers.DocumentBuilder ; 61 import javax.xml.parsers.DocumentBuilderFactory ; 62 import javax.xml.parsers.FactoryConfigurationError ; 63 import javax.xml.parsers.ParserConfigurationException ; 64 import javax.xml.parsers.SAXParser ; 65 import javax.xml.parsers.SAXParserFactory ; 66 67 import org.oddjob.arooa.ArooaException; 68 import org.xml.sax.SAXException ; 69 import org.xml.sax.XMLReader ; 70 71 80 public class JAXPUtils { 81 82 83 88 private static SAXParserFactory parserFactory = null; 89 90 94 private static SAXParserFactory nsParserFactory = null; 95 96 100 private static DocumentBuilderFactory builderFactory = null; 101 102 109 public static synchronized SAXParserFactory getParserFactory() 110 throws ArooaException { 111 112 if (parserFactory == null) { 113 parserFactory = newParserFactory(); 114 } 115 return parserFactory; 116 } 117 118 125 public static synchronized SAXParserFactory getNSParserFactory() 126 throws ArooaException { 127 128 if (nsParserFactory == null) { 129 nsParserFactory = newParserFactory(); 130 nsParserFactory.setNamespaceAware(true); 131 } 132 return nsParserFactory; 133 } 134 135 139 public static SAXParserFactory newParserFactory() throws ArooaException { 140 141 try { 142 return SAXParserFactory.newInstance(); 143 } catch (FactoryConfigurationError e) { 144 throw new ArooaException("XML parser factory has not been " 145 + "configured correctly: " 146 + e.getMessage(), e); 147 } 148 } 149 150 157 public static XMLReader getXMLReader() throws ArooaException { 158 try { 159 return newSAXParser(getParserFactory()).getXMLReader(); 160 } catch (SAXException e) { 161 throw convertToArooaException(e); 162 } 163 } 164 165 171 public static XMLReader getNamespaceXMLReader() throws ArooaException { 172 try { 173 return newSAXParser(getNSParserFactory()).getXMLReader(); 174 } catch (SAXException e) { 175 throw convertToArooaException(e); 176 } 177 } 178 179 184 public static DocumentBuilder getDocumentBuilder() throws ArooaException { 185 try { 186 return getDocumentBuilderFactory().newDocumentBuilder(); 187 } catch (ParserConfigurationException e) { 188 throw new ArooaException(e); 189 } 190 } 191 192 197 private static SAXParser newSAXParser(SAXParserFactory factory) 198 throws ArooaException { 199 try { 200 return factory.newSAXParser(); 201 } catch (ParserConfigurationException e) { 202 throw new ArooaException("Cannot create parser for the given " 203 + "configuration: " + e.getMessage(), e); 204 } catch (SAXException e) { 205 throw convertToArooaException(e); 206 } 207 } 208 209 213 private static ArooaException convertToArooaException(SAXException e) { 214 Exception nested = e.getException(); 215 if (nested != null) { 216 return new ArooaException(nested); 217 } else { 218 return new ArooaException(e); 219 } 220 } 221 222 226 private static synchronized 227 DocumentBuilderFactory getDocumentBuilderFactory() 228 throws ArooaException { 229 if (builderFactory == null) { 230 try { 231 builderFactory = DocumentBuilderFactory.newInstance(); 232 } catch (FactoryConfigurationError e) { 233 throw new ArooaException("Document builder factory has not " 234 + "been configured correctly: " 235 + e.getMessage(), e); 236 } 237 } 238 return builderFactory; 239 } 240 241 } 242 | Popular Tags |