KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > declarations > DeclareFieldTag


1 package org.jahia.taglibs.declarations;
2
3 import java.util.Hashtable JavaDoc;
4
5 import javax.servlet.ServletRequest JavaDoc;
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.JspTagException JavaDoc;
8 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
9
10 import org.jahia.data.JahiaData;
11 import org.jahia.data.fields.FieldTypes;
12 import org.jahia.exceptions.JahiaException;
13 import org.jahia.resourcebundle.ResourceBundleMarker;
14
15 /**
16  * <p>Title: </p>
17  * <p>Description: </p>
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: </p>
20  * @author Serge Huber
21  * @version 1.0
22  * @todo FIXME add javadoc
23  * @todo implement resource bundle marker support
24  */

25
26 public class DeclareFieldTag extends BodyTagSupport JavaDoc {
27
28     private static org.apache.log4j.Logger logger =
29         org.apache.log4j.Logger.getLogger(DeclareFieldTag.class);
30
31     public DeclareFieldTag () {
32     }
33
34     protected String JavaDoc name = "";
35     private String JavaDoc title = "";
36     private String JavaDoc value = "";
37     private String JavaDoc type = null;
38     private String JavaDoc titleKey = null;
39     private String JavaDoc bundleKey = null;
40     private String JavaDoc valueKey = null;
41
42     public void setName (String JavaDoc name) {
43         this.name = name;
44     }
45
46     public void setTitle (String JavaDoc title) {
47         this.title = title;
48     }
49
50     public void setValue (String JavaDoc value) {
51         this.value = value;
52     }
53
54     public String JavaDoc getType () {
55         return type;
56     }
57
58     public void setType (String JavaDoc type) {
59         this.type = type;
60     }
61
62     public String JavaDoc getTitleKey () {
63         return titleKey;
64     }
65
66     public void setTitleKey (String JavaDoc titleKey) {
67         this.titleKey = titleKey;
68     }
69
70     public String JavaDoc getBundleKey () {
71         return bundleKey;
72     }
73
74     public void setBundleKey (String JavaDoc bundleKey) {
75         this.bundleKey = bundleKey;
76     }
77
78     public String JavaDoc getValueKey() {
79         return valueKey;
80     }
81
82     public void setValueKey(String JavaDoc valueKey) {
83         this.valueKey = valueKey;
84     }
85
86     public int doStartTag ()
87         throws JspTagException JavaDoc {
88
89         if (this.title.equals("")) {
90             this.title = this.name;
91         }
92
93         ServletRequest JavaDoc request = pageContext.getRequest();
94
95         JahiaData jData = (JahiaData) request.getAttribute(
96             "org.jahia.data.JahiaData");
97
98         // checks if we are inside a container
99
DeclareContainerTag parent = (DeclareContainerTag)
100                                      findAncestorWithClass(this,
101             DeclareContainerTag.class);
102         if (parent != null) {
103
104             DeclareContainerListTag declareContainerListTag = (
105                 DeclareContainerListTag) parent.getParent();
106             if (declareContainerListTag != null) {
107                 // must be declared as a field of the enclosing container list, if not already done
108
// 28.02.2002 NK, added the check bellow to avoid multiple declaration
109
// of the same field in the container list structure.
110
if (!declareContainerListTag.isChildAlreadyDeclared(this.name)) {
111
112                     declareContainerListTag.addChild(this.name);
113                     try {
114                         jData.containers().declareField( declareContainerListTag.getName(), this.name, resolveTitle(),
115                             resolveType(), resolveValue());
116                     } catch (JahiaException je) {
117                         logger.error("Field " + this.name +
118                                      " was already in container", je);
119                     }
120                 }
121                 return SKIP_BODY;
122             }
123
124         } else {
125             // the current page is the page that declares the absolute field
126
// then we have to check if the field is already declared
127
if (!jData.fields().checkDeclared(this.name)) {
128                 // declare it now !
129
try {
130                     jData.fields().declareField(this.name, resolveTitle(),
131                                                 resolveType(),
132                                                 resolveValue());
133                 } catch (JahiaException je) {
134                     logger.error(
135                         "Error while declaring field attached to page " +
136                         this.name + " : ", je);
137                 }
138             }
139
140         }
141
142         return SKIP_BODY;
143     }
144
145     private int resolveType () {
146         String JavaDoc typeName = getType();
147         Hashtable JavaDoc fieldTypes = FieldTypes.getInstance().getFieldTypes();
148         Integer JavaDoc fieldTypeInt = (Integer JavaDoc) fieldTypes.get(typeName);
149         return fieldTypeInt.intValue();
150     }
151
152     private String JavaDoc resolveTitle() {
153         if ((titleKey != null) && (bundleKey != null)) {
154             return ResourceBundleMarker.drawMarker(bundleKey, titleKey, title);
155         } else {
156             return title;
157         }
158     }
159
160     private String JavaDoc resolveValue() {
161         if ((valueKey != null) && (bundleKey != null)) {
162             return ResourceBundleMarker.drawMarker(bundleKey, valueKey, value);
163         } else {
164             return value;
165         }
166     }
167
168     public int doEndTag ()
169         throws JspException JavaDoc {
170         // let's reinitialize the tag variables to allow tag object reuse in
171
// pooling.
172
name = "";
173         title = "";
174         value = "";
175         type = "";
176         titleKey = null;
177         bundleKey = null;
178         valueKey = null;
179         return EVAL_PAGE;
180     }
181
182 }
183
Popular Tags