KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspWriter JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 import org.dom4j.Node;
29 import org.dom4j.XPath;
30 import org.dom4j.DocumentHelper;
31 import org.dom4j.rule.Stylesheet;
32
33
34 /** The body of this tag defines a stylesheet which is implemented via calling
35   * a JSP include.
36   *
37   * @author James Strachan
38   */

39 public class ApplyTemplatesTag extends AbstractTag {
40
41     /** Holds value of property mode. */
42     private String JavaDoc mode;
43
44     /** Holds the XPath object */
45     private XPath xpath;
46     
47
48     public ApplyTemplatesTag() {
49     }
50
51     // Tag interface
52
//-------------------------------------------------------------------------
53
public int doStartTag() throws JspException JavaDoc {
54         return SKIP_BODY;
55     }
56     
57     public int doEndTag() throws JspException JavaDoc {
58         flush();
59         Stylesheet stylesheet = getStylesheet();
60         if ( stylesheet == null ) {
61             throw new JspException JavaDoc(
62                 "applytemplates tag requires a Stylesheet to be in scope"
63             );
64         }
65         else {
66             try {
67                 // Tell the current TemplateTag to put its output in the Stylesheet's output list,
68
// otherwise the order of the output will be incorrect.
69
TemplateTag templateTag = (TemplateTag) findAncestorWithClass(
70                     this, TemplateTag.class
71                 );
72                 templateTag.preApplyTemplates();
73                 
74                 Object JavaDoc context = getInputNodes();
75                 if ( xpath != null ) {
76                     stylesheet.applyTemplates( context, xpath );
77                 }
78                 else {
79                     stylesheet.applyTemplates( context );
80                 }
81                 // restore the context back to what it was
82
setInputNodes( context );
83             }
84             catch (JspException JavaDoc e) {
85                 throw e;
86             }
87             catch (Exception JavaDoc e) {
88                 handleException(e);
89             }
90         }
91         return EVAL_PAGE;
92     }
93
94     public void release() {
95         super.release();
96         xpath = null;
97         mode = null;
98     }
99
100     // Properties
101
//-------------------------------------------------------------------------
102

103     public void setSelect( String JavaDoc select ) {
104         xpath = createXPath( select );
105     }
106     
107     /** Setter for property mode.
108      * @param mode New value of property mode.
109      */

110     public void setMode(String JavaDoc mode) {
111         this.mode = mode;
112     }
113 }
114
Popular Tags