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 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
36
37
38
39
40
41 public class FacesConfigEntityResolver implements EntityResolver
42 {
43 private static final Logger log = Logger.getLogger(FacesConfigEntityResolver.class.getName());
44
45 private static final String FACES_CONFIG_1_0_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_0.dtd";
46 private static final String FACES_CONFIG_1_0_DTD_RESOURCE
47 = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_0.dtd";
48 private static final String FACES_CONFIG_1_1_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_1.dtd";
49 private static final String FACES_CONFIG_1_1_DTD_RESOURCE
50 = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_1.dtd";
51
52 private ExternalContext _externalContext = null;
53
54 public FacesConfigEntityResolver(ExternalContext context)
55 {
56 _externalContext = context;
57 }
58
59 public FacesConfigEntityResolver()
60 {
61 }
62
63 public InputSource resolveEntity(String publicId,
64 String systemId)
65 throws IOException
66 {
67 InputStream stream;
68 if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID))
69 {
70 stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE);
71 }
72 else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID))
73 {
74 stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE);
75 }
76
77 else if (systemId.startsWith("jar:"))
78 {
79 URL url = new URL(systemId);
80 JarURLConnection conn = (JarURLConnection) url.openConnection();
81
82
83 conn.setUseCaches(false);
84 JarEntry jarEntry = conn.getJarEntry();
85 if (jarEntry == null)
86 {
87 log.severe("JAR entry '" + systemId + "' not found.");
88 }
89
90 stream = conn.getJarFile().getInputStream(jarEntry);
91 }
92 else
93 {
94 if (_externalContext == null)
95 {
96 stream = ClassUtils.getResourceAsStream(systemId);
97 }
98 else
99 {
100 if (systemId.startsWith("file:"))
101 {
102 systemId = systemId.substring(7);
103 }
104 stream = _externalContext.getResourceAsStream(systemId);
105 }
106 }
107
108 if (stream == null)
109 {
110 return null;
111 }
112 InputSource is = new InputSource(stream);
113 is.setPublicId(publicId);
114 is.setSystemId(systemId);
115 is.setEncoding("ISO-8859-1");
116 return is;
117 }
118
119 }