KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > tools > configuration > DTDResolver


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.tools.configuration;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import org.xml.sax.EntityResolver JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15
16 /**
17  * A Class to help to resolve Entitys for items such as DTDs or
18  * Schemas.
19  *
20  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
21  * @version $Revision: 1.7 $ $Date: 2002/08/06 11:57:42 $
22  */

23 public class DTDResolver
24     implements EntityResolver JavaDoc
25 {
26     /**
27      * The list of DTDs that can be resolved by this class.
28      */

29     private final DTDInfo[] m_dtdInfos;
30
31     /**
32      * The ClassLoader to use when loading resources for DTDs.
33      */

34     private final ClassLoader JavaDoc m_classLoader;
35
36     /**
37      * Construct a resolver using specified DTDInfos where resources are loaded
38      * from specified ClassLoader.
39      */

40     public DTDResolver( final DTDInfo[] dtdInfos, final ClassLoader JavaDoc classLoader )
41     {
42         m_dtdInfos = dtdInfos;
43         m_classLoader = classLoader;
44     }
45
46     /**
47      * Resolve an entity in the XML file.
48      * Called by parser to resolve DTDs.
49      */

50     public InputSource JavaDoc resolveEntity( final String JavaDoc publicId, final String JavaDoc systemId )
51         throws IOException JavaDoc, SAXException JavaDoc
52     {
53         for( int i = 0; i < m_dtdInfos.length; i++ )
54         {
55             final DTDInfo info = m_dtdInfos[ i ];
56
57             if( (publicId != null && publicId.equals( info.getPublicId() )) ||
58                 (systemId != null && systemId.equals( info.getSystemId() )) )
59             {
60                 final ClassLoader JavaDoc classLoader = getClassLoader();
61                 final InputStream JavaDoc inputStream =
62                     classLoader.getResourceAsStream( info.getResource() );
63                 return new InputSource JavaDoc( inputStream );
64             }
65         }
66
67         return null;
68     }
69
70     /**
71      * Return CLassLoader to load resource from.
72      * If a ClassLoader is specified in the constructor use that,
73      * else use ContextClassLoader unless that is null in which case
74      * use the current classes ClassLoader.
75      */

76     private ClassLoader JavaDoc getClassLoader()
77     {
78         ClassLoader JavaDoc classLoader = m_classLoader;
79         if( null == classLoader )
80         {
81             classLoader = Thread.currentThread().getContextClassLoader();
82         }
83         if( null == classLoader )
84         {
85             classLoader = getClass().getClassLoader();
86         }
87         return classLoader;
88     }
89 }
90
Popular Tags