KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > tools > configuration > ConfigurationBuilder


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.tools.configuration;
9
10 import java.io.IOException JavaDoc;
11 import javax.xml.parsers.ParserConfigurationException JavaDoc;
12 import javax.xml.parsers.SAXParser JavaDoc;
13 import javax.xml.parsers.SAXParserFactory JavaDoc;
14 import org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
16 import org.xml.sax.InputSource JavaDoc;
17 import org.xml.sax.SAXException JavaDoc;
18 import org.xml.sax.XMLReader JavaDoc;
19
20 /**
21  * Utility class used to load Configuration trees from XML files.
22  *
23  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
24  * @version $Revision: 1.7.2.1 $ $Date: 2002/09/06 23:37:12 $
25  */

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     /**
54      * Private constructor to block instantiation.
55      */

56     private ConfigurationBuilder()
57     {
58     }
59
60     /**
61      * Utility method to create a new XML reader.
62      */

63     private static XMLReader JavaDoc createXMLReader()
64         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
65     {
66         final SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
67         saxParserFactory.setNamespaceAware( false );
68         final SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
69         return saxParser.getXMLReader();
70     }
71
72     /**
73      * Internally sets up the XMLReader
74      */

75     private static void setupXMLReader( final XMLReader JavaDoc reader,
76                                         final SAXConfigurationHandler handler,
77                                         final boolean validate )
78         throws SAXException JavaDoc
79     {
80         reader.setEntityResolver( c_resolver );
81         reader.setContentHandler( handler );
82         reader.setErrorHandler( handler );
83
84         if( validate )
85         {
86             // Request validation
87
reader.setFeature( "http://xml.org/sax/features/validation", true );
88         }
89     }
90
91     /**
92      * Build a configuration object using an URI
93      */

94     public static Configuration build( final String JavaDoc uri )
95         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
96     {
97         return build( new InputSource JavaDoc( uri ) );
98     }
99
100     /**
101      * Build a configuration object using an URI, and
102      * optionally validate the xml against the DTD.
103      */

104     public static Configuration build( final String JavaDoc uri, boolean validate )
105         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
106     {
107         return build( new InputSource JavaDoc( uri ), validate );
108     }
109
110     /**
111      * Build a configuration object using an XML InputSource object
112      */

113     public static Configuration build( final InputSource JavaDoc input )
114         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
115     {
116         return build( input, false );
117     }
118
119     /**
120      * Build a configuration object using an XML InputSource object, and
121      * optionally validate the xml against the DTD.
122      */

123     public static Configuration build( final InputSource JavaDoc input, boolean validate )
124         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
125     {
126         final XMLReader JavaDoc 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