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