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