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.Collection;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.logging.Level;
29  import java.util.logging.Logger;
30  
31  import javax.el.ELException;
32  import javax.el.ExpressionFactory;
33  import javax.faces.FacesException;
34  import javax.faces.context.FacesContext;
35  import javax.faces.view.facelets.FaceletException;
36  import javax.faces.view.facelets.FaceletHandler;
37  import javax.faces.view.facelets.TagDecorator;
38  
39  import org.apache.myfaces.config.element.FaceletsProcessing;
40  import org.apache.myfaces.view.facelets.tag.CompositeTagDecorator;
41  import org.apache.myfaces.view.facelets.tag.CompositeTagLibrary;
42  import org.apache.myfaces.view.facelets.tag.TagLibrary;
43  import org.apache.myfaces.view.facelets.tag.ui.UILibrary;
44  import org.apache.myfaces.view.facelets.util.ParameterCheck;
45  import org.apache.myfaces.view.facelets.util.ReflectionUtil;
46  
47  /**
48   * A Compiler instance may handle compiling multiple sources
49   * 
50   * @author Jacob Hookom
51   * @version $Id: Compiler.java 1300255 2012-03-13 17:38:54Z lu4242 $
52   */
53  public abstract class Compiler
54  {
55  
56      //protected final static Logger log = Logger.getLogger("facelets.compiler");
57      protected final static Logger log = Logger.getLogger(Compiler.class.getName());
58  
59      public final static String EXPRESSION_FACTORY = "compiler.ExpressionFactory";
60  
61      private static final TagLibrary EMPTY_LIBRARY = new CompositeTagLibrary(new TagLibrary[0]);
62  
63      private static final TagDecorator EMPTY_DECORATOR = new CompositeTagDecorator(new TagDecorator[0]);
64  
65      private boolean validating = false;
66  
67      private boolean trimmingWhitespace = false;
68  
69      private boolean trimmingComments = false;
70  
71      private final List<TagLibrary> libraries = new ArrayList<TagLibrary>();
72  
73      private final List<TagDecorator> decorators = new ArrayList<TagDecorator>();
74  
75      private final Map<String, String> features = new HashMap<String, String>();
76  
77      private boolean initialized = false;
78      
79      private boolean developmentProjectStage = false;
80  
81      private Collection<FaceletsProcessing> faceletsProcessingConfigurations;
82  
83      /**
84       * 
85       */
86      public Compiler()
87      {
88  
89      }
90  
91      private synchronized void initialize()
92      {
93          if (this.initialized)
94          {
95              return;
96          }
97          log.fine("Initializing");
98          try
99          {
100             TagLibraryConfig cfg = new TagLibraryConfig();
101             cfg.loadImplicit(FacesContext.getCurrentInstance(), this);
102 
103             if (!this.createTagLibrary().containsNamespace(UILibrary.NAMESPACE))
104             {
105                 log.severe("Missing Built-in Tag Libraries! Make sure they are included within "
106                            + "the META-INF directory of Facelets' Jar");
107             }
108 
109         }
110         catch (IOException e)
111         {
112             log.log(Level.SEVERE, "Compiler Initialization Error", e);
113         }
114         finally
115         {
116             this.initialized = true;
117         }
118         log.fine("Initialization Successful");
119     }
120 
121     public final FaceletHandler compile(URL src, String alias) throws IOException, FaceletException, ELException,
122             FacesException
123     {
124         if (!this.initialized)
125         {
126             this.initialize();
127         }
128         return this.doCompile(src, alias);
129     }
130     
131     public final FaceletHandler compileViewMetadata(URL src, String alias)
132             throws IOException, FaceletException, ELException, FacesException
133     {
134         if (!this.initialized)
135         {
136             this.initialize();
137         }
138         return this.doCompileViewMetadata(src, alias);
139     }
140     
141     public final FaceletHandler compileCompositeComponentMetadata(URL src, String alias)
142             throws IOException, FaceletException, ELException, FacesException
143     {
144         if (!this.initialized)
145         {
146             this.initialize();
147         }
148         return this.doCompileCompositeComponentMetadata(src, alias);
149     }
150 
151     protected abstract FaceletHandler doCompile(URL src, String alias)
152             throws IOException, FaceletException, ELException, FacesException;
153 
154     protected abstract FaceletHandler doCompileViewMetadata(URL src, String alias)
155             throws IOException, FaceletException, ELException, FacesException;
156     
157     protected abstract FaceletHandler doCompileCompositeComponentMetadata(URL src, String alias)
158             throws IOException, FaceletException, ELException, FacesException;
159     
160     public final TagDecorator createTagDecorator()
161     {
162         if (this.decorators.size() > 0)
163         {
164             return new CompositeTagDecorator(this.decorators.toArray(new TagDecorator[this.decorators.size()]));
165         }
166         return EMPTY_DECORATOR;
167     }
168 
169     public final void addTagDecorator(TagDecorator decorator)
170     {
171         ParameterCheck.notNull("decorator", decorator);
172         if (!this.decorators.contains(decorator))
173         {
174             this.decorators.add(decorator);
175         }
176     }
177 
178     public final ExpressionFactory createExpressionFactory()
179     {
180         ExpressionFactory el = null;
181         el = (ExpressionFactory) this.featureInstance(EXPRESSION_FACTORY);
182         if (el == null)
183         {
184             try
185             {
186                 el = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
187                 if (el == null)
188                 {
189                     log.warning("No default ExpressionFactory from Faces Implementation, "
190                                 + "attempting to load from Feature["
191                                 + EXPRESSION_FACTORY + "]");
192                 }
193             }
194             catch (Exception e)
195             {
196                 // do nothing
197             }
198         }
199         
200         return el;
201     }
202 
203     private final Object featureInstance(String name)
204     {
205         String type = (String) this.features.get(name);
206         if (type != null)
207         {
208             try
209             {
210                 return ReflectionUtil.forName(type).newInstance();
211             }
212             catch (Throwable t)
213             {
214                 throw new FaceletException("Could not instantiate feature[" + name + "]: " + type);
215             }
216         }
217         return null;
218     }
219 
220     public final TagLibrary createTagLibrary()
221     {
222         if (this.libraries.size() > 0)
223         {
224             return new CompositeTagLibrary(this.libraries.toArray(new TagLibrary[this.libraries.size()]));
225         }
226         return EMPTY_LIBRARY;
227     }
228 
229     public final void addTagLibrary(TagLibrary library)
230     {
231         ParameterCheck.notNull("library", library);
232         if (!this.libraries.contains(library))
233         {
234             this.libraries.add(library);
235         }
236     }
237 
238     public final void setFeature(String name, String value)
239     {
240         this.features.put(name, value);
241     }
242 
243     public final String getFeature(String name)
244     {
245         return (String) this.features.get(name);
246     }
247 
248     public final boolean isTrimmingComments()
249     {
250         return this.trimmingComments;
251     }
252 
253     public final void setTrimmingComments(boolean trimmingComments)
254     {
255         this.trimmingComments = trimmingComments;
256     }
257 
258     public final boolean isTrimmingWhitespace()
259     {
260         return this.trimmingWhitespace;
261     }
262 
263     public final void setTrimmingWhitespace(boolean trimmingWhitespace)
264     {
265         this.trimmingWhitespace = trimmingWhitespace;
266     }
267 
268     public final boolean isValidating()
269     {
270         return this.validating;
271     }
272 
273     public final void setValidating(boolean validating)
274     {
275         this.validating = validating;
276     }
277     
278     public final boolean isDevelopmentProjectStage()
279     {
280         return this.developmentProjectStage;
281     }
282     
283     public final void setDevelopmentProjectStage(boolean developmentProjectStage)
284     {
285         this.developmentProjectStage = developmentProjectStage;
286     }
287 
288     /**
289      * 
290      * @since 2.1.0
291      * @return
292      */
293     public Collection<FaceletsProcessing> getFaceletsProcessingConfigurations()
294     {
295         return faceletsProcessingConfigurations;
296     }
297 
298     /**
299      * 
300      * @since 2.1.0
301      * @return
302      */
303     public void setFaceletsProcessingConfigurations(
304             Collection<FaceletsProcessing> faceletsProcessingConfigurations)
305     {
306         this.faceletsProcessingConfigurations = faceletsProcessingConfigurations;
307     }
308 }
309