KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > config > format > jdom > ConfigEntityResolver


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.config.format.jdom;
19
20 import java.io.InputStream JavaDoc;
21
22 import org.xml.sax.InputSource JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.DefaultHandler JavaDoc;
25
26 /**
27  * <p>Used to resolve entities in the JDOMConfigurationFactory.
28  * The major functionality added in this over the DefaultHandler
29  * is the ability to specify locations inside the classpath
30  * for enties such as <code>classpath://com/sapient/foo.dtd</code>
31  * thus allowing the validation documents to be inside the
32  * jars of the functional classes.</p>
33  *
34  * Copyright 2002 Sapient
35  * @since carbon 2.0
36  * @author Jordan Reed, March 2003
37  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:17 $)
38  */

39 public class ConfigEntityResolver extends DefaultHandler JavaDoc {
40     /** Protocol prefix indicating to classload the path. */
41     private static final String JavaDoc CLASSPATH_PREFIX = "classpath://";
42
43     /** Holds the name of the node being loaded. */
44     protected String JavaDoc name;
45
46     /**
47      * Constructs a new ConfigEntityResolver.
48      *
49      * @param name the name of the node being loaded.
50      */

51     public ConfigEntityResolver(String JavaDoc name) {
52         super();
53         this.name = name;
54     }
55
56     /**
57      * Resolves the external entity.
58      * <p>
59      * The only functionality added here is to check for the CLASSPATH_PREFIX
60      * and then attempt to classload the external entity from the location
61      * given. If there is no CLASSPATH_PREFIX or it fails to load, it will
62      * pass off resolution to the DefaultHandler it extends.
63      * </p>
64      *
65      * @param publicId The public identifier of the external entity
66      * being referenced, or null if none was supplied
67      * @param systemId The system identifier of the external entity being
68      * referenced.
69      * @return An InputSource object describing the new input source, or
70      * null to request that the parser open a regular URI connection
71      * to the system identifier.
72      * @throws SAXException indicates an error resolving the entity
73      */

74     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
75         throws SAXException JavaDoc {
76
77         InputSource JavaDoc inputSource = null;
78
79         if (systemId.startsWith(CLASSPATH_PREFIX)) {
80             String JavaDoc classpathLocation =
81                 systemId.substring(CLASSPATH_PREFIX.length());
82
83             InputStream JavaDoc inputStream =
84                 getClass().getClassLoader().getResourceAsStream(classpathLocation);
85
86             if (inputStream != null) {
87                 inputSource = new InputSource JavaDoc(inputStream);
88             }
89         }
90
91         if (inputSource != null) {
92             return inputSource;
93         } else {
94             return super.resolveEntity(publicId, systemId);
95         }
96     }
97 }
98
Popular Tags