1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.kernel.util.KeyValuePair;
26  
27  import java.io.InputStream;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  import org.xml.sax.InputSource;
33  
34  /**
35   * <a href="EntityResolver.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class EntityResolver implements org.xml.sax.EntityResolver {
41  
42      public InputSource resolveEntity(String publicId, String systemId) {
43          ClassLoader classLoader = getClass().getClassLoader();
44  
45          if (_log.isDebugEnabled()) {
46              _log.debug("Resolving entity " + publicId + " " + systemId);
47          }
48  
49          if (publicId != null) {
50              for (int i = 0; i < _PUBLIC_IDS.length; i++) {
51                  KeyValuePair kvp = _PUBLIC_IDS[i];
52  
53                  if (publicId.equals(kvp.getKey())) {
54                      InputStream is = classLoader.getResourceAsStream(
55                          _DEFINITIONS_PATH + kvp.getValue());
56  
57                      if (_log.isDebugEnabled()) {
58                          _log.debug("Entity found for public id " + systemId);
59                      }
60  
61                      return new InputSource(is);
62                  }
63              }
64          }
65          else if (systemId != null) {
66              for (int i = 0; i < _SYSTEM_IDS.length; i++) {
67                  KeyValuePair kvp = _SYSTEM_IDS[i];
68  
69                  if (systemId.equals(kvp.getKey())) {
70                      InputStream is = classLoader.getResourceAsStream(
71                          _DEFINITIONS_PATH + kvp.getValue());
72  
73                      if (_log.isDebugEnabled()) {
74                          _log.debug("Entity found for system id " + systemId);
75                      }
76  
77                      return new InputSource(is);
78                  }
79              }
80          }
81  
82          if (_log.isDebugEnabled()) {
83              _log.debug("No entity found for " + publicId + " " + systemId);
84          }
85  
86          return null;
87      }
88  
89      private static String _DEFINITIONS_PATH = "com/liferay/portal/definitions/";
90  
91      private static KeyValuePair[] _PUBLIC_IDS = {
92          new KeyValuePair(
93              "datatypes",
94              "datatypes.dtd"
95          ),
96  
97          new KeyValuePair(
98              "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN",
99              "facelet-taglib_1_0.dtd"
100         ),
101 
102         new KeyValuePair(
103             "-//Liferay//DTD Display 2.0.0//EN",
104             "liferay-display_2_0_0.dtd"
105         ),
106 
107         new KeyValuePair(
108             "-//Liferay//DTD Display 3.5.0//EN",
109             "liferay-display_3_5_0.dtd"
110         ),
111 
112         new KeyValuePair(
113             "-//Liferay//DTD Display 4.0.0//EN",
114             "liferay-display_4_0_0.dtd"
115         ),
116 
117         new KeyValuePair(
118             "-//Liferay//DTD Display 5.0.0//EN",
119             "liferay-display_5_0_0.dtd"
120         ),
121 
122         new KeyValuePair(
123             "-//Liferay//DTD Display 5.1.0//EN",
124             "liferay-display_5_1_0.dtd"
125         ),
126 
127         new KeyValuePair(
128             "-//Liferay//DTD Hook 5.1.0//EN",
129             "liferay-hook_5_1_0.dtd"
130         ),
131 
132         new KeyValuePair(
133             "-//Liferay//DTD Layout Templates 3.6.0//EN",
134             "liferay-layout-templates_3_6_0.dtd"
135         ),
136 
137         new KeyValuePair(
138             "-//Liferay//DTD Layout Templates 4.0.0//EN",
139             "liferay-layout-templates_4_0_0.dtd"
140         ),
141 
142         new KeyValuePair(
143             "-//Liferay//DTD Layout Templates 4.3.0//EN",
144             "liferay-layout-templates_4_3_0.dtd"
145         ),
146 
147         new KeyValuePair(
148             "-//Liferay//DTD Layout Templates 5.0.0//EN",
149             "liferay-layout-templates_5_0_0.dtd"
150         ),
151 
152         new KeyValuePair(
153             "-//Liferay//DTD Layout Templates 5.1.0//EN",
154             "liferay-layout-templates_5_1_0.dtd"
155         ),
156 
157         new KeyValuePair(
158             "-//Liferay//DTD Look and Feel 3.5.0//EN",
159             "liferay-look-and-feel_3_5_0.dtd"
160         ),
161 
162         new KeyValuePair(
163             "-//Liferay//DTD Look and Feel 4.0.0//EN",
164             "liferay-look-and-feel_4_0_0.dtd"
165         ),
166 
167         new KeyValuePair(
168             "-//Liferay//DTD Look and Feel 4.3.0//EN",
169             "liferay-look-and-feel_4_3_0.dtd"
170         ),
171 
172         new KeyValuePair(
173             "-//Liferay//DTD Look and Feel 5.0.0//EN",
174             "liferay-look-and-feel_5_0_0.dtd"
175         ),
176 
177         new KeyValuePair(
178             "-//Liferay//DTD Look and Feel 5.1.0//EN",
179             "liferay-look-and-feel_5_1_0.dtd"
180         ),
181 
182         new KeyValuePair(
183             "-//Liferay//DTD Plugin Package 4.3.0//EN",
184             "liferay-plugin-package_4_3_0.dtd"
185         ),
186 
187         new KeyValuePair(
188             "-//Liferay//DTD Plugin Package 5.0.0//EN",
189             "liferay-plugin-package_5_0_0.dtd"
190         ),
191 
192         new KeyValuePair(
193             "-//Liferay//DTD Plugin Package 5.1.0//EN",
194             "liferay-plugin-package_5_1_0.dtd"
195         ),
196 
197         new KeyValuePair(
198             "-//Liferay//DTD Plugin Repository 4.3.0//EN",
199             "liferay-plugin-repository_4_3_0.dtd"
200         ),
201 
202         new KeyValuePair(
203             "-//Liferay//DTD Plugin Repository 5.0.0//EN",
204             "liferay-plugin-repository_5_0_0.dtd"
205         ),
206 
207         new KeyValuePair(
208             "-//Liferay//DTD Plugin Repository 5.1.0//EN",
209             "liferay-plugin-repository_5_1_0.dtd"
210         ),
211 
212         new KeyValuePair(
213             "-//Liferay//DTD Portlet Application 3.5.0//EN",
214             "liferay-portlet-app_3_5_0.dtd"
215         ),
216 
217         new KeyValuePair(
218             "-//Liferay//DTD Portlet Application 4.0.0//EN",
219             "liferay-portlet-app_4_0_0.dtd"
220         ),
221 
222         new KeyValuePair(
223             "-//Liferay//DTD Portlet Application 4.1.0//EN",
224             "liferay-portlet-app_4_1_0.dtd"
225         ),
226 
227         new KeyValuePair(
228             "-//Liferay//DTD Portlet Application 4.2.0//EN",
229             "liferay-portlet-app_4_2_0.dtd"
230         ),
231 
232         new KeyValuePair(
233             "-//Liferay//DTD Portlet Application 4.3.0//EN",
234             "liferay-portlet-app_4_3_0.dtd"
235         ),
236 
237         new KeyValuePair(
238             "-//Liferay//DTD Portlet Application 4.3.1//EN",
239             "liferay-portlet-app_4_3_1.dtd"
240         ),
241 
242         new KeyValuePair(
243             "-//Liferay//DTD Portlet Application 4.3.2//EN",
244             "liferay-portlet-app_4_3_2.dtd"
245         ),
246 
247         new KeyValuePair(
248             "-//Liferay//DTD Portlet Application 4.3.3//EN",
249             "liferay-portlet-app_4_3_3.dtd"
250         ),
251 
252         new KeyValuePair(
253             "-//Liferay//DTD Portlet Application 4.3.6//EN",
254             "liferay-portlet-app_4_3_6.dtd"
255         ),
256 
257         new KeyValuePair(
258             "-//Liferay//DTD Portlet Application 4.4.0//EN",
259             "liferay-portlet-app_4_4_0.dtd"
260         ),
261 
262         new KeyValuePair(
263             "-//Liferay//DTD Portlet Application 5.0.0//EN",
264             "liferay-portlet-app_5_0_0.dtd"
265         ),
266 
267         new KeyValuePair(
268             "-//Liferay//DTD Portlet Application 5.1.0//EN",
269             "liferay-portlet-app_5_1_0.dtd"
270         ),
271 
272         new KeyValuePair(
273             "-//Liferay//DTD Service Builder 3.5.0//EN",
274             "liferay-service-builder_3_5_0.dtd"
275         ),
276 
277         new KeyValuePair(
278             "-//Liferay//DTD Service Builder 3.6.1//EN",
279             "liferay-service-builder_3_6_1.dtd"
280         ),
281 
282         new KeyValuePair(
283             "-//Liferay//DTD Service Builder 4.0.0//EN",
284             "liferay-service-builder_4_0_0.dtd"
285         ),
286 
287         new KeyValuePair(
288             "-//Liferay//DTD Service Builder 4.2.0//EN",
289             "liferay-service-builder_4_2_0.dtd"
290         ),
291 
292         new KeyValuePair(
293             "-//Liferay//DTD Service Builder 4.3.0//EN",
294             "liferay-service-builder_4_3_0.dtd"
295         ),
296 
297         new KeyValuePair(
298             "-//Liferay//DTD Service Builder 4.3.3//EN",
299             "liferay-service-builder_4_3_3.dtd"
300         ),
301 
302         new KeyValuePair(
303             "-//Liferay//DTD Service Builder 4.4.0//EN",
304             "liferay-service-builder_4_4_0.dtd"
305         ),
306 
307         new KeyValuePair(
308             "-//Liferay//DTD Service Builder 5.0.0//EN",
309             "liferay-service-builder_5_0_0.dtd"
310         ),
311 
312         new KeyValuePair(
313             "-//Liferay//DTD Service Builder 5.1.0//EN",
314             "liferay-service-builder_5_1_0.dtd"
315         ),
316 
317         new KeyValuePair(
318             "-//Liferay//DTD Theme Loader 4.3.0//EN",
319             "liferay-theme-loader_4_3_0.dtd"
320         ),
321 
322         new KeyValuePair(
323             "-//Liferay//DTD Theme Loader 5.0.0//EN",
324             "liferay-theme-loader_5_0_0.dtd"
325         ),
326 
327         new KeyValuePair(
328             "-//Liferay//DTD Theme Loader 5.1.0//EN",
329             "liferay-theme-loader_5_1_0.dtd"
330         ),
331 
332         new KeyValuePair(
333             "-//MuleSource //DTD mule-configuration XML V1.0//EN",
334             "mule-configuration.dtd"
335         ),
336 
337         new KeyValuePair(
338             "-//SPRING//DTD BEAN//EN",
339             "spring-beans.dtd"
340         ),
341 
342         new KeyValuePair(
343             "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
344             "struts-config_1_2.dtd"
345         ),
346 
347         new KeyValuePair(
348             "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
349             "tiles-config_1_1.dtd"
350         ),
351 
352         new KeyValuePair(
353             "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
354             "web-app_2_3.dtd"
355         ),
356 
357         new KeyValuePair(
358             "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
359             "web-facesconfig_1_0.dtd"
360         ),
361 
362         new KeyValuePair(
363             "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
364             "web-facesconfig_1_1.dtd"
365         ),
366 
367         new KeyValuePair(
368             "-//W3C//DTD XMLSCHEMA 200102//EN",
369             "XMLSchema.dtd"
370         )
371     };
372 
373     private static KeyValuePair[] _SYSTEM_IDS = {
374         new KeyValuePair(
375             "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd",
376             "portlet-app_1_0.xsd"
377         ),
378 
379         new KeyValuePair(
380             "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd",
381             "portlet-app_2_0.xsd"
382         ),
383 
384         new KeyValuePair(
385             "http://www.w3.org/2001/xml.xsd",
386             "xml.xsd"
387         )
388     };
389 
390     private static Log _log = LogFactory.getLog(EntityResolver.class);
391 
392 }