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