KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > text > template > xhtml > Element


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.text.template.xhtml;
6
7 import java.io.IOException JavaDoc;
8 import java.io.Writer JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.MissingResourceException JavaDoc;
12 import java.util.ResourceBundle JavaDoc;
13 import org.exoplatform.text.template.*;
14 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Feb 1, 2005
17  * @version $Id$
18  */

19 abstract public class Element {
20   private List JavaDoc elements_ = new ArrayList JavaDoc() ;
21   protected Element[] children_ ;
22   private boolean locked_ = false ;
23   protected String JavaDoc cssClass_ ;
24   protected String JavaDoc cssStyle_ ;
25   protected Class JavaDoc dataHandlerType_ ;
26   protected ObjectFormater formater_ ;
27   
28   public Class JavaDoc getDataHandlerType() { return dataHandlerType_ ; }
29   public Element setDataHandlerType(Class JavaDoc 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 JavaDoc getCssClass() { return cssClass_ ; }
40   public Element setCssClass(String JavaDoc css) {
41     cssClass_ = css ;
42     return this ;
43   }
44   
45   public String JavaDoc getStyle() { return cssStyle_ ; }
46   public Element setStyle(String JavaDoc style) {
47     cssStyle_ = style ;
48     return this ;
49   }
50   
51   public void addElement(Element element) {
52     if(locked_)
53       throw new RuntimeException JavaDoc("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 JavaDoc("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 JavaDoc resolveValueAsString(Value value, DataHandler handler , ResourceBundle JavaDoc res ) {
85     if(value instanceof ResourceBindingValue) {
86       ResourceBindingValue rv = (ResourceBindingValue) value ;
87       try {
88         return res.getString(rv.getKey()) ;
89       } catch (MissingResourceException JavaDoc 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 JavaDoc res, Writer JavaDoc w) throws IOException JavaDoc ;
100 }
Popular Tags