1 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 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 boolean trimmingXmlDeclarations = false; 66 67 private boolean trimmingDoctypeDeclarations = false; 68 69 private final List libraries = new ArrayList (); 70 71 private final List decorators = new ArrayList (); 72 73 private final Map features = new HashMap (); 74 75 private boolean initialized = false; 76 77 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 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 src, String alias) 105 throws IOException , FaceletException, ELException, FacesException { 106 if (!this.initialized) 107 this.initialize(); 108 return this.doCompile(src, alias); 109 } 110 111 protected abstract FaceletHandler doCompile(URL src, String alias) 112 throws IOException , 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 e) { 141 } 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 featureInstance(String name) { 152 String type = (String ) this.features.get(name); 153 if (type != null) { 154 try { 155 return Class.forName(type, true, Thread.currentThread().getContextClassLoader()).newInstance(); 156 } catch (Throwable 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 name, String value) { 180 this.features.put(name, value); 181 } 182 183 public final String getFeature(String name) { 184 return (String ) 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 |