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.lang.*;
028: import java.util.*;
029:
030: /**
031: * Implements a Vector subclass that handles CVS Arguments used
032: * in CVSRequest objects.
033: *
034: * @version $Revision: 2.3 $
035: * @author Timothy Gerard Endres, <a href="mailto:time@ice.com">time@ice.com</a>.
036: * @see CVSClient
037: * @see CVSProject
038: */
039:
040: public class CVSArgumentVector extends Vector {
041: static public final String RCS_ID = "$Id: CVSArgumentVector.java,v 2.3 2003/07/27 01:08:32 time Exp $";
042: static public final String RCS_REV = "$Revision: 2.3 $";
043:
044: public CVSArgumentVector() {
045: super ();
046: }
047:
048: public CVSArgumentVector(int initCap) {
049: super (initCap);
050: }
051:
052: public CVSArgumentVector(int initCap, int capIncr) {
053: super (initCap, capIncr);
054: }
055:
056: public String argumentAt(int index) {
057: return (String) this .elementAt(index);
058: }
059:
060: public void appendArgument(String argument) {
061: this .addElement(argument);
062: }
063:
064: public void appendArguments(Vector args) {
065: for (int i = 0, sz = args.size(); i < sz; ++i)
066: this .addElement(args.elementAt(i));
067: }
068:
069: public boolean containsArgument(String argument) {
070: int i;
071: String argStr;
072:
073: for (i = 0; i < this .size(); ++i) {
074: argStr = (String) this .elementAt(i);
075:
076: if (argStr.equals(argument))
077: return true;
078:
079: if (argStr.startsWith("-")) {
080: ++i; // skip this argument's parameter
081: }
082: }
083:
084: return false;
085: }
086:
087: public boolean containsString(String string) {
088: int i;
089: String argStr;
090:
091: for (i = 0; i < this .size(); ++i) {
092: argStr = (String) this .elementAt(i);
093:
094: if (argStr.equals(string))
095: return true;
096: }
097:
098: return false;
099: }
100:
101: public static CVSArgumentVector parseArgumentString(String argStr) {
102: String token;
103: String newDelim = null;
104: boolean matchQuote = false;
105:
106: CVSArgumentVector result = new CVSArgumentVector();
107:
108: StringTokenizer toker = new StringTokenizer(argStr, " '\"",
109: true);
110:
111: boolean startArg = true;
112: StringBuffer argBuf = new StringBuffer(argStr.length());
113:
114: for (; toker.hasMoreTokens();) {
115: try {
116: token = (newDelim == null ? toker.nextToken() : toker
117: .nextToken(newDelim));
118:
119: newDelim = null;
120: } catch (NoSuchElementException ex) {
121: break;
122: }
123:
124: if (token.equals(" ")) {
125: if (!startArg) {
126: result.addElement(argBuf.toString());
127: argBuf.setLength(0);
128: }
129: startArg = true;
130: continue;
131: } else if (token.equals("'")) {
132: startArg = false;
133: if (matchQuote) {
134: newDelim = " '\"";
135: matchQuote = false;
136: } else {
137: newDelim = "'";
138: matchQuote = true;
139: }
140: } else if (token.equals("\"")) {
141: startArg = false;
142: if (matchQuote) {
143: newDelim = " '\"";
144: matchQuote = false;
145: } else {
146: newDelim = "\"";
147: matchQuote = true;
148: }
149: } else {
150: startArg = false;
151: argBuf.append(token);
152: }
153: }
154:
155: if (!startArg) {
156: result.addElement(argBuf.toString());
157: }
158:
159: return result;
160: }
161:
162: }
|