1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.KeyValuePair;
20  
21  import java.io.InputStream;
22  
23  import org.xml.sax.InputSource;
24  
25  /**
26   * <a href="EntityResolver.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class EntityResolver implements org.xml.sax.EntityResolver {
31  
32      public InputSource resolveEntity(String publicId, String systemId) {
33          ClassLoader classLoader = getClass().getClassLoader();
34  
35          if (_log.isDebugEnabled()) {
36              _log.debug("Resolving entity " + publicId + " " + systemId);
37          }
38  
39          if (publicId != null) {
40              for (int i = 0; i < _PUBLIC_IDS.length; i++) {
41                  KeyValuePair kvp = _PUBLIC_IDS[i];
42  
43                  if (publicId.equals(kvp.getKey())) {
44                      InputStream is = classLoader.getResourceAsStream(
45                          _DEFINITIONS_PATH + kvp.getValue());
46  
47                      if (_log.isDebugEnabled()) {
48                          _log.debug("Entity found for public id " + systemId);
49                      }
50  
51                      return new InputSource(is);
52                  }
53              }
54          }
55          else if (systemId != null) {
56              for (int i = 0; i < _SYSTEM_IDS.length; i++) {
57                  KeyValuePair kvp = _SYSTEM_IDS[i];
58  
59                  if (systemId.equals(kvp.getKey())) {
60                      InputStream is = classLoader.getResourceAsStream(
61                          _DEFINITIONS_PATH + kvp.getValue());
62  
63                      if (_log.isDebugEnabled()) {
64                          _log.debug("Entity found for system id " + systemId);
65                      }
66  
67                      return new InputSource(is);
68                  }
69              }
70          }
71  
72          if (_log.isDebugEnabled()) {
73              _log.debug("No entity found for " + publicId + " " + systemId);
74          }
75  
76          return null;
77      }
78  
79      private static String _DEFINITIONS_PATH = "com/liferay/portal/definitions/";
80  
81      private static KeyValuePair[] _PUBLIC_IDS = {
82          new KeyValuePair(
83              "datatypes",
84              "datatypes.dtd"
85          ),
86  
87          new KeyValuePair(
88              "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN",
89              "facelet-taglib_1_0.dtd"
90          ),
91  
92          new KeyValuePair(
93              "-//Hibernate/Hibernate Mapping DTD 3.0//EN",
94              "hibernate-mapping-3.0.dtd"
95          ),
96  
97          new KeyValuePair(
98              "-//Liferay//DTD Display 2.0.0//EN",
99              "liferay-display_2_0_0.dtd"
100         ),
101 
102         new KeyValuePair(
103             "-//Liferay//DTD Display 3.5.0//EN",
104             "liferay-display_3_5_0.dtd"
105         ),
106 
107         new KeyValuePair(
108             "-//Liferay//DTD Display 4.0.0//EN",
109             "liferay-display_4_0_0.dtd"
110         ),
111 
112         new KeyValuePair(
113             "-//Liferay//DTD Display 5.0.0//EN",
114             "liferay-display_5_0_0.dtd"
115         ),
116 
117         new KeyValuePair(
118             "-//Liferay//DTD Display 5.1.0//EN",
119             "liferay-display_5_1_0.dtd"
120         ),
121 
122         new KeyValuePair(
123             "-//Liferay//DTD Display 5.2.0//EN",
124             "liferay-display_5_2_0.dtd"
125         ),
126 
127         new KeyValuePair(
128             "-//Liferay//DTD Display 6.0.0//EN",
129             "liferay-display_6_0_0.dtd"
130         ),
131 
132         new KeyValuePair(
133             "-//Liferay//DTD Hook 5.1.0//EN",
134             "liferay-hook_5_1_0.dtd"
135         ),
136 
137         new KeyValuePair(
138             "-//Liferay//DTD Hook 5.2.0//EN",
139             "liferay-hook_5_2_0.dtd"
140         ),
141 
142         new KeyValuePair(
143             "-//Liferay//DTD Hook 6.0.0//EN",
144             "liferay-hook_6_0_0.dtd"
145         ),
146 
147         new KeyValuePair(
148             "-//Liferay//DTD Layout Templates 3.6.0//EN",
149             "liferay-layout-templates_3_6_0.dtd"
150         ),
151 
152         new KeyValuePair(
153             "-//Liferay//DTD Layout Templates 4.0.0//EN",
154             "liferay-layout-templates_4_0_0.dtd"
155         ),
156 
157         new KeyValuePair(
158             "-//Liferay//DTD Layout Templates 4.3.0//EN",
159             "liferay-layout-templates_4_3_0.dtd"
160         ),
161 
162         new KeyValuePair(
163             "-//Liferay//DTD Layout Templates 5.0.0//EN",
164             "liferay-layout-templates_5_0_0.dtd"
165         ),
166 
167         new KeyValuePair(
168             "-//Liferay//DTD Layout Templates 5.1.0//EN",
169             "liferay-layout-templates_5_1_0.dtd"
170         ),
171 
172         new KeyValuePair(
173             "-//Liferay//DTD Layout Templates 5.2.0//EN",
174             "liferay-layout-templates_5_2_0.dtd"
175         ),
176 
177         new KeyValuePair(
178             "-//Liferay//DTD Layout Templates 6.0.0//EN",
179             "liferay-layout-templates_6_0_0.dtd"
180         ),
181 
182         new KeyValuePair(
183             "-//Liferay//DTD Look and Feel 3.5.0//EN",
184             "liferay-look-and-feel_3_5_0.dtd"
185         ),
186 
187         new KeyValuePair(
188             "-//Liferay//DTD Look and Feel 4.0.0//EN",
189             "liferay-look-and-feel_4_0_0.dtd"
190         ),
191 
192         new KeyValuePair(
193             "-//Liferay//DTD Look and Feel 4.3.0//EN",
194             "liferay-look-and-feel_4_3_0.dtd"
195         ),
196 
197         new KeyValuePair(
198             "-//Liferay//DTD Look and Feel 5.0.0//EN",
199             "liferay-look-and-feel_5_0_0.dtd"
200         ),
201 
202         new KeyValuePair(
203             "-//Liferay//DTD Look and Feel 5.1.0//EN",
204             "liferay-look-and-feel_5_1_0.dtd"
205         ),
206 
207         new KeyValuePair(
208             "-//Liferay//DTD Look and Feel 5.2.0//EN",
209             "liferay-look-and-feel_5_2_0.dtd"
210         ),
211 
212         new KeyValuePair(
213             "-//Liferay//DTD Look and Feel 6.0.0//EN",
214             "liferay-look-and-feel_6_0_0.dtd"
215         ),
216 
217         new KeyValuePair(
218             "-//Liferay//DTD Plugin Package 4.3.0//EN",
219             "liferay-plugin-package_4_3_0.dtd"
220         ),
221 
222         new KeyValuePair(
223             "-//Liferay//DTD Plugin Package 5.0.0//EN",
224             "liferay-plugin-package_5_0_0.dtd"
225         ),
226 
227         new KeyValuePair(
228             "-//Liferay//DTD Plugin Package 5.1.0//EN",
229             "liferay-plugin-package_5_1_0.dtd"
230         ),
231 
232         new KeyValuePair(
233             "-//Liferay//DTD Plugin Package 5.2.0//EN",
234             "liferay-plugin-package_5_2_0.dtd"
235         ),
236 
237         new KeyValuePair(
238             "-//Liferay//DTD Plugin Package 6.0.0//EN",
239             "liferay-plugin-package_6_0_0.dtd"
240         ),
241 
242         new KeyValuePair(
243             "-//Liferay//DTD Plugin Repository 4.3.0//EN",
244             "liferay-plugin-repository_4_3_0.dtd"
245         ),
246 
247         new KeyValuePair(
248             "-//Liferay//DTD Plugin Repository 5.0.0//EN",
249             "liferay-plugin-repository_5_0_0.dtd"
250         ),
251 
252         new KeyValuePair(
253             "-//Liferay//DTD Plugin Repository 5.1.0//EN",
254             "liferay-plugin-repository_5_1_0.dtd"
255         ),
256 
257         new KeyValuePair(
258             "-//Liferay//DTD Plugin Repository 5.2.0//EN",
259             "liferay-plugin-repository_5_2_0.dtd"
260         ),
261 
262         new KeyValuePair(
263             "-//Liferay//DTD Plugin Repository 6.0.0//EN",
264             "liferay-plugin-repository_6_0_0.dtd"
265         ),
266 
267         new KeyValuePair(
268             "-//Liferay//DTD Portlet Application 3.5.0//EN",
269             "liferay-portlet-app_3_5_0.dtd"
270         ),
271 
272         new KeyValuePair(
273             "-//Liferay//DTD Portlet Application 4.0.0//EN",
274             "liferay-portlet-app_4_0_0.dtd"
275         ),
276 
277         new KeyValuePair(
278             "-//Liferay//DTD Portlet Application 4.1.0//EN",
279             "liferay-portlet-app_4_1_0.dtd"
280         ),
281 
282         new KeyValuePair(
283             "-//Liferay//DTD Portlet Application 4.2.0//EN",
284             "liferay-portlet-app_4_2_0.dtd"
285         ),
286 
287         new KeyValuePair(
288             "-//Liferay//DTD Portlet Application 4.3.0//EN",
289             "liferay-portlet-app_4_3_0.dtd"
290         ),
291 
292         new KeyValuePair(
293             "-//Liferay//DTD Portlet Application 4.3.1//EN",
294             "liferay-portlet-app_4_3_1.dtd"
295         ),
296 
297         new KeyValuePair(
298             "-//Liferay//DTD Portlet Application 4.3.2//EN",
299             "liferay-portlet-app_4_3_2.dtd"
300         ),
301 
302         new KeyValuePair(
303             "-//Liferay//DTD Portlet Application 4.3.3//EN",
304             "liferay-portlet-app_4_3_3.dtd"
305         ),
306 
307         new KeyValuePair(
308             "-//Liferay//DTD Portlet Application 4.3.6//EN",
309             "liferay-portlet-app_4_3_6.dtd"
310         ),
311 
312         new KeyValuePair(
313             "-//Liferay//DTD Portlet Application 4.4.0//EN",
314             "liferay-portlet-app_4_4_0.dtd"
315         ),
316 
317         new KeyValuePair(
318             "-//Liferay//DTD Portlet Application 5.0.0//EN",
319             "liferay-portlet-app_5_0_0.dtd"
320         ),
321 
322         new KeyValuePair(
323             "-//Liferay//DTD Portlet Application 5.1.0//EN",
324             "liferay-portlet-app_5_1_0.dtd"
325         ),
326 
327         new KeyValuePair(
328             "-//Liferay//DTD Portlet Application 5.2.0//EN",
329             "liferay-portlet-app_5_2_0.dtd"
330         ),
331 
332         new KeyValuePair(
333             "-//Liferay//DTD Portlet Application 6.0.0//EN",
334             "liferay-portlet-app_6_0_0.dtd"
335         ),
336 
337         new KeyValuePair(
338             "-//Liferay//DTD Service Builder 3.5.0//EN",
339             "liferay-service-builder_3_5_0.dtd"
340         ),
341 
342         new KeyValuePair(
343             "-//Liferay//DTD Service Builder 3.6.1//EN",
344             "liferay-service-builder_3_6_1.dtd"
345         ),
346 
347         new KeyValuePair(
348             "-//Liferay//DTD Service Builder 4.0.0//EN",
349             "liferay-service-builder_4_0_0.dtd"
350         ),
351 
352         new KeyValuePair(
353             "-//Liferay//DTD Service Builder 4.2.0//EN",
354             "liferay-service-builder_4_2_0.dtd"
355         ),
356 
357         new KeyValuePair(
358             "-//Liferay//DTD Service Builder 4.3.0//EN",
359             "liferay-service-builder_4_3_0.dtd"
360         ),
361 
362         new KeyValuePair(
363             "-//Liferay//DTD Service Builder 4.3.3//EN",
364             "liferay-service-builder_4_3_3.dtd"
365         ),
366 
367         new KeyValuePair(
368             "-//Liferay//DTD Service Builder 4.4.0//EN",
369             "liferay-service-builder_4_4_0.dtd"
370         ),
371 
372         new KeyValuePair(
373             "-//Liferay//DTD Service Builder 5.0.0//EN",
374             "liferay-service-builder_5_0_0.dtd"
375         ),
376 
377         new KeyValuePair(
378             "-//Liferay//DTD Service Builder 5.1.0//EN",
379             "liferay-service-builder_5_1_0.dtd"
380         ),
381 
382         new KeyValuePair(
383             "-//Liferay//DTD Service Builder 5.2.0//EN",
384             "liferay-service-builder_5_2_0.dtd"
385         ),
386 
387         new KeyValuePair(
388             "-//Liferay//DTD Service Builder 6.0.0//EN",
389             "liferay-service-builder_6_0_0.dtd"
390         ),
391 
392         new KeyValuePair(
393             "-//Liferay//DTD Theme Loader 4.3.0//EN",
394             "liferay-theme-loader_4_3_0.dtd"
395         ),
396 
397         new KeyValuePair(
398             "-//Liferay//DTD Theme Loader 5.0.0//EN",
399             "liferay-theme-loader_5_0_0.dtd"
400         ),
401 
402         new KeyValuePair(
403             "-//Liferay//DTD Theme Loader 5.1.0//EN",
404             "liferay-theme-loader_5_1_0.dtd"
405         ),
406 
407         new KeyValuePair(
408             "-//Liferay//DTD Theme Loader 5.2.0//EN",
409             "liferay-theme-loader_5_2_0.dtd"
410         ),
411 
412         new KeyValuePair(
413             "-//Liferay//DTD Theme Loader 6.0.0//EN",
414             "liferay-theme-loader_6_0_0.dtd"
415         ),
416 
417         new KeyValuePair(
418             "-//MuleSource //DTD mule-configuration XML V1.0//EN",
419             "mule-configuration.dtd"
420         ),
421 
422         new KeyValuePair(
423             "-//SPRING//DTD BEAN//EN",
424             "spring-beans.dtd"
425         ),
426 
427         new KeyValuePair(
428             "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
429             "struts-config_1_2.dtd"
430         ),
431 
432         new KeyValuePair(
433             "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
434             "tiles-config_1_1.dtd"
435         ),
436 
437         new KeyValuePair(
438             "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
439             "web-app_2_3.dtd"
440         ),
441 
442         new KeyValuePair(
443             "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
444             "web-facesconfig_1_0.dtd"
445         ),
446 
447         new KeyValuePair(
448             "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
449             "web-facesconfig_1_1.dtd"
450         ),
451 
452         new KeyValuePair(
453             "-//W3C//DTD XMLSCHEMA 200102//EN",
454             "XMLSchema.dtd"
455         )
456     };
457 
458     private static KeyValuePair[] _SYSTEM_IDS = {
459         new KeyValuePair(
460             "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd",
461             "portlet-app_1_0.xsd"
462         ),
463 
464         new KeyValuePair(
465             "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd",
466             "portlet-app_2_0.xsd"
467         ),
468 
469         new KeyValuePair(
470             "http://www.w3.org/2001/xml.xsd",
471             "xml.xsd"
472         )
473     };
474 
475     private static Log _log = LogFactoryUtil.getLog(EntityResolver.class);
476 
477 }