2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shale.tiger.config;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URL;
23 import org.apache.commons.digester.Digester;
24 import org.apache.commons.digester.RuleSet;
25 import org.apache.shale.tiger.managed.rules.ManagedBeansRuleSet;
26 import org.xml.sax.InputSource;
27 import org.xml.sax.SAXException;
28
29 /***
30 * <p>Parser utility for processing <code>faces-config.xml</code> resources.
31 * Information parsed by each call to the <code>parse()</code> method is
32 * merged with any previous information stored in the specified
33 * {@link FacesConfigConfig} bean.</p>
34 *
35 * <p>To use this utility, instantiate a new instance and set the
36 * <code>facesConfig</code>, <code>resource</code>, and <code>validating</code>
37 * properties. Then, call the <code>parse()</code> method. You can parse
38 * more than one resource by resetting the <code>resource</code> property
39 * and calling <code>parse()</code> again.</p>
40 */
41 public class FacesConfigParser {
42
43
44
45
46 /***
47 * <p>Registration information for the DTD used to validate the specified
48 * configuration resources.</p>
49 */
50 private static final String[] REGISTRATIONS =
51 { "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN",
52 "/org/apache/shale/tiger/resources/web-facesconfig_1_0.dtd",
53 "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN",
54 "/org/apache/shale/tiger/resources/web-facesconfig_1_1.dtd",
55 };
56
57
58
59
60
61 /***
62 * <p>Configuration resource summarizing all of the configuration metadata
63 * from resources that have been parsed so far.</p>
64 */
65 private FacesConfigConfig facesConfig = null;
66
67
68 /***
69 * <p>Return the {@link FacesConfigConfig} bean summarizing all of the
70 * configuration metadata from resources that have been parsed so far.</p>
71 */
72 public FacesConfigConfig getFacesConfig() {
73 return this.facesConfig;
74 }
75
76
77 /***
78 * <p>Set the {@link FacesConfigConfig} bean that will summarize all of the
79 * configuration metadata from parsed resources.</p>
80 *
81 * @param facesConfig New {@link FacesConfigConfig} instance to be populated
82 */
83 public void setFacesConfig(FacesConfigConfig facesConfig) {
84 this.facesConfig = facesConfig;
85 }
86
87
88 /***
89 * <p>The URL of the configuration resource to be parsed.</p>
90 */
91 private URL resource = null;
92
93
94 /***
95 * <p>Return the URL of the configuration resource to be parsed.</p>
96 */
97 public URL getResource() {
98 return this.resource;
99 }
100
101
102 /***
103 * <p>Set the URL of the configuration resource to be parsed.</p>
104 *
105 * @param resource The new configuration resource URL
106 */
107 public void setResource(URL resource) {
108 this.resource = resource;
109 }
110
111
112 /***
113 * <p>Flag indicating whether we should do a validating parse.</p>
114 */
115 private boolean validating = true;
116
117
118 /***
119 * <p>Return the validating parse flag.</p>
120 */
121 public boolean isValidating() {
122 return this.validating;
123 }
124
125
126 /***
127 * <p>Set the validating parse flag.</p>
128 *
129 * @param validating The new validating parse flag
130 */
131 public void setValidating(boolean validating) {
132 this.validating = validating;
133 }
134
135
136
137
138
139 /***
140 * <p>Parse the configuration resource specified by the <code>resource</code>
141 * property, storing resulting information in the configuration bean
142 * specified by the <code>facesConfig</code> property.</p>
143 *
144 * @exception IOException if an input/output error occurs
145 * @exception SAXException if an XML parsing error occurs
146 */
147 public void parse() throws IOException, SAXException {
148
149 Digester digester = digester();
150 digester.clear();
151 digester.push(getFacesConfig());
152 InputSource source = new InputSource(getResource().toExternalForm());
153 InputStream stream = null;
154 try {
155 stream = getResource().openStream();
156 source.setByteStream(stream);
157 digester.parse(source);
158 } catch (IOException e) {
159 throw e;
160 } catch (SAXException e) {
161 throw e;
162 } finally {
163 if (stream != null) {
164 try {
165 stream.close();
166 } catch (IOException e) {
167 ;
168 }
169 }
170 }
171
172 }
173
174
175
176
177
178 /***
179 * <p>Configured <code>Digester</code> instance to be used (lazily
180 * instantiated.</p>
181 */
182 private Digester digester = null;
183
184
185 /***
186 * <p>Return a fully configured <code>Digester</code> instance.</p>
187 */
188 private Digester digester() {
189
190
191 if (digester != null) {
192 return digester;
193 }
194
195
196 digester = new Digester();
197 digester.setNamespaceAware(false);
198 digester.setUseContextClassLoader(true);
199 digester.setValidating(isValidating());
200
201
202 for (int i = 0; i < REGISTRATIONS.length; i += 2) {
203 URL url = this.getClass().getResource(REGISTRATIONS[i + 1]);
204 digester.register(REGISTRATIONS[i], url.toString());
205 }
206
207
208 RuleSet ruleSet = null;
209 ruleSet = new ManagedBeansRuleSet();
210 ruleSet.addRuleInstances(digester);
211
212
213 return digester;
214
215 }
216
217
218 }