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