1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 2001-2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 package com.sun.org.apache.xerces.internal.util;
22 
23 
24 import com.sun.org.apache.xerces.internal.xni.XNIException;
25 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
26 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
27 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
28 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
29 
30 import org.w3c.dom.ls.LSResourceResolver;
31 import org.w3c.dom.ls.LSInput;
32 
33 import java.io.InputStream;
34 import java.io.IOException;
35 import java.io.Reader;
36 import java.io.StringReader;
37 
38 
39 /**
40  * This class wraps DOM entity resolver to XNI entity resolver.
41  *
42  * @see LSResourceResolver
43  *
44  * @author Gopal Sharma, SUN MicroSystems Inc.
45  * @author Elena Litani, IBM
46  * @author Ramesh Mandava, Sun Microsystems
47  */
48 public class DOMEntityResolverWrapper
49     implements XMLEntityResolver {
50 
51     //
52     // Data
53     //
54 
55     /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
56     private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml";
57 
58     /** XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
59     private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
60 
61     /** The DOM entity resolver. */
62     protected LSResourceResolver fEntityResolver;
63 
64     //
65     // Constructors
66     //
67 
68     /** Default constructor. */
DOMEntityResolverWrapper()69     public DOMEntityResolverWrapper() {}
70 
71     /** Wraps the specified DOM entity resolver. */
DOMEntityResolverWrapper(LSResourceResolver entityResolver)72     public DOMEntityResolverWrapper(LSResourceResolver entityResolver) {
73         setEntityResolver(entityResolver);
74     } // LSResourceResolver
75 
76     //
77     // Public methods
78     //
79 
80     /** Sets the DOM entity resolver. */
setEntityResolver(LSResourceResolver entityResolver)81     public void setEntityResolver(LSResourceResolver entityResolver) {
82         fEntityResolver = entityResolver;
83     } // setEntityResolver(LSResourceResolver)
84 
85     /** Returns the DOM entity resolver. */
getEntityResolver()86     public LSResourceResolver getEntityResolver() {
87         return fEntityResolver;
88     } // getEntityResolver():LSResourceResolver
89 
90     //
91     // XMLEntityResolver methods
92     //
93 
94     /**
95      * Resolves an external parsed entity. If the entity cannot be
96      * resolved, this method should return null.
97      *
98      * @param resourceIdentifier        description of the resource to be revsoved
99      * @throws XNIException Thrown on general error.
100      * @throws IOException  Thrown if resolved entity stream cannot be
101      *                      opened or some other i/o error occurs.
102      */
resolveEntity(XMLResourceIdentifier resourceIdentifier)103     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
104         throws XNIException, IOException {
105         // resolve entity using DOM entity resolver
106         if (fEntityResolver != null) {
107             // For entity resolution the type of the resource would be  XML TYPE
108             // DOM L3 LS spec mention only the XML 1.0 recommendation right now
109             LSInput inputSource =
110                 resourceIdentifier == null
111                     ? fEntityResolver.resolveResource(
112                         null,
113                         null,
114                         null,
115                         null,
116                         null)
117                     : fEntityResolver.resolveResource(
118                         getType(resourceIdentifier),
119                         resourceIdentifier.getNamespace(),
120                         resourceIdentifier.getPublicId(),
121                         resourceIdentifier.getLiteralSystemId(),
122                         resourceIdentifier.getBaseSystemId());
123             if (inputSource != null) {
124                 String publicId = inputSource.getPublicId();
125                 String systemId = inputSource.getSystemId();
126                 String baseSystemId = inputSource.getBaseURI();
127                 InputStream byteStream = inputSource.getByteStream();
128                 Reader charStream = inputSource.getCharacterStream();
129                 String encoding = inputSource.getEncoding();
130                 String data = inputSource.getStringData();
131 
132                 /**
133                  * An LSParser looks at inputs specified in LSInput in
134                  * the following order: characterStream, byteStream,
135                  * stringData, systemId, publicId.
136                  */
137                 XMLInputSource xmlInputSource =
138                     new XMLInputSource(publicId, systemId, baseSystemId);
139 
140                 if (charStream != null) {
141                     xmlInputSource.setCharacterStream(charStream);
142                 }
143                 else if (byteStream != null) {
144                     xmlInputSource.setByteStream((InputStream) byteStream);
145                 }
146                 else if (data != null && data.length() != 0) {
147                     xmlInputSource.setCharacterStream(new StringReader(data));
148                 }
149                 xmlInputSource.setEncoding(encoding);
150                 return xmlInputSource;
151             }
152         }
153 
154         // unable to resolve entity
155         return null;
156 
157     } // resolveEntity(String,String,String):XMLInputSource
158 
159     /** Determines the type of resource being resolved **/
getType(XMLResourceIdentifier resourceIdentifier)160     private String getType(XMLResourceIdentifier resourceIdentifier) {
161         if (resourceIdentifier instanceof XMLGrammarDescription) {
162             XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
163             if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
164                 return XSD_TYPE;
165             }
166         }
167         return XML_TYPE;
168     } // getType(XMLResourceIdentifier):String
169 
170 } // DOMEntityResolverWrapper
171