KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > LockhartEntityResolver


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.tools.admingui;
25
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28
29 import java.io.Serializable JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.URLDecoder JavaDoc;
32
33 import com.iplanet.jato.util.NonSyncStringBuffer;
34 import com.sun.web.ui.common.CCSystem;
35
36
37 /**
38  * This class implements the default entity resolver for common components.
39  *
40  * This entity resolver class specifically handles external references
41  * in the XML files used by the common component internal servlets.
42  * Typically, these are system identifier references to the DTD files
43  * for the component XML definitions. The class handles system identifier
44  * references with the following design patterns in the DOCTYPE element:
45  * <.p>
46  * "com_sun_web_ui/dtd/name.dtd"
47  * <.bk>
48  * "/com_sun_web_ui/dtd/name.dtd"
49  * <.bk>
50  * "tags/dtd/name.dtd"
51  * <.bk>
52  * "/tags/dtd/name.dtd"
53  * <.bk>
54  * "app_name/dtd/name.dtd"
55  * <.bk>
56  * "/app_name/dtd/name.dtd"
57  * <.p>
58  * Where "app_name" is the context path name of a web application and
59  * "name.dtd" is a DTD file name.
60  * <.p>
61  * Note that some parsers will attempt to resolve a reference
62  * of this form (without a scheme prefix) as a file system relative
63  * path name, so will add the "file://" scheme prefix to the reference.
64  * If the system identifier reference does not begin with a slash
65  * character, the parser may also prepend the directory specified in
66  * the System property "user.dir". If either prefix is encountered,
67  * this class will remove the prefix and prepend the "http://host:port/"
68  * to the reference, where "host" is the virtual server host name and
69  * "port" is the console non-secure port number.
70  *
71  * @version 1.10 05/10/04
72  * @author Sun Microsystems, Inc.
73  */

74 public class LockhartEntityResolver implements EntityResolver JavaDoc, Serializable JavaDoc {
75
76     // Define constants
77
private static final String JavaDoc COM_SUN_WEB_UI = "com_sun_web_ui";
78     private static final String JavaDoc DTD_DIR = "dtd";
79     private static final String JavaDoc DEFAULT_XML_ENCODING = "UTF-8";
80
81     // Set XML file encoding to its expected default. Really should get
82
// this value from the parser, who reads it from the XML file itself.
83
// For now, we set it to the recommended (by W3C and Lockhart) encoding.
84
private static String JavaDoc xmlEncoding = DEFAULT_XML_ENCODING;
85
86     /**
87      * Default constructor
88      * Set the port number by reading the appropriate System property.
89      * Set the host name from the requset object made available by
90      * the Jato RequestManager.
91      */

92
93     /**
94      * Resolve an external entity. If external entity cannot be resolved
95      * return null, so that the parser will use the system identifier
96      * provided in the XML document. This method is being overriden to
97      * supply the tag DTDs from the com_sun_web_ui web application that
98      * will be part of the 2.1 installation. The DTDs will be accessed
99      * thru the non secure console port similar to help. For now the port
100      * is being set as a System parameter by the start up script.
101      *
102      * @param publicId The public identifier of the external reference
103      * @param systemId The system identifier of the external reference
104      *
105      * @return An InputSource object encapsulating the fixed reference
106      */

107
108     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) {
109
110     // Fix the system identifier for an XML document.
111
// Remove any "file://" prefix from the system identifier
112
// and optionally the working directory for the JVM.
113
// Some parsers prefix "file://<working_directory>" to the
114
// system identifier in a misguided attempt to use file I/O
115
// to find the dtd file. Remove the working directory path.
116
// Handle a DOCTYPE tag with the following SYSTEM value syntax
117
// after removing the file prefix and working directory path:
118
//
119
// "filename.dtd" (dynamically generated files)
120
// "/tags/dtd/filename.dtd" (old 1.0 web apps)
121
// "tags/dtd/filename.dtd"
122
// "/com_sun_web_ui/dtd/filename.dtd" (2.0 and later web apps)
123
// "com_sun_web_ui/dtd/filename.dtd"
124
// "/appname/dtd/filename.dtd" (be nice to web apps)
125
// "appname/dtd/filename.dtd"
126
//
127
// Resulting system identifier will be one of:
128
//
129
// "http://hostname:unsecure_port/resource_context/dtd/filename.dtd"
130
// "http://hostname:unsecure_port/appname/dtd/filename.dtd"
131
//
132
// If we do not match our design pattern or cannot form the
133
// http prefix, just return null to signal the parser that
134
// it is on its own. Typically, this will result in the
135
// parser throwing an exception.
136

137     String JavaDoc fileName = getSystemId(systemId);
138     if (fileName == null) {
139         return (null);
140     }
141
142     InputSource JavaDoc isrc = null;
143     try {
144             isrc = buildInputSource(fileName);
145     } catch (Exception JavaDoc e) {
146             System.out.println("cannot convert DTD, using default behaviour: "
147         + e.getMessage());
148         isrc = null;
149     }
150     return (isrc);
151
152     } // resolveEntity
153

154     // ==================================================================
155
//
156
// Protected methods
157
//
158
// ==================================================================
159

160     /**
161      * Process the system identifier. If a "file:" URL, removes the
162      * scheme prefix. If a fully qualified path name was specified that
163      * begins with the server process current working directory, that
164      * portion of the path is removed (some parsers arbitrarily prepend
165      * the current working directory to a relative URI system Id in hopes
166      * of finding the external reference via file I/O). All path separators
167      * are converted to forward slashes, if necessary. If not a file
168      * based URL and not a relative URI, a null reference is returned.
169      *
170      * @param systemId The unprocessed system Id from the parser
171      *
172      * @return The processed system Id string
173      */

174     protected String JavaDoc getSystemId(String JavaDoc systemId) {
175
176     if (systemId == null) {
177         return (null);
178     }
179         //System.out.println("entity resolver systemid: " + systemId);
180
String JavaDoc path;
181     try {
182         path = URLDecoder.decode(systemId, xmlEncoding);
183     } catch (Exception JavaDoc ex) {
184         // Bad encoding scheme, try raw system Id
185
path = systemId;
186     }
187
188     int index = path.lastIndexOf('/');
189     path = path.substring(index+1);
190     return (path);
191
192     } // getSystemId
193

194     // ==================================================================
195
//
196
// Private methods
197
//
198
// ==================================================================
199

200     // Build the new sytem identifier. Generate a URL using the HTTP
201
// scheme and unsecure port. Encode the URL string to handle spaces.
202

203     private InputSource JavaDoc buildInputSource(String JavaDoc fileName) {
204
205     String JavaDoc str;
206     NonSyncStringBuffer buffer =
207         new NonSyncStringBuffer(128);
208     buffer.append(COM_SUN_WEB_UI);
209     buffer.append(CCSystem.URL_SEPARATOR + DTD_DIR);
210     buffer.append(CCSystem.URL_SEPARATOR + fileName);
211
212     String JavaDoc url = buffer.toString();
213     InputStream JavaDoc is = getClass().getClassLoader().getResourceAsStream(url);
214     return (new InputSource JavaDoc(is));
215
216     } // buildInputSource
217

218 } // CCDefaultEntityResolver
219
Popular Tags