KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > config > impl > FacesConfigEntityResolver


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.config.impl;
17
18 import org.apache.myfaces.util.ClassUtils;
19 import org.xml.sax.EntityResolver JavaDoc;
20 import org.xml.sax.InputSource JavaDoc;
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 JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.net.JarURLConnection JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31
32 /**
33  * DOCUMENT ME!
34  * @author Manfred Geiler (latest modification by $Author: matze $)
35  * @author Thomas Spiegl
36  * @version $Revision: 1.2 $ $Date: 2004/10/13 11:50:59 $
37  */

38 public class FacesConfigEntityResolver
39     implements EntityResolver JavaDoc
40 {
41     private static final Log log = LogFactory.getLog(FacesConfigEntityResolver.class);
42
43     private static final String JavaDoc FACES_CONFIG_1_0_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_0.dtd";
44     private static final String JavaDoc FACES_CONFIG_1_0_DTD_RESOURCE
45             = "org.apache.myfaces.resource".replace('.', '/') + "/web-facesconfig_1_0.dtd";
46     private static final String JavaDoc FACES_CONFIG_1_1_DTD_SYSTEM_ID = "http://java.sun.com/dtd/web-facesconfig_1_1.dtd";
47     private static final String JavaDoc 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 JavaDoc resolveEntity(String JavaDoc publicId,
62                                      String JavaDoc systemId)
63         throws IOException JavaDoc
64     {
65         InputStream JavaDoc 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 JavaDoc url = new URL JavaDoc(systemId);
78             JarURLConnection JavaDoc conn = (JarURLConnection JavaDoc) url.openConnection();
79             JarEntry JavaDoc jarEntry = conn.getJarEntry();
80             if (jarEntry == null)
81             {
82                 log.fatal("JAR entry '" + systemId + "' not found.");
83             }
84             //_jarFile.getInputStream(jarEntry);
85
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); // remove file://
97
}
98                 stream = _externalContext.getResourceAsStream(systemId);
99             }
100         }
101
102         if (stream == null) {
103             return null;
104         }
105         InputSource JavaDoc is = new InputSource JavaDoc(stream);
106         is.setPublicId(publicId);
107         is.setSystemId(systemId);
108         is.setEncoding("ISO-8859-1");
109         return is;
110     }
111
112 }
113
Popular Tags