|
|||||||||||||||||||
| 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 | |||||||||||||||
| ConfigKitEntityResolver.java | 92.9% | 96.2% | 100% | 95.3% |
|
||||||||||||||
| 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 org.xml.sax.EntityResolver;
|
|
| 13 |
import org.xml.sax.InputSource;
|
|
| 14 |
import org.xml.sax.SAXException;
|
|
| 15 |
|
|
| 16 |
/**
|
|
| 17 |
* A Class to help to resolve Entitys for items such as DTDs or Schemas.
|
|
| 18 |
*
|
|
| 19 |
* @author Peter Donald
|
|
| 20 |
* @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $
|
|
| 21 |
*/
|
|
| 22 |
class ConfigKitEntityResolver
|
|
| 23 |
implements EntityResolver
|
|
| 24 |
{
|
|
| 25 |
/** The list of DTDs that can be resolved by this class. */
|
|
| 26 |
private final EntityInfo[] m_infos;
|
|
| 27 |
|
|
| 28 |
/** The ClassLoader to use when loading resources for DTDs. */
|
|
| 29 |
private final ClassLoader m_classLoader;
|
|
| 30 |
|
|
| 31 |
/**
|
|
| 32 |
* Construct a resolver using specified DTDInfos where resources are loaded
|
|
| 33 |
* from specified ClassLoader.
|
|
| 34 |
*/
|
|
| 35 | 26 |
ConfigKitEntityResolver( final EntityInfo[] infos, |
| 36 |
final ClassLoader classLoader ) |
|
| 37 |
{
|
|
| 38 | 26 |
if( null == infos ) |
| 39 |
{
|
|
| 40 | 1 |
throw new NullPointerException( "infos" ); |
| 41 |
} |
|
| 42 | 25 |
m_infos = infos; |
| 43 | 25 |
m_classLoader = classLoader; |
| 44 |
} |
|
| 45 |
|
|
| 46 |
/**
|
|
| 47 |
* Resolve an entity in the XML file. Called by parser to resolve DTDs.
|
|
| 48 |
*/
|
|
| 49 | 22 |
public InputSource resolveEntity( final String publicId,
|
| 50 |
final String systemId ) |
|
| 51 |
throws IOException, SAXException
|
|
| 52 |
{
|
|
| 53 | 22 |
for( int i = 0; i < m_infos.length; i++ ) |
| 54 |
{
|
|
| 55 | 35 |
final EntityInfo info = m_infos[ i ]; |
| 56 | 35 |
if( ( publicId != null && publicId.equals( info.getPublicId() ) ) || |
| 57 |
( systemId != null && systemId.equals( info.getSystemId() ) ) )
|
|
| 58 |
{
|
|
| 59 | 15 |
final ClassLoader classLoader = getClassLoader(); |
| 60 | 15 |
final String resource = info.getResource(); |
| 61 | 15 |
final InputStream inputStream = |
| 62 |
classLoader.getResourceAsStream( resource ); |
|
| 63 | 15 |
if( null == inputStream ) |
| 64 |
{
|
|
| 65 | 1 |
final String message = |
| 66 |
"Unable to locate resource " +
|
|
| 67 |
resource + |
|
| 68 |
" for entity with publicId=" +
|
|
| 69 |
publicId + |
|
| 70 |
" and systemId=" +
|
|
| 71 |
systemId + |
|
| 72 |
". " +
|
|
| 73 |
"Looking in classloader " + classLoader + "."; |
|
| 74 | 1 |
throw new IOException( message ); |
| 75 |
} |
|
| 76 | 14 |
final InputSource inputSource = new InputSource( inputStream );
|
| 77 | 14 |
inputSource.setPublicId( info.getPublicId() ); |
| 78 |
|
|
| 79 |
//Always try to have at least a basic resource id
|
|
| 80 |
//in system id as some tools will look at extension to
|
|
| 81 |
//determine how to handle resource (ie .dtd is handled
|
|
| 82 |
//differently)
|
|
| 83 | 14 |
if( null == info.getSystemId() ) |
| 84 |
{
|
|
| 85 | 0 |
inputSource.setSystemId( resource ); |
| 86 |
} |
|
| 87 |
else
|
|
| 88 |
{
|
|
| 89 | 14 |
inputSource.setSystemId( info.getSystemId() ); |
| 90 |
} |
|
| 91 | 14 |
return inputSource;
|
| 92 |
} |
|
| 93 |
} |
|
| 94 |
|
|
| 95 | 7 |
return null; |
| 96 |
} |
|
| 97 |
|
|
| 98 |
/**
|
|
| 99 |
* Return CLassLoader to load resource from. If a ClassLoader is specified
|
|
| 100 |
* in the constructor use that, else use ContextClassLoader unless that is
|
|
| 101 |
* null in which case use the current classes ClassLoader.
|
|
| 102 |
*/
|
|
| 103 | 15 |
private ClassLoader getClassLoader()
|
| 104 |
{
|
|
| 105 | 15 |
ClassLoader classLoader = m_classLoader; |
| 106 | 15 |
if( null == classLoader ) |
| 107 |
{
|
|
| 108 | 4 |
classLoader = Thread.currentThread().getContextClassLoader(); |
| 109 |
} |
|
| 110 | 15 |
if( null == classLoader ) |
| 111 |
{
|
|
| 112 | 2 |
classLoader = getClass().getClassLoader(); |
| 113 |
} |
|
| 114 | 15 |
return classLoader;
|
| 115 |
} |
|
| 116 |
} |
|
| 117 |
|
|
||||||||||