1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  */
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  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  * @LastModified: Nov 2017
48  */
49 public class DOMEntityResolverWrapper
50     implements XMLEntityResolver {
51 
52     //
53     // Data
54     //
55 
56     /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
57     private static final String XML_TYPE = "http://www.w3.org/TR/REC-xml";
58 
59     /** XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
60     private static final String XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
61 
62     /** The DOM entity resolver. */
63     protected LSResourceResolver fEntityResolver;
64 
65     //
66     // Constructors
67     //
68 
69     /** Default constructor. */
DOMEntityResolverWrapper()70     public DOMEntityResolverWrapper() {}
71 
72     /** Wraps the specified DOM entity resolver. */
DOMEntityResolverWrapper(LSResourceResolver entityResolver)73     public DOMEntityResolverWrapper(LSResourceResolver entityResolver) {
74         setEntityResolver(entityResolver);
75     } // LSResourceResolver
76 
77     //
78     // Public methods
79     //
80 
81     /** Sets the DOM entity resolver. */
setEntityResolver(LSResourceResolver entityResolver)82     public void setEntityResolver(LSResourceResolver entityResolver) {
83         fEntityResolver = entityResolver;
84     } // setEntityResolver(LSResourceResolver)
85 
86     /** Returns the DOM entity resolver. */
getEntityResolver()87     public LSResourceResolver getEntityResolver() {
88         return fEntityResolver;
89     } // getEntityResolver():LSResourceResolver
90 
91     //
92     // XMLEntityResolver methods
93     //
94 
95     /**
96      * Resolves an external parsed entity. If the entity cannot be
97      * resolved, this method should return null.
98      *
99      * @param resourceIdentifier        description of the resource to be revsoved
100      * @throws XNIException Thrown on general error.
101      * @throws IOException  Thrown if resolved entity stream cannot be
102      *                      opened or some other i/o error occurs.
103      */
resolveEntity(XMLResourceIdentifier resourceIdentifier)104     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
105         throws XNIException, IOException {
106         // resolve entity using DOM entity resolver
107         if (fEntityResolver != null) {
108             // For entity resolution the type of the resource would be  XML TYPE
109             // DOM L3 LS spec mention only the XML 1.0 recommendation right now
110             LSInput inputSource =
111                 resourceIdentifier == null
112                     ? fEntityResolver.resolveResource(
113                         null,
114                         null,
115                         null,
116                         null,
117                         null)
118                     : fEntityResolver.resolveResource(
119                         getType(resourceIdentifier),
120                         resourceIdentifier.getNamespace(),
121                         resourceIdentifier.getPublicId(),
122                         resourceIdentifier.getLiteralSystemId(),
123                         resourceIdentifier.getBaseSystemId());
124             if (inputSource != null) {
125                 String publicId = inputSource.getPublicId();
126                 String systemId = inputSource.getSystemId();
127                 String baseSystemId = inputSource.getBaseURI();
128                 InputStream byteStream = inputSource.getByteStream();
129                 Reader charStream = inputSource.getCharacterStream();
130                 String encoding = inputSource.getEncoding();
131                 String data = inputSource.getStringData();
132 
133                 /**
134                  * An LSParser looks at inputs specified in LSInput in
135                  * the following order: characterStream, byteStream,
136                  * stringData, systemId, publicId.
137                  */
138                 XMLInputSource xmlInputSource =
139                     new XMLInputSource(publicId, systemId, baseSystemId, true);
140 
141                 if (charStream != null) {
142                     xmlInputSource.setCharacterStream(charStream);
143                 }
144                 else if (byteStream != null) {
145                     xmlInputSource.setByteStream(byteStream);
146                 }
147                 else if (data != null && data.length() != 0) {
148                     xmlInputSource.setCharacterStream(new StringReader(data));
149                 }
150                 xmlInputSource.setEncoding(encoding);
151                 return xmlInputSource;
152             }
153         }
154 
155         // unable to resolve entity
156         return null;
157 
158     } // resolveEntity(String,String,String):XMLInputSource
159 
160     /** Determines the type of resource being resolved **/
getType(XMLResourceIdentifier resourceIdentifier)161     private String getType(XMLResourceIdentifier resourceIdentifier) {
162         if (resourceIdentifier instanceof XMLGrammarDescription) {
163             XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
164             if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
165                 return XSD_TYPE;
166             }
167         }
168         return XML_TYPE;
169     } // getType(XMLResourceIdentifier):String
170 
171 } // DOMEntityResolverWrapper
172