KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > util > DOMEntityResolverWrapper


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.util;
59
60
61 import com.sun.org.apache.xerces.internal.xni.XNIException;
62 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
63 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
64 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
65 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
66
67 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
68 import org.w3c.dom.ls.LSInput JavaDoc;
69
70 import java.io.InputStream JavaDoc;
71 import java.io.IOException JavaDoc;
72 import java.io.Reader JavaDoc;
73 import java.io.StringReader JavaDoc;
74
75
76 /**
77  * This class wraps DOM entity resolver to XNI entity resolver.
78  *
79  * @see LSResourceResolver
80  *
81  * @author Gopal Sharma, SUN MicroSystems Inc.
82  * @author Elena Litani, IBM
83  * @author Ramesh Mandava, Sun Microsystems
84  * @version $Id: DOMEntityResolverWrapper.java,v 1.12 2004/02/25 19:46:15 kohsuke Exp $
85  */

86 public class DOMEntityResolverWrapper
87     implements XMLEntityResolver {
88
89     //
90
// Data
91
//
92

93     /** XML 1.0 type constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
94     private static final String JavaDoc XML_TYPE = "http://www.w3.org/TR/REC-xml";
95     
96     /** XML Schema constant according to DOM L3 LS CR spec "http://www.w3.org/TR/2003/CR-DOM-Level-3-LS-20031107" */
97     private static final String JavaDoc XSD_TYPE = "http://www.w3.org/2001/XMLSchema";
98
99     /** The DOM entity resolver. */
100     protected LSResourceResolver JavaDoc fEntityResolver;
101
102     //
103
// Constructors
104
//
105

106     /** Default constructor. */
107     public DOMEntityResolverWrapper() {}
108
109     /** Wraps the specified DOM entity resolver. */
110     public DOMEntityResolverWrapper(LSResourceResolver JavaDoc entityResolver) {
111         setEntityResolver(entityResolver);
112     } // LSResourceResolver
113

114     //
115
// Public methods
116
//
117

118     /** Sets the DOM entity resolver. */
119     public void setEntityResolver(LSResourceResolver JavaDoc entityResolver) {
120         fEntityResolver = entityResolver;
121     } // setEntityResolver(LSResourceResolver)
122

123     /** Returns the DOM entity resolver. */
124     public LSResourceResolver JavaDoc getEntityResolver() {
125         return fEntityResolver;
126     } // getEntityResolver():LSResourceResolver
127

128     //
129
// XMLEntityResolver methods
130
//
131

132     /**
133      * Resolves an external parsed entity. If the entity cannot be
134      * resolved, this method should return null.
135      *
136      * @param resourceIdentifier description of the resource to be revsoved
137      * @throws XNIException Thrown on general error.
138      * @throws IOException Thrown if resolved entity stream cannot be
139      * opened or some other i/o error occurs.
140      */

141     public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier)
142         throws XNIException, IOException JavaDoc {
143         // resolve entity using DOM entity resolver
144
if (fEntityResolver != null) {
145             // For entity resolution the type of the resource would be XML TYPE
146
// DOM L3 LS spec mention only the XML 1.0 recommendation right now
147
LSInput JavaDoc inputSource =
148                 resourceIdentifier == null
149                     ? fEntityResolver.resolveResource(
150                         null,
151                         null,
152                         null,
153                         null,
154                         null)
155                     : fEntityResolver.resolveResource(
156                         getType(resourceIdentifier),
157                         resourceIdentifier.getNamespace(),
158                         resourceIdentifier.getPublicId(),
159                         resourceIdentifier.getLiteralSystemId(),
160                         resourceIdentifier.getBaseSystemId());
161             if (inputSource != null) {
162                 String JavaDoc publicId = inputSource.getPublicId();
163                 String JavaDoc systemId = inputSource.getSystemId();
164                 String JavaDoc baseSystemId = inputSource.getBaseURI();
165                 InputStream JavaDoc byteStream = inputSource.getByteStream();
166                 Reader JavaDoc charStream = inputSource.getCharacterStream();
167                 String JavaDoc encoding = inputSource.getEncoding();
168                 String JavaDoc data = inputSource.getStringData();
169                 XMLInputSource xmlInputSource =
170                     new XMLInputSource(publicId, systemId, baseSystemId);
171
172                 if (charStream != null) {
173                     xmlInputSource.setCharacterStream(charStream);
174                 }
175                 if (byteStream != null) {
176                     xmlInputSource.setByteStream((InputStream JavaDoc) byteStream);
177                 }
178                 if (data != null && data.length() != 0) {
179                     xmlInputSource.setCharacterStream(
180                         new StringReader JavaDoc(data));
181                 }
182                 xmlInputSource.setEncoding(encoding);
183                 return xmlInputSource;
184             }
185         }
186
187         // unable to resolve entity
188
return null;
189
190     } // resolveEntity(String,String,String):XMLInputSource
191

192     /** Determines the type of resource being resolved **/
193     private String JavaDoc getType(XMLResourceIdentifier resourceIdentifier) {
194         if (resourceIdentifier instanceof XMLGrammarDescription) {
195             XMLGrammarDescription desc = (XMLGrammarDescription) resourceIdentifier;
196             if (XMLGrammarDescription.XML_SCHEMA.equals(desc.getGrammarType())) {
197                 return XSD_TYPE;
198             }
199         }
200         return XML_TYPE;
201     } // getType(XMLResourceIdentifier):String
202

203 } // DOMEntityResolverWrapper
204
Popular Tags