001: /*
002: * $Id: StringAdapter.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.xslt;
022:
023: import java.io.StringReader;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import org.w3c.dom.Node;
031:
032: import org.xml.sax.InputSource;
033:
034: import com.opensymphony.xwork2.util.DomHelper;
035:
036: /**
037: * StringAdapter adapts a Java String value to a DOM Element with the specified
038: * property name containing the String's text.
039: * e.g. a property <pre>String getFoo() { return "My Text!"; }</pre>
040: * will appear in the result DOM as:
041: * <foo>MyText!</foo>
042: *
043: * Subclasses may override the getStringValue() method in order to use StringAdapter
044: * as a simplified custom XML adapter for Java types. A subclass can enable XML
045: * parsing of the value string via the setParseStringAsXML() method and then
046: * override getStringValue() to return a String containing the custom formatted XML.
047: *
048: */
049: public class StringAdapter extends AbstractAdapterElement {
050:
051: private Log log = LogFactory.getLog(this .getClass());
052: boolean parseStringAsXML;
053:
054: public StringAdapter() {
055: }
056:
057: public StringAdapter(AdapterFactory adapterFactory,
058: AdapterNode parent, String propertyName, String value) {
059: setContext(adapterFactory, parent, propertyName, value);
060: }
061:
062: /**
063: * Get the object to be adapted as a String value.
064: * <p/>
065: * This method can be overridden by subclasses that wish to use StringAdapter
066: * as a simplified customizable XML adapter for Java types. A subclass can
067: * enable parsing of the value string as containing XML text via the
068: * setParseStringAsXML() method and then override getStringValue() to return a
069: * String containing the custom formatted XML.
070: */
071: protected String getStringValue() {
072: return getPropertyValue().toString();
073: }
074:
075: protected List<Node> buildChildAdapters() {
076: Node node;
077: if (getParseStringAsXML()) {
078: log.debug("parsing string as xml: " + getStringValue());
079: // Parse the String to a DOM, then proxy that as our child
080: node = DomHelper.parse(new InputSource(new StringReader(
081: getStringValue())));
082: node = getAdapterFactory().proxyNode(this , node);
083: } else {
084: log.debug("using string as is: " + getStringValue());
085: // Create a Text node as our child
086: node = new SimpleTextNode(getAdapterFactory(), this ,
087: "text", getStringValue());
088: }
089:
090: List<Node> children = new ArrayList<Node>();
091: children.add(node);
092: return children;
093: }
094:
095: /**
096: * Is this StringAdapter to interpret its string values as containing
097: * XML Text?
098: *
099: * @see #setParseStringAsXML(boolean)
100: */
101: public boolean getParseStringAsXML() {
102: return parseStringAsXML;
103: }
104:
105: /**
106: * When set to true the StringAdapter will interpret its String value
107: * as containing XML text and parse it to a DOM Element. The new DOM
108: * Element will be a child of this String element. (i.e. wrapped in an
109: * element of the property name specified for this StringAdapter).
110: *
111: * @param parseStringAsXML
112: * @see #getParseStringAsXML()
113: */
114: public void setParseStringAsXML(boolean parseStringAsXML) {
115: this.parseStringAsXML = parseStringAsXML;
116: }
117:
118: }
|