1 50 51 package org.apache.avalon.meta; 52 53 import java.io.IOException ; 54 import java.net.MalformedURLException ; 55 import javax.xml.parsers.ParserConfigurationException ; 56 import javax.xml.parsers.SAXParser ; 57 import javax.xml.parsers.SAXParserFactory ; 58 import org.apache.avalon.framework.configuration.Configuration; 59 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.SAXException ; 62 import org.xml.sax.XMLReader ; 63 64 70 public class ConfigurationBuilder 71 { 72 private static final DTDInfo[] DTD_INFO = new DTDInfo[] 73 { 74 new DTDInfo( "-//AVALON/Component Type DTD Version 1.0//EN", 75 "http://avalon.apache.org/dtds/meta/type_1_0.dtd", 76 "org/apache/avalon/meta/type.dtd" ), 77 new DTDInfo( "-//AVALON/Component Type DTD Version 1.1//EN", 78 "http://avalon.apache.org/dtds/meta/type_1_1.dtd", 79 "org/apache/avalon/meta/type.dtd" ), 80 new DTDInfo( "-//AVALON/Service DTD Version 1.0//EN", 81 "http://avalon.apache.org/dtds/meta/service_1_0.dtd", 82 "org/apache/avalon/meta/service.dtd" ), 83 new DTDInfo( "-//PHOENIX/Block Info DTD Version 1.0//EN", 84 "http://avalon.apache.org/dtds/phoenix/blockinfo_1.0.dtd", 85 "org/apache/avalon/meta/blockinfo.dtd" ), 86 }; 87 88 private static final DTDResolver DTD_RESOLVER = 89 new DTDResolver( DTD_INFO, ConfigurationBuilder.class.getClassLoader() ); 90 91 94 private ConfigurationBuilder() 95 { 96 } 97 98 101 private static XMLReader createXMLReader() 102 throws SAXException , ParserConfigurationException 103 { 104 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 105 saxParserFactory.setNamespaceAware( false ); 106 final SAXParser saxParser = saxParserFactory.newSAXParser(); 107 return saxParser.getXMLReader(); 108 } 109 110 113 private static void setupXMLReader( final XMLReader reader, 114 final SAXConfigurationHandler handler ) 115 { 116 reader.setEntityResolver( DTD_RESOLVER ); 117 reader.setContentHandler( handler ); 118 reader.setErrorHandler( handler ); 119 } 120 121 130 public static Configuration build( final String uri ) 131 throws SAXException , ParserConfigurationException , IOException 132 { 133 try 134 { 135 return build( new InputSource ( uri ) ); 136 } 137 catch( MalformedURLException mue ) 138 { 139 final String error = 140 "Invalid input source uri: " + uri; 141 throw new IOException ( error ); 142 } 143 } 144 145 154 public static Configuration build( final InputSource input ) 155 throws SAXException , ParserConfigurationException , IOException 156 { 157 if( input == null ) 158 { 159 throw new NullPointerException ( "input" ); 160 } 161 162 final XMLReader reader = createXMLReader(); 163 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 164 setupXMLReader( reader, handler ); 165 reader.parse( input ); 166 return handler.getConfiguration(); 167 } 168 } 169 | Popular Tags |