KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > deprecated > taglibs > field > page > PageFieldTag


1 package org.jahia.deprecated.taglibs.field.page;
2
3 import java.io.IOException 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.containers.JahiaContainer;
12 import org.jahia.data.fields.FieldTypes;
13 import org.jahia.data.fields.JahiaField;
14 import org.jahia.deprecated.taglibs.container.ContainerListTag;
15 import org.jahia.deprecated.taglibs.container.ContainerTag;
16 import org.jahia.exceptions.JahiaException;
17 import org.jahia.services.pages.JahiaPage;
18 import org.jahia.utils.JahiaConsole;
19
20
21
22 /**
23  * Class PageFieldTag : retrieves a Jahia page field inside the current container,
24  * so that the enclosed tags such as <pageTitle> can access
25  * the page attributes and display their values.
26  *
27  * @author Jerome Tamiotti
28  */

29 public class PageFieldTag extends BodyTagSupport JavaDoc {
30
31
32     protected String JavaDoc name = "";
33     protected String JavaDoc title = "";
34     protected String JavaDoc value = "";
35     private int pageId = -1;
36     private int pageLevel = -1;
37
38     private boolean display = false;
39
40     private JahiaPage page = 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 void setPageId(String JavaDoc pageId) {
55         try {
56             this.pageId = Integer.parseInt(pageId);
57         } catch (NumberFormatException JavaDoc nfe) {
58             JahiaConsole.println("PageFieldTag: setPageId", "The given page id is not a number");
59         }
60     }
61
62     public void setPageLevel(String JavaDoc pageLevel) {
63         try {
64             this.pageLevel = Integer.parseInt(pageLevel);
65         } catch (NumberFormatException JavaDoc nfe) {
66             JahiaConsole.println("PageFieldTag: setPageLevel", "The given page level is not a number");
67         }
68     }
69
70     public JahiaPage getPage() {
71         return this.page;
72     }
73
74     public boolean displayBody() {
75         return display;
76     }
77
78
79     public int doStartTag() throws JspTagException JavaDoc {
80
81         // JahiaConsole.println("PageFieldTag.doStartTag", "called.");
82

83         if (this.title.equals("")) {
84             this.title = this.name;
85         }
86         ServletRequest JavaDoc request = pageContext.getRequest();
87
88         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
89
90         try {
91             // checks if the field is absolute
92
if (this.pageId == -1) {
93                 // check for page level
94
if (this.pageLevel != -1) {
95                  this.pageId = jData.gui().getLevelID(this.pageLevel);
96                 }
97             }
98         } catch (JahiaException je) {
99             JahiaConsole.printe("PageFieldTag.doStartTag:getLevelID call", je);
100         }
101
102         // checks if in declaration phase or if there are already container
103
JahiaContainer container = null;
104         // checks if we are inside a container
105
ContainerTag parent = (ContainerTag) findAncestorWithClass(this,ContainerTag.class);
106         if (parent != null) {
107             container = parent.getContainer();
108         }
109
110
111         JahiaField theField = null;
112         try {
113             // searches for the field that contains the page
114
if (parent == null) {
115                 // at root
116
theField = getField(jData);
117                 this.display = true;
118
119             } else {
120                 // in a container
121
ContainerListTag containerListTag = (ContainerListTag)findAncestorWithClass(this,
122                 ContainerListTag.class);
123                 if (containerListTag != null) {
124                         // declares the page as a field of the container list
125
if (containerListTag.isDeclarationPass()) {
126                         containerListTag.addField(this.name);
127                         // declares the field when it is not an absolute field,
128
// or when it is an absolute one and the current page is the home page
129
if ( this.pageId == -1 || this.pageId == jData.page().getID() ) {
130                             // and when it's not already declared
131
jData.containers().declareField( containerListTag.getName(), this.name, this.title, FieldTypes.PAGE, this.value );
132                         }
133                         this.display = false;
134                         return EVAL_BODY_BUFFERED;
135                     }
136                 }
137                 if (parent.displayBody()) {
138                     // must display the field of the current container
139
if (container != null) {
140                         theField = container.getField(this.name);
141                         this.display = true;
142                     }
143                 }
144             }
145             if (theField != null) {
146                 this.page = (JahiaPage) theField.getObject();
147                 if ( this.display ) {
148                     return EVAL_BODY_BUFFERED;
149                 }
150             }
151         } catch (JahiaException je) {
152             JahiaConsole.println("PageFieldTag: doStartTag ", je.toString());
153         } catch (NullPointerException JavaDoc npe) {
154             JahiaConsole.println("PageFieldTag: doStartTag ", npe.toString());
155         }
156         return SKIP_BODY;
157     }
158
159
160     public int doAfterBody() throws JspException JavaDoc {
161
162         if (this.display) {
163             try {
164                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
165             } catch (IOException JavaDoc ioe) {
166                 throw new JspTagException JavaDoc();
167             }
168         } else {
169             bodyContent.clearBody();
170         }
171         return SKIP_BODY;
172     }
173
174
175     private JahiaField getField(JahiaData jData) throws JahiaException {
176
177         if ( this.pageId == -1 // the field is not absolute
178
|| jData.page().getID()== this.pageId ) {
179              // the current page is the page that declares the absolute field
180

181              // then we have to check if the field is already declared
182
if ( !jData.fields().checkDeclared(this.name) ) {
183                 // declare it now !
184
jData.fields().declareField( this.name, this.title, FieldTypes.PAGE, this.value );
185              }
186              return jData.fields().getField(this.name);
187         }
188         // according to the previous tests, the only remaining case is
189
// when the field is absolute and the current page is not the declaring page,
190
// so return an absolute field of another page
191
return jData.fields().getAbsoluteField(this.name, this.pageId);
192     }
193
194     public int doEndTag() throws JspException JavaDoc {
195         // let's reinitialize the tag variables to allow tag object reuse in
196
// pooling.
197
name = "";
198         title = "";
199         value = "";
200         pageId = -1;
201         pageLevel = -1;
202
203         display = false;
204
205         page = null;
206         return EVAL_PAGE;
207     }
208
209 }
210
Popular Tags