|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ResolverFactory.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 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 | 13 |
public static final EntityResolver createResolver( |
| 60 |
final ClassLoader classLoader ) |
|
| 61 |
throws ParserConfigurationException, SAXException, IOException
|
|
| 62 |
{
|
|
| 63 | 13 |
if( null == classLoader ) |
| 64 |
{
|
|
| 65 | 2 |
throw new NullPointerException( "classLoader" ); |
| 66 |
} |
|
| 67 | 11 |
final List entitys = new ArrayList();
|
| 68 | 11 |
final Enumeration resources = classLoader.getResources( |
| 69 |
CATALOG_RESOURCE ); |
|
| 70 | 11 |
while( resources.hasMoreElements() )
|
| 71 |
{
|
|
| 72 | 11 |
final URL url = (URL)resources.nextElement(); |
| 73 | 11 |
parseCatalog( url, entitys ); |
| 74 |
} |
|
| 75 | 10 |
final EntityInfo[] infos = (EntityInfo[])entitys. |
| 76 |
toArray( new EntityInfo[ entitys.size() ] );
|
|
| 77 | 10 |
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 | 11 |
private static void parseCatalog( final URL url, final List entitys ) |
| 90 |
throws ParserConfigurationException, SAXException, IOException
|
|
| 91 |
{
|
|
| 92 | 11 |
final InputStream inputStream = url.openStream(); |
| 93 | 11 |
try
|
| 94 |
{
|
|
| 95 | 11 |
final XMLReader xmlReader = createXMLReader(); |
| 96 | 10 |
final CatalogHandler handler = new CatalogHandler( entitys );
|
| 97 | 10 |
xmlReader.setContentHandler( handler ); |
| 98 | 10 |
xmlReader.setErrorHandler( handler ); |
| 99 | 10 |
xmlReader.parse( new InputSource( inputStream ) );
|
| 100 |
} |
|
| 101 |
finally
|
|
| 102 |
{
|
|
| 103 | 11 |
try
|
| 104 |
{
|
|
| 105 | 11 |
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 | 11 |
private static XMLReader createXMLReader() |
| 121 |
throws SAXException, ParserConfigurationException
|
|
| 122 |
{
|
|
| 123 | 11 |
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
| 124 | 10 |
saxParserFactory.setNamespaceAware( false );
|
| 125 | 10 |
return saxParserFactory.newSAXParser().getXMLReader();
|
| 126 |
} |
|
| 127 |
} |
|
| 128 |
|
|
||||||||||