001    /**
002     * Copyright (c) 2000-2011 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                                            InputSource inputSource = new InputSource(is);
066    
067                                            inputSource.setSystemId(kvp.getKey());
068    
069                                            return inputSource;
070                                    }
071                            }
072                    }
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug("No entity found for " + publicId + " " + systemId);
076                    }
077    
078                    return null;
079            }
080    
081            private static String _DEFINITIONS_PATH = "com/liferay/portal/definitions/";
082    
083            private static KeyValuePair[] _PUBLIC_IDS = {
084                    new KeyValuePair(
085                            "datatypes",
086                            "datatypes.dtd"
087                    ),
088    
089                    new KeyValuePair(
090                            "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN",
091                            "facelet-taglib_1_0.dtd"
092                    ),
093    
094                    new KeyValuePair(
095                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN",
096                            "hibernate-mapping-3.0.dtd"
097                    ),
098    
099                    new KeyValuePair(
100                            "-//Liferay//DTD Display 2.0.0//EN",
101                            "liferay-display_2_0_0.dtd"
102                    ),
103    
104                    new KeyValuePair(
105                            "-//Liferay//DTD Display 3.5.0//EN",
106                            "liferay-display_3_5_0.dtd"
107                    ),
108    
109                    new KeyValuePair(
110                            "-//Liferay//DTD Display 4.0.0//EN",
111                            "liferay-display_4_0_0.dtd"
112                    ),
113    
114                    new KeyValuePair(
115                            "-//Liferay//DTD Display 5.0.0//EN",
116                            "liferay-display_5_0_0.dtd"
117                    ),
118    
119                    new KeyValuePair(
120                            "-//Liferay//DTD Display 5.1.0//EN",
121                            "liferay-display_5_1_0.dtd"
122                    ),
123    
124                    new KeyValuePair(
125                            "-//Liferay//DTD Display 5.2.0//EN",
126                            "liferay-display_5_2_0.dtd"
127                    ),
128    
129                    new KeyValuePair(
130                            "-//Liferay//DTD Display 6.0.0//EN",
131                            "liferay-display_6_0_0.dtd"
132                    ),
133    
134                    new KeyValuePair(
135                            "-//Liferay//DTD Display 6.1.0//EN",
136                            "liferay-display_6_1_0.dtd"
137                    ),
138    
139                    new KeyValuePair(
140                            "-//Liferay//DTD Friendly URL Routes 6.0.0//EN",
141                            "liferay-friendly-url-routes_6_0_0.dtd"
142                    ),
143    
144                    new KeyValuePair(
145                            "-//Liferay//DTD Friendly URL Routes 6.1.0//EN",
146                            "liferay-friendly-url-routes_6_1_0.dtd"
147                    ),
148    
149                    new KeyValuePair(
150                            "-//Liferay//DTD Hook 5.1.0//EN",
151                            "liferay-hook_5_1_0.dtd"
152                    ),
153    
154                    new KeyValuePair(
155                            "-//Liferay//DTD Hook 5.2.0//EN",
156                            "liferay-hook_5_2_0.dtd"
157                    ),
158    
159                    new KeyValuePair(
160                            "-//Liferay//DTD Hook 6.0.0//EN",
161                            "liferay-hook_6_0_0.dtd"
162                    ),
163    
164                    new KeyValuePair(
165                            "-//Liferay//DTD Hook 6.1.0//EN",
166                            "liferay-hook_6_1_0.dtd"
167                    ),
168    
169                    new KeyValuePair(
170                            "-//Liferay//DTD Layout Templates 3.6.0//EN",
171                            "liferay-layout-templates_3_6_0.dtd"
172                    ),
173    
174                    new KeyValuePair(
175                            "-//Liferay//DTD Layout Templates 4.0.0//EN",
176                            "liferay-layout-templates_4_0_0.dtd"
177                    ),
178    
179                    new KeyValuePair(
180                            "-//Liferay//DTD Layout Templates 4.3.0//EN",
181                            "liferay-layout-templates_4_3_0.dtd"
182                    ),
183    
184                    new KeyValuePair(
185                            "-//Liferay//DTD Layout Templates 5.0.0//EN",
186                            "liferay-layout-templates_5_0_0.dtd"
187                    ),
188    
189                    new KeyValuePair(
190                            "-//Liferay//DTD Layout Templates 5.1.0//EN",
191                            "liferay-layout-templates_5_1_0.dtd"
192                    ),
193    
194                    new KeyValuePair(
195                            "-//Liferay//DTD Layout Templates 5.2.0//EN",
196                            "liferay-layout-templates_5_2_0.dtd"
197                    ),
198    
199                    new KeyValuePair(
200                            "-//Liferay//DTD Layout Templates 6.0.0//EN",
201                            "liferay-layout-templates_6_0_0.dtd"
202                    ),
203    
204                    new KeyValuePair(
205                            "-//Liferay//DTD Layout Templates 6.1.0//EN",
206                            "liferay-layout-templates_6_1_0.dtd"
207                    ),
208    
209                    new KeyValuePair(
210                            "-//Liferay//DTD Look and Feel 3.5.0//EN",
211                            "liferay-look-and-feel_3_5_0.dtd"
212                    ),
213    
214                    new KeyValuePair(
215                            "-//Liferay//DTD Look and Feel 4.0.0//EN",
216                            "liferay-look-and-feel_4_0_0.dtd"
217                    ),
218    
219                    new KeyValuePair(
220                            "-//Liferay//DTD Look and Feel 4.3.0//EN",
221                            "liferay-look-and-feel_4_3_0.dtd"
222                    ),
223    
224                    new KeyValuePair(
225                            "-//Liferay//DTD Look and Feel 5.0.0//EN",
226                            "liferay-look-and-feel_5_0_0.dtd"
227                    ),
228    
229                    new KeyValuePair(
230                            "-//Liferay//DTD Look and Feel 5.1.0//EN",
231                            "liferay-look-and-feel_5_1_0.dtd"
232                    ),
233    
234                    new KeyValuePair(
235                            "-//Liferay//DTD Look and Feel 5.2.0//EN",
236                            "liferay-look-and-feel_5_2_0.dtd"
237                    ),
238    
239                    new KeyValuePair(
240                            "-//Liferay//DTD Look and Feel 6.0.0//EN",
241                            "liferay-look-and-feel_6_0_0.dtd"
242                    ),
243    
244                    new KeyValuePair(
245                            "-//Liferay//DTD Look and Feel 6.1.0//EN",
246                            "liferay-look-and-feel_6_1_0.dtd"
247                    ),
248    
249                    new KeyValuePair(
250                            "-//Liferay//DTD Plugin Package 4.3.0//EN",
251                            "liferay-plugin-package_4_3_0.dtd"
252                    ),
253    
254                    new KeyValuePair(
255                            "-//Liferay//DTD Plugin Package 5.0.0//EN",
256                            "liferay-plugin-package_5_0_0.dtd"
257                    ),
258    
259                    new KeyValuePair(
260                            "-//Liferay//DTD Plugin Package 5.1.0//EN",
261                            "liferay-plugin-package_5_1_0.dtd"
262                    ),
263    
264                    new KeyValuePair(
265                            "-//Liferay//DTD Plugin Package 5.2.0//EN",
266                            "liferay-plugin-package_5_2_0.dtd"
267                    ),
268    
269                    new KeyValuePair(
270                            "-//Liferay//DTD Plugin Package 6.0.0//EN",
271                            "liferay-plugin-package_6_0_0.dtd"
272                    ),
273    
274                    new KeyValuePair(
275                            "-//Liferay//DTD Plugin Package 6.1.0//EN",
276                            "liferay-plugin-package_6_1_0.dtd"
277                    ),
278    
279                    new KeyValuePair(
280                            "-//Liferay//DTD Plugin Repository 4.3.0//EN",
281                            "liferay-plugin-repository_4_3_0.dtd"
282                    ),
283    
284                    new KeyValuePair(
285                            "-//Liferay//DTD Plugin Repository 5.0.0//EN",
286                            "liferay-plugin-repository_5_0_0.dtd"
287                    ),
288    
289                    new KeyValuePair(
290                            "-//Liferay//DTD Plugin Repository 5.1.0//EN",
291                            "liferay-plugin-repository_5_1_0.dtd"
292                    ),
293    
294                    new KeyValuePair(
295                            "-//Liferay//DTD Plugin Repository 5.2.0//EN",
296                            "liferay-plugin-repository_5_2_0.dtd"
297                    ),
298    
299                    new KeyValuePair(
300                            "-//Liferay//DTD Plugin Repository 6.0.0//EN",
301                            "liferay-plugin-repository_6_0_0.dtd"
302                    ),
303    
304                    new KeyValuePair(
305                            "-//Liferay//DTD Plugin Repository 6.1.0//EN",
306                            "liferay-plugin-repository_6_1_0.dtd"
307                    ),
308    
309                    new KeyValuePair(
310                            "-//Liferay//DTD Portlet Application 3.5.0//EN",
311                            "liferay-portlet-app_3_5_0.dtd"
312                    ),
313    
314                    new KeyValuePair(
315                            "-//Liferay//DTD Portlet Application 4.0.0//EN",
316                            "liferay-portlet-app_4_0_0.dtd"
317                    ),
318    
319                    new KeyValuePair(
320                            "-//Liferay//DTD Portlet Application 4.1.0//EN",
321                            "liferay-portlet-app_4_1_0.dtd"
322                    ),
323    
324                    new KeyValuePair(
325                            "-//Liferay//DTD Portlet Application 4.2.0//EN",
326                            "liferay-portlet-app_4_2_0.dtd"
327                    ),
328    
329                    new KeyValuePair(
330                            "-//Liferay//DTD Portlet Application 4.3.0//EN",
331                            "liferay-portlet-app_4_3_0.dtd"
332                    ),
333    
334                    new KeyValuePair(
335                            "-//Liferay//DTD Portlet Application 4.3.1//EN",
336                            "liferay-portlet-app_4_3_1.dtd"
337                    ),
338    
339                    new KeyValuePair(
340                            "-//Liferay//DTD Portlet Application 4.3.2//EN",
341                            "liferay-portlet-app_4_3_2.dtd"
342                    ),
343    
344                    new KeyValuePair(
345                            "-//Liferay//DTD Portlet Application 4.3.3//EN",
346                            "liferay-portlet-app_4_3_3.dtd"
347                    ),
348    
349                    new KeyValuePair(
350                            "-//Liferay//DTD Portlet Application 4.3.6//EN",
351                            "liferay-portlet-app_4_3_6.dtd"
352                    ),
353    
354                    new KeyValuePair(
355                            "-//Liferay//DTD Portlet Application 4.4.0//EN",
356                            "liferay-portlet-app_4_4_0.dtd"
357                    ),
358    
359                    new KeyValuePair(
360                            "-//Liferay//DTD Portlet Application 5.0.0//EN",
361                            "liferay-portlet-app_5_0_0.dtd"
362                    ),
363    
364                    new KeyValuePair(
365                            "-//Liferay//DTD Portlet Application 5.1.0//EN",
366                            "liferay-portlet-app_5_1_0.dtd"
367                    ),
368    
369                    new KeyValuePair(
370                            "-//Liferay//DTD Portlet Application 5.2.0//EN",
371                            "liferay-portlet-app_5_2_0.dtd"
372                    ),
373    
374                    new KeyValuePair(
375                            "-//Liferay//DTD Portlet Application 6.0.0//EN",
376                            "liferay-portlet-app_6_0_0.dtd"
377                    ),
378    
379                    new KeyValuePair(
380                            "-//Liferay//DTD Portlet Application 6.1.0//EN",
381                            "liferay-portlet-app_6_1_0.dtd"
382                    ),
383    
384                    new KeyValuePair(
385                            "-//Liferay//DTD Resource Action Mapping 6.0.0//EN",
386                            "liferay-resource-action-mapping_6_0_0.dtd"
387                    ),
388    
389                    new KeyValuePair(
390                            "-//Liferay//DTD Resource Action Mapping 6.1.0//EN",
391                            "liferay-resource-action-mapping_6_1_0.dtd"
392                    ),
393    
394                    new KeyValuePair(
395                            "-//Liferay//DTD Service Builder 3.5.0//EN",
396                            "liferay-service-builder_3_5_0.dtd"
397                    ),
398    
399                    new KeyValuePair(
400                            "-//Liferay//DTD Service Builder 3.6.1//EN",
401                            "liferay-service-builder_3_6_1.dtd"
402                    ),
403    
404                    new KeyValuePair(
405                            "-//Liferay//DTD Service Builder 4.0.0//EN",
406                            "liferay-service-builder_4_0_0.dtd"
407                    ),
408    
409                    new KeyValuePair(
410                            "-//Liferay//DTD Service Builder 4.2.0//EN",
411                            "liferay-service-builder_4_2_0.dtd"
412                    ),
413    
414                    new KeyValuePair(
415                            "-//Liferay//DTD Service Builder 4.3.0//EN",
416                            "liferay-service-builder_4_3_0.dtd"
417                    ),
418    
419                    new KeyValuePair(
420                            "-//Liferay//DTD Service Builder 4.3.3//EN",
421                            "liferay-service-builder_4_3_3.dtd"
422                    ),
423    
424                    new KeyValuePair(
425                            "-//Liferay//DTD Service Builder 4.4.0//EN",
426                            "liferay-service-builder_4_4_0.dtd"
427                    ),
428    
429                    new KeyValuePair(
430                            "-//Liferay//DTD Service Builder 5.0.0//EN",
431                            "liferay-service-builder_5_0_0.dtd"
432                    ),
433    
434                    new KeyValuePair(
435                            "-//Liferay//DTD Service Builder 5.1.0//EN",
436                            "liferay-service-builder_5_1_0.dtd"
437                    ),
438    
439                    new KeyValuePair(
440                            "-//Liferay//DTD Service Builder 5.2.0//EN",
441                            "liferay-service-builder_5_2_0.dtd"
442                    ),
443    
444                    new KeyValuePair(
445                            "-//Liferay//DTD Service Builder 6.0.0//EN",
446                            "liferay-service-builder_6_0_0.dtd"
447                    ),
448    
449                    new KeyValuePair(
450                            "-//Liferay//DTD Service Builder 6.1.0//EN",
451                            "liferay-service-builder_6_1_0.dtd"
452                    ),
453    
454                    new KeyValuePair(
455                            "-//Liferay//DTD Social 6.1.0//EN",
456                            "liferay-social_6_1_0.dtd"
457                    ),
458    
459                    new KeyValuePair(
460                            "-//Liferay//DTD Theme Loader 4.3.0//EN",
461                            "liferay-theme-loader_4_3_0.dtd"
462                    ),
463    
464                    new KeyValuePair(
465                            "-//Liferay//DTD Theme Loader 5.0.0//EN",
466                            "liferay-theme-loader_5_0_0.dtd"
467                    ),
468    
469                    new KeyValuePair(
470                            "-//Liferay//DTD Theme Loader 5.1.0//EN",
471                            "liferay-theme-loader_5_1_0.dtd"
472                    ),
473    
474                    new KeyValuePair(
475                            "-//Liferay//DTD Theme Loader 5.2.0//EN",
476                            "liferay-theme-loader_5_2_0.dtd"
477                    ),
478    
479                    new KeyValuePair(
480                            "-//Liferay//DTD Theme Loader 6.0.0//EN",
481                            "liferay-theme-loader_6_0_0.dtd"
482                    ),
483    
484                    new KeyValuePair(
485                            "-//Liferay//DTD Theme Loader 6.1.0//EN",
486                            "liferay-theme-loader_6_1_0.dtd"
487                    ),
488    
489                    new KeyValuePair(
490                            "-//MuleSource //DTD mule-configuration XML V1.0//EN",
491                            "mule-configuration.dtd"
492                    ),
493    
494                    new KeyValuePair(
495                            "-//SPRING//DTD BEAN//EN",
496                            "spring-beans.dtd"
497                    ),
498    
499                    new KeyValuePair(
500                            "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN",
501                            "struts-config_1_2.dtd"
502                    ),
503    
504                    new KeyValuePair(
505                            "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
506                            "tiles-config_1_1.dtd"
507                    ),
508    
509                    new KeyValuePair(
510                            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
511                            "web-app_2_3.dtd"
512                    ),
513    
514                    new KeyValuePair(
515                            "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
516                            "web-facesconfig_1_0.dtd"
517                    ),
518    
519                    new KeyValuePair(
520                            "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
521                            "web-facesconfig_1_1.dtd"
522                    ),
523    
524                    new KeyValuePair(
525                            "-//W3C//DTD XMLSCHEMA 200102//EN",
526                            "XMLSchema.dtd"
527                    )
528            };
529    
530            private static KeyValuePair[] _SYSTEM_IDS = {
531                    new KeyValuePair(
532                            "http://java.sun.com/xml/ns/j2ee/j2ee_1_4.xsd",
533                            "j2ee_1_4.xsd"
534                    ),
535    
536                    new KeyValuePair(
537                            "http://www.ibm.com/webservices/xsd/" +
538                                    "j2ee_web_services_client_1_1.xsd",
539                            "j2ee_web_services_client_1_1.xsd"
540                    ),
541    
542                    new KeyValuePair(
543                            "http://java.sun.com/xml/ns/javaee/javaee_5.xsd",
544                            "javaee_5.xsd"
545                    ),
546    
547                    new KeyValuePair(
548                            "http://java.sun.com/xml/ns/javaee/javaee_6.xsd",
549                            "javaee_6.xsd"
550                    ),
551    
552                    new KeyValuePair(
553                            "http://java.sun.com/xml/ns/javaee/" +
554                                    "javaee_web_services_client_1_2.xsd",
555                            "javaee_web_services_client_1_2.xsd"
556                    ),
557    
558                    new KeyValuePair(
559                            "http://java.sun.com/xml/ns/javaee/" +
560                                    "javaee_web_services_client_1_3.xsd",
561                            "javaee_web_services_client_1_3.xsd"
562                    ),
563    
564                    new KeyValuePair(
565                            "http://java.sun.com/xml/ns/j2ee/jsp_2_0.xsd",
566                            "jsp_2_0.xsd"
567                    ),
568    
569                    new KeyValuePair(
570                            "http://java.sun.com/xml/ns/javaee/jsp_2_1.xsd",
571                            "jsp_2_1.xsd"
572                    ),
573    
574                    new KeyValuePair(
575                            "http://java.sun.com/xml/ns/javaee/jsp_2_2.xsd",
576                            "jsp_2_2.xsd"
577                    ),
578    
579                    new KeyValuePair(
580                            "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd",
581                            "portlet-app_1_0.xsd"
582                    ),
583    
584                    new KeyValuePair(
585                            "http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd",
586                            "portlet-app_2_0.xsd"
587                    ),
588    
589                    new KeyValuePair(
590                            "http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd",
591                            "web-app_2_4.xsd"
592                    ),
593    
594                    new KeyValuePair(
595                            "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd",
596                            "web-app_2_5.xsd"
597                    ),
598    
599                    new KeyValuePair(
600                            "http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd",
601                            "web-app_3_0.xsd"
602                    ),
603    
604                    new KeyValuePair(
605                            "http://java.sun.com/xml/ns/javaee/web-common_3_0.xsd",
606                            "web-common_3_0.xsd"
607                    ),
608    
609                    new KeyValuePair(
610                            "http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd",
611                            "web-facesconfig_1_2.xsd"
612                    ),
613    
614                    new KeyValuePair(
615                            "http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd",
616                            "web-facesconfig_2_0.xsd"
617                    ),
618    
619                    new KeyValuePair(
620                            "http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd",
621                            "web-facesconfig_2_1.xsd"
622                    ),
623    
624                    new KeyValuePair(
625                            "http://www.w3.org/2001/xml.xsd",
626                            "xml.xsd"
627                    )
628            };
629    
630            private static Log _log = LogFactoryUtil.getLog(EntityResolver.class);
631    
632    }