KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > compiler > Compiler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.compiler;
16
17 import java.io.IOException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import javax.el.ELException;
27 import javax.el.ExpressionFactory;
28 import javax.faces.FacesException;
29 import javax.faces.context.FacesContext;
30
31 import com.sun.facelets.FaceletException;
32 import com.sun.facelets.FaceletHandler;
33 import com.sun.facelets.tag.CompositeTagDecorator;
34 import com.sun.facelets.tag.CompositeTagLibrary;
35 import com.sun.facelets.tag.TagDecorator;
36 import com.sun.facelets.tag.TagLibrary;
37 import com.sun.facelets.tag.ui.UILibrary;
38 import com.sun.facelets.util.ParameterCheck;
39 import com.sun.facelets.util.FacesAPI;
40
41 /**
42  * A Compiler instance may handle compiling multiple sources
43  *
44  * @author Jacob Hookom
45  * @version $Id: Compiler.java,v 1.14 2006/03/29 04:10:02 jhook Exp $
46  */

47 public abstract class Compiler {
48
49     protected final static Logger JavaDoc log = Logger.getLogger("facelets.compiler");
50
51     public final static String JavaDoc EXPRESSION_FACTORY = "compiler.ExpressionFactory";
52
53     private static final TagLibrary EMPTY_LIBRARY = new CompositeTagLibrary(
54             new TagLibrary[0]);
55
56     private static final TagDecorator EMPTY_DECORATOR = new CompositeTagDecorator(
57             new TagDecorator[0]);
58
59     private boolean validating = false;
60
61     private boolean trimmingWhitespace = false;
62
63     private boolean trimmingComments = false;
64     
65     private boolean trimmingXmlDeclarations = false;
66     
67     private boolean trimmingDoctypeDeclarations = false;
68
69     private final List JavaDoc libraries = new ArrayList JavaDoc();
70
71     private final List JavaDoc decorators = new ArrayList JavaDoc();
72
73     private final Map JavaDoc features = new HashMap JavaDoc();
74
75     private boolean initialized = false;
76
77     /**
78      *
79      */

80     public Compiler() {
81         
82     }
83
84     private synchronized void initialize() {
85         if (this.initialized)
86             return;
87         log.fine("Initializing");
88         try {
89             TagLibraryConfig cfg = new TagLibraryConfig();
90             cfg.loadImplicit(this);
91             
92             if (!this.createTagLibrary().containsNamespace(UILibrary.Namespace)) {
93                 log.severe("Missing Built-in Tag Libraries! Make sure they are included within the META-INF directory of Facelets' Jar");
94             }
95             
96         } catch (IOException JavaDoc e) {
97             log.log(Level.SEVERE, "Compiler Initialization Error", e);
98         } finally {
99             this.initialized = true;
100         }
101         log.fine("Initialization Successful");
102     }
103
104     public final FaceletHandler compile(URL JavaDoc src, String JavaDoc alias)
105             throws IOException JavaDoc, FaceletException, ELException, FacesException {
106         if (!this.initialized)
107             this.initialize();
108         return this.doCompile(src, alias);
109     }
110
111     protected abstract FaceletHandler doCompile(URL JavaDoc src, String JavaDoc alias)
112             throws IOException JavaDoc, FaceletException, ELException, FacesException;
113
114     public final TagDecorator createTagDecorator() {
115         if (this.decorators.size() > 0) {
116             return new CompositeTagDecorator((TagDecorator[]) this.decorators
117                     .toArray(new TagDecorator[this.decorators.size()]));
118         }
119         return EMPTY_DECORATOR;
120     }
121
122     public final void addTagDecorator(TagDecorator decorator) {
123         ParameterCheck.notNull("decorator", decorator);
124         if (!this.decorators.contains(decorator)) {
125             this.decorators.add(decorator);
126         }
127     }
128
129     public final ExpressionFactory createExpressionFactory() {
130         ExpressionFactory el = null;
131         el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
132         if (el == null && FacesAPI.getVersion() >= 12) {
133             try {
134                 el = FacesContext.getCurrentInstance().getApplication()
135                         .getExpressionFactory();
136                 if (el == null) {
137                     log.warning("No default ExpressionFactory from Faces Implementation, attempting to load from Feature["
138                                 + EXPRESSION_FACTORY + "]");
139                 }
140             } catch (Exception JavaDoc e) {
141                 // do nothing
142
}
143         }
144         if (el == null) {
145             this.features.put(EXPRESSION_FACTORY, "com.sun.el.ExpressionFactoryImpl");
146             el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
147         }
148         return el;
149     }
150
151     private final Object JavaDoc featureInstance(String JavaDoc name) {
152         String JavaDoc type = (String JavaDoc) this.features.get(name);
153         if (type != null) {
154             try {
155                 return Class.forName(type, true, Thread.currentThread().getContextClassLoader()).newInstance();
156             } catch (Throwable JavaDoc t) {
157                 throw new FaceletException("Could not instantiate feature["
158                         + name + "]: " + type);
159             }
160         }
161         return null;
162     }
163
164     public final TagLibrary createTagLibrary() {
165         if (this.libraries.size() > 0) {
166             return new CompositeTagLibrary((TagLibrary[]) this.libraries
167                     .toArray(new TagLibrary[this.libraries.size()]));
168         }
169         return EMPTY_LIBRARY;
170     }
171
172     public final void addTagLibrary(TagLibrary library) {
173         ParameterCheck.notNull("library", library);
174         if (!this.libraries.contains(library)) {
175             this.libraries.add(library);
176         }
177     }
178
179     public final void setFeature(String JavaDoc name, String JavaDoc value) {
180         this.features.put(name, value);
181     }
182
183     public final String JavaDoc getFeature(String JavaDoc name) {
184         return (String JavaDoc) this.features.get(name);
185     }
186     
187     public final boolean isTrimmingDoctypeDeclarations() {
188         return this.trimmingDoctypeDeclarations;
189     }
190     
191     public final void setTrimmingDoctypeDeclarations(boolean trimmingDoctypeDeclarations) {
192         this.trimmingDoctypeDeclarations = trimmingDoctypeDeclarations;
193     }
194     
195     public final boolean isTrimmingXmlDeclarations() {
196         return this.trimmingXmlDeclarations;
197     }
198     
199     public final void setTrimmingXmlDeclarations(boolean trimmingXmlDeclarations) {
200         this.trimmingXmlDeclarations = trimmingXmlDeclarations;
201     }
202
203     public final boolean isTrimmingComments() {
204         return this.trimmingComments;
205     }
206
207     public final void setTrimmingComments(boolean trimmingComments) {
208         this.trimmingComments = trimmingComments;
209     }
210
211     public final boolean isTrimmingWhitespace() {
212         return this.trimmingWhitespace;
213     }
214
215     public final void setTrimmingWhitespace(boolean trimmingWhitespace) {
216         this.trimmingWhitespace = trimmingWhitespace;
217     }
218
219     public final boolean isValidating() {
220         return this.validating;
221     }
222
223     public final void setValidating(boolean validating) {
224         this.validating = validating;
225     }
226 }
227
Popular Tags