KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > xpath > TemplateTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.xtags.xpath;
18
19
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30
31 import org.dom4j.Node;
32 import org.dom4j.XPath;
33 import org.dom4j.DocumentHelper;
34 import org.dom4j.rule.Action;
35 import org.dom4j.rule.Pattern;
36 import org.dom4j.rule.Rule;
37 import org.dom4j.rule.RuleManager;
38
39 /** The body of this tag defines a stylesheet which is implemented via calling
40   * a JSP include.
41   *
42   * @author James Strachan
43   */

44 public class TemplateTag extends AbstractBodyTag {
45
46     /** Holds value of property name. */
47     private String JavaDoc name;
48     
49     /** Holds value of property mode. */
50     private String JavaDoc mode;
51     
52     /** Holds value of property priority. */
53     private double priority;
54     
55     /** Holds value of property jsp. */
56     private String JavaDoc jsp;
57     
58     /** Holds value of property rule. */
59     private Rule rule;
60
61     /** Holds value of property action. */
62     private Action action;
63
64     /** Holds value of property avt (attribute value template). */
65     private boolean avt;
66
67     /** The pattern to match */
68     private Pattern pattern;
69
70     /** The xpath expression to match */
71     private String JavaDoc match;
72     
73     private StylesheetTag stylesheetTag;
74     private Object JavaDoc oldContext = null;
75     
76     //-------------------------------------------------------------------------
77
public TemplateTag() {
78     }
79
80     // Tag interface
81
//-------------------------------------------------------------------------
82
public int doStartTag() throws JspException JavaDoc {
83         StylesheetTag tag = getStylesheetTag();
84         if (tag.getCurrentState() == StylesheetTag.INITIALISE_STYLESHEET) {
85             Rule rule = getRule();
86             if ( rule != null ) {
87                 if ( tag != null ) {
88                     tag.addTemplate( rule );
89                 }
90             }
91             return SKIP_BODY;
92         } else {
93             TemplateExecution te = tag.getTemplateExecution(match);
94             if ( te != null ) {
95                 oldContext = TagHelper.getInputNodes( pageContext );
96                 TagHelper.setInputNodes( pageContext, te.getNode() );
97
98                 return EVAL_BODY_TAG;
99             } else {
100                 return SKIP_BODY;
101             }
102         }
103     }
104
105     public int doAfterBody() throws JspException JavaDoc {
106         try {
107             String JavaDoc text = bodyContent.getString();
108             if (isAvt() == true) {
109                 text = getStylesheetTag().processAVTs(text);
110             }
111             getStylesheetTag().addOutput( text );
112             bodyContent.clearBody();
113             if (oldContext != null) {
114                 TagHelper.setInputNodes( pageContext, oldContext );
115                 oldContext = null;
116             }
117         } catch (IOException JavaDoc e) {
118             handleException(e);
119         }
120         return SKIP_BODY;
121     }
122     
123     
124     public void release() {
125         super.release();
126         name = null;
127         jsp = null;
128         match = null;
129         rule = null;
130         action = null;
131         pattern = null;
132         oldContext = null;
133     }
134
135     
136     void preApplyTemplates() throws IOException JavaDoc {
137         // An applyTemplates tag is about to be run, so we need to stick our body in the Stylesheets output
138
// list at this point (as it needs to be output just before the applyTemplates actions are output).
139
String JavaDoc text = bodyContent.getString();
140         if (isAvt() == true) {
141             text = getStylesheetTag().processAVTs(text);
142         }
143         getStylesheetTag().addOutput( text );
144         bodyContent.clearBody();
145     }
146     
147     // Properties
148
//-------------------------------------------------------------------------
149

150     /** Getter for property avt.
151      * @return Value of property avt.
152      */

153     public boolean isAvt() {
154         return avt;
155     }
156     
157     /** Setter for property avt.
158      * @param avt New value of property avt.
159      */

160     public void setAvt(boolean avt) {
161         this.avt = avt;
162     }
163     
164     public void setMatch( String JavaDoc match ) {
165         // XXXX: can we do this at compile time? / Class load?
166
this.match = match;
167         
168         StylesheetTag tag = getStylesheetTag();
169         if (tag.getCurrentState() == StylesheetTag.INITIALISE_STYLESHEET ) {
170             pattern = createPattern( match );
171         }
172     }
173     
174     /** Getter for property priority.
175      * @return Value of property priority.
176      */

177     public double getPriority() {
178         return priority;
179     }
180     
181     /** Setter for property priority.
182      * @param priority New value of property priority.
183      */

184     public void setPriority(double priority) {
185         this.priority = priority;
186     }
187     
188     /** Getter for property name.
189      * @return Value of property name.
190      */

191     public String JavaDoc getName() {
192         return name;
193     }
194     
195     /** Setter for property name.
196      * @param name New value of property name.
197      */

198     public void setName(String JavaDoc name) {
199         this.name = name;
200     }
201     
202     /** Getter for property stylesheetTag.
203      * @return Value of property stylesheetTag.
204      */

205     public StylesheetTag getStylesheetTag() {
206         if ( stylesheetTag == null ) {
207             stylesheetTag = (StylesheetTag) findAncestorWithClass(
208                 this, StylesheetTag.class
209             );
210         }
211         return stylesheetTag;
212     }
213
214     
215     /** Getter for property action.
216      * @return Value of property action.
217      */

218     public Action getAction() {
219         if ( action == null ) {
220             action = createAction();
221         }
222         return action;
223     }
224     
225     /** Setter for property action.
226      * @param action New value of property action.
227      */

228     public void setAction(Action action) {
229         this.action = action;
230     }
231     
232     /** Setter for property mode.
233      * @param mode New value of property mode.
234      */

235     public void setMode(String JavaDoc mode) {
236         this.mode = mode;
237     }
238     
239     /** Getter for property jsp.
240      * @return Value of property jsp.
241      */

242     public String JavaDoc getJsp() {
243         return jsp;
244     }
245     
246     /** Setter for property jsp.
247      * @param jsp New value of property jsp.
248      */

249     public void setJsp(String JavaDoc jsp) {
250         this.jsp = jsp;
251     }
252     
253     /** Getter for property rule.
254      * @return Value of property rule.
255      */

256     public Rule getRule() {
257         if ( rule == null ) {
258             rule = createRule();
259         }
260         return rule;
261     }
262     
263     /** Setter for property rule.
264      * @param rule New value of property rule.
265      */

266     public void setRule(Rule rule) {
267         this.rule = rule;
268     }
269     
270     
271     
272     
273     // Implementation methods
274
//-------------------------------------------------------------------------
275
protected Rule createRule() {
276         Rule rule = new Rule( pattern, getAction() );
277         rule.setMode( mode );
278         return rule;
279     }
280     
281     protected Pattern createPattern( String JavaDoc xpath ) {
282         return DocumentHelper.createPattern( xpath );
283     }
284     
285     // Action interface
286
//-------------------------------------------------------------------------
287
protected Action createAction() {
288         if ( jsp != null ) {
289             return new JspAction( pageContext, jsp );
290         }
291         else {
292             return new BodyAction( getStylesheetTag(), this.match );
293         }
294     }
295 }
296
Popular Tags