1 8 package org.apache.avalon.phoenix.tools.configuration; 9 10 import java.io.IOException ; 11 import javax.xml.parsers.ParserConfigurationException ; 12 import javax.xml.parsers.SAXParser ; 13 import javax.xml.parsers.SAXParserFactory ; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 16 import org.xml.sax.InputSource ; 17 import org.xml.sax.SAXException ; 18 import org.xml.sax.XMLReader ; 19 20 26 public class ConfigurationBuilder 27 { 28 private static final DTDInfo[] c_dtdInfo = new DTDInfo[] 29 { 30 new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN", 31 "http://jakarta.apache.org/phoenix/blockinfo_1_0.dtd", 32 "org/apache/avalon/phoenix/tools/blockinfo.dtd" ), 33 new DTDInfo( "-//PHOENIX/Assembly DTD Version 1.0//EN", 34 "http://jakarta.apache.org/phoenix/assembly_1_0.dtd", 35 "org/apache/avalon/phoenix/tools/assembly.dtd" ), 36 new DTDInfo( "-//PHOENIX/Mx Info DTD Version 1.0//EN", 37 "http://jakarta.apache.org/phoenix/mxinfo_1_0.dtd", 38 "org/apache/avalon/phoenix/tools/mxinfo.dtd" ), 39 new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN", 40 "http://jakarta.apache.org/avalon/dtds/phoenix/blockinfo_1_0.dtd", 41 "org/apache/avalon/phoenix/tools/blockinfo.dtd" ), 42 new DTDInfo( "-//PHOENIX/Assembly DTD Version 1.0//EN", 43 "http://jakarta.apache.org/avalon/dtds/phoenix/assembly_1_0.dtd", 44 "org/apache/avalon/phoenix/tools/assembly.dtd" ), 45 new DTDInfo( "-//PHOENIX/Mx Info DTD Version 1.0//EN", 46 "http://jakarta.apache.org/avalon/dtds/phoenix/mxinfo_1_0.dtd", 47 "org/apache/avalon/phoenix/tools/mxinfo.dtd" ) 48 }; 49 50 private static final DTDResolver c_resolver = 51 new DTDResolver( c_dtdInfo, ConfigurationBuilder.class.getClassLoader() ); 52 53 56 private ConfigurationBuilder() 57 { 58 } 59 60 63 private static XMLReader createXMLReader() 64 throws SAXException , ParserConfigurationException 65 { 66 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 67 saxParserFactory.setNamespaceAware( false ); 68 final SAXParser saxParser = saxParserFactory.newSAXParser(); 69 return saxParser.getXMLReader(); 70 } 71 72 75 private static void setupXMLReader( final XMLReader reader, 76 final SAXConfigurationHandler handler, 77 final boolean validate ) 78 throws SAXException 79 { 80 reader.setEntityResolver( c_resolver ); 81 reader.setContentHandler( handler ); 82 reader.setErrorHandler( handler ); 83 84 if( validate ) 85 { 86 reader.setFeature( "http://xml.org/sax/features/validation", true ); 88 } 89 } 90 91 94 public static Configuration build( final String uri ) 95 throws SAXException , ParserConfigurationException , IOException 96 { 97 return build( new InputSource ( uri ) ); 98 } 99 100 104 public static Configuration build( final String uri, boolean validate ) 105 throws SAXException , ParserConfigurationException , IOException 106 { 107 return build( new InputSource ( uri ), validate ); 108 } 109 110 113 public static Configuration build( final InputSource input ) 114 throws SAXException , ParserConfigurationException , IOException 115 { 116 return build( input, false ); 117 } 118 119 123 public static Configuration build( final InputSource input, boolean validate ) 124 throws SAXException , ParserConfigurationException , IOException 125 { 126 final XMLReader reader = createXMLReader(); 127 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 128 setupXMLReader( reader, handler, validate ); 129 reader.parse( input ); 130 return handler.getConfiguration(); 131 } 132 } 133 | Popular Tags |