001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file ../GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.dax;
016:
017: import org.griphyn.vdl.dax.*;
018: import java.util.*;
019: import java.io.Writer;
020: import java.io.IOException;
021:
022: /**
023: * This class extends the base class <code>Leaf</code> by adding an
024: * attribute to store the content of a the textual data of mixed
025: * content. The <code>PseudoText</code> is not an element!
026: *
027: * @author Jens-S. Vöckler
028: * @author Yong Zhao
029: * @version $Revision: 50 $
030: *
031: * @see Leaf
032: */
033: public class PseudoText extends Leaf implements Cloneable {
034: /**
035: * Stores the content of the textual element.
036: */
037: private String m_content;
038:
039: /**
040: * Creates and returns a copy of this object.
041: * @return a new instance.
042: */
043: public Object clone() {
044: // java.lang.String implements inherently copy-on-write.
045: return new PseudoText(this .m_content);
046: }
047:
048: /**
049: * Default ctor.
050: */
051: public PseudoText() {
052: super ();
053: }
054:
055: /**
056: * Ctor to initialize the content while constructing the class.
057: * This is a convenience ctor.
058: */
059: public PseudoText(String content) {
060: super ();
061: this .m_content = content;
062: }
063:
064: /**
065: * Gets the content state of this object. The text may contain
066: * other elements which are not quoted or changed in any way,
067: * because the text element is designed to be a leaf node.
068: *
069: * @return The current state of content. The text may be null.
070: * @see #setContent(String)
071: */
072: public String getContent() {
073: return this .m_content;
074: }
075:
076: /**
077: * Overwrites the internal state with new content. The supplied content
078: * will become effectively the active state of the object. Usually, this
079: * method will be called during SAX assembly of the instance structure.
080: *
081: * @param content is the new state to register.
082: * @see #getContent()
083: **/
084: public void setContent(String content) {
085: this .m_content = content;
086: }
087:
088: /**
089: * Converts the active state into something meant for human consumption.
090: * The method will be called when recursively traversing the instance
091: * tree. This method overwrites the default method, since it appears
092: * to be faster to do directly.
093: *
094: * @return The current content enclosed in quotes.
095: */
096: public String toString() {
097: StringBuffer result = new StringBuffer(
098: this .m_content.length() + 2);
099: result.append('"');
100: if (this .m_content != null)
101: result.append(escape(this .m_content));
102: result.append('"');
103: return result.toString();
104: }
105:
106: /**
107: * Converts the active state into something meant for human consumption.
108: * The method will be called when recursively traversing the instance
109: * tree.
110: *
111: * @param stream is a stream opened and ready for writing. This can also
112: * be a string stream for efficient output.
113: */
114: public void toString(Writer stream) throws IOException {
115: stream.write('"');
116: if (this .m_content != null)
117: stream.write(escape(this .m_content));
118: stream.write('"');
119: }
120:
121: // static private final String empty_element = "</text>";
122:
123: /**
124: * Converts the active state into something meant for computer consumption.
125: * The method will be called when recursively traversing the instance
126: * tree. This method overwrites the inherited methods since it appears
127: * to be faster to do it this way.<p>
128: *
129: * FIXME: Contents will not be properly XML quoted.
130: *
131: * @param indent is an arbitrary string to prefix a line with for
132: * pretty printing. Usually, an initial amount of zero spaces are
133: * used. Unused in this case.
134: * @param namespace is the XML schema namespace prefix. If neither
135: * empty nor null, each element will be prefixed with this prefix,
136: * and the root element will map the XML namespace.
137: * @return a string which contains the current string. Since the string
138: * is part of mixed content, no element tags are supplied, nor any
139: * additional whitespaces.
140: */
141: public String toXML(String indent, String namespace) {
142: // if ( m_content != null && m_content.length() > 0 ) {
143: // StringBuffer result = new StringBuffer( m_content.length() + 16 );
144: // result.append( "<text>"). append( quote(this.m_content,false) ).append("</text>");
145: // return result.toString();
146: // } else {
147: // return empty_element;
148: // }
149: if (this .m_content != null) {
150: return new String(quote(this .m_content, false));
151: } else {
152: return new String();
153: }
154: }
155:
156: /**
157: * Dump the state of the current element as XML output. This function
158: * traverses all sibling classes as necessary, and converts the data
159: * into pretty-printed XML output. The stream interface should be able
160: * to handle large output efficiently.<p>
161: *
162: * FIXME: Contents will not be properly XML quoted.
163: *
164: * @param stream is a stream opened and ready for writing. This can also
165: * be a string stream for efficient output.
166: * @param indent is a <code>String</code> of spaces used for pretty
167: * printing. The initial amount of spaces should be an empty string.
168: * The parameter is used internally for the recursive traversal.
169: * @param namespace is the XML schema namespace prefix. If neither
170: * empty nor null, each element will be prefixed with this prefix,
171: * and the root element will map the XML namespace.
172: * @exception IOException if something fishy happens to the stream.
173: */
174: public void toXML(Writer stream, String indent, String namespace)
175: throws IOException {
176: // if ( m_content != null && m_content.length() > 0 ) {
177: // stream.write( "<text>");
178: // stream.write( quote(this.m_content,false) );
179: // stream.write("</text>");
180: // } else {
181: // stream.write(empty_element);
182: // }
183: if (this .m_content != null) {
184: stream.write(quote(this .m_content, false));
185: }
186: }
187: }
|