KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > ConfigurationBuilder


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
26     "Apache Software Foundation" must not be used to endorse or promote
27     products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50
51 package org.apache.avalon.meta;
52
53 import java.io.IOException JavaDoc;
54 import java.net.MalformedURLException JavaDoc;
55 import javax.xml.parsers.ParserConfigurationException JavaDoc;
56 import javax.xml.parsers.SAXParser JavaDoc;
57 import javax.xml.parsers.SAXParserFactory JavaDoc;
58 import org.apache.avalon.framework.configuration.Configuration;
59 import org.apache.avalon.framework.configuration.SAXConfigurationHandler;
60 import org.xml.sax.InputSource JavaDoc;
61 import org.xml.sax.SAXException JavaDoc;
62 import org.xml.sax.XMLReader JavaDoc;
63
64 /**
65  * Utility class used to load Configuration trees from XML files.
66  *
67  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
68  * @version $Revision: 1.1.1.1 $ $Date: 2003/07/10 12:10:02 $
69  */

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

94     private ConfigurationBuilder()
95     {
96     }
97
98     /**
99      * Utility method to create a new XML reader.
100      */

101     private static XMLReader JavaDoc createXMLReader()
102         throws SAXException JavaDoc, ParserConfigurationException JavaDoc
103     {
104         final SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
105         saxParserFactory.setNamespaceAware( false );
106         final SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
107         return saxParser.getXMLReader();
108     }
109
110     /**
111      * Internally sets up the XMLReader
112      */

113     private static void setupXMLReader( final XMLReader JavaDoc reader,
114                                         final SAXConfigurationHandler handler )
115     {
116         reader.setEntityResolver( DTD_RESOLVER );
117         reader.setContentHandler( handler );
118         reader.setErrorHandler( handler );
119     }
120
121     /**
122      * Build a configuration object using an URI
123      * @param uri an input source system identifier
124      * @return the contfiguration instance
125      * @exception SAXException is a parser exception is encountered
126      * @exception ParserConfigurationException if a parser configuration failure occurs
127      * @exception IOException if an IO exception occurs while attempting to read the
128      * resource identified by the system identifier
129      */

130     public static Configuration build( final String JavaDoc uri )
131         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
132     {
133         try
134         {
135             return build( new InputSource JavaDoc( uri ) );
136         }
137         catch( MalformedURLException JavaDoc mue )
138         {
139             final String JavaDoc error =
140               "Invalid input source uri: " + uri;
141             throw new IOException JavaDoc( error );
142         }
143     }
144
145     /**
146      * Build a configuration object using an XML InputSource object
147      * @param input an input source
148      * @return the contfiguration instance
149      * @exception SAXException is a parser exception is encountered
150      * @exception ParserConfigurationException if a parser configuration failure occurs
151      * @exception IOException if an IO exception occurs while attempting to read the
152      * resource associated with the input source
153      */

154     public static Configuration build( final InputSource JavaDoc input )
155         throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc
156     {
157         if( input == null )
158         {
159             throw new NullPointerException JavaDoc( "input" );
160         }
161
162         final XMLReader JavaDoc 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