1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.config.impl;
20
21 import org.apache.myfaces.shared_impl.util.ClassUtils;
22 import org.xml.sax.EntityResolver;
23 import org.xml.sax.InputSource;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import javax.faces.context.ExternalContext;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.JarURLConnection;
32 import java.net.URL;
33 import java.util.jar.JarEntry;
34
35
36
37
38
39
40
41 public class FacesConfigEntityResolver
42 implements EntityResolver
43 {
44 private static final Log log = LogFactory.getLog(FacesConfigEntityResolver.class);
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 conn.setUseCaches(false);
83 JarEntry jarEntry = conn.getJarEntry();
84 if (jarEntry == null)
85 {
86 log.fatal("JAR entry '" + systemId + "' not found.");
87 }
88
89 stream = conn.getJarFile().getInputStream(jarEntry);
90 }
91 else
92 {
93 if (_externalContext == null)
94 {
95 stream = ClassUtils.getResourceAsStream(systemId);
96 }
97 else
98 {
99 if (systemId.startsWith("file:")) {
100 systemId = systemId.substring(7);
101 }
102 stream = _externalContext.getResourceAsStream(systemId);
103 }
104 }
105
106 if (stream == null) {
107 return null;
108 }
109 InputSource is = new InputSource(stream);
110 is.setPublicId(publicId);
111 is.setSystemId(systemId);
112 is.setEncoding("ISO-8859-1");
113 return is;
114 }
115
116 }