View Javadoc

    1   /*
    2    * $Id: StringAdapter.java 471756 2006-11-06 15:01:43Z husted $
    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  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              // Parse the String to a DOM, then proxy that as our child
    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              // Create a Text node as our child
    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 }