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