001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.config.impl;
017:
018: import org.apache.myfaces.shared_impl.util.ClassUtils;
019: import org.xml.sax.EntityResolver;
020: import org.xml.sax.InputSource;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import javax.faces.context.ExternalContext;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.net.JarURLConnection;
029: import java.net.URL;
030: import java.util.jar.JarEntry;
031:
032: /**
033: * DOCUMENT ME!
034: * @author Manfred Geiler (latest modification by $Author: schof $)
035: * @author Thomas Spiegl
036: * @version $Revision: 382015 $ $Date: 2006-03-01 14:47:11 +0100 (Mi, 01 Mrz 2006) $
037: */
038: public class FacesConfigEntityResolver implements EntityResolver {
039: private static final Log log = LogFactory
040: .getLog(FacesConfigEntityResolver.class);
041:
042: private static final String FACES_CONFIG_1_0_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_0.dtd";
043: private static final String FACES_CONFIG_1_0_DTD_RESOURCE = "org.apache.myfaces.resource"
044: .replace('.', '/')
045: + "/web-facesconfig_1_0.dtd";
046: private static final String FACES_CONFIG_1_1_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_1.dtd";
047: private static final String FACES_CONFIG_1_1_DTD_RESOURCE = "org.apache.myfaces.resource"
048: .replace('.', '/')
049: + "/web-facesconfig_1_1.dtd";
050:
051: private ExternalContext _externalContext = null;
052:
053: public FacesConfigEntityResolver(ExternalContext context) {
054: _externalContext = context;
055: }
056:
057: public FacesConfigEntityResolver() {
058: }
059:
060: public InputSource resolveEntity(String publicId, String systemId)
061: throws IOException {
062: InputStream stream;
063: if (systemId.equals(FACES_CONFIG_1_0_DTD_SYSTEM_ID)) {
064: stream = ClassUtils
065: .getResourceAsStream(FACES_CONFIG_1_0_DTD_RESOURCE);
066: } else if (systemId.equals(FACES_CONFIG_1_1_DTD_SYSTEM_ID)) {
067: stream = ClassUtils
068: .getResourceAsStream(FACES_CONFIG_1_1_DTD_RESOURCE);
069: }
070:
071: else if (systemId.startsWith("jar:")) {
072: URL url = new URL(systemId);
073: JarURLConnection conn = (JarURLConnection) url
074: .openConnection();
075: JarEntry jarEntry = conn.getJarEntry();
076: if (jarEntry == null) {
077: log.fatal("JAR entry '" + systemId + "' not found.");
078: }
079: //_jarFile.getInputStream(jarEntry);
080: stream = conn.getJarFile().getInputStream(jarEntry);
081: } else {
082: if (_externalContext == null) {
083: stream = ClassUtils.getResourceAsStream(systemId);
084: } else {
085: if (systemId.startsWith("file:")) {
086: systemId = systemId.substring(7); // remove file://
087: }
088: stream = _externalContext.getResourceAsStream(systemId);
089: }
090: }
091:
092: if (stream == null) {
093: return null;
094: }
095: InputSource is = new InputSource(stream);
096: is.setPublicId(publicId);
097: is.setSystemId(systemId);
098: is.setEncoding("ISO-8859-1");
099: return is;
100: }
101:
102: }
|