1 5 package org.exoplatform.text.template.xhtml; 6 7 import java.io.IOException ; 8 import java.io.Writer ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.MissingResourceException ; 12 import java.util.ResourceBundle ; 13 import org.exoplatform.text.template.*; 14 19 abstract public class Element { 20 private List elements_ = new ArrayList () ; 21 protected Element[] children_ ; 22 private boolean locked_ = false ; 23 protected String cssClass_ ; 24 protected String cssStyle_ ; 25 protected Class dataHandlerType_ ; 26 protected ObjectFormater formater_ ; 27 28 public Class getDataHandlerType() { return dataHandlerType_ ; } 29 public Element setDataHandlerType(Class type) { 30 dataHandlerType_ = type ; 31 return this ; 32 } 33 34 public Element setFomater(ObjectFormater formater) { 35 formater_ = formater ; 36 return this ; 37 } 38 39 public String getCssClass() { return cssClass_ ; } 40 public Element setCssClass(String css) { 41 cssClass_ = css ; 42 return this ; 43 } 44 45 public String getStyle() { return cssStyle_ ; } 46 public Element setStyle(String style) { 47 cssStyle_ = style ; 48 return this ; 49 } 50 51 public void addElement(Element element) { 52 if(locked_) 53 throw new RuntimeException ("The element has been optimized, rebuild the tree") ; 54 elements_.add(element) ; 55 } 56 57 public Element add(Element element) { 58 if(locked_) 59 throw new RuntimeException ("The element has been optimized, rebuild the tree") ; 60 elements_.add(element) ; 61 return this ; 62 } 63 64 final public Element optimize() { 65 optimize(null) ; 66 return this ; 67 } 68 69 final public Element optimize(Element parent) { 70 children_ = new Element[elements_.size()] ; 71 if(dataHandlerType_ == null && parent != null) { 72 dataHandlerType_ = parent.getDataHandlerType() ; 73 } 74 75 for(int i = 0; i < children_.length; i++) { 76 children_[i] = (Element) elements_.get(i) ; 77 children_[i].optimize(this) ; 78 } 79 locked_ = true ; 80 elements_ = null ; 81 return this ; 82 } 83 84 static protected String resolveValueAsString(Value value, DataHandler handler , ResourceBundle res ) { 85 if(value instanceof ResourceBindingValue) { 86 ResourceBindingValue rv = (ResourceBindingValue) value ; 87 try { 88 return res.getString(rv.getKey()) ; 89 } catch (MissingResourceException ex) { 90 return rv.getExpression() ; 91 } 92 } else if(value instanceof DataBindingValue) { 93 return handler.getValueAsString((DataBindingValue) value) ; 94 } 95 return ((StringValue)value).getValue() ; 96 } 97 98 abstract public void render(XhtmlDataHandlerManager manager, 99 ResourceBundle res, Writer w) throws IOException ; 100 } | Popular Tags |