View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.compiler;
20  
21  import java.io.IOException;
22  import java.net.URL;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.logging.Level;
28  import java.util.logging.Logger;
29  
30  import javax.el.ELException;
31  import javax.el.ExpressionFactory;
32  import javax.faces.FacesException;
33  import javax.faces.context.FacesContext;
34  import javax.faces.view.facelets.FaceletException;
35  import javax.faces.view.facelets.FaceletHandler;
36  import javax.faces.view.facelets.TagDecorator;
37  
38  import org.apache.myfaces.view.facelets.tag.CompositeTagDecorator;
39  import org.apache.myfaces.view.facelets.tag.CompositeTagLibrary;
40  import org.apache.myfaces.view.facelets.tag.TagLibrary;
41  import org.apache.myfaces.view.facelets.tag.ui.UILibrary;
42  import org.apache.myfaces.view.facelets.util.ParameterCheck;
43  import org.apache.myfaces.view.facelets.util.ReflectionUtil;
44  
45  /**
46   * A Compiler instance may handle compiling multiple sources
47   * 
48   * @author Jacob Hookom
49   * @version $Id: Compiler.java 1300254 2012-03-13 17:38:40Z lu4242 $
50   */
51  public abstract class Compiler
52  {
53  
54      //protected final static Logger log = Logger.getLogger("facelets.compiler");
55      protected final static Logger log = Logger.getLogger(Compiler.class.getName());
56  
57      public final static String EXPRESSION_FACTORY = "compiler.ExpressionFactory";
58  
59      private static final TagLibrary EMPTY_LIBRARY = new CompositeTagLibrary(new TagLibrary[0]);
60  
61      private static final TagDecorator EMPTY_DECORATOR = new CompositeTagDecorator(new TagDecorator[0]);
62  
63      private boolean validating = false;
64  
65      private boolean trimmingWhitespace = false;
66  
67      private boolean trimmingComments = false;
68  
69      private final List<TagLibrary> libraries = new ArrayList<TagLibrary>();
70  
71      private final List<TagDecorator> decorators = new ArrayList<TagDecorator>();
72  
73      private final Map<String, String> features = new HashMap<String, String>();
74  
75      private boolean initialized = false;
76      
77      private boolean developmentProjectStage = false;
78  
79      /**
80       * 
81       */
82      public Compiler()
83      {
84  
85      }
86  
87      private synchronized void initialize()
88      {
89          if (this.initialized)
90          {
91              return;
92          }
93          log.fine("Initializing");
94          try
95          {
96              TagLibraryConfig cfg = new TagLibraryConfig();
97              cfg.loadImplicit(FacesContext.getCurrentInstance(), this);
98  
99              if (!this.createTagLibrary().containsNamespace(UILibrary.Namespace))
100             {
101                 log.severe("Missing Built-in Tag Libraries! Make sure they are included within "
102                            + "the META-INF directory of Facelets' Jar");
103             }
104 
105         }
106         catch (IOException e)
107         {
108             log.log(Level.SEVERE, "Compiler Initialization Error", e);
109         }
110         finally
111         {
112             this.initialized = true;
113         }
114         log.fine("Initialization Successful");
115     }
116 
117     public final FaceletHandler compile(URL src, String alias) throws IOException, FaceletException, ELException,
118             FacesException
119     {
120         if (!this.initialized)
121         {
122             this.initialize();
123         }
124         return this.doCompile(src, alias);
125     }
126     
127     public final FaceletHandler compileViewMetadata(URL src, String alias)
128             throws IOException, FaceletException, ELException, FacesException
129     {
130         if (!this.initialized)
131         {
132             this.initialize();
133         }
134         return this.doCompileViewMetadata(src, alias);
135     }
136     
137     public final FaceletHandler compileCompositeComponentMetadata(URL src, String alias)
138             throws IOException, FaceletException, ELException, FacesException
139     {
140         if (!this.initialized)
141         {
142             this.initialize();
143         }
144         return this.doCompileCompositeComponentMetadata(src, alias);
145     }
146 
147     protected abstract FaceletHandler doCompile(URL src, String alias)
148             throws IOException, FaceletException, ELException, FacesException;
149 
150     protected abstract FaceletHandler doCompileViewMetadata(URL src, String alias)
151             throws IOException, FaceletException, ELException, FacesException;
152     
153     protected abstract FaceletHandler doCompileCompositeComponentMetadata(URL src, String alias)
154             throws IOException, FaceletException, ELException, FacesException;
155     
156     public final TagDecorator createTagDecorator()
157     {
158         if (this.decorators.size() > 0)
159         {
160             return new CompositeTagDecorator(this.decorators.toArray(new TagDecorator[this.decorators.size()]));
161         }
162         return EMPTY_DECORATOR;
163     }
164 
165     public final void addTagDecorator(TagDecorator decorator)
166     {
167         ParameterCheck.notNull("decorator", decorator);
168         if (!this.decorators.contains(decorator))
169         {
170             this.decorators.add(decorator);
171         }
172     }
173 
174     public final ExpressionFactory createExpressionFactory()
175     {
176         ExpressionFactory el = null;
177         el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
178         if (el == null)
179         {
180             try
181             {
182                 el = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
183                 if (el == null)
184                 {
185                     log.warning("No default ExpressionFactory from Faces Implementation, "
186                                 + "attempting to load from Feature["
187                                 + EXPRESSION_FACTORY + "]");
188                 }
189             }
190             catch (Exception e)
191             {
192                 // do nothing
193             }
194         }
195         
196         return el;
197     }
198 
199     private final Object featureInstance(String name)
200     {
201         String type = (String) this.features.get(name);
202         if (type != null)
203         {
204             try
205             {
206                 return ReflectionUtil.forName(type).newInstance();
207             }
208             catch (Throwable t)
209             {
210                 throw new FaceletException("Could not instantiate feature[" + name + "]: " + type);
211             }
212         }
213         return null;
214     }
215 
216     public final TagLibrary createTagLibrary()
217     {
218         if (this.libraries.size() > 0)
219         {
220             return new CompositeTagLibrary(this.libraries.toArray(new TagLibrary[this.libraries.size()]));
221         }
222         return EMPTY_LIBRARY;
223     }
224 
225     public final void addTagLibrary(TagLibrary library)
226     {
227         ParameterCheck.notNull("library", library);
228         if (!this.libraries.contains(library))
229         {
230             this.libraries.add(library);
231         }
232     }
233 
234     public final void setFeature(String name, String value)
235     {
236         this.features.put(name, value);
237     }
238 
239     public final String getFeature(String name)
240     {
241         return (String) this.features.get(name);
242     }
243 
244     public final boolean isTrimmingComments()
245     {
246         return this.trimmingComments;
247     }
248 
249     public final void setTrimmingComments(boolean trimmingComments)
250     {
251         this.trimmingComments = trimmingComments;
252     }
253 
254     public final boolean isTrimmingWhitespace()
255     {
256         return this.trimmingWhitespace;
257     }
258 
259     public final void setTrimmingWhitespace(boolean trimmingWhitespace)
260     {
261         this.trimmingWhitespace = trimmingWhitespace;
262     }
263 
264     public final boolean isValidating()
265     {
266         return this.validating;
267     }
268 
269     public final void setValidating(boolean validating)
270     {
271         this.validating = validating;
272     }
273     
274     public final boolean isDevelopmentProjectStage()
275     {
276         return this.developmentProjectStage;
277     }
278     
279     public final void setDevelopmentProjectStage(boolean developmentProjectStage)
280     {
281         this.developmentProjectStage = developmentProjectStage;
282     }
283 }
284