KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > node > SaxParserHandlerBundled


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.node;
25
26 import com.sun.enterprise.deployment.util.DOLUtils;
27 import java.io.InputStream JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31
32 /**
33  *Provides access to schemas and DTDs to Java Web Start-launched app clients
34  *that do not have an app server product installation at hand.
35  *<p>
36  *The DTDs and schemas are assumed to be in the classpath so that
37  *schemas are at /schemas/<schema-name> and DTDs at /dtds/<dtd-name>
38  *
39  * @author tjquinn
40  */

41 public class SaxParserHandlerBundled extends SaxParserHandler {
42
43     /** prefixes for the paths to use for looking up schemas and dtds as resources */
44     private static final String JavaDoc BUNDLED_SCHEMA_ROOT = "/schemas";
45     private static final String JavaDoc BUNDLED_DTD_ROOT = "/dtds";
46     
47     /** Creates a new instance of SaxParserHandlerBundled */
48     public SaxParserHandlerBundled() {
49     }
50     
51     /**
52      *Returns an InputSource for the requested DTD or schema.
53      *<p>
54      *This implementation returns an InputSource that wraps the result
55      *of getResourceAsStream, having
56      *located the requested schema in the classpath.
57      *@param the public ID of the requested entity
58      *@param the system ID of the requested entity
59      *@return InputSource for the requested entity; null if not available
60      *@throws SAXException in case of errors resolving the entity
61      */

62     public InputSource JavaDoc resolveEntity(String JavaDoc publicID, String JavaDoc systemID) throws SAXException JavaDoc {
63         InputSource JavaDoc result = null;
64         
65         /*
66          *This logic was patterned after that in the superclass.
67          */

68         try {
69             if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
70                 DOLUtils.getDefaultLogger().fine("Asked to resolve " + publicID + " system id = " + systemID);
71             }
72             if (publicID==null) {
73                 // unspecified schema
74
if (systemID==null || systemID.lastIndexOf('/')==systemID.length()) {
75                     return null;
76                 }
77                 
78                 /*
79                  *Attempt to open a stream to the requested resource as a schema.
80                  */

81                 InputStream JavaDoc is = openSchemaStream(systemID);
82                 
83                 if (is != null) {
84                     /*
85                      *We found the entity, so wrap an InputSource around the stream.
86                      */

87                     result = new InputSource JavaDoc(is);
88                 } else {
89                     /*
90                      *The entity was not found, so wrap an InputSource around the system ID string instead.
91                      */

92                     result = new InputSource JavaDoc(systemID);
93                 }
94             } else {
95                 /*
96                  *Try to find a DTD for the entity.
97                  */

98                 if (mapping.containsKey(publicID)) {
99                     this.publicID = publicID;
100                     InputStream JavaDoc is = openDTDStream(publicID);
101                     if (is != null) {
102                         result = new InputSource JavaDoc(is);
103                     }
104                 } else if (systemID != null) {
105                     /*
106                      *The DTD lookup failed but a system ID was also specified. Try
107                      *looking up the DTD in the schemas path, because some reside
108                      *there and were not registered in the DTD map by SaxParserHandler.
109                      */

110                     InputStream JavaDoc is = openSchemaStream(systemID);
111                     if (is != null) {
112                         result = new InputSource JavaDoc(is);
113                     }
114                 }
115             }
116         } catch (Exception JavaDoc exc) {
117             exc.printStackTrace();
118             throw new SAXException JavaDoc(exc);
119         }
120         return result;
121     }
122     
123     /**
124      *Returns an InputStream for the schema with the requested system ID.
125      *@param systemID of the schema
126      *@return an InputStream to the selected schema; null if the schema is not available
127      */

128     private InputStream JavaDoc openSchemaStream(String JavaDoc systemID) {
129         String JavaDoc targetID = BUNDLED_SCHEMA_ROOT + "/" + systemID.substring(systemID.lastIndexOf("/") + 1 );
130         InputStream JavaDoc result = this.getClass().getResourceAsStream(targetID);
131         return result;
132     }
133     
134     /**
135      *Returns an InputStream for the DTD with the requested public ID.
136      *@param the publicID of the DTD requested
137      *@return an InputStream to the selected DTD; null if the DTD is not available
138      */

139     private InputStream JavaDoc openDTDStream(String JavaDoc publicID) {
140         String JavaDoc targetID = BUNDLED_DTD_ROOT + "/" + mapping.get(publicID);
141         InputStream JavaDoc result = this.getClass().getResourceAsStream(targetID);
142         return result;
143     }
144 }
145
Popular Tags