KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > jsfmeta > MetadataXmlParser


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.jsfmeta;
35
36 import java.io.BufferedInputStream JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.net.URL JavaDoc;
41
42 import org.apache.commons.digester.Digester;
43 import org.xml.sax.InputSource JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46 import com.sun.rave.jsfmeta.beans.FacesConfigBean;
47 import com.sun.rave.jsfmeta.rules.FacesConfigRuleSet;
48 import java.net.URLConnection JavaDoc;
49
50 /*
51  * Parser for metadata.
52  *
53  * TODO:
54  * separate dtd to a catalog class
55  */

56
57
58 public class MetadataXmlParser {
59     
60     private static final String JavaDoc FACES_CONFIG_1_0_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN";
61     
62     private static final String JavaDoc FACES_CONFIG_1_0_DTD = "conf/dtd/web-facesconfig_1_0.dtd";
63     
64     private static final String JavaDoc FACES_CONFIG_1_1_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN";
65     
66     private static final String JavaDoc FACES_CONFIG_1_1_DTD = "conf/dtd/web-facesconfig_1_1.dtd";
67     
68     private static final String JavaDoc FACES_CONFIG_1_2_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.2//EN";
69     
70     private static final String JavaDoc FACES_CONFIG_1_2_DTD = "conf/dtd/web-facesconfig_1_2.dtd";
71     
72     private static final String JavaDoc FACES_OVERLAY_1_0_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.0//EN";
73     
74     private static final String JavaDoc FACES_OVERLAY_1_0_DTD = "conf/dtd/web-facesconfig-overlay_1_0.dtd";
75     
76     private static final String JavaDoc FACES_OVERLAY_1_1_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.1//EN";
77     
78     private static final String JavaDoc FACES_OVERLAY_1_1_DTD = "conf/dtd/web-facesconfig-overlay_1_1.dtd";
79     
80     private static final String JavaDoc FACES_OVERLAY_1_2_ID = "-//Sun Microsystems, Inc.//DTD JavaServer Faces Overlay 1.2//EN";
81     
82     private static final String JavaDoc FACES_OVERLAY_1_2_DTD = "conf/dtd/web-facesconfig-overlay_1_2.dtd";
83     
84     private boolean configured;
85     
86     private Digester digester;
87     
88     private boolean design;
89     
90     private boolean generate;
91     
92     private boolean runtime;
93     
94     public MetadataXmlParser() {
95         
96         configured = false;
97         digester = new Digester();
98         design = true;
99         generate = true;
100         runtime = false;
101         digester.setNamespaceAware(false);
102         digester.setUseContextClassLoader(true);
103         
104         //TODO: need to validating
105
digester.setValidating(false);
106         URL JavaDoc url = null;
107         url = getClass().getClassLoader().getResource(FACES_CONFIG_1_0_DTD);
108         if (url != null){
109             register(FACES_CONFIG_1_0_ID, url);
110         }
111         
112         url = getClass().getClassLoader().getResource(FACES_CONFIG_1_1_DTD);
113         if (url != null){
114             register(FACES_CONFIG_1_1_ID, url);
115         }
116                 
117         url = getClass().getClassLoader().getResource(FACES_CONFIG_1_2_DTD);
118         if (url != null){
119             register(FACES_CONFIG_1_2_ID, url);
120         }
121         
122         url = getClass().getClassLoader().getResource(FACES_OVERLAY_1_0_DTD);
123         if (url != null){
124             register( FACES_OVERLAY_1_0_ID, url);
125         }
126         
127         url = getClass().getClassLoader().getResource(FACES_OVERLAY_1_1_DTD);
128         if (url != null){
129             register( FACES_OVERLAY_1_1_ID, url);
130         }
131         
132         url = getClass().getClassLoader().getResource(FACES_OVERLAY_1_2_DTD);
133         if (url != null){
134             register(FACES_OVERLAY_1_2_ID, url);
135         }
136     }
137     
138     public boolean isDesign() {
139         return design;
140     }
141     
142     public void setDesign(boolean design) {
143         this.design = design;
144     }
145     
146     public boolean isGenerate() {
147         return generate;
148     }
149     
150     public void setGenerate(boolean generate) {
151         this.generate = generate;
152     }
153     
154     public boolean isRuntime() {
155         return runtime;
156     }
157     
158     public void setRuntime(boolean runtime) {
159         this.runtime = runtime;
160     }
161     
162     public FacesConfigBean parse(URL JavaDoc url) throws IOException JavaDoc, SAXException JavaDoc {
163         return parse(url, new FacesConfigBean());
164     }
165     
166     public FacesConfigBean parse(File JavaDoc url) throws IOException JavaDoc, SAXException JavaDoc {
167         return parse(url.toURL(), new FacesConfigBean());
168     }
169     
170     public FacesConfigBean parse(URL JavaDoc url, FacesConfigBean metadata)throws IOException JavaDoc, SAXException JavaDoc{
171         
172         configure();
173         digester.clear();
174         digester.push(metadata);
175         InputSource JavaDoc source = null;
176         InputStream JavaDoc stream = null;
177         FacesConfigBean fcb = null;
178         try {
179             URLConnection JavaDoc urlConnection = url.openConnection();
180             stream = new BufferedInputStream JavaDoc(urlConnection.getInputStream());
181             source = new InputSource JavaDoc(url.toString());
182             source.setByteStream(stream);
183             fcb = (FacesConfigBean) digester.parse(source);
184         } catch(SAXException JavaDoc e){
185             System.err.println("Please check the syntax for the following file: "+ url.getFile());
186             e.printStackTrace();
187             System.exit(1);
188             
189         } catch(IOException JavaDoc e){
190             System.err.println("Please check the syntax for the following file: "+ url.getFile());
191             e.printStackTrace();
192             System.exit(1);
193             
194         } finally {
195         
196             if (stream != null)
197                 try {
198                     stream.close();
199                 } catch (IOException JavaDoc e) {
200                     System.err.println("Please check the following file:"+url.getFile());
201                     e.printStackTrace();
202                     System.exit(1);
203                 }
204             stream = null;
205         }
206         return fcb;
207     }
208     
209     public void register(String JavaDoc publicId, URL JavaDoc entityURL) {
210         digester.register(publicId, entityURL.toString());
211     }
212     
213     private void configure() {
214         if (!configured) {
215             digester.addRuleSet(new FacesConfigRuleSet(design, generate,
216                     runtime));
217             configured = true;
218         }
219     }
220     
221 }
222
Popular Tags