View Javadoc

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