001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.util;
019:
020: import java.io.InputStream;
021: import java.io.IOException;
022: import java.io.Reader;
023:
024: import org.apache.xerces.impl.ExternalSubsetResolver;
025: import org.apache.xerces.impl.XMLEntityDescription;
026: import org.apache.xerces.xni.XMLResourceIdentifier;
027: import org.apache.xerces.xni.XNIException;
028: import org.apache.xerces.xni.grammars.XMLDTDDescription;
029: import org.apache.xerces.xni.parser.XMLInputSource;
030:
031: import org.xml.sax.ext.EntityResolver2;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034:
035: /**
036: * <p>This class wraps a SAX entity resolver (EntityResolver2) in an XNI entity resolver.</p>
037: *
038: * @author Michael Glavassevich, IBM
039: *
040: * @version $Id: EntityResolver2Wrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
041: */
042: public class EntityResolver2Wrapper implements ExternalSubsetResolver {
043:
044: //
045: // Data
046: //
047:
048: /** An instance of SAX2 Extensions 1.1's EntityResolver2. */
049: protected EntityResolver2 fEntityResolver;
050:
051: //
052: // Constructors
053: //
054:
055: /** Default constructor. */
056: public EntityResolver2Wrapper() {
057: }
058:
059: /**
060: * <p>Creates a new instance wrapping the given SAX entity resolver.</p>
061: *
062: * @param entityResolver the SAX entity resolver to wrap
063: */
064: public EntityResolver2Wrapper(EntityResolver2 entityResolver) {
065: setEntityResolver(entityResolver);
066: } // <init>(EntityResolver2)
067:
068: //
069: // Public methods
070: //
071:
072: /**
073: * <p>Sets the SAX entity resolver wrapped by this object.</p>
074: *
075: * @param entityResolver the SAX entity resolver to wrap
076: */
077: public void setEntityResolver(EntityResolver2 entityResolver) {
078: fEntityResolver = entityResolver;
079: } // setEntityResolver(EntityResolver2)
080:
081: /**
082: * <p>Returns the SAX entity resolver wrapped by this object.</p>
083: *
084: * @return the SAX entity resolver wrapped by this object
085: */
086: public EntityResolver2 getEntityResolver() {
087: return fEntityResolver;
088: } // getEntityResolver():EntityResolver2
089:
090: //
091: // ExternalSubsetResolver methods
092: //
093:
094: /**
095: * <p>Locates an external subset for documents which do not explicitly
096: * provide one. If no external subset is provided, this method should
097: * return <code>null</code>.</p>
098: *
099: * @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(
106: XMLDTDDescription grammarDescription) throws XNIException,
107: IOException {
108:
109: if (fEntityResolver != null) {
110:
111: String name = grammarDescription.getRootName();
112: String baseURI = grammarDescription.getBaseSystemId();
113:
114: // Resolve using EntityResolver2
115: try {
116: InputSource inputSource = fEntityResolver
117: .getExternalSubset(name, baseURI);
118: return (inputSource != null) ? createXMLInputSource(
119: inputSource, baseURI) : null;
120: }
121: // error resolving external subset
122: catch (SAXException e) {
123: Exception ex = e.getException();
124: if (ex == null) {
125: ex = e;
126: }
127: throw new XNIException(ex);
128: }
129: }
130:
131: // unable to resolve external subset
132: return null;
133:
134: } // getExternalSubset(XMLDTDDescription):XMLInputSource
135:
136: //
137: // XMLEntityResolver methods
138: //
139:
140: /**
141: * Resolves an external parsed entity. If the entity cannot be
142: * resolved, this method should return null.
143: *
144: * @param resourceIdentifier contains the physical co-ordinates of the resource to be resolved
145: *
146: * @throws XNIException Thrown on general error.
147: * @throws IOException Thrown if resolved entity stream cannot be
148: * opened or some other i/o error occurs.
149: */
150: public XMLInputSource resolveEntity(
151: XMLResourceIdentifier resourceIdentifier)
152: throws XNIException, IOException {
153:
154: if (fEntityResolver != null) {
155:
156: String pubId = resourceIdentifier.getPublicId();
157: String sysId = resourceIdentifier.getLiteralSystemId();
158: String baseURI = resourceIdentifier.getBaseSystemId();
159: String name = null;
160: if (resourceIdentifier instanceof XMLDTDDescription) {
161: name = "[dtd]";
162: } else if (resourceIdentifier instanceof XMLEntityDescription) {
163: name = ((XMLEntityDescription) resourceIdentifier)
164: .getEntityName();
165: }
166:
167: // When both pubId and sysId are null, the user's entity resolver
168: // can do nothing about it. We'd better not bother calling it.
169: // This happens when the resourceIdentifier is a GrammarDescription,
170: // which describes a schema grammar of some namespace, but without
171: // any schema location hint. -Sg
172: if (pubId == null && sysId == null) {
173: return null;
174: }
175:
176: // Resolve using EntityResolver2
177: try {
178: InputSource inputSource = fEntityResolver
179: .resolveEntity(name, pubId, baseURI, sysId);
180: return (inputSource != null) ? createXMLInputSource(
181: inputSource, baseURI) : null;
182: }
183: // error resolving entity
184: catch (SAXException e) {
185: Exception ex = e.getException();
186: if (ex == null) {
187: ex = e;
188: }
189: throw new XNIException(ex);
190: }
191: }
192:
193: // unable to resolve entity
194: return null;
195:
196: } // resolveEntity(XMLResourceIdentifier):XMLInputSource
197:
198: /**
199: * Creates an XMLInputSource from a SAX InputSource.
200: */
201: private XMLInputSource createXMLInputSource(InputSource source,
202: String baseURI) {
203:
204: String publicId = source.getPublicId();
205: String systemId = source.getSystemId();
206: String baseSystemId = baseURI;
207: InputStream byteStream = source.getByteStream();
208: Reader charStream = source.getCharacterStream();
209: String encoding = source.getEncoding();
210: XMLInputSource xmlInputSource = new XMLInputSource(publicId,
211: systemId, baseSystemId);
212: xmlInputSource.setByteStream(byteStream);
213: xmlInputSource.setCharacterStream(charStream);
214: xmlInputSource.setEncoding(encoding);
215: return xmlInputSource;
216:
217: } // createXMLInputSource(InputSource,String):XMLInputSource
218:
219: } // class EntityResolver2Wrapper
|