KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.Tag JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29 import org.dom4j.Node;
30 import org.dom4j.io.OutputFormat;
31 import org.dom4j.io.XMLWriter;
32 import org.dom4j.rule.Stylesheet;
33
34 /** A number of helper methods
35   *
36   * @author James Strachan
37   */

38 public class TagHelper {
39
40     /** Request scope attribute name used to pass the context between
41       * JSP files
42       */

43     public static final String JavaDoc REQUEST_KEY_CONTEXT = "org.apache.taglibs.xtags.taglib.Context";
44     
45     /** Request scope attribute name used to pass the stylesheet between
46       * JSP files */

47     public static final String JavaDoc REQUEST_KEY_STYLESHEET = "org.apache.taglibs.xtags.taglib.Stylesheet";
48
49     protected static final OutputFormat outputFormat;
50     
51     /** Request scope attribute name used to pass the XMLWriter between
52       * JSP files
53       */

54     public static final String JavaDoc REQUEST_KEY_XML_WRITER = "org.apache.taglibs.xtags.XMLWriter";
55     
56     static {
57         outputFormat = new OutputFormat();
58         outputFormat.setSuppressDeclaration(true);
59     }
60     
61     
62     public static OutputFormat getOutputFormat( PageContext JavaDoc pageContext ) {
63         return outputFormat;
64     }
65    
66     public static XMLWriter getXMLWriter(PageContext JavaDoc pageContext, Tag JavaDoc thisTag) {
67         OutputTag tag = (OutputTag) TagSupport.findAncestorWithClass(
68             thisTag, OutputTag.class
69         );
70         if ( tag != null ) {
71             return tag.getXMLWriter();
72         }
73         return new XMLWriter( pageContext.getOut(), getOutputFormat( pageContext ) );
74     }
75
76     public static XMLWriter createXMLWriter(PageContext JavaDoc pageContext) {
77         return new XMLWriter( pageContext.getOut(), getOutputFormat( pageContext ) );
78     }
79
80    
81     
82     /** @return the input node on which to make a selction
83       */

84     public static Object JavaDoc getInputNodes(PageContext JavaDoc pageContext) {
85         Object JavaDoc nodes = pageContext.getAttribute(
86             REQUEST_KEY_CONTEXT, PageContext.PAGE_SCOPE
87         );
88         if (nodes == null) {
89             nodes = pageContext.getAttribute(
90                 REQUEST_KEY_CONTEXT, PageContext.REQUEST_SCOPE
91             );
92         }
93         return nodes;
94     }
95     
96     /** @return the input node on which to make a selction
97       */

98     public static Object JavaDoc getInputNodes(PageContext JavaDoc pageContext, Tag JavaDoc thisTag, boolean warn) {
99         Object JavaDoc context = null;
100         ContextNodeTag tag = (ContextNodeTag) TagSupport.findAncestorWithClass(
101             thisTag, ContextNodeTag.class
102         );
103         if ( tag != null ) {
104             context = tag.getContext();
105         }
106         if ( context == null ) {
107             context = getInputNodes( pageContext );
108         }
109 /*
110         if ( context == null && warn ) {
111             pageContext.getServletContext().log( "WARNING: No Input Node found!" );
112             Exception e = new Exception();
113             e.printStackTrace();
114         }
115 */

116         return context;
117     }
118     
119     public static void setInputNodes( PageContext JavaDoc pageContext, Object JavaDoc inputNodes ) {
120         if ( inputNodes == null ) {
121             pageContext.removeAttribute(
122                 REQUEST_KEY_CONTEXT,
123                 PageContext.PAGE_SCOPE
124             );
125             pageContext.removeAttribute(
126                 REQUEST_KEY_CONTEXT,
127                 PageContext.REQUEST_SCOPE
128             );
129         }
130         else {
131             pageContext.setAttribute(
132                 REQUEST_KEY_CONTEXT,
133                 inputNodes,
134                 PageContext.PAGE_SCOPE
135             );
136             pageContext.setAttribute(
137                 REQUEST_KEY_CONTEXT,
138                 inputNodes,
139                 PageContext.REQUEST_SCOPE
140             );
141         }
142     }
143     
144     public static Stylesheet getStylesheet(PageContext JavaDoc pageContext) {
145         return (Stylesheet) pageContext.getAttribute(
146             REQUEST_KEY_STYLESHEET,
147             PageContext.REQUEST_SCOPE
148         );
149     }
150     
151     public static void setStylesheet(PageContext JavaDoc pageContext, Stylesheet stylesheet) {
152         pageContext.setAttribute(
153             REQUEST_KEY_STYLESHEET,
154             stylesheet,
155             PageContext.REQUEST_SCOPE
156         );
157     }
158     
159     public static void defineVariable(PageContext JavaDoc pageContext, String JavaDoc id, Object JavaDoc value ) {
160         if ( id != null ) {
161             pageContext.setAttribute( id, value );
162         }
163         setInputNodes( pageContext, value );
164     }
165 }
166
Popular Tags