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 org.apache.xerces.xni.XNIException;
021: import org.apache.xerces.xni.XMLResourceIdentifier;
022: import org.apache.xerces.xni.grammars.XMLGrammarDescription;
023: import org.apache.xerces.xni.parser.XMLEntityResolver;
024: import org.apache.xerces.xni.parser.XMLInputSource;
025:
026: import org.w3c.dom.ls.LSResourceResolver;
027: import org.w3c.dom.ls.LSInput;
028:
029: import java.io.InputStream;
030: import java.io.IOException;
031: import java.io.Reader;
032: import java.io.StringReader;
033:
034: /**
035: * This class wraps DOM entity resolver to XNI entity resolver.
036: *
037: * @see LSResourceResolver
038: *
039: * @author Gopal Sharma, SUN MicroSystems Inc.
040: * @author Elena Litani, IBM
041: * @author Ramesh Mandava, Sun Microsystems
042: * @version $Id: DOMEntityResolverWrapper.java 447241 2006-09-18 05:12:57Z mrglavas $
043: */
044: public class DOMEntityResolverWrapper implements XMLEntityResolver {
045:
046: //
047: // Data
048: //
049:
050: /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
051: private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml";
052:
053: /** XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
054: private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
055:
056: /** The DOM entity resolver. */
057: protected LSResourceResolver fEntityResolver;
058:
059: //
060: // Constructors
061: //
062:
063: /** Default constructor. */
064: public DOMEntityResolverWrapper() {
065: }
066:
067: /** Wraps the specified DOM entity resolver. */
068: public DOMEntityResolverWrapper(LSResourceResolver entityResolver) {
069: setEntityResolver(entityResolver);
070: } // LSResourceResolver
071:
072: //
073: // Public methods
074: //
075:
076: /** Sets the DOM entity resolver. */
077: public void setEntityResolver(LSResourceResolver entityResolver) {
078: fEntityResolver = entityResolver;
079: } // setEntityResolver(LSResourceResolver)
080:
081: /** Returns the DOM entity resolver. */
082: public LSResourceResolver getEntityResolver() {
083: return fEntityResolver;
084: } // getEntityResolver():LSResourceResolver
085:
086: //
087: // XMLEntityResolver methods
088: //
089:
090: /**
091: * Resolves an external parsed entity. If the entity cannot be
092: * resolved, this method should return null.
093: *
094: * @param resourceIdentifier description of the resource to be revsoved
095: * @throws XNIException Thrown on general error.
096: * @throws IOException Thrown if resolved entity stream cannot be
097: * opened or some other i/o error occurs.
098: */
099: public XMLInputSource resolveEntity(
100: XMLResourceIdentifier resourceIdentifier)
101: throws XNIException, IOException {
102: // resolve entity using DOM entity resolver
103: if (fEntityResolver != null) {
104: // For entity resolution the type of the resource would be XML TYPE
105: // DOM L3 LS spec mention only the XML 1.0 recommendation right now
106: LSInput inputSource = resourceIdentifier == null ? fEntityResolver
107: .resolveResource(null, null, null, null, null)
108: : fEntityResolver.resolveResource(
109: getType(resourceIdentifier),
110: resourceIdentifier.getNamespace(),
111: resourceIdentifier.getPublicId(),
112: resourceIdentifier.getLiteralSystemId(),
113: resourceIdentifier.getBaseSystemId());
114: if (inputSource != null) {
115: String publicId = inputSource.getPublicId();
116: String systemId = inputSource.getSystemId();
117: String baseSystemId = inputSource.getBaseURI();
118: InputStream byteStream = inputSource.getByteStream();
119: Reader charStream = inputSource.getCharacterStream();
120: String encoding = inputSource.getEncoding();
121: String data = inputSource.getStringData();
122:
123: /**
124: * An LSParser looks at inputs specified in LSInput in
125: * the following order: characterStream, byteStream,
126: * stringData, systemId, publicId.
127: */
128: XMLInputSource xmlInputSource = new XMLInputSource(
129: publicId, systemId, baseSystemId);
130:
131: if (charStream != null) {
132: xmlInputSource.setCharacterStream(charStream);
133: } else if (byteStream != null) {
134: xmlInputSource
135: .setByteStream((InputStream) byteStream);
136: } else if (data != null && data.length() != 0) {
137: xmlInputSource.setCharacterStream(new StringReader(
138: data));
139: }
140: xmlInputSource.setEncoding(encoding);
141: return xmlInputSource;
142: }
143: }
144:
145: // unable to resolve entity
146: return null;
147:
148: } // resolveEntity(String,String,String):XMLInputSource
149:
150: /** Determines the type of resource being resolved **/
151: private String getType(XMLResourceIdentifier resourceIdentifier) {
152: if (resourceIdentifier instanceof XMLGrammarDescription) {
153: XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
154: if (XMLGrammarDescription.XML_SCHEMA.equals(desc
155: .getGrammarType())) {
156: return XSD_TYPE;
157: }
158: }
159: return XML_TYPE;
160: } // getType(XMLResourceIdentifier):String
161:
162: } // DOMEntityResolverWrapper
|