001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.jsfmeta;
035:
036: import java.io.BufferedInputStream;
037: import java.io.File;
038: import java.io.IOException;
039: import java.io.InputStream;
040: import java.net.URL;
041:
042: import org.apache.commons.digester.Digester;
043: import org.xml.sax.InputSource;
044: import org.xml.sax.SAXException;
045:
046: import com.sun.rave.jsfmeta.beans.FacesConfigBean;
047: import com.sun.rave.jsfmeta.rules.FacesConfigRuleSet;
048: import java.net.URLConnection;
049:
050: /*
051: * Parser for metadata.
052: *
053: * TODO:
054: * separate dtd to a catalog class
055: */
056:
057: public class MetadataXmlParser {
058:
059: private static final String FACES_CONFIG_1_0_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN";
060:
061: private static final String FACES_CONFIG_1_0_DTD = "conf/dtd/web-facesconfig_1_0.dtd";
062:
063: private static final String FACES_CONFIG_1_1_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN";
064:
065: private static final String FACES_CONFIG_1_1_DTD = "conf/dtd/web-facesconfig_1_1.dtd";
066:
067: private static final String FACES_CONFIG_1_2_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.2//EN";
068:
069: private static final String FACES_CONFIG_1_2_DTD = "conf/dtd/web-facesconfig_1_2.dtd";
070:
071: private static final String FACES_OVERLAY_1_0_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.0//EN";
072:
073: private static final String FACES_OVERLAY_1_0_DTD = "conf/dtd/web-facesconfig-overlay_1_0.dtd";
074:
075: private static final String FACES_OVERLAY_1_1_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.1//EN";
076:
077: private static final String FACES_OVERLAY_1_1_DTD = "conf/dtd/web-facesconfig-overlay_1_1.dtd";
078:
079: private static final String FACES_OVERLAY_1_2_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.2//EN";
080:
081: private static final String FACES_OVERLAY_1_2_DTD = "conf/dtd/web-facesconfig-overlay_1_2.dtd";
082:
083: private boolean configured;
084:
085: private Digester digester;
086:
087: private boolean design;
088:
089: private boolean generate;
090:
091: private boolean runtime;
092:
093: public MetadataXmlParser() {
094:
095: configured = false;
096: digester = new Digester();
097: design = true;
098: generate = true;
099: runtime = false;
100: digester.setNamespaceAware(false);
101: digester.setUseContextClassLoader(true);
102:
103: //TODO: need to validating
104: digester.setValidating(false);
105: URL url = null;
106: url = getClass().getClassLoader().getResource(
107: FACES_CONFIG_1_0_DTD);
108: if (url != null) {
109: register(FACES_CONFIG_1_0_ID, url);
110: }
111:
112: url = getClass().getClassLoader().getResource(
113: FACES_CONFIG_1_1_DTD);
114: if (url != null) {
115: register(FACES_CONFIG_1_1_ID, url);
116: }
117:
118: url = getClass().getClassLoader().getResource(
119: FACES_CONFIG_1_2_DTD);
120: if (url != null) {
121: register(FACES_CONFIG_1_2_ID, url);
122: }
123:
124: url = getClass().getClassLoader().getResource(
125: FACES_OVERLAY_1_0_DTD);
126: if (url != null) {
127: register(FACES_OVERLAY_1_0_ID, url);
128: }
129:
130: url = getClass().getClassLoader().getResource(
131: FACES_OVERLAY_1_1_DTD);
132: if (url != null) {
133: register(FACES_OVERLAY_1_1_ID, url);
134: }
135:
136: url = getClass().getClassLoader().getResource(
137: FACES_OVERLAY_1_2_DTD);
138: if (url != null) {
139: register(FACES_OVERLAY_1_2_ID, url);
140: }
141: }
142:
143: public boolean isDesign() {
144: return design;
145: }
146:
147: public void setDesign(boolean design) {
148: this .design = design;
149: }
150:
151: public boolean isGenerate() {
152: return generate;
153: }
154:
155: public void setGenerate(boolean generate) {
156: this .generate = generate;
157: }
158:
159: public boolean isRuntime() {
160: return runtime;
161: }
162:
163: public void setRuntime(boolean runtime) {
164: this .runtime = runtime;
165: }
166:
167: public FacesConfigBean parse(URL url) throws IOException,
168: SAXException {
169: return parse(url, new FacesConfigBean());
170: }
171:
172: public FacesConfigBean parse(File url) throws IOException,
173: SAXException {
174: return parse(url.toURL(), new FacesConfigBean());
175: }
176:
177: public FacesConfigBean parse(URL url, FacesConfigBean metadata)
178: throws IOException, SAXException {
179:
180: configure();
181: digester.clear();
182: digester.push(metadata);
183: InputSource source = null;
184: InputStream stream = null;
185: FacesConfigBean fcb = null;
186: try {
187: URLConnection urlConnection = url.openConnection();
188: stream = new BufferedInputStream(urlConnection
189: .getInputStream());
190: source = new InputSource(url.toString());
191: source.setByteStream(stream);
192: fcb = (FacesConfigBean) digester.parse(source);
193: } catch (SAXException e) {
194: System.err
195: .println("Please check the syntax for the following file: "
196: + url.getFile());
197: e.printStackTrace();
198: System.exit(1);
199:
200: } catch (IOException e) {
201: System.err
202: .println("Please check the syntax for the following file: "
203: + url.getFile());
204: e.printStackTrace();
205: System.exit(1);
206:
207: } finally {
208:
209: if (stream != null)
210: try {
211: stream.close();
212: } catch (IOException e) {
213: System.err
214: .println("Please check the following file:"
215: + url.getFile());
216: e.printStackTrace();
217: System.exit(1);
218: }
219: stream = null;
220: }
221: return fcb;
222: }
223:
224: public void register(String publicId, URL entityURL) {
225: digester.register(publicId, entityURL.toString());
226: }
227:
228: private void configure() {
229: if (!configured) {
230: digester.addRuleSet(new FacesConfigRuleSet(design,
231: generate, runtime));
232: configured = true;
233: }
234: }
235:
236: }
|