KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > WriteTag


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4 import javax.servlet.jsp.PageContext JavaDoc;
5
6 import org.apache.struts.taglib.html.Constants;
7 import org.apache.struts.util.ResponseUtils;
8
9 import fr.improve.struts.taglib.layout.el.Expression;
10 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
11 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
12 import fr.improve.struts.taglib.layout.formatter.FormatException;
13 import fr.improve.struts.taglib.layout.util.LayoutUtils;
14 import fr.improve.struts.taglib.layout.util.NestedHelper;
15 import fr.improve.struts.taglib.layout.util.ParentFinder;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17
18 /**
19  * Insert the type's description here.
20  * Creation date: (12/09/2001 11:58:27)
21  * @author: JeanNoŽl Ribette
22  */

23 public class WriteTag extends org.apache.struts.taglib.bean.WriteTag implements LayoutTag {
24     protected String JavaDoc type;
25     protected String JavaDoc styleClass;
26     protected String JavaDoc style;
27     protected boolean layout = true;
28     
29     protected String JavaDoc jspProperty;
30     protected String JavaDoc jspName;
31     
32     protected static Class JavaDoc[] parameters = { PageContext JavaDoc.class, Object JavaDoc.class };
33     
34     public WriteTag() {
35         super();
36         name = Constants.BEAN_KEY;
37     }
38   
39     public void setLayout(boolean in_b) {
40         layout = in_b;
41     }
42
43     public boolean isLayout() {
44         return layout;
45     }
46     public String JavaDoc getStyle() {
47         return style;
48     }
49
50     public void setStyle(String JavaDoc in_style) {
51         this.style = in_style;
52     }
53
54     public String JavaDoc getStyleClass() {
55         return styleClass;
56     }
57
58     public void setStyleClass(String JavaDoc in_styleClass) {
59         this.styleClass = in_styleClass;
60     }
61   
62   public final int doStartTag() throws JspException JavaDoc {
63     // Evaluate property.
64
jspProperty = property;
65     property = Expression.evaluate(property, pageContext); // EL support
66
property = NestedHelper.getAdjustedProperty(property, pageContext); // Nested support
67

68     // Evaluate name.
69
jspName = name;
70     name = Expression.evaluate(name, pageContext);
71     
72     // Register the tag.
73
ParentFinder.registerTag(pageContext, this);
74     
75     // Start the tag.
76
return doStartLayoutTag();
77   }
78   
79   public final int doEndTag() throws JspException JavaDoc {
80     try {
81         // End the tag.
82
return doEndLayoutTag();
83     } finally {
84         // Deregister the tag.
85
ParentFinder.deregisterTag(pageContext);
86         
87         // Restore the attributes.
88
property = jspProperty;
89         jspProperty = null;
90         name = jspName;
91         jspName = null;
92     }
93   }
94   
95   public int doEndLayoutTag() throws JspException JavaDoc {
96     return super.doEndTag();
97   }
98
99     /**
100      * Process the start tag.
101      *
102      * @exception JspException if a JSP exception has occurred
103      */

104     public int doStartLayoutTag() throws JspException JavaDoc {
105         doStartLayout();
106
107         String JavaDoc lc_value = write(pageContext, name, property, type, scope);
108         if (filter) {
109             TagUtils.write(pageContext, ResponseUtils.filter(lc_value));
110         } else {
111             TagUtils.write(pageContext, lc_value);
112         }
113
114         doEndLayout();
115         // Continue processing this page
116
return (SKIP_BODY);
117     }
118     /**
119      * Print the required HTML code so that the tag can be nested in panel and line tags.<br>
120      * This won't do anything if styleClass is null.
121      */

122     private void doStartLayout() throws JspException JavaDoc {
123         if (layout) {
124             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc("<th");
125             if (styleClass!=null) {
126                 lc_buffer.append(" class=\"");
127                 lc_buffer.append(styleClass);
128                 lc_buffer.append("\"");
129             }
130             if (style!=null) {
131                 lc_buffer.append(" style=\"");
132                 lc_buffer.append(style);
133                 lc_buffer.append("\"");
134             }
135             
136             lc_buffer.append(" colspan=\"");
137             lc_buffer.append(LayoutUtils.getSkin(pageContext.getSession()).getFieldInterface().getColumnNumber());
138             lc_buffer.append("\">");
139             new StartLayoutEvent(this, lc_buffer.toString()).send();
140         }
141     }
142     
143     /**
144      * Print the required HTML code so that the tag can be nested in panel and line tags.<br>
145      * This won't do anything if styleClass is null.
146      */

147     private void doEndLayout() throws JspException JavaDoc {
148         if (layout) {
149             new EndLayoutEvent(this,"</th>").send();
150         }
151     }
152     public void release() {
153         super.release();
154         this.type = null;
155         this.style = null;
156         this.styleClass = null;
157         this.name = Constants.BEAN_KEY;
158         layout = true;
159     }
160     public void setType(String JavaDoc in_type) {
161         this.type = in_type;
162     }
163 public static String JavaDoc write(PageContext JavaDoc pageContext, Object JavaDoc value, String JavaDoc type) throws JspException JavaDoc {
164     if (value == null) {
165         return ""; // Nothing to output
166
}
167     if (type==null || type.length()==0) return value.toString();
168     
169     // Format the value
170
String JavaDoc lc_formattedValue = null;
171     try {
172         lc_formattedValue = LayoutUtils.getSkin(pageContext.getSession()).getFormatter().format(value, type, pageContext);
173     } catch (FormatException fe) {
174         throw new JspException JavaDoc("Format " + type + " failed (" + fe.getMessage() + ")");
175     }
176     return (lc_formattedValue == null ? "" : lc_formattedValue);
177 }
178 public static String JavaDoc write(PageContext JavaDoc pageContext, Object JavaDoc bean, String JavaDoc property, String JavaDoc type) throws JspException JavaDoc {
179     // Look up the requested property value
180
Object JavaDoc value = null;
181     try {
182         value = fr.improve.struts.taglib.layout.util.LayoutUtils.getProperty(bean, property);
183     } catch (JspException JavaDoc e) {
184         return ""; // Nothing to output
185
}
186     return write(pageContext, value, type);
187 }
188 public static String JavaDoc write(PageContext JavaDoc pageContext, String JavaDoc name, String JavaDoc property, String JavaDoc type, String JavaDoc scope) throws JspException JavaDoc {
189     Object JavaDoc bean = pageContext.findAttribute(name);
190     return write(pageContext, bean, property, type);
191     }
192
193   public PageContext JavaDoc getPageContext() {
194     return pageContext;
195   }
196
197 }
198
Popular Tags