KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > html > BaseModelTag


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.view.jsp.html;
8
9
10 import javax.servlet.jsp.JspException JavaDoc;
11
12 import com.inversoft.util.StringTools;
13 import com.inversoft.verge.mvc.view.HtmlViewToolkit;
14 import com.inversoft.verge.mvc.view.jsp.JspTools;
15
16
17 /**
18  * <p>
19  * This class is the base class for all model tags in the
20  * Inversoft Portal framework or custom code written on top
21  * of the framework.
22  * </p>
23  *
24  * <p>
25  * HTML tags do not use the model attribute, but because Java
26  * is lame about multiple inheritance, I had to make a base
27  * class in this package so that I did not duplicate work all
28  * over the place.
29  * </p>
30  *
31  * @author Brian Pontarelli
32  * @since 2.0
33  * @version 2.0
34  */

35 public class BaseModelTag extends HtmlBaseTag {
36
37     private String JavaDoc model;
38     protected String JavaDoc localModel;
39     private Object JavaDoc value;
40     protected Object JavaDoc localValue;
41     protected boolean getValue = true;
42     protected boolean setValue = true;
43     protected String JavaDoc key;
44     protected String JavaDoc property;
45
46
47     /**
48      * Retrieves the tags model attribute
49      *
50      * @return The tags model attribute
51      */

52     public String JavaDoc getModel() {
53         return model;
54     }
55
56     /**
57      * Populates the tags model attribute
58      *
59      * @param model The tags model attribute
60      */

61     public void setModel(String JavaDoc model) {
62         this.model = model;
63     }
64
65     /**
66      * Retrieves the tags value attribute
67      *
68      * @return Returns the tags value attribute
69      */

70     public Object JavaDoc getValue() {
71         return value;
72     }
73
74     /**
75      * Populates the tags value attribute
76      *
77      * @param value The value of the tags value attribute
78      */

79     public void setValue(Object JavaDoc value) {
80         this.value = value;
81     }
82
83     /**
84      * <p>
85      * Returns the local value. This is the value that is the result of any
86      * expansion, modifications, model fetching, etc. This can be used by
87      * other tags or other APIs. The getValue method MUST NEVER BE USED except
88      * by the container.
89      * </p>
90      *
91      * <p>
92      * <font color="red" size="4">NOTE: This will return null if used prior to
93      * {@link #initialize() initialize()} being called.
94      * </p>
95      */

96     public Object JavaDoc getLocalValue() {
97         return localValue;
98     }
99
100     /**
101      * Retrieves the tags getValue attribute
102      *
103      * @return The tags getValue attribute
104      */

105     public boolean isGetValue() {
106         return getValue;
107     }
108
109     /**
110      * Populates the tags getValue attribute
111      *
112      * @param getValue The tags getValue attribute
113      */

114     public void setGetValue(boolean getValue) {
115         this.getValue = getValue;
116     }
117
118     /**
119      * Retrieves the tags setValue attribute
120      *
121      * @return The tags setValue attribute
122      */

123     public boolean isSetValue() {
124         return setValue;
125     }
126
127     /**
128      * Populates the tags setValue attribute
129      *
130      * @param setValue The tags setValue attribute
131      */

132     public void setSetValue(boolean setValue) {
133         this.setValue = setValue;
134     }
135
136     /**
137      * Initializes the tag but does not do any retrieval of the models value or
138      * any work on the model definition property (getModel()). If any sub-classes
139      * want to do this work, they must implement it themselves. They can also
140      * call the {@link #initializeKeyProperty() initializeKeyProperty} method
141      * on this class to break apart the key and the property within the model
142      * definition.
143      */

144     protected void initialize() throws JspException JavaDoc {
145         super.initialize();
146
147         localModel = model;
148         localValue = value;
149
150         // Expand the attributes if not JSP 2.0+.
151
if (!JspTools.JSP_20) {
152             localModel = (String JavaDoc) JspTools.expand("model", model, String JavaDoc.class,
153                 this, pageContext);
154
155             if (value instanceof String JavaDoc) {
156                 localValue = JspTools.expand("value", (String JavaDoc) value, Object JavaDoc.class,
157                     this, pageContext);
158             }
159         }
160
161         // Initialize the name (if needed).
162
if (!hasName()) {
163             localName = HtmlViewToolkit.createUniqueName();
164         }
165     }
166
167     /**
168      * Initializes the key and property using the model attribute. This is not
169      * a required call in order to initailize this class. Only sub-classes that
170      * wish to use the functionality of keys and properties need to call this
171      */

172     protected void initializeKeyProperty() throws JspException JavaDoc {
173         int index = localModel.indexOf(".");
174         if (index == -1) {
175             throw new JspException JavaDoc("Invalid model attribute: " + localModel +
176                 ". The model attribute should contain a property definition");
177         }
178
179         key = localModel.substring(0, index);
180         property = localModel.substring(index + 1);
181         if (StringTools.isEmpty(key) || StringTools.isEmpty(property)) {
182             throw new JspException JavaDoc("Invalid model attribute '" + localModel +
183                 "'. The model attribute should contain a bean and property " +
184                 "definition");
185         }
186     }
187
188     /**
189      * Specification method. This calls cleanUp.
190      *
191      * @see javax.servlet.jsp.tagext.Tag#release()
192      */

193     public void release() {
194         super.release(); // Calls cleanUp
195
setModel(null);
196         setValue(null);
197         setGetValue(true);
198         setSetValue(true);
199     }
200 }
Popular Tags