1 6 package org.logicalcobwebs.proxool.configuration; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.ProxoolException; 11 import org.xml.sax.InputSource ; 12 import org.xml.sax.SAXException ; 13 import org.xml.sax.SAXNotRecognizedException ; 14 import org.xml.sax.SAXNotSupportedException ; 15 import org.xml.sax.XMLReader ; 16 17 import javax.xml.parsers.ParserConfigurationException ; 18 import javax.xml.parsers.SAXParser ; 19 import javax.xml.parsers.SAXParserFactory ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileReader ; 22 import java.io.IOException ; 23 import java.io.Reader ; 24 25 41 public class JAXPConfigurator { 42 private static final Log LOG = LogFactory.getLog(JAXPConfigurator.class); 43 private static final boolean NAMESPACE_AWARE = true; 44 45 51 public static void configure(String xmlFileName, boolean validate) throws ProxoolException { 52 try { 53 if (LOG.isDebugEnabled()) { 54 LOG.debug("Configuring from xml file: " + xmlFileName); 55 } 56 configure(new InputSource (new FileReader (xmlFileName)), validate); 57 } catch (FileNotFoundException e) { 58 throw new ProxoolException(e); 59 } 60 } 61 62 68 public static void configure(InputSource inputSource, boolean validate) throws ProxoolException { 69 try { 70 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 71 if (LOG.isDebugEnabled()) { 72 LOG.debug("SAXParserFactory class: " + saxParserFactory.getClass().getName()); 73 } 74 saxParserFactory.setValidating(validate); 75 final SAXParser saxParser = saxParserFactory.newSAXParser(); 76 if (LOG.isDebugEnabled()) { 77 LOG.debug("sax parser class" + saxParser.getClass().getName()); 78 } 79 final XMLReader xmlReader = saxParser.getXMLReader(); 80 if (LOG.isDebugEnabled()) { 81 LOG.debug("XML reader class: " + xmlReader.getClass().getName()); 82 } 83 final XMLConfigurator xmlConfigurator = new XMLConfigurator(); 84 xmlReader.setErrorHandler(xmlConfigurator); 85 setSAXFeature(xmlReader, "http://xml.org/sax/features/namespaces", NAMESPACE_AWARE); 86 setSAXFeature(xmlReader, "http://xml.org/sax/features/namespace-prefixes", !NAMESPACE_AWARE); 87 saxParser.parse(inputSource, xmlConfigurator); 88 } catch (ParserConfigurationException pce) { 89 throw new ProxoolException("Parser configuration failed", pce); 90 } catch (SAXException se) { 91 throw new ProxoolException("Parsing failed.", se); 92 } catch (IOException ioe) { 93 throw new ProxoolException("Parsing failed.", ioe); 94 } 95 } 96 97 103 public static void configure(Reader reader, boolean validate) throws ProxoolException { 104 if (LOG.isDebugEnabled()) { 105 LOG.debug("Configuring from reader: " + reader); 106 } 107 configure(new InputSource (reader), validate); 108 } 109 110 private static void setSAXFeature(XMLReader xmlReader, String feature, boolean state) { 113 if (LOG.isDebugEnabled()) { 114 LOG.debug("Setting sax feature: '" + feature + "'. State: " + state + "."); 115 } 116 try { 117 xmlReader.setFeature(feature, state); 118 } catch (SAXNotRecognizedException e) { 119 LOG.warn("Feature: '" + feature + "' not recognised by xml reader " + xmlReader + ".", e); 120 } catch (SAXNotSupportedException e) { 121 LOG.warn("Feature: '" + feature + "' not supported by xml reader " + xmlReader + ".", e); 122 } 123 } 124 } 125 126 | Popular Tags |