KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-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
20 import org.apache.xerces.xni.XNIException;
21 import org.apache.xerces.xni.XMLResourceIdentifier;
22 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
23 import org.apache.xerces.xni.parser.XMLEntityResolver;
24 import org.apache.xerces.xni.parser.XMLInputSource;
25
26 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
27 import org.w3c.dom.ls.LSInput JavaDoc;
28
29 import java.io.InputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.StringReader JavaDoc;
33
34
35 /**
36  * This class wraps DOM entity resolver to XNI entity resolver.
37  *
38  * @see LSResourceResolver
39  *
40  * @author Gopal Sharma, SUN MicroSystems Inc.
41  * @author Elena Litani, IBM
42  * @author Ramesh Mandava, Sun Microsystems
43  * @version $Id: DOMEntityResolverWrapper.java,v 1.13 2004/05/27 12:26:56 mrglavas Exp $
44  */

45 public class DOMEntityResolverWrapper
46     implements XMLEntityResolver {
47
48     //
49
// Data
50
//
51

52     /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
53     private static final String JavaDoc XML_TYPE = "http://www.w3.org/TR/REC-xml";
54     
55     /** XML Schema 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 JavaDoc XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
57
58     /** The DOM entity resolver. */
59     protected LSResourceResolver JavaDoc fEntityResolver;
60
61     //
62
// Constructors
63
//
64

65     /** Default constructor. */
66     public DOMEntityResolverWrapper() {}
67
68     /** Wraps the specified DOM entity resolver. */
69     public DOMEntityResolverWrapper(LSResourceResolver JavaDoc entityResolver) {
70         setEntityResolver(entityResolver);
71     } // LSResourceResolver
72

73     //
74
// Public methods
75
//
76

77     /** Sets the DOM entity resolver. */
78     public void setEntityResolver(LSResourceResolver JavaDoc entityResolver) {
79         fEntityResolver = entityResolver;
80     } // setEntityResolver(LSResourceResolver)
81

82     /** Returns the DOM entity resolver. */
83     public LSResourceResolver JavaDoc getEntityResolver() {
84         return fEntityResolver;
85     } // getEntityResolver():LSResourceResolver
86

87     //
88
// XMLEntityResolver methods
89
//
90

91     /**
92      * Resolves an external parsed entity. If the entity cannot be
93      * resolved, this method should return null.
94      *
95      * @param resourceIdentifier description of the resource to be revsoved
96      * @throws XNIException Thrown on general error.
97      * @throws IOException Thrown if resolved entity stream cannot be
98      * opened or some other i/o error occurs.
99      */

100     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
101         throws XNIException, IOException JavaDoc {
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 JavaDoc inputSource =
107                 resourceIdentifier == null
108                     ? fEntityResolver.resolveResource(
109                         null,
110                         null,
111                         null,
112                         null,
113                         null)
114                     : fEntityResolver.resolveResource(
115                         getType(resourceIdentifier),
116                         resourceIdentifier.getNamespace(),
117                         resourceIdentifier.getPublicId(),
118                         resourceIdentifier.getLiteralSystemId(),
119                         resourceIdentifier.getBaseSystemId());
120             if (inputSource != null) {
121                 String JavaDoc publicId = inputSource.getPublicId();
122                 String JavaDoc systemId = inputSource.getSystemId();
123                 String JavaDoc baseSystemId = inputSource.getBaseURI();
124                 InputStream JavaDoc byteStream = inputSource.getByteStream();
125                 Reader JavaDoc charStream = inputSource.getCharacterStream();
126                 String JavaDoc encoding = inputSource.getEncoding();
127                 String JavaDoc data = inputSource.getStringData();
128
129                 /**
130                  * An LSParser looks at inputs specified in LSInput in
131                  * the following order: characterStream, byteStream,
132                  * stringData, systemId, publicId.
133                  */

134                 XMLInputSource xmlInputSource =
135                     new XMLInputSource(publicId, systemId, baseSystemId);
136                 
137                 if (charStream != null) {
138                     xmlInputSource.setCharacterStream(charStream);
139                 }
140                 else if (byteStream != null) {
141                     xmlInputSource.setByteStream((InputStream JavaDoc) byteStream);
142                 }
143                 else if (data != null && data.length() != 0) {
144                     xmlInputSource.setCharacterStream(new StringReader JavaDoc(data));
145                 }
146                 xmlInputSource.setEncoding(encoding);
147                 return xmlInputSource;
148             }
149         }
150
151         // unable to resolve entity
152
return null;
153
154     } // resolveEntity(String,String,String):XMLInputSource
155

156     /** Determines the type of resource being resolved **/
157     private String JavaDoc getType(XMLResourceIdentifier resourceIdentifier) {
158         if (resourceIdentifier instanceof XMLGrammarDescription) {
159             XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
160             if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
161                 return XSD_TYPE;
162             }
163         }
164         return XML_TYPE;
165     } // getType(XMLResourceIdentifier):String
166

167 } // DOMEntityResolverWrapper
168
Popular Tags