KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001, 2002,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 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.xni.XNIException;
24 import org.apache.xerces.xni.XMLResourceIdentifier;
25 import org.apache.xerces.xni.parser.XMLEntityResolver;
26 import org.apache.xerces.xni.parser.XMLInputSource;
27
28 import org.xml.sax.EntityResolver JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  * This class wraps a SAX entity resolver in an XNI entity resolver.
34  *
35  * @see EntityResolver
36  *
37  * @author Andy Clark, IBM
38  *
39  * @version $Id: EntityResolverWrapper.java,v 1.6 2004/02/24 23:15:53 mrglavas Exp $
40  */

41 public class EntityResolverWrapper
42     implements XMLEntityResolver {
43
44     //
45
// Data
46
//
47

48     /** The SAX entity resolver. */
49     protected EntityResolver fEntityResolver;
50
51     //
52
// Constructors
53
//
54

55     /** Default constructor. */
56     public EntityResolverWrapper() {}
57
58     /** Wraps the specified SAX entity resolver. */
59     public EntityResolverWrapper(EntityResolver entityResolver) {
60         setEntityResolver(entityResolver);
61     } // <init>(EntityResolver)
62

63     //
64
// Public methods
65
//
66

67     /** Sets the SAX entity resolver. */
68     public void setEntityResolver(EntityResolver entityResolver) {
69         fEntityResolver = entityResolver;
70     } // setEntityResolver(EntityResolver)
71

72     /** Returns the SAX entity resolver. */
73     public EntityResolver getEntityResolver() {
74         return fEntityResolver;
75     } // getEntityResolver():EntityResolver
76

77     //
78
// XMLEntityResolver methods
79
//
80

81     /**
82      * Resolves an external parsed entity. If the entity cannot be
83      * resolved, this method should return null.
84      *
85      * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
86      *
87      * @throws XNIException Thrown on general error.
88      * @throws IOException Thrown if resolved entity stream cannot be
89      * opened or some other i/o error occurs.
90      */

91     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
92         throws XNIException, IOException JavaDoc {
93
94         // When both pubId and sysId are null, the user's entity resolver
95
// can do nothing about it. We'd better not bother calling it.
96
// This happens when the resourceIdentifier is a GrammarDescription,
97
// which describes a schema grammar of some namespace, but without
98
// any schema location hint. -Sg
99
String JavaDoc pubId = resourceIdentifier.getPublicId();
100         String JavaDoc sysId = resourceIdentifier.getExpandedSystemId();
101         if (pubId == null && sysId == null)
102             return null;
103
104         // resolve entity using SAX entity resolver
105
if (fEntityResolver != null && resourceIdentifier != null) {
106             try {
107                 InputSource inputSource = fEntityResolver.resolveEntity(pubId, sysId);
108                 if (inputSource != null) {
109                     String JavaDoc publicId = inputSource.getPublicId();
110                     String JavaDoc systemId = inputSource.getSystemId();
111                     String JavaDoc baseSystemId = resourceIdentifier.getBaseSystemId();
112                     InputStream JavaDoc byteStream = inputSource.getByteStream();
113                     Reader JavaDoc charStream = inputSource.getCharacterStream();
114                     String JavaDoc encoding = inputSource.getEncoding();
115                     XMLInputSource xmlInputSource =
116                         new XMLInputSource(publicId, systemId, baseSystemId);
117                     xmlInputSource.setByteStream(byteStream);
118                     xmlInputSource.setCharacterStream(charStream);
119                     xmlInputSource.setEncoding(encoding);
120                     return xmlInputSource;
121                 }
122             }
123
124             // error resolving entity
125
catch (SAXException JavaDoc e) {
126                 Exception JavaDoc ex = e.getException();
127                 if (ex == null) {
128                     ex = e;
129                 }
130                 throw new XNIException(ex);
131             }
132         }
133
134         // unable to resolve entity
135
return null;
136
137     } // resolveEntity(String,String,String):XMLInputSource
138
}
139
Popular Tags