001: /**
002: * Licensed under the Common Development and Distribution License,
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.sun.com/cddl/
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * permissions and limitations under the License.
013: */package com.sun.facelets.compiler;
014:
015: import java.io.IOException;
016: import java.net.URL;
017: import java.util.ArrayList;
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023:
024: import javax.el.ELException;
025: import javax.el.ExpressionFactory;
026: import javax.faces.FacesException;
027: import javax.faces.context.FacesContext;
028:
029: import com.sun.facelets.FaceletException;
030: import com.sun.facelets.FaceletHandler;
031: import com.sun.facelets.tag.CompositeTagDecorator;
032: import com.sun.facelets.tag.CompositeTagLibrary;
033: import com.sun.facelets.tag.TagDecorator;
034: import com.sun.facelets.tag.TagLibrary;
035: import com.sun.facelets.tag.ui.UILibrary;
036: import com.sun.facelets.util.ParameterCheck;
037: import com.sun.facelets.util.FacesAPI;
038: import com.sun.facelets.util.ReflectionUtil;
039:
040: /**
041: * A Compiler instance may handle compiling multiple sources
042: *
043: * @author Jacob Hookom
044: * @version $Id: Compiler.java,v 1.15 2006/10/19 03:48:12 jhook Exp $
045: */
046: public abstract class Compiler {
047:
048: protected final static Logger log = Logger
049: .getLogger("facelets.compiler");
050:
051: public final static String EXPRESSION_FACTORY = "compiler.ExpressionFactory";
052:
053: private static final TagLibrary EMPTY_LIBRARY = new CompositeTagLibrary(
054: new TagLibrary[0]);
055:
056: private static final TagDecorator EMPTY_DECORATOR = new CompositeTagDecorator(
057: new TagDecorator[0]);
058:
059: private boolean validating = false;
060:
061: private boolean trimmingWhitespace = false;
062:
063: private boolean trimmingComments = false;
064:
065: private final List libraries = new ArrayList();
066:
067: private final List decorators = new ArrayList();
068:
069: private final Map features = new HashMap();
070:
071: private boolean initialized = false;
072:
073: /**
074: *
075: */
076: public Compiler() {
077:
078: }
079:
080: private synchronized void initialize() {
081: if (this .initialized)
082: return;
083: log.fine("Initializing");
084: try {
085: TagLibraryConfig cfg = new TagLibraryConfig();
086: cfg.loadImplicit(this );
087:
088: if (!this .createTagLibrary().containsNamespace(
089: UILibrary.Namespace)) {
090: log
091: .severe("Missing Built-in Tag Libraries! Make sure they are included within the META-INF directory of Facelets' Jar");
092: }
093:
094: } catch (IOException e) {
095: log.log(Level.SEVERE, "Compiler Initialization Error", e);
096: } finally {
097: this .initialized = true;
098: }
099: log.fine("Initialization Successful");
100: }
101:
102: public final FaceletHandler compile(URL src, String alias)
103: throws IOException, FaceletException, ELException,
104: FacesException {
105: if (!this .initialized)
106: this .initialize();
107: return this .doCompile(src, alias);
108: }
109:
110: protected abstract FaceletHandler doCompile(URL src, String alias)
111: throws IOException, FaceletException, ELException,
112: FacesException;
113:
114: public final TagDecorator createTagDecorator() {
115: if (this .decorators.size() > 0) {
116: return new CompositeTagDecorator(
117: (TagDecorator[]) this .decorators
118: .toArray(new TagDecorator[this .decorators
119: .size()]));
120: }
121: return EMPTY_DECORATOR;
122: }
123:
124: public final void addTagDecorator(TagDecorator decorator) {
125: ParameterCheck.notNull("decorator", decorator);
126: if (!this .decorators.contains(decorator)) {
127: this .decorators.add(decorator);
128: }
129: }
130:
131: public final ExpressionFactory createExpressionFactory() {
132: ExpressionFactory el = null;
133: el = (ExpressionFactory) this
134: .featureInstance(EXPRESSION_FACTORY);
135: if (el == null && FacesAPI.getVersion() >= 12) {
136: try {
137: el = FacesContext.getCurrentInstance().getApplication()
138: .getExpressionFactory();
139: if (el == null) {
140: log
141: .warning("No default ExpressionFactory from Faces Implementation, attempting to load from Feature["
142: + EXPRESSION_FACTORY + "]");
143: }
144: } catch (Exception e) {
145: // do nothing
146: }
147: }
148: if (el == null) {
149: this .features.put(EXPRESSION_FACTORY,
150: "com.sun.el.ExpressionFactoryImpl");
151: el = (ExpressionFactory) this
152: .featureInstance(EXPRESSION_FACTORY);
153: }
154: return el;
155: }
156:
157: private final Object featureInstance(String name) {
158: String type = (String) this .features.get(name);
159: if (type != null) {
160: try {
161: return ReflectionUtil.forName(type).newInstance();
162: } catch (Throwable t) {
163: throw new FaceletException(
164: "Could not instantiate feature[" + name + "]: "
165: + type);
166: }
167: }
168: return null;
169: }
170:
171: public final TagLibrary createTagLibrary() {
172: if (this .libraries.size() > 0) {
173: return new CompositeTagLibrary(
174: (TagLibrary[]) this .libraries
175: .toArray(new TagLibrary[this .libraries
176: .size()]));
177: }
178: return EMPTY_LIBRARY;
179: }
180:
181: public final void addTagLibrary(TagLibrary library) {
182: ParameterCheck.notNull("library", library);
183: if (!this .libraries.contains(library)) {
184: this .libraries.add(library);
185: }
186: }
187:
188: public final void setFeature(String name, String value) {
189: this .features.put(name, value);
190: }
191:
192: public final String getFeature(String name) {
193: return (String) this .features.get(name);
194: }
195:
196: public final boolean isTrimmingComments() {
197: return this .trimmingComments;
198: }
199:
200: public final void setTrimmingComments(boolean trimmingComments) {
201: this .trimmingComments = trimmingComments;
202: }
203:
204: public final boolean isTrimmingWhitespace() {
205: return this .trimmingWhitespace;
206: }
207:
208: public final void setTrimmingWhitespace(boolean trimmingWhitespace) {
209: this .trimmingWhitespace = trimmingWhitespace;
210: }
211:
212: public final boolean isValidating() {
213: return this .validating;
214: }
215:
216: public final void setValidating(boolean validating) {
217: this.validating = validating;
218: }
219: }
|