KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 import org.dom4j.io.HTMLWriter;
26 import org.dom4j.io.OutputFormat;
27 import org.dom4j.io.XMLWriter;
28
29
30 /** This tag is used to specify the output format of the XML
31   *
32   * @author James Strachan
33   */

34 public class OutputTag extends TagSupport JavaDoc {
35
36     /** The XMLWriter used by my child tags */
37     private XMLWriter xmlWriter;
38     
39     /** Holds value of property method. */
40     private String JavaDoc method;
41     
42     /** Holds value of property indent. */
43     private String JavaDoc indent;
44     
45     /** Holds value of property omitXmlDeclaration. */
46     private String JavaDoc omitXmlDeclaration;
47     
48     
49     public OutputTag() {
50     }
51
52     
53     public XMLWriter getXMLWriter() {
54         return xmlWriter;
55     }
56     
57     // Tag interface
58
//-------------------------------------------------------------------------
59
public int doStartTag() throws JspException JavaDoc {
60         OutputFormat format = null;
61         if (indent != null) {
62             format = OutputFormat.createPrettyPrint();
63             format.setIndent(indent);
64         }
65         else {
66             format = new OutputFormat();
67         }
68         format.setOmitEncoding( asBoolean( omitXmlDeclaration ) );
69
70         Writer JavaDoc out = pageContext.getOut();
71         if ( method != null && method.equalsIgnoreCase( "html" ) ) {
72             xmlWriter = new HTMLWriter( out, format );
73         }
74         else {
75             xmlWriter = new XMLWriter( out, format );
76         }
77         return EVAL_BODY_INCLUDE;
78     }
79     
80     public void release() {
81         super.release();
82         xmlWriter = null;
83         indent = null;
84         omitXmlDeclaration = null;
85         method = null;
86     }
87
88     // Properties
89
//-------------------------------------------------------------------------
90
public void setIndent(String JavaDoc indent) {
91         this.indent = indent;
92     }
93     
94     public void setOmitXmlDeclaration(String JavaDoc omitXmlDeclaration) {
95         this.omitXmlDeclaration = omitXmlDeclaration;
96     }
97     
98     public void setMethod(String JavaDoc method) {
99         this.method = method;
100     }
101     
102
103     // Implementation methods
104
//-------------------------------------------------------------------------
105
protected boolean asBoolean( String JavaDoc text ) {
106         if ( text != null ) {
107             return indent.equalsIgnoreCase( "yes" ) || indent.equalsIgnoreCase( "true" );
108         }
109         return false;
110     }
111         
112 }
113
Popular Tags