KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > EntityResolver2Wrapper


1 /*
2  * Copyright 2004 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 com.sun.org.apache.xerces.internal.util;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 import com.sun.org.apache.xerces.internal.impl.ExternalSubsetResolver;
24 import com.sun.org.apache.xerces.internal.impl.XMLEntityDescription;
25 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
26 import com.sun.org.apache.xerces.internal.xni.XNIException;
27 import com.sun.org.apache.xerces.internal.xni.grammars.XMLDTDDescription;
28 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
29
30 import org.xml.sax.EntityResolver JavaDoc;
31 import org.xml.sax.ext.EntityResolver2 JavaDoc;
32 import org.xml.sax.InputSource JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * <p>This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.</p>
37  *
38  * @author Michael Glavassevich, IBM
39  *
40  * @version $Id: EntityResolver2Wrapper.java,v 1.2 2004/05/07 09:00:32 vk112360 Exp $
41  */

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

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

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

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

68     //
69
// Public methods
70
//
71

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

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

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

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

90     //
91
// ExternalSubsetResolver methods
92
//
93

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

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

133     //
134
// XMLEntityResolver methods
135
//
136

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

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

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

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

213 } // class EntityResolver2Wrapper
214
Popular Tags