Source code: com/sun/facelets/compiler/Compiler.java
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;
18 import java.net.URL;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
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 log = Logger.getLogger("facelets.compiler");
50
51 public final static String 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 final List libraries = new ArrayList();
66
67 private final List decorators = new ArrayList();
68
69 private final Map features = new HashMap();
70
71 private boolean initialized = false;
72
73 /**
74 *
75 */
76 public Compiler() {
77
78 }
79
80 private synchronized void initialize() {
81 if (this.initialized)
82 return;
83 log.fine("Initializing");
84 try {
85 TagLibraryConfig cfg = new TagLibraryConfig();
86 cfg.loadImplicit(this);
87
88 if (!this.createTagLibrary().containsNamespace(UILibrary.Namespace)) {
89 log.severe("Missing Built-in Tag Libraries! Make sure they are included within the META-INF directory of Facelets' Jar");
90 }
91
92 } catch (IOException e) {
93 log.log(Level.SEVERE, "Compiler Initialization Error", e);
94 } finally {
95 this.initialized = true;
96 }
97 log.fine("Initialization Successful");
98 }
99
100 public final FaceletHandler compile(URL src, String alias)
101 throws IOException, FaceletException, ELException, FacesException {
102 if (!this.initialized)
103 this.initialize();
104 return this.doCompile(src, alias);
105 }
106
107 protected abstract FaceletHandler doCompile(URL src, String alias)
108 throws IOException, FaceletException, ELException, FacesException;
109
110 public final TagDecorator createTagDecorator() {
111 if (this.decorators.size() > 0) {
112 return new CompositeTagDecorator((TagDecorator[]) this.decorators
113 .toArray(new TagDecorator[this.decorators.size()]));
114 }
115 return EMPTY_DECORATOR;
116 }
117
118 public final void addTagDecorator(TagDecorator decorator) {
119 ParameterCheck.notNull("decorator", decorator);
120 if (!this.decorators.contains(decorator)) {
121 this.decorators.add(decorator);
122 }
123 }
124
125 public final ExpressionFactory createExpressionFactory() {
126 ExpressionFactory el = null;
127 el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
128 if (el == null && FacesAPI.getVersion() >= 12) {
129 try {
130 el = FacesContext.getCurrentInstance().getApplication()
131 .getExpressionFactory();
132 if (el == null) {
133 log.warning("No default ExpressionFactory from Faces Implementation, attempting to load from Feature["
134 + EXPRESSION_FACTORY + "]");
135 }
136 } catch (Exception e) {
137 // do nothing
138 }
139 }
140 if (el == null) {
141 this.features.put(EXPRESSION_FACTORY, "com.sun.el.ExpressionFactoryImpl");
142 el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
143 }
144 return el;
145 }
146
147 private final Object featureInstance(String name) {
148 String type = (String) this.features.get(name);
149 if (type != null) {
150 try {
151 return Class.forName(type, true, Thread.currentThread().getContextClassLoader()).newInstance();
152 } catch (Throwable t) {
153 throw new FaceletException("Could not instantiate feature["
154 + name + "]: " + type);
155 }
156 }
157 return null;
158 }
159
160 public final TagLibrary createTagLibrary() {
161 if (this.libraries.size() > 0) {
162 return new CompositeTagLibrary((TagLibrary[]) this.libraries
163 .toArray(new TagLibrary[this.libraries.size()]));
164 }
165 return EMPTY_LIBRARY;
166 }
167
168 public final void addTagLibrary(TagLibrary library) {
169 ParameterCheck.notNull("library", library);
170 if (!this.libraries.contains(library)) {
171 this.libraries.add(library);
172 }
173 }
174
175 public final void setFeature(String name, String value) {
176 this.features.put(name, value);
177 }
178
179 public final String getFeature(String name) {
180 return (String) this.features.get(name);
181 }
182
183 public final boolean isTrimmingComments() {
184 return this.trimmingComments;
185 }
186
187 public final void setTrimmingComments(boolean trimmingComments) {
188 this.trimmingComments = trimmingComments;
189 }
190
191 public final boolean isTrimmingWhitespace() {
192 return this.trimmingWhitespace;
193 }
194
195 public final void setTrimmingWhitespace(boolean trimmingWhitespace) {
196 this.trimmingWhitespace = trimmingWhitespace;
197 }
198
199 public final boolean isValidating() {
200 return this.validating;
201 }
202
203 public final void setValidating(boolean validating) {
204 this.validating = validating;
205 }
206 }