KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > xml > SerializeXML


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.xml;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import org.apache.beehive.netui.tags.AbstractClassicTag;
23 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
24 import org.apache.beehive.netui.util.logging.Logger;
25 import org.apache.xmlbeans.XmlObject;
26
27 /**
28  * <p>
29  * A tag that serializes an XMLBean into the output of a JSP page in order to move data to the browser for data binding.
30  * </p>
31  * @jsptagref.tagdescription
32  * A tag that serializes an XMLBean into the output of a JSP page in order to move data to the browser for data binding.
33  * @netui:tag name="serializeXML"
34  * description="A tag that serializes an XMLBean into the output of a JSP page in order to move data to the browser for data binding."
35  */

36 public class SerializeXML
37         extends AbstractClassicTag {
38
39     private static final Logger LOGGER = Logger.getInstance(SerializeXML.class);
40
41     private Object JavaDoc _source;
42     private String JavaDoc _divName;
43
44     /**
45      * The name of this tag; this value is used for error reporting.
46      * @return the String name of this tag
47      */

48     public String JavaDoc getTagName() {
49         return "SerializeXML";
50     }
51
52     /**
53      * Set the source document to be serialized. The object referenced here should be an instance of
54      * {@link XmlObject}.
55      * @param source the source
56      * @jsptagref.attributedescription
57      * Set the source document to be serialized. The object referenced here should be an instance of
58      * {@link XmlObject}.
59      * @jsptagref.attributesyntaxvalue <i>object_source</i>
60      * @netui:attribute required="true" rtexprvalue="true"
61      */

62     public void setSource(Object JavaDoc source) {
63         _source = source;
64     }
65
66     /**
67      * Set the name of the div into which this XML will be rendered.
68      * @param divName the div name
69      * @jsptagref.attributedescription
70      * Set the name of the div into which this XML will be rendered.
71      * @jsptagref.attributesyntaxvalue <i>string_divName</i>
72      * @netui:attribute required="true" rtexprvalue="true"
73      */

74     public void setDivName(String JavaDoc divName) {
75         _divName = divName;
76     }
77
78     public int doStartTag() {
79         return SKIP_BODY;
80     }
81
82     public int doEndTag()
83         throws JspException JavaDoc {
84
85         if(_source instanceof XmlObject && _source != null) {
86             XmlObject xmlObject = (XmlObject)_source;
87             String JavaDoc xml = xmlObject.xmlText();
88
89             InternalStringBuilder buf = new InternalStringBuilder();
90             buf.append("<div");
91             buf.append(" id=\"");
92             buf.append(_divName);
93             buf.append("\">\n<!--\n");
94             buf.append(xml);
95             buf.append("\n-->\n</div>");
96
97             write(buf.toString());
98         }
99         else {
100             // @todo: write anything to the page?
101
if(LOGGER.isInfoEnabled())
102                 LOGGER.info("The expression \"" + _source + "\" resulted in an object that was not an XMLBean");
103         }
104
105         localRelease();
106
107         return EVAL_PAGE;
108     }
109
110     public void localRelease() {
111         super.localRelease();
112
113         _source = null;
114         _divName = null;
115     }
116 }
117
Popular Tags