1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.configkit;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.Enumeration;
15 import java.util.List;
16 import javax.xml.parsers.ParserConfigurationException;
17 import javax.xml.parsers.SAXParserFactory;
18 import org.xml.sax.EntityResolver;
19 import org.xml.sax.InputSource;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.XMLReader;
22
23 /***
24 * This is a utility class for creating an EntityResolver that can resolve all
25 * entitys contained within ClassLoader. The entitys are discovered by looking
26 * in a catalog file <code>META-INF/spice/catalog.xml</code>. The format of the
27 * catalog file is;
28 *
29 * <pre>
30 * <catalog version="1.0">
31 * <entity publicId="-//PHOENIX/Assembly DTD Version 1.0//EN"
32 * resource="org/apache/avalon/phoenix/tools/assembly.dtd"/>
33 * <entity systemId="http://jakarta.apache.org/phoenix/assembly_1_0.dtd"
34 * resource="org/apache/avalon/phoenix/tools/assembly.dtd"/>
35 * <entity publicId=""-//PHOENIX/Mx Info DTD Version 1.0//EN""
36 * systemId="http://jakarta.apache.org/phoenix/mxinfo_1_0.dtd"
37 * resource="org/apache/avalon/phoenix/tools/mxinfo.dtd"/>
38 * </catalog>
39 * </pre>
40 *
41 * <p>Note that at least one of <code>publicId</code> or <code>systemId</code>
42 * must be specified and <code>resource</code> must always be specified.
43 *
44 * @author Peter Donald
45 * @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $
46 */
47 public final class ResolverFactory
48 {
49 /*** Constant for location where catalog is loaded. */
50 private static final String CATALOG_RESOURCE = "META-INF/spice/catalog.xml";
51
52 /***
53 * @param classLoader the ClassLoader to scan for catalog files
54 * @return an Entity Resolver that will resolver all the entitys defined in
55 * catalog and loadable from ClassLoader
56 * @throws SAXException if unable to parse a Catalog file
57 * @throws IOException if unable to load a Catalog file
58 */
59 public static final EntityResolver createResolver(
60 final ClassLoader classLoader )
61 throws ParserConfigurationException, SAXException, IOException
62 {
63 if( null == classLoader )
64 {
65 throw new NullPointerException( "classLoader" );
66 }
67 final List entitys = new ArrayList();
68 final Enumeration resources = classLoader.getResources(
69 CATALOG_RESOURCE );
70 while( resources.hasMoreElements() )
71 {
72 final URL url = (URL)resources.nextElement();
73 parseCatalog( url, entitys );
74 }
75 final EntityInfo[] infos = (EntityInfo[])entitys.
76 toArray( new EntityInfo[ entitys.size() ] );
77 return new ConfigKitEntityResolver( infos, classLoader );
78 }
79
80 /***
81 * Helper method to parse a catalog specified by url and add all the
82 * discovered entitys to the entity list.
83 *
84 * @param url the url of catalog
85 * @param entitys the list of entitys
86 * @throws SAXException if unable to parse catalog
87 * @throws IOException if unable to read catalog from url
88 */
89 private static void parseCatalog( final URL url, final List entitys )
90 throws ParserConfigurationException, SAXException, IOException
91 {
92 final InputStream inputStream = url.openStream();
93 try
94 {
95 final XMLReader xmlReader = createXMLReader();
96 final CatalogHandler handler = new CatalogHandler( entitys );
97 xmlReader.setContentHandler( handler );
98 xmlReader.setErrorHandler( handler );
99 xmlReader.parse( new InputSource( inputStream ) );
100 }
101 finally
102 {
103 try
104 {
105 inputStream.close();
106 }
107 catch( final IOException ioe )
108 {
109 //ignore as probably means it already closed
110 }
111 }
112 }
113
114 /***
115 * Create an XMLReader.
116 *
117 * @return the created XMLReader
118 * @throws SAXException if unable to create reader
119 */
120 private static XMLReader createXMLReader()
121 throws SAXException, ParserConfigurationException
122 {
123 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
124 saxParserFactory.setNamespaceAware( false );
125 return saxParserFactory.newSAXParser().getXMLReader();
126 }
127 }
This page was automatically generated by Maven