View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.config.impl;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.JarURLConnection;
24  import java.net.URL;
25  import java.util.jar.JarEntry;
26  import java.util.logging.Logger;
27  
28  import javax.faces.context.ExternalContext;
29  
30  import org.apache.myfaces.shared.util.ClassUtils;
31  import org.xml.sax.EntityResolver;
32  import org.xml.sax.InputSource;
33  
34  /**
35   * DOCUMENT ME!
36   * @author Manfred Geiler (latest modification by $Author: bommel $)
37   * @author Thomas Spiegl
38   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
39   */
40  public class FacesConfigEntityResolver
41      implements EntityResolver
42  {
43      //private static final Log log = LogFactory.getLog(FacesConfigEntityResolver.class);
44      private static final Logger log = Logger.getLogger(FacesConfigEntityResolver.class.getName());
45  
46      private static final String FACES_CONFIG_1_0_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_0.dtd";
47      private static final String FACES_CONFIG_1_0_DTD_RESOURCE
48              = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_0.dtd";
49      private static final String FACES_CONFIG_1_1_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_1.dtd";
50      private static final String FACES_CONFIG_1_1_DTD_RESOURCE
51              = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_1.dtd";
52  
53      private ExternalContext _externalContext = null;
54  
55      public FacesConfigEntityResolver(ExternalContext context)
56      {
57          _externalContext = context;
58      }
59  
60      public FacesConfigEntityResolver()
61      {
62      }
63  
64      public InputSource resolveEntity(String publicId,
65                                       String systemId)
66          throws IOException
67      {
68          InputStream stream;
69          if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID))
70          {
71              stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE);
72          }
73          else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID))
74          {
75              stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE);
76          }
77  
78          else if (systemId.startsWith("jar:"))
79          {
80              URL url = new URL(systemId);
81              JarURLConnection conn = (JarURLConnection) url.openConnection();
82              
83              // see MYFACES-1982
84              conn.setUseCaches(false);
85              JarEntry jarEntry = conn.getJarEntry();
86              if (jarEntry == null)
87              {
88                  log.severe("JAR entry '" + systemId + "' not found.");
89              }
90              //_jarFile.getInputStream(jarEntry);
91              stream = conn.getJarFile().getInputStream(jarEntry);
92          }
93          else
94          {
95              if (_externalContext == null)
96              {
97                  stream = ClassUtils.getResourceAsStream(systemId);
98              }
99              else
100             {
101                 if (systemId.startsWith("file:")) {
102                     systemId = systemId.substring(7); // remove file://
103                 }
104                 stream = _externalContext.getResourceAsStream(systemId);
105             }
106         }
107 
108         if (stream == null) {
109             return null;
110         }
111         InputSource is = new InputSource(stream);
112         is.setPublicId(publicId);
113         is.setSystemId(systemId);
114         is.setEncoding("ISO-8859-1");
115         return is;
116     }
117 
118 }