001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.KeyValuePair;
020    
021    import java.io.InputStream;
022    
023    import org.xml.sax.InputSource;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class EntityResolver implements org.xml.sax.EntityResolver {
029    
030            public InputSource resolveEntity(String publicId, String systemId) {
031                    ClassLoader classLoader = getClass().getClassLoader();
032    
033                    if (_log.isDebugEnabled()) {
034                            _log.debug("Resolving entity " + publicId + " " + systemId);
035                    }
036    
037                    if (publicId != null) {
038                            for (int i = 0; i < _PUBLIC_IDS.length; i++) {
039                                    KeyValuePair kvp = _PUBLIC_IDS[i];
040    
041                                    if (publicId.equals(kvp.getKey())) {
042                                            InputStream is = classLoader.getResourceAsStream(
043                                                    _DEFINITIONS_PATH + kvp.getValue());
044    
045                                            if (_log.isDebugEnabled()) {
046                                                    _log.debug("Entity found for public id " + systemId);
047                                            }
048    
049                                            return new InputSource(is);
050                                    }
051                            }
052                    }
053                    else if (systemId != null) {
054                            for (int i = 0; i < _SYSTEM_IDS.length; i++) {
055                                    KeyValuePair kvp = _SYSTEM_IDS[i];
056    
057                                    if (systemId.equals(kvp.getKey())) {
058                                            InputStream is = classLoader.getResourceAsStream(
059                                                    _DEFINITIONS_PATH + kvp.getValue());
060    
061                                            if (_log.isDebugEnabled()) {
062                                                    _log.debug("Entity found for system id " + systemId);
063                                            }
064    
065                                            return new InputSource(is);
066                                    }
067                            }
068                    }
069    
070                    if (_log.isDebugEnabled()) {
071                            _log.debug("No entity found for " + publicId + " " + systemId);
072                    }
073    
074                    return null;
075            }
076    
077            private static String _DEFINITIONS_PATH = "com/liferay/portal/definitions/";
078    
079            private static KeyValuePair[] _PUBLIC_IDS = {
080                    new KeyValuePair(
081                            "datatypes",
082                            "datatypes.dtd"
083                    ),
084    
085                    new KeyValuePair(
086                            "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN",
087                            "facelet-taglib_1_0.dtd"
088                    ),
089    
090                    new KeyValuePair(
091                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN",
092                            "hibernate-mapping-3.0.dtd"
093                    ),
094    
095                    new KeyValuePair(
096                            "-//Liferay//DTD Display 2.0.0//EN",
097                            "liferay-display_2_0_0.dtd"
098                    ),
099    
100                    new KeyValuePair(
101                            "-//Liferay//DTD Display 3.5.0//EN",
102                            "liferay-display_3_5_0.dtd"
103                    ),
104    
105                    new KeyValuePair(
106                            "-//Liferay//DTD Display 4.0.0//EN",
107                            "liferay-display_4_0_0.dtd"
108                    ),
109    
110                    new KeyValuePair(
111                            "-//Liferay//DTD Display 5.0.0//EN",
112                            "liferay-display_5_0_0.dtd"
113                    ),
114    
115                    new KeyValuePair(
116                            "-//Liferay//DTD Display 5.1.0//EN",
117                            "liferay-display_5_1_0.dtd"
118                    ),
119    
120                    new KeyValuePair(
121                            "-//Liferay//DTD Display 5.2.0//EN",
122                            "liferay-display_5_2_0.dtd"
123                    ),
124    
125                    new KeyValuePair(
126                            "-//Liferay//DTD Display 6.0.0//EN",
127                            "liferay-display_6_0_0.dtd"
128                    ),
129    
130                    new KeyValuePair(
131                            "-//Liferay//DTD Friendly URL Routes 6.0.0//EN",
132                            "liferay-friendly-url-routes_6_0_0.dtd"
133                    ),
134    
135                    new KeyValuePair(
136                            "-//Liferay//DTD Hook 5.1.0//EN",
137                            "liferay-hook_5_1_0.dtd"
138                    ),
139    
140                    new KeyValuePair(
141                            "-//Liferay//DTD Hook 5.2.0//EN",
142                            "liferay-hook_5_2_0.dtd"
143                    ),
144    
145                    new KeyValuePair(
146                            "-//Liferay//DTD Hook 6.0.0//EN",
147                            "liferay-hook_6_0_0.dtd"
148                    ),
149    
150                    new KeyValuePair(
151                            "-//Liferay//DTD Layout Templates 3.6.0//EN",
152                            "liferay-layout-templates_3_6_0.dtd"
153                    ),
154    
155                    new KeyValuePair(
156                            "-//Liferay//DTD Layout Templates 4.0.0//EN",
157                            "liferay-layout-templates_4_0_0.dtd"
158                    ),
159    
160                    new KeyValuePair(
161                            "-//Liferay//DTD Layout Templates 4.3.0//EN",
162                            "liferay-layout-templates_4_3_0.dtd"
163                    ),
164    
165                    new KeyValuePair(
166                            "-//Liferay//DTD Layout Templates 5.0.0//EN",
167                            "liferay-layout-templates_5_0_0.dtd"
168                    ),
169    
170                    new KeyValuePair(
171                            "-//Liferay//DTD Layout Templates 5.1.0//EN",
172                            "liferay-layout-templates_5_1_0.dtd"
173                    ),
174    
175                    new KeyValuePair(
176                            "-//Liferay//DTD Layout Templates 5.2.0//EN",
177                            "liferay-layout-templates_5_2_0.dtd"
178                    ),
179    
180                    new KeyValuePair(
181                            "-//Liferay//DTD Layout Templates 6.0.0//EN",
182                            "liferay-layout-templates_6_0_0.dtd"
183                    ),
184    
185                    new KeyValuePair(
186                            "-//Liferay//DTD Look and Feel 3.5.0//EN",
187                            "liferay-look-and-feel_3_5_0.dtd"
188                    ),
189    
190                    new KeyValuePair(
191                            "-//Liferay//DTD Look and Feel 4.0.0//EN",
192                            "liferay-look-and-feel_4_0_0.dtd"
193                    ),
194    
195                    new KeyValuePair(
196                            "-//Liferay//DTD Look and Feel 4.3.0//EN",
197                            "liferay-look-and-feel_4_3_0.dtd"
198                    ),
199    
200                    new KeyValuePair(
201                            "-//Liferay//DTD Look and Feel 5.0.0//EN",
202                            "liferay-look-and-feel_5_0_0.dtd"
203                    ),
204    
205                    new KeyValuePair(
206                            "-//Liferay//DTD Look and Feel 5.1.0//EN",
207                            "liferay-look-and-feel_5_1_0.dtd"
208                    ),
209    
210                    new KeyValuePair(
211                            "-//Liferay//DTD Look and Feel 5.2.0//EN",
212                            "liferay-look-and-feel_5_2_0.dtd"
213                    ),
214    
215                    new KeyValuePair(
216                            "-//Liferay//DTD Look and Feel 6.0.0//EN",
217                            "liferay-look-and-feel_6_0_0.dtd"
218                    ),
219    
220                    new KeyValuePair(
221                            "-//Liferay//DTD Plugin Package 4.3.0//EN",
222                            "liferay-plugin-package_4_3_0.dtd"
223                    ),
224    
225                    new KeyValuePair(
226                            "-//Liferay//DTD Plugin Package 5.0.0//EN",
227                            "liferay-plugin-package_5_0_0.dtd"
228                    ),
229    
230                    new KeyValuePair(
231                            "-//Liferay//DTD Plugin Package 5.1.0//EN",
232                            "liferay-plugin-package_5_1_0.dtd"
233                    ),
234    
235                    new KeyValuePair(
236                            "-//Liferay//DTD Plugin Package 5.2.0//EN",
237                            "liferay-plugin-package_5_2_0.dtd"
238                    ),
239    
240                    new KeyValuePair(
241                            "-//Liferay//DTD Plugin Package 6.0.0//EN",
242                            "liferay-plugin-package_6_0_0.dtd"
243                    ),
244    
245                    new KeyValuePair(
246                            "-//Liferay//DTD Plugin Repository 4.3.0//EN",
247                            "liferay-plugin-repository_4_3_0.dtd"
248                    ),
249    
250                    new KeyValuePair(
251                            "-//Liferay//DTD Plugin Repository 5.0.0//EN",
252                            "liferay-plugin-repository_5_0_0.dtd"
253                    ),
254    
255                    new KeyValuePair(
256                            "-//Liferay//DTD Plugin Repository 5.1.0//EN",
257                            "liferay-plugin-repository_5_1_0.dtd"
258                    ),
259    
260                    new KeyValuePair(
261                            "-//Liferay//DTD Plugin Repository 5.2.0//EN",
262                            "liferay-plugin-repository_5_2_0.dtd"
263                    ),
264    
265                    new KeyValuePair(
266                            "-//Liferay//DTD Plugin Repository 6.0.0//EN",
267                            "liferay-plugin-repository_6_0_0.dtd"
268                    ),
269    
270                    new KeyValuePair(
271                            "-//Liferay//DTD Portlet Application 3.5.0//EN",
272                            "liferay-portlet-app_3_5_0.dtd"
273                    ),
274    
275                    new KeyValuePair(
276                            "-//Liferay//DTD Portlet Application 4.0.0//EN",
277                            "liferay-portlet-app_4_0_0.dtd"
278                    ),
279    
280                    new KeyValuePair(
281                            "-//Liferay//DTD Portlet Application 4.1.0//EN",
282                            "liferay-portlet-app_4_1_0.dtd"
283                    ),
284    
285                    new KeyValuePair(
286                            "-//Liferay//DTD Portlet Application 4.2.0//EN",
287                            "liferay-portlet-app_4_2_0.dtd"
288                    ),
289    
290                    new KeyValuePair(
291                            "-//Liferay//DTD Portlet Application 4.3.0//EN",
292                            "liferay-portlet-app_4_3_0.dtd"
293                    ),
294    
295                    new KeyValuePair(
296                            "-//Liferay//DTD Portlet Application 4.3.1//EN",
297                            "liferay-portlet-app_4_3_1.dtd"
298                    ),
299    
300                    new KeyValuePair(
301                            "-//Liferay//DTD Portlet Application 4.3.2//EN",
302                            "liferay-portlet-app_4_3_2.dtd"
303                    ),
304    
305                    new KeyValuePair(
306                            "-//Liferay//DTD Portlet Application 4.3.3//EN",
307                            "liferay-portlet-app_4_3_3.dtd"
308                    ),
309    
310                    new KeyValuePair(
311                            "-//Liferay//DTD Portlet Application 4.3.6//EN",
312                            "liferay-portlet-app_4_3_6.dtd"
313                    ),
314    
315                    new KeyValuePair(
316                            "-//Liferay//DTD Portlet Application 4.4.0//EN",
317                            "liferay-portlet-app_4_4_0.dtd"
318                    ),
319    
320                    new KeyValuePair(
321                            "-//Liferay//DTD Portlet Application 5.0.0//EN",
322                            "liferay-portlet-app_5_0_0.dtd"
323                    ),
324    
325                    new KeyValuePair(
326                            "-//Liferay//DTD Portlet Application 5.1.0//EN",
327                            "liferay-portlet-app_5_1_0.dtd"
328                    ),
329    
330                    new KeyValuePair(
331                            "-//Liferay//DTD Portlet Application 5.2.0//EN",
332                            "liferay-portlet-app_5_2_0.dtd"
333                    ),
334    
335                    new KeyValuePair(
336                            "-//Liferay//DTD Portlet Application 6.0.0//EN",
337                            "liferay-portlet-app_6_0_0.dtd"
338                    ),
339    
340                    new KeyValuePair(
341                            "-//Liferay//DTD Service Builder 3.5.0//EN",
342                            "liferay-service-builder_3_5_0.dtd"
343                    ),
344    
345                    new KeyValuePair(
346                            "-//Liferay//DTD Service Builder 3.6.1//EN",
347                            "liferay-service-builder_3_6_1.dtd"
348                    ),
349    
350                    new KeyValuePair(
351                            "-//Liferay//DTD Service Builder 4.0.0//EN",
352                            "liferay-service-builder_4_0_0.dtd"
353                    ),
354    
355                    new KeyValuePair(
356                            "-//Liferay//DTD Service Builder 4.2.0//EN",
357                            "liferay-service-builder_4_2_0.dtd"
358                    ),
359    
360                    new KeyValuePair(
361                            "-//Liferay//DTD Service Builder 4.3.0//EN",
362                            "liferay-service-builder_4_3_0.dtd"
363                    ),
364    
365                    new KeyValuePair(
366                            "-//Liferay//DTD Service Builder 4.3.3//EN",
367                            "liferay-service-builder_4_3_3.dtd"
368                    ),
369    
370                    new KeyValuePair(
371                            "-//Liferay//DTD Service Builder 4.4.0//EN",
372                            "liferay-service-builder_4_4_0.dtd"
373                    ),
374    
375                    new KeyValuePair(
376                            "-//Liferay//DTD Service Builder 5.0.0//EN",
377                            "liferay-service-builder_5_0_0.dtd"
378                    ),
379    
380                    new KeyValuePair(
381                            "-//Liferay//DTD Service Builder 5.1.0//EN",
382                            "liferay-service-builder_5_1_0.dtd"
383                    ),
384    
385                    new KeyValuePair(
386                            "-//Liferay//DTD Service Builder 5.2.0//EN",
387                            "liferay-service-builder_5_2_0.dtd"
388                    ),
389    
390                    new KeyValuePair(
391                            "-//Liferay//DTD Service Builder 6.0.0//EN",
392                            "liferay-service-builder_6_0_0.dtd"
393                    ),
394    
395                    new KeyValuePair(
396                            "-//Liferay//DTD Theme Loader 4.3.0//EN",
397                            "liferay-theme-loader_4_3_0.dtd"
398                    ),
399    
400                    new KeyValuePair(
401                            "-//Liferay//DTD Theme Loader 5.0.0//EN",
402                            "liferay-theme-loader_5_0_0.dtd"
403                    ),
404    
405                    new KeyValuePair(
406                            "-//Liferay//DTD Theme Loader 5.1.0//EN",
407                            "liferay-theme-loader_5_1_0.dtd"
408                    ),
409    
410                    new KeyValuePair(
411                            "-//Liferay//DTD Theme Loader 5.2.0//EN",
412                            "liferay-theme-loader_5_2_0.dtd"
413                    ),
414    
415                    new KeyValuePair(
416                            "-//Liferay//DTD Theme Loader 6.0.0//EN",
417                            "liferay-theme-loader_6_0_0.dtd"
418                    ),
419    
420                    new KeyValuePair(
421                            "-//MuleSource //DTD mule-configuration XML V1.0//EN",
422                            "mule-configuration.dtd"
423                    ),
424    
425                    new KeyValuePair(
426                            "-//SPRING//DTD BEAN//EN",
427                            "spring-beans.dtd"
428                    ),
429    
430                    new KeyValuePair(
431                            "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
432                            "struts-config_1_2.dtd"
433                    ),
434    
435                    new KeyValuePair(
436                            "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
437                            "tiles-config_1_1.dtd"
438                    ),
439    
440                    new KeyValuePair(
441                            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
442                            "web-app_2_3.dtd"
443                    ),
444    
445                    new KeyValuePair(
446                            "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
447                            "web-facesconfig_1_0.dtd"
448                    ),
449    
450                    new KeyValuePair(
451                            "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
452                            "web-facesconfig_1_1.dtd"
453                    ),
454    
455                    new KeyValuePair(
456                            "-//W3C//DTD XMLSCHEMA 200102//EN",
457                            "XMLSchema.dtd"
458                    )
459            };
460    
461            private static KeyValuePair[] _SYSTEM_IDS = {
462                    new KeyValuePair(
463                            "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd",
464                            "portlet-app_1_0.xsd"
465                    ),
466    
467                    new KeyValuePair(
468                            "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd",
469                            "portlet-app_2_0.xsd"
470                    ),
471    
472                    new KeyValuePair(
473                            "http://www.w3.org/2001/xml.xsd",
474                            "xml.xsd"
475                    )
476            };
477    
478            private static Log _log = LogFactoryUtil.getLog(EntityResolver.class);
479    
480    }