View Javadoc

1   /*
2    * $Id: StringAdapter.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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              // Parse the String to a DOM, then proxy that as our child
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              // Create a Text node as our child
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 }