KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.Tag JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 import org.dom4j.Document;
31 import org.dom4j.Node;
32 import org.dom4j.NodeFilter;
33 import org.dom4j.DocumentFactory;
34 import org.dom4j.DocumentHelper;
35 import org.dom4j.XPath;
36 import org.dom4j.rule.Stylesheet;
37
38 import org.jaxen.VariableContext;
39
40 import org.apache.taglibs.xtags.util.JspNestedException;
41 import org.apache.taglibs.xtags.util.JspVariableContext;
42
43
44
45 /** A tag which performs an XPath expression on the current context Node
46   *
47   * @author James Strachan
48   */

49 public abstract class AbstractTag extends TagSupport JavaDoc {
50
51     protected static final Document EMPTY_DOCUMENT = DocumentHelper.createDocument();
52     
53     protected static final boolean ALLOW_FLUSH = false;
54     
55     protected Object JavaDoc context;
56     
57     
58     public AbstractTag() {
59     }
60
61     public void release() {
62         context = null;
63     }
64
65     public void flush() throws JspException JavaDoc {
66         if ( ALLOW_FLUSH ) {
67             try {
68                 pageContext.getOut().flush();
69             }
70             catch (IOException JavaDoc e) {
71                 handleException(e);
72             }
73         }
74     }
75     
76     // Properties
77
//-------------------------------------------------------------------------
78
public void setContext(Object JavaDoc context) {
79         this.context = context;
80     }
81
82     
83     // Helper methods
84
//-------------------------------------------------------------------------
85
/** @return true if the given filter matches a node in the
86       * input nodes
87       */

88     public boolean matches(NodeFilter filter) {
89         Object JavaDoc input = getInputNodes( false );
90         if ( input == null ) {
91             // use an empty document to support
92
// filters that just use XPath variables
93
// such as "$foo='bar'"
94
input = EMPTY_DOCUMENT;
95         }
96         if ( input instanceof List JavaDoc ) {
97             List JavaDoc list = (List JavaDoc) input;
98             for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
99                 Object JavaDoc object = iter.next();
100                 if ( object instanceof Node ) {
101                     Node node = (Node) object;
102                     if ( filter.matches( node ) ) {
103                         return true;
104                     }
105                 }
106             }
107         }
108         else if ( input instanceof Node ) {
109             Node node = (Node) input;
110             if ( filter.matches( node ) ) {
111                 return true;
112             }
113         }
114         return false;
115     }
116
117     /** @return the input node on which to make a selction
118       */

119     public Object JavaDoc getInputNodes() {
120         return getInputNodes( true );
121     }
122     
123     public Object JavaDoc getInputNodes( boolean warn ) {
124         if ( context == null ) {
125             return TagHelper.getInputNodes( pageContext, this, warn );
126         }
127         return context;
128     }
129     
130     public void setInputNodes( Object JavaDoc inputNodes ) {
131         TagHelper.setInputNodes( pageContext, inputNodes );
132     }
133     
134     public Stylesheet getStylesheet() {
135         StylesheetTag tag = (StylesheetTag) findAncestorWithClass(
136             this, StylesheetTag.class
137         );
138         if ( tag != null ) {
139             return tag.getStylesheet();
140         }
141         else {
142             return TagHelper.getStylesheet( pageContext );
143         }
144     }
145
146     
147     // Implementation methods
148
//-------------------------------------------------------------------------
149

150     /** A factory method to create new XPath instances */
151     protected XPath createXPath(String JavaDoc xpathExpression) {
152         VariableContext variableContext = JspVariableContext.getInstance( pageContext );
153         return getDocumentFactory().createXPath( xpathExpression, variableContext );
154     }
155
156     /** A factory method to create new XPath filter */
157     protected NodeFilter createXPathFilter(String JavaDoc xpathExpression) {
158         VariableContext variableContext = JspVariableContext.getInstance( pageContext );
159         return getDocumentFactory().createXPathFilter( xpathExpression, variableContext );
160     }
161
162     /** @return the factory used to create XPath instances */
163     protected DocumentFactory getDocumentFactory() {
164         return DocumentFactory.getInstance();
165     }
166     
167     /** Handles non-JspExceptions thrown in this instance
168       */

169     protected void handleException( Exception JavaDoc e ) throws JspException JavaDoc {
170         if ( e instanceof JspException JavaDoc ) {
171             throw (JspException JavaDoc) e;
172         }
173         else {
174             pageContext.getServletContext().log( e.getMessage(), e );
175             throw new JspNestedException( e );
176         }
177     }
178 }
179
Popular Tags