001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.databinding.xml;
020:
021: import java.lang.reflect.Method;
022: import java.lang.reflect.InvocationTargetException;
023: import javax.servlet.jsp.JspException;
024:
025: import org.apache.beehive.netui.tags.AbstractClassicTag;
026: import org.apache.beehive.netui.util.internal.InternalStringBuilder;
027: import org.apache.beehive.netui.util.logging.Logger;
028:
029: /**
030: * <p>
031: * A tag that serializes an XMLBean into the output of a JSP page in order to move data to the browser for data binding.
032: * </p>
033: * @jsptagref.tagdescription
034: * A tag that serializes an XMLBean into the output of a JSP page in order to move data to the browser for data binding.
035: * @netui:tag name="serializeXML"
036: * 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."
037: */
038: public class SerializeXML extends AbstractClassicTag {
039:
040: private static final Logger LOGGER = Logger
041: .getInstance(SerializeXML.class);
042: private static final Object[] EMPTY_ARRAY = new Object[0];
043: private static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
044:
045: private Object _source;
046: private String _divName;
047:
048: /**
049: * The name of this tag; this value is used for error reporting.
050: * @return the String name of this tag
051: */
052: public String getTagName() {
053: return "SerializeXML";
054: }
055:
056: /**
057: * Set the source document to be serialized. The object referenced here should be an instance of
058: * {@link org.apache.xmlbeans.XmlObject}.
059: * @param source the source
060: * @jsptagref.attributedescription
061: * Set the source document to be serialized. The object referenced here should be an instance of
062: * {@link org.apache.xmlbeans.XmlObject}.
063: * @jsptagref.attributesyntaxvalue <i>object_source</i>
064: * @netui:attribute required="true" rtexprvalue="true"
065: */
066: public void setSource(Object source) {
067: _source = source;
068: }
069:
070: /**
071: * Set the name of the div into which this XML will be rendered.
072: * @param divName the div name
073: * @jsptagref.attributedescription
074: * Set the name of the div into which this XML will be rendered.
075: * @jsptagref.attributesyntaxvalue <i>string_divName</i>
076: * @netui:attribute required="true" rtexprvalue="true"
077: */
078: public void setDivName(String divName) {
079: _divName = divName;
080: }
081:
082: public int doStartTag() {
083: return SKIP_BODY;
084: }
085:
086: public int doEndTag() throws JspException {
087:
088: Object xmlText = null;
089: if (_source != null) {
090: Class xmlObject = null;
091: try {
092: xmlObject = Class
093: .forName("org.apache.xmlbeans.XmlObject");
094:
095: if (xmlObject.isAssignableFrom(_source.getClass())) {
096: Method method = xmlObject.getMethod("xmlText",
097: EMPTY_CLASS_ARRAY);
098: xmlText = method.invoke(_source, EMPTY_ARRAY);
099: }
100: } catch (ClassNotFoundException e) {
101: String msg = "Unable to serialize object; Apache XMLBeans is not available. To fix this problem, add XMLBeans to your project";
102: LOGGER.error(msg);
103: registerTagError(msg, e);
104: reportErrors();
105: localRelease();
106: return EVAL_PAGE;
107: } catch (NoSuchMethodException e) {
108: String msg = "Unexpected exception occurred serializing supposed XMLBean. Caues: "
109: + e;
110: assert false : msg;
111: LOGGER.error(msg);
112: localRelease();
113: return EVAL_PAGE;
114: } catch (InvocationTargetException e) {
115: String msg = "Unexpected exception occurred serializing supposed XMLBean. Caues: "
116: + e;
117: assert false : msg;
118: LOGGER.error(msg);
119: localRelease();
120: return EVAL_PAGE;
121: } catch (IllegalAccessException e) {
122: String msg = "Unexpected exception occurred serializing supposed XMLBean. Caues: "
123: + e;
124: assert false : msg;
125: LOGGER.error(msg);
126: localRelease();
127: return EVAL_PAGE;
128: }
129:
130: InternalStringBuilder buf = new InternalStringBuilder();
131: buf.append("<div");
132: buf.append(" id=\"");
133: buf.append(_divName);
134: buf.append("\">\n<!--\n");
135: buf.append(xmlText);
136: buf.append("\n-->\n</div>");
137:
138: write(buf.toString());
139: } else
140: LOGGER.info("The object to serialize was not an XMLBean");
141:
142: localRelease();
143:
144: return EVAL_PAGE;
145: }
146:
147: public void localRelease() {
148: super.localRelease();
149:
150: _source = null;
151: _divName = null;
152: }
153: }
|