1 16 package org.apache.myfaces.config.impl; 17 18 import org.apache.myfaces.util.ClassUtils; 19 import org.xml.sax.EntityResolver ; 20 import org.xml.sax.InputSource ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 import javax.faces.context.ExternalContext; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.net.JarURLConnection ; 29 import java.net.URL ; 30 import java.util.jar.JarEntry ; 31 32 38 public class FacesConfigEntityResolver 39 implements EntityResolver 40 { 41 private static final Log log = LogFactory.getLog(FacesConfigEntityResolver.class); 42 43 private static final String FACES_CONFIG_1_0_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_0.dtd"; 44 private static final String FACES_CONFIG_1_0_DTD_RESOURCE 45 = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_0.dtd"; 46 private static final String FACES_CONFIG_1_1_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_1.dtd"; 47 private static final String FACES_CONFIG_1_1_DTD_RESOURCE 48 = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_1.dtd"; 49 50 private ExternalContext _externalContext = null; 51 52 public FacesConfigEntityResolver(ExternalContext context) 53 { 54 _externalContext = context; 55 } 56 57 public FacesConfigEntityResolver() 58 { 59 } 60 61 public InputSource resolveEntity(String publicId, 62 String systemId) 63 throws IOException 64 { 65 InputStream stream; 66 if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID)) 67 { 68 stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE); 69 } 70 else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID)) 71 { 72 stream = ClassUtils.getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE); 73 } 74 75 else if (systemId.startsWith("jar:")) 76 { 77 URL url = new URL (systemId); 78 JarURLConnection conn = (JarURLConnection ) url.openConnection(); 79 JarEntry jarEntry = conn.getJarEntry(); 80 if (jarEntry == null) 81 { 82 log.fatal("JAR entry '" + systemId + "' not found."); 83 } 84 stream = conn.getJarFile().getInputStream(jarEntry); 86 } 87 else 88 { 89 if (_externalContext == null) 90 { 91 stream = ClassUtils.getResourceAsStream(systemId); 92 } 93 else 94 { 95 if (systemId.startsWith("file:")) { 96 systemId = systemId.substring(7); } 98 stream = _externalContext.getResourceAsStream(systemId); 99 } 100 } 101 102 if (stream == null) { 103 return null; 104 } 105 InputSource is = new InputSource (stream); 106 is.setPublicId(publicId); 107 is.setSystemId(systemId); 108 is.setEncoding("ISO-8859-1"); 109 return is; 110 } 111 112 } 113 | Popular Tags |