View Javadoc
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 ConfigKitEntityResolver( final EntityInfo[] infos, 36 final ClassLoader classLoader ) 37 { 38 if( null == infos ) 39 { 40 throw new NullPointerException( "infos" ); 41 } 42 m_infos = infos; 43 m_classLoader = classLoader; 44 } 45 46 /*** 47 * Resolve an entity in the XML file. Called by parser to resolve DTDs. 48 */ 49 public InputSource resolveEntity( final String publicId, 50 final String systemId ) 51 throws IOException, SAXException 52 { 53 for( int i = 0; i < m_infos.length; i++ ) 54 { 55 final EntityInfo info = m_infos[ i ]; 56 if( ( publicId != null && publicId.equals( info.getPublicId() ) ) || 57 ( systemId != null && systemId.equals( info.getSystemId() ) ) ) 58 { 59 final ClassLoader classLoader = getClassLoader(); 60 final String resource = info.getResource(); 61 final InputStream inputStream = 62 classLoader.getResourceAsStream( resource ); 63 if( null == inputStream ) 64 { 65 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 throw new IOException( message ); 75 } 76 final InputSource inputSource = new InputSource( inputStream ); 77 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 if( null == info.getSystemId() ) 84 { 85 inputSource.setSystemId( resource ); 86 } 87 else 88 { 89 inputSource.setSystemId( info.getSystemId() ); 90 } 91 return inputSource; 92 } 93 } 94 95 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 private ClassLoader getClassLoader() 104 { 105 ClassLoader classLoader = m_classLoader; 106 if( null == classLoader ) 107 { 108 classLoader = Thread.currentThread().getContextClassLoader(); 109 } 110 if( null == classLoader ) 111 { 112 classLoader = getClass().getClassLoader(); 113 } 114 return classLoader; 115 } 116 }

This page was automatically generated by Maven