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