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.invocation;
016:
017: import java.util.*;
018: import java.io.Writer;
019: import java.io.IOException;
020:
021: /**
022: * This class is transient for XML parsing. The data value will be
023: * incorporated into the <code>StatCall</code> class.
024: *
025: * @author Jens-S. Vöckler
026: * @author Yong Zhao
027: * @version $Revision: 50 $
028: * @see StatCall
029: */
030: public class Data extends Invocation implements HasText {
031: /**
032: * This is the data contained between the tags. A <code>null</code>
033: * value is not valid.
034: */
035: private StringBuffer m_value;
036:
037: /**
038: * Indicates, if the data is only partial.
039: */
040: private boolean m_truncated;
041:
042: /**
043: * Default c'tor: Construct a hollow shell and allow further
044: * information to be added later.
045: */
046: public Data() {
047: m_value = null;
048: m_truncated = false;
049: }
050:
051: /**
052: * Constructs a piece of data.
053: * @param value is the data to remember. The string may be empty,
054: * but it must not be <code>null</code>.
055: * @exception NullPointerException if the argument was null.
056: */
057: public Data(String value) {
058: if (value == null)
059: throw new NullPointerException(
060: "the value to the <data> tag constructor must not be null");
061: else
062: m_value = new StringBuffer(value);
063: }
064:
065: /**
066: * Constructs a piece of data.
067: * @param value is the data to remember. The string may be empty,
068: * but it must not be <code>null</code>.
069: * @param truncated is a flag to indicate that the data is partial.
070: * @exception NullPointerException if the argument was null.
071: */
072: public Data(String value, boolean truncated) {
073: if (value == null)
074: throw new NullPointerException(
075: "the value to the <data> tag constructor must not be null");
076: else
077: m_value = new StringBuffer(value);
078: m_truncated = truncated;
079: }
080:
081: /**
082: * Appends a piece of text to the existing text.
083: * @param fragment is a piece of text to append to existing text.
084: * Appending <code>null</code> is a noop.
085: */
086: public void appendValue(String fragment) {
087: if (fragment != null) {
088: if (this .m_value == null)
089: this .m_value = new StringBuffer(fragment);
090: else
091: this .m_value.append(fragment);
092: }
093: }
094:
095: /**
096: * Accessor
097: *
098: * @see #setTruncated(boolean)
099: */
100: public boolean getTruncated() {
101: return this .m_truncated;
102: }
103:
104: /**
105: * Accessor.
106: *
107: * @param truncated
108: * @see #getTruncated()
109: */
110: public void setTruncated(boolean truncated) {
111: this .m_truncated = truncated;
112: }
113:
114: /**
115: * Accessor
116: *
117: * @see #setValue(String)
118: */
119: public String getValue() {
120: return (m_value == null ? null : m_value.toString());
121: }
122:
123: /**
124: * Accessor.
125: *
126: * @param value is the new value to set.
127: * @see #getValue()
128: */
129: public void setValue(String value) {
130: this .m_value = (value == null ? null : new StringBuffer(value));
131: }
132:
133: /**
134: * Converts the active state into something meant for human consumption.
135: * The method will be called when recursively traversing the instance
136: * tree.
137: *
138: * @param stream is a stream opened and ready for writing. This can also
139: * be a string stream for efficient output.
140: */
141: public void toString(Writer stream) throws IOException {
142: throw new IOException(
143: "method not implemented, please contact vds-support@griphyn.org");
144: }
145:
146: /**
147: * Dumps the state of the current element as XML output. This function
148: * can return the necessary data more efficiently, thus overwriting
149: * the inherited method.
150: *
151: * @param indent is a <code>String</code> of spaces used for pretty
152: * printing. The initial amount of spaces should be an empty string.
153: * The parameter is used internally for the recursive traversal.
154: *
155: * @return a String which contains the state of the current class and
156: * its siblings using XML. Note that these strings might become large.
157: */
158: public String toXML(String indent) {
159: if (m_value != null) {
160: String newline = System.getProperty("line.separator",
161: "\r\n");
162: StringBuffer result = new StringBuffer(
163: m_value.length() + 24);
164:
165: if (indent != null && indent.length() > 0)
166: result.append(indent);
167: result.append("<data truncated=\"");
168: result.append(Boolean.toString(m_truncated));
169: result.append("\">").append(quote(getValue(), false))
170: .append("</data>");
171: if (indent != null)
172: result.append(newline);
173: return result.toString();
174: } else {
175: return new String();
176: }
177: }
178:
179: /**
180: * Dump the state of the current element as XML output. This function
181: * traverses all sibling classes as necessary, and converts the data
182: * into pretty-printed XML output. The stream interface should be able
183: * to handle large output efficiently.
184: *
185: * @param stream is a stream opened and ready for writing. This can also
186: * be a string stream for efficient output.
187: * @param indent is a <code>String</code> of spaces used for pretty
188: * printing. The initial amount of spaces should be an empty string.
189: * The parameter is used internally for the recursive traversal.
190: * If a <code>null</code> value is specified, no indentation nor
191: * linefeeds will be generated.
192: * @param namespace is the XML schema namespace prefix. If neither
193: * empty nor null, each element will be prefixed with this prefix,
194: * and the root element will map the XML namespace.
195: * @exception IOException if something fishy happens to the stream.
196: */
197: public void toXML(Writer stream, String indent, String namespace)
198: throws IOException {
199: if (this .m_value != null) {
200: String tag = (namespace != null && namespace.length() > 0) ? namespace
201: + ":data"
202: : "data";
203:
204: // open tag
205: if (indent != null && indent.length() > 0)
206: stream.write(indent);
207: stream.write('<');
208: stream.write(tag);
209: writeAttribute(stream, " truncated=\"", Boolean
210: .toString(m_truncated));
211: stream.write('>');
212:
213: // dump content
214: stream.write(quote(getValue(), false));
215:
216: // close tag
217: stream.write("</");
218: stream.write(tag);
219: stream.write('>');
220: if (indent != null)
221: stream.write(System.getProperty("line.separator",
222: "\r\n"));
223: }
224: }
225: }
|