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