1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.views.xslt;
22
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.w3c.dom.Node;
31
32 import org.xml.sax.InputSource;
33
34 import com.opensymphony.xwork2.util.DomHelper;
35
36 /***
37 * StringAdapter adapts a Java String value to a DOM Element with the specified
38 * property name containing the String's text.
39 * e.g. a property <pre>String getFoo() { return "My Text!"; }</pre>
40 * will appear in the result DOM as:
41 * <foo>MyText!</foo>
42 *
43 * Subclasses may override the getStringValue() method in order to use StringAdapter
44 * as a simplified custom XML adapter for Java types. A subclass can enable XML
45 * parsing of the value string via the setParseStringAsXML() method and then
46 * override getStringValue() to return a String containing the custom formatted XML.
47 *
48 */
49 public class StringAdapter extends AbstractAdapterElement {
50
51 private Log log = LogFactory.getLog(this.getClass());
52 boolean parseStringAsXML;
53
54 public StringAdapter() {
55 }
56
57 public StringAdapter(AdapterFactory adapterFactory, AdapterNode parent, String propertyName, String value) {
58 setContext(adapterFactory, parent, propertyName, value);
59 }
60
61 /***
62 * Get the object to be adapted as a String value.
63 * <p/>
64 * This method can be overridden by subclasses that wish to use StringAdapter
65 * as a simplified customizable XML adapter for Java types. A subclass can
66 * enable parsing of the value string as containing XML text via the
67 * setParseStringAsXML() method and then override getStringValue() to return a
68 * String containing the custom formatted XML.
69 */
70 protected String getStringValue() {
71 return getPropertyValue().toString();
72 }
73
74 protected List<Node> buildChildAdapters() {
75 Node node;
76 if (getParseStringAsXML()) {
77 log.debug("parsing string as xml: " + getStringValue());
78
79 node = DomHelper.parse(new InputSource(new StringReader(getStringValue())));
80 node = getAdapterFactory().proxyNode(this, node);
81 } else {
82 log.debug("using string as is: " + getStringValue());
83
84 node = new SimpleTextNode(getAdapterFactory(), this, "text", getStringValue());
85 }
86
87 List<Node> children = new ArrayList<Node>();
88 children.add(node);
89 return children;
90 }
91
92 /***
93 * Is this StringAdapter to interpret its string values as containing
94 * XML Text?
95 *
96 * @see #setParseStringAsXML(boolean)
97 */
98 public boolean getParseStringAsXML() {
99 return parseStringAsXML;
100 }
101
102 /***
103 * When set to true the StringAdapter will interpret its String value
104 * as containing XML text and parse it to a DOM Element. The new DOM
105 * Element will be a child of this String element. (i.e. wrapped in an
106 * element of the property name specified for this StringAdapter).
107 *
108 * @param parseStringAsXML
109 * @see #getParseStringAsXML()
110 */
111 public void setParseStringAsXML(boolean parseStringAsXML) {
112 this.parseStringAsXML = parseStringAsXML;
113 }
114
115 }