KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > util > EntityResolver2Wrapper


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.util;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 import org.apache.xerces.impl.ExternalSubsetResolver;
24 import org.apache.xerces.impl.XMLEntityDescription;
25 import org.apache.xerces.xni.XMLResourceIdentifier;
26 import org.apache.xerces.xni.XNIException;
27 import org.apache.xerces.xni.grammars.XMLDTDDescription;
28 import org.apache.xerces.xni.parser.XMLInputSource;
29
30 import org.xml.sax.ext.EntityResolver2 JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * <p>This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.</p>
36  *
37  * @author Michael Glavassevich, IBM
38  *
39  * @version $Id: EntityResolver2Wrapper.java,v 1.5 2005/05/02 21:44:37 mrglavas Exp $
40  */

41 public class EntityResolver2Wrapper
42     implements ExternalSubsetResolver {
43
44     //
45
// Data
46
//
47

48     /** An instance of SAX2 Extensions 1.1's EntityResolver2. */
49     protected EntityResolver2 JavaDoc fEntityResolver;
50     
51     //
52
// Constructors
53
//
54

55     /** Default constructor. */
56     public EntityResolver2Wrapper() {}
57
58     /**
59      * <p>Creates a new instance wrapping the given SAX entity resolver.</p>
60      *
61      * @param entityResolver the SAX entity resolver to wrap
62      */

63     public EntityResolver2Wrapper(EntityResolver2 JavaDoc entityResolver) {
64         setEntityResolver(entityResolver);
65     } // <init>(EntityResolver2)
66

67     //
68
// Public methods
69
//
70

71     /**
72      * <p>Sets the SAX entity resolver wrapped by this object.</p>
73      *
74      * @param entityResolver the SAX entity resolver to wrap
75      */

76     public void setEntityResolver(EntityResolver2 JavaDoc entityResolver) {
77         fEntityResolver = entityResolver;
78     } // setEntityResolver(EntityResolver2)
79

80     /**
81      * <p>Returns the SAX entity resolver wrapped by this object.</p>
82      *
83      * @return the SAX entity resolver wrapped by this object
84      */

85     public EntityResolver2 JavaDoc getEntityResolver() {
86         return fEntityResolver;
87     } // getEntityResolver():EntityResolver2
88

89     //
90
// ExternalSubsetResolver methods
91
//
92

93     /**
94      * <p>Locates an external subset for documents which do not explicitly
95      * provide one. If no external subset is provided, this method should
96      * return <code>null</code>.</p>
97      *
98      * @param grammarDescription a description of the DTD
99      *
100      * @throws XNIException Thrown on general error.
101      * @throws IOException Thrown if resolved entity stream cannot be
102      * opened or some other i/o error occurs.
103      */

104     public XMLInputSource getExternalSubset(XMLDTDDescription grammarDescription)
105             throws XNIException, IOException JavaDoc {
106         
107         if (fEntityResolver != null) {
108             
109             String JavaDoc name = grammarDescription.getRootName();
110             String JavaDoc baseURI = grammarDescription.getBaseSystemId();
111             
112             // Resolve using EntityResolver2
113
try {
114                 InputSource inputSource = fEntityResolver.getExternalSubset(name, baseURI);
115                 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
116             }
117             // error resolving external subset
118
catch (SAXException JavaDoc e) {
119                 Exception JavaDoc ex = e.getException();
120                 if (ex == null) {
121                     ex = e;
122                 }
123                 throw new XNIException(ex);
124             }
125         }
126         
127         // unable to resolve external subset
128
return null;
129         
130     } // getExternalSubset(XMLDTDDescription):XMLInputSource
131

132     //
133
// XMLEntityResolver methods
134
//
135

136     /**
137      * Resolves an external parsed entity. If the entity cannot be
138      * resolved, this method should return null.
139      *
140      * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
141      *
142      * @throws XNIException Thrown on general error.
143      * @throws IOException Thrown if resolved entity stream cannot be
144      * opened or some other i/o error occurs.
145      */

146     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
147             throws XNIException, IOException JavaDoc {
148         
149         if (fEntityResolver != null) {
150             
151             String JavaDoc pubId = resourceIdentifier.getPublicId();
152             String JavaDoc sysId = resourceIdentifier.getLiteralSystemId();
153             String JavaDoc baseURI = resourceIdentifier.getBaseSystemId();
154             String JavaDoc name = null;
155             if (resourceIdentifier instanceof XMLDTDDescription) {
156                 name = "[dtd]";
157             }
158             else if (resourceIdentifier instanceof XMLEntityDescription) {
159                 name = ((XMLEntityDescription) resourceIdentifier).getEntityName();
160             }
161             
162             // When both pubId and sysId are null, the user's entity resolver
163
// can do nothing about it. We'd better not bother calling it.
164
// This happens when the resourceIdentifier is a GrammarDescription,
165
// which describes a schema grammar of some namespace, but without
166
// any schema location hint. -Sg
167
if (pubId == null && sysId == null) {
168                 return null;
169             }
170             
171             // Resolve using EntityResolver2
172
try {
173                 InputSource inputSource =
174                     fEntityResolver.resolveEntity(name, pubId, baseURI, sysId);
175                 return (inputSource != null) ? createXMLInputSource(inputSource, baseURI) : null;
176             }
177             // error resolving entity
178
catch (SAXException JavaDoc e) {
179                 Exception JavaDoc ex = e.getException();
180                 if (ex == null) {
181                     ex = e;
182                 }
183                 throw new XNIException(ex);
184             }
185         }
186         
187         // unable to resolve entity
188
return null;
189         
190     } // resolveEntity(XMLResourceIdentifier):XMLInputSource
191

192     /**
193      * Creates an XMLInputSource from a SAX InputSource.
194      */

195     private XMLInputSource createXMLInputSource(InputSource source, String JavaDoc baseURI) {
196  
197         String JavaDoc publicId = source.getPublicId();
198         String JavaDoc systemId = source.getSystemId();
199         String JavaDoc baseSystemId = baseURI;
200         InputStream JavaDoc byteStream = source.getByteStream();
201         Reader JavaDoc charStream = source.getCharacterStream();
202         String JavaDoc encoding = source.getEncoding();
203         XMLInputSource xmlInputSource =
204             new XMLInputSource(publicId, systemId, baseSystemId);
205         xmlInputSource.setByteStream(byteStream);
206         xmlInputSource.setCharacterStream(charStream);
207         xmlInputSource.setEncoding(encoding);
208         return xmlInputSource;
209         
210     } // createXMLInputSource(InputSource,String):XMLInputSource
211

212 } // class EntityResolver2Wrapper
213
Popular Tags