1 56 57 package org.jdom.input; 58 59 import java.util.*; 60 61 import javax.xml.parsers.*; 62 63 import org.jdom.*; 64 import org.xml.sax.*; 65 66 72 class JAXPParserFactory { 74 private static final String CVS_ID = 75 "@(#) $RCSfile: JAXPParserFactory.java,v $ $Revision: 1.5 $ $Date: 2004/02/27 21:08:47 $ $Name: $"; 76 77 78 private static final String JAXP_SCHEMA_LANGUAGE_PROPERTY = 79 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 80 81 82 private static final String JAXP_SCHEMA_LOCATION_PROPERTY = 83 "http://java.sun.com/xml/jaxp/properties/schemaSource"; 84 85 89 private JAXPParserFactory() { 90 } 92 93 105 106 119 public static XMLReader createParser(boolean validating, 120 Map features, Map properties) throws JDOMException { 121 try { 122 SAXParser parser = null; 123 124 SAXParserFactory factory = SAXParserFactory.newInstance(); 126 factory.setValidating(validating); 127 factory.setNamespaceAware(true); 128 129 try { 130 parser = factory.newSAXParser(); 132 } 133 catch (ParserConfigurationException e) { 134 throw new JDOMException("Could not allocate JAXP SAX Parser", e); 135 } 136 137 setProperty(parser, properties, JAXP_SCHEMA_LANGUAGE_PROPERTY); 139 setProperty(parser, properties, JAXP_SCHEMA_LOCATION_PROPERTY); 140 141 return parser.getXMLReader(); 143 } 144 catch (SAXException e) { 145 throw new JDOMException("Could not allocate JAXP SAX Parser", e); 146 } 147 } 148 149 160 private static void setProperty(SAXParser parser, 161 Map properties, String name) throws JDOMException { 162 try { 163 if (properties.containsKey(name)) { 164 parser.setProperty(name, properties.get(name)); 165 } 166 } 167 catch (SAXNotSupportedException e) { 168 throw new JDOMException( 169 name + " property not supported for JAXP parser " + 170 parser.getClass().getName()); 171 } 172 catch (SAXNotRecognizedException e) { 173 throw new JDOMException( 174 name + " property not recognized for JAXP parser " + 175 parser.getClass().getName()); 176 } 177 } 178 } 179 180 | Popular Tags |