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:
016: package org.griphyn.vdl.classes;
017:
018: import org.griphyn.vdl.classes.*;
019: import java.util.*;
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.io.Serializable;
023:
024: /**
025: * This class extends the base class <code>Leaf</code> by adding
026: * an attribute to store the content of a pure textual element. The
027: * <code>Text</code> element is designed to be a leaf class in the
028: * instance tree.
029: *
030: * @author Jens-S. Vöckler
031: * @author Yong Zhao
032: * @version $Revision: 50 $
033: *
034: * @see Leaf
035: * @see Text
036: * @see LFN
037: */
038: public class Text extends Leaf implements Cloneable, Serializable {
039: /**
040: * Stores the content of the textual element.
041: */
042: private String m_content;
043:
044: /**
045: * Creates and returns a copy of this object.
046: * @return a new instance.
047: */
048: public Object clone() {
049: // java.lang.String implements inherently copy-on-write.
050: return new Text(this .m_content);
051: }
052:
053: /**
054: * Default ctor. Calls the parent initialization.
055: */
056: public Text() {
057: super ();
058: }
059:
060: /**
061: * Ctor to initialize the content while constructing the class.
062: * This is a convenience ctor.
063: */
064: public Text(String content) {
065: super ();
066: this .m_content = content;
067: }
068:
069: /**
070: * Gets the content state of this object. The text may contain
071: * other elements which are not quoted or changed in any way,
072: * because the text element is designed to be a leaf node.
073: *
074: * @return The current state of content. The text may be null.
075: * @see #setContent(String)
076: */
077: public String getContent() {
078: return this .m_content;
079: }
080:
081: /**
082: * Overwrites the internal state with new content. The supplied content
083: * will become effectively the active state of the object. Usually, this
084: * method will be called during SAX assembly of the instance structure.
085: *
086: * @param content is the new state to register.
087: * @see #getContent()
088: **/
089: public void setContent(String content) {
090: this .m_content = content;
091: }
092:
093: /**
094: * Converts the active state into something meant for human consumption.
095: * The method will be called when recursively traversing the instance
096: * tree. This method overwrites the base class default as it can be
097: * more efficiently implemented.
098: *
099: * @return The current content enclosed in quotes.
100: */
101: public String toString() {
102: return (this .m_content == null ? "\"\"" : "\"" + this .m_content
103: + "\"");
104: }
105:
106: /**
107: * Prints the current content onto the stream.
108: *
109: * @param stream is a stream opened and ready for writing. This can also
110: * be a string stream for efficient output.
111: * @throws IOException if something happens to the stream.
112: */
113: public void toString(Writer stream) throws IOException {
114: stream.write('"');
115: if (this .m_content != null)
116: stream.write(escape(this .m_content));
117: stream.write('"');
118: }
119:
120: /**
121: * Dump the state of the current element as XML output. This function
122: * traverses all sibling classes as necessary, and converts the data
123: * into pretty-printed XML output. The stream interface should be able
124: * to handle large output efficiently, if you use a buffered writer.
125: *
126: * @param stream is a stream opened and ready for writing. This can also
127: * be a string stream for efficient output.
128: * @param indent is a <code>String</code> of spaces used for pretty
129: * printing. The initial amount of spaces should be an empty string.
130: * The parameter is used internally for the recursive traversal.
131: * @param namespace is the XML schema namespace prefix. If neither
132: * empty nor null, each element will be prefixed with this prefix,
133: * and the root element will map the XML namespace.
134: * @exception IOException if something fishy happens to the stream.
135: */
136: public void toXML(Writer stream, String indent, String namespace)
137: throws IOException {
138: String tag = (namespace != null && namespace.length() > 0) ? namespace
139: + ":text"
140: : "text";
141:
142: if (indent != null && indent.length() > 0)
143: stream.write(indent);
144: if (this .m_content != null && this .m_content.length() > 0) {
145: stream.write('<');
146: stream.write(tag);
147: stream.write('>');
148: stream.write(quote(this .m_content, false));
149: stream.write("</");
150: stream.write(tag);
151: stream.write('>');
152: } else {
153: stream.write('<');
154: stream.write(tag);
155: stream.write("/>");
156: }
157: if (indent != null)
158: stream.write(System.getProperty("line.separator", "\r\n"));
159: }
160: }
|