001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.xslt;
006:
007: import org.w3c.dom.Node;
008: import org.xml.sax.SAXException;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import javax.xml.parsers.ParserConfigurationException;
013: import java.util.ArrayList;
014: import java.util.List;
015: import java.io.IOException;
016:
017: import com.opensymphony.util.XMLUtils;
018: import com.opensymphony.webwork.WebWorkException;
019:
020: /**
021: * StringAdapter adapts a Java String value to a DOM Element with the specified
022: * property name containing the String's text.
023: * e.g. a property <pre>String getFoo() { return "My Text!"; }</pre>
024: * will appear in the result DOM as:
025: * <foo>MyText!</foo>
026: *
027: * Subclasses may override the getStringValue() method in order to use StringAdapter
028: * as a simplified custom XML adapter for Java types. A subclass can enable XML
029: * parsing of the value string via the setParseStringAsXML() method and then
030: * override getStringValue() to return a String containing the custom formatted XML.
031: *
032: * @author <a href="mailto:meier@meisterbohne.de">Philipp Meier</a>
033: * @author Pat Niemeyer (pat@pat.net)
034: */
035: public class StringAdapter extends AbstractAdapterElement {
036: private Log log = LogFactory.getLog(this .getClass());
037: boolean parseStringAsXML = false;
038:
039: //~ Constructors ///////////////////////////////////////////////////////////
040:
041: public StringAdapter() {
042: }
043:
044: public StringAdapter(AdapterFactory adapterFactory,
045: AdapterNode parent, String propertyName, String value) {
046: setContext(adapterFactory, parent, propertyName, value);
047: }
048:
049: /**
050: * Get the object to be adapted as a String value.
051: * <p/>
052: * This method can be overridden by subclasses that wish to use StringAdapter
053: * as a simplified customizable XML adapter for Java types. A subclass can
054: * enable parsing of the value string as containing XML text via the
055: * setParseStringAsXML() method and then override getStringValue() to return a
056: * String containing the custom formatted XML.
057: */
058: protected String getStringValue() {
059: return (String) getPropertyValue().toString();
060: }
061:
062: //~ Methods ////////////////////////////////////////////////////////////////
063:
064: protected List buildChildAdapters() {
065: Node node;
066: if (getParseStringAsXML()) {
067: log.debug("parsing string as xml: " + getStringValue());
068: // Parse the String to a DOM, then proxy that as our child
069: try {
070: node = XMLUtils.parse(getStringValue());
071: } catch (ParserConfigurationException e) {
072: throw new WebWorkException(e);
073: } catch (IOException e) {
074: throw new WebWorkException(e);
075: } catch (SAXException e) {
076: throw new WebWorkException(e);
077: }
078: node = getAdapterFactory().proxyNode(this , node);
079: } else {
080: log.debug("using string as is: " + getStringValue());
081: // Create a Text node as our child
082: node = new SimpleTextNode(getAdapterFactory(), this ,
083: "text", getStringValue());
084: }
085:
086: List children = new ArrayList();
087: children.add(node);
088: return children;
089: }
090:
091: /**
092: * Is this StringAdapter to interpret its string values as containing
093: * XML Text?
094: *
095: * @see #setParseStringAsXML(boolean)
096: */
097: public boolean getParseStringAsXML() {
098: return parseStringAsXML;
099: }
100:
101: /**
102: * When set to true the StringAdapter will interpret its String value
103: * as containing XML text and parse it to a DOM Element. The new DOM
104: * Element will be a child of this String element. (i.e. wrapped in an
105: * element of the property name specified for this StringAdapter).
106: *
107: * @param parseStringAsXML
108: * @see #getParseStringAsXML()
109: */
110: public void setParseStringAsXML(boolean parseStringAsXML) {
111: this.parseStringAsXML = parseStringAsXML;
112: }
113:
114: }
|