001: /*
002: ** Java cvs client library package.
003: ** Copyright (c) 1997-2002 by Timothy Gerard Endres
004: **
005: ** This program is free software.
006: **
007: ** You may redistribute it and/or modify it under the terms of the GNU
008: ** Library General Public License (LGPL) as published by the Free Software
009: ** Foundation.
010: **
011: ** Version 2 of the license should be included with this distribution in
012: ** the file LICENSE.txt, as well as License.html. If the license is not
013: ** included with this distribution, you may find a copy at the FSF web
014: ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the Free
015: ** Software Foundation at 59 Temple Place - Suite 330, Boston, MA 02111 USA.
016: **
017: ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
018: ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
019: ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
020: ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
021: ** REDISTRIBUTION OF THIS SOFTWARE.
022: **
023: */
024:
025: package com.ice.cvsc;
026:
027: import java.io.*;
028: import java.lang.*;
029: import java.text.*;
030: import java.util.*;
031:
032: /**
033: * The CVSResponse class encapsulates a CVS server's response to
034: * a request. The response will contain a list of the server's
035: * response lines, as well as all downloaded files (which are
036: * stored in temporary files). Once you are finished with a
037: * CVSResponse <strong>it is important</strong> to call the
038: * <em>deleteTempFile()</em> method of the reponse object,
039: * or temporary files will go undeleted and populate the local
040: * temp directory.
041: *
042: * @version $Revision: 2.3 $
043: * @author Timothy Gerard Endres, <a href="mailto:time@ice.com">time@ice.com</a>.
044: * @see CVSClient
045: * @see CVSRequest
046: */
047:
048: public class CVSResponse extends Object {
049: static public final String RCS_ID = "$Id: CVSResponse.java,v 2.3 2003/07/27 01:08:32 time Exp $";
050: static public final String RCS_REV = "$Revision: 2.3 $";
051:
052: static public final int OK = 0;
053: static public final int ERROR = 1;
054:
055: private boolean valid;
056:
057: private int status;
058:
059: private String errorCode;
060: private String errorText;
061:
062: private StringBuffer stdErrStr;
063: private StringBuffer stdOutStr;
064:
065: private CVSRespItemVector itemList;
066:
067: public CVSResponse() {
068: super ();
069:
070: this .valid = true;
071:
072: this .itemList = new CVSRespItemVector();
073:
074: this .errorCode = "";
075: this .errorText = "";
076:
077: this .stdErrStr = new StringBuffer(4096);
078: this .stdOutStr = new StringBuffer(32);
079: }
080:
081: public void appendStdOut(String text) {
082: this .stdOutStr.append(text);
083: }
084:
085: public void appendStdErr(String text) {
086: this .stdErrStr.append(text);
087: }
088:
089: public void addResponseItem(CVSResponseItem item) {
090: this .itemList.appendItem(item);
091: }
092:
093: public CVSRespItemVector getItemList() {
094: return this .itemList;
095: }
096:
097: public boolean isValid() {
098: return this .valid;
099: }
100:
101: public void setValid(boolean valid) {
102: this .valid = valid;
103: }
104:
105: public int getStatus() {
106: return this .status;
107: }
108:
109: public void setStatus(int status) {
110: this .status = status;
111:
112: this .errorCode = "";
113: this .errorText = "";
114: }
115:
116: public void setErrorStatus(String codeStr, String textStr) {
117: this .status = CVSResponse.ERROR;
118:
119: this .errorCode = codeStr;
120: this .errorText = textStr;
121: }
122:
123: public String getErrorCode() {
124: return this .errorCode;
125: }
126:
127: public String getErrorText() {
128: return this .errorText;
129: }
130:
131: public String getStderr() {
132: return this .stdErrStr.toString();
133: }
134:
135: public String getStdout() {
136: return this .stdOutStr.toString();
137: }
138:
139: public void appendStderr(String msg) {
140: this .stdErrStr.append(msg);
141: }
142:
143: public void appendStdout(String msg) {
144: this .stdOutStr.append(msg);
145: }
146:
147: public int itemTypeCount(int type) {
148: int count = 0;
149: CVSResponseItem item;
150:
151: for (int i = 0; i < this .itemList.size(); ++i) {
152: item = this .itemList.itemAt(i);
153: if (item.getType() == type)
154: count++;
155: }
156:
157: return count;
158: }
159:
160: public CVSResponseItem getFirstItemByType(int type) {
161: CVSResponseItem item;
162:
163: for (int i = 0; i < this .itemList.size(); ++i) {
164: item = this .itemList.itemAt(i);
165: if (item.getType() == type)
166: return item;
167: }
168:
169: return null;
170: }
171:
172: public CVSResponseItem getNextItemByType(int type,
173: CVSResponseItem lastItem) {
174: int i;
175: CVSResponseItem item;
176:
177: for (i = 0; i < this .itemList.size(); ++i) {
178: item = this .itemList.itemAt(i);
179: if (item == lastItem) {
180: ++i;
181: break;
182: }
183: }
184:
185: for (; i < this .itemList.size(); ++i) {
186: item = this .itemList.itemAt(i);
187: if (item.getType() == type)
188: return item;
189: }
190:
191: return null;
192: }
193:
194: public void printResponse(PrintStream out) {
195: out
196: .println("=============================================================");
197:
198: out.println("RESPONSE has " + this .itemList.size() + " items:");
199: if (this .itemList.size() > 0) {
200: this .itemList.printResponseItemList(out, " ");
201: }
202:
203: out.println("\n" + this .getStderr() + "\n" + this .getStdout());
204:
205: out
206: .println("=============================================================");
207: }
208:
209: //
210: // For now we just clean up the temporary files...
211: //
212: public boolean deleteTempFiles() {
213: boolean err;
214: boolean result = true;
215:
216: for (int i = 0; i < this .itemList.size(); ++i) {
217: CVSResponseItem item = this .itemList.itemAt(i);
218:
219: err = item.deleteFile();
220:
221: if (!err)
222: result = false;
223: }
224:
225: return result;
226: }
227:
228: public String getDisplayResults() {
229: StringBuffer finalResult = new StringBuffer(1024);
230:
231: finalResult.append(this .getResultText());
232: finalResult.append(this .getResultStatus());
233:
234: return finalResult.toString();
235: }
236:
237: public String getResultText() {
238: StringBuffer resultBuf = new StringBuffer(1024);
239:
240: String stdout = this .getStdout();
241: String stderr = this .getStderr();
242:
243: if (stderr.length() > 0 || stdout.length() > 0) {
244: if (stderr.length() > 0) {
245: resultBuf.append(stderr);
246: if (stdout.length() > 0)
247: resultBuf.append("\n");
248: }
249:
250: if (stdout.length() > 0) {
251: resultBuf.append(stdout);
252: }
253: }
254:
255: return resultBuf.toString();
256: }
257:
258: public String getResultStatus() {
259: if (this .getStatus() == CVSResponse.OK) {
260: return "\n** The command completed successfully.";
261: } else {
262: return "\n** The command completed with an error status.";
263: }
264: }
265:
266: public String toString() {
267: if (this .valid) {
268: return "CVSResponse: " + this .itemList.size() + " items.\n"
269: + this .stdErrStr + "\n" + this .stdOutStr;
270: } else {
271: return "CVSResponse: not valid";
272: }
273: }
274: }
|