001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * AttribTokenizer.java
044: *
045: * Created on October 22, 2001, 1:40 PM
046: */
047:
048: package org.netbeans.xtest.harness;
049:
050: import java.io.StringReader;
051: import java.io.PushbackReader;
052: import java.io.IOException;
053: import java.util.ArrayList;
054:
055: /**
056: *
057: * @author mk97936
058: * @version
059: */
060: public class AttribTokenizer extends PushbackReader implements
061: AttribTypes {
062:
063: private ArrayList tokens = new ArrayList();
064: private int curpos = -1;
065:
066: /** Creates new AttribTokenizer */
067: public AttribTokenizer(String str) {
068:
069: super (new StringReader(str));
070:
071: AToken atok = getNextToken0();
072: tokens.add(atok);
073: do {
074: atok = getNextToken0();
075: tokens.add(atok);
076: } while (atok.type != EOF);
077:
078: }
079:
080: public AToken getNextToken() {
081: AToken rat = (AToken) tokens.get(++curpos);
082: return (rat);
083: }
084:
085: public AToken getPeakToken() {
086: AToken rat = (AToken) tokens.get(curpos + 1);
087: return rat;
088: }
089:
090: private AToken getNextToken0() {
091:
092: String retstr = null;
093: int rettp = 0;
094:
095: try {
096:
097: int irc = read();
098: char rc = (char) irc;
099:
100: if (Character.isWhitespace(rc)) {
101: // ignore whitespaces
102: while (Character.isWhitespace(rc)) {
103: rc = (char) read();
104: }
105: }
106:
107: if (irc == -1 || irc == 65535) { // EOF
108:
109: retstr = new String("EOF");
110: rettp = EOF;
111:
112: } else if (rc == '(') { // OP_PAR
113:
114: retstr = new String("(");
115: rettp = OP_PAR;
116:
117: } else if (rc == ')') { // CL_PAR
118:
119: retstr = new String(")");
120: rettp = CL_PAR;
121:
122: } else if (rc == '!') { // LOG_NOT
123:
124: retstr = new String("!");
125: rettp = LOG_NOT;
126:
127: } else if (rc == '|') { // LOG_OR
128:
129: rc = (char) read();
130: if (rc == '|') {
131: retstr = new String("||");
132: rettp = LOG_OR;
133: } else {
134: unread(rc);
135: }
136:
137: } else if (rc == '&') { // LOG_AND
138:
139: rc = (char) read();
140: if (rc == '&') {
141: retstr = new String("&&");
142: rettp = LOG_AND;
143: } else {
144: unread(rc);
145: }
146:
147: } else if (rc == ',') { // LOG_OR
148:
149: char[] chp = new char[] { ',' };
150: retstr = new String(chp);
151: rettp = LOG_OR;
152:
153: } else if ((rc >= 'a' && rc <= 'z')
154: || (rc >= 'A' && rc <= 'Z')
155: || (rc >= '0' && rc <= '9') || rc == '-'
156: || rc == '_') { // ATTRIBUTE
157:
158: StringBuffer strb = new StringBuffer();
159: while ((rc >= 'a' && rc <= 'z')
160: || (rc >= 'A' && rc <= 'Z')
161: || (rc >= '0' && rc <= '9') || rc == '-'
162: || rc == '_') {
163: strb.append(rc);
164: rc = (char) read();
165: }
166: unread(rc);
167:
168: if (strb.toString().equalsIgnoreCase("and")) {
169: rettp = LOG_AND;
170: retstr = strb.toString();
171: } else if (strb.toString().equalsIgnoreCase("or")) {
172: rettp = LOG_OR;
173: retstr = strb.toString();
174: } else if (strb.toString().equalsIgnoreCase("not")) {
175: rettp = LOG_NOT;
176: retstr = strb.toString();
177: } else {
178: rettp = ATTR;
179: retstr = strb.toString();
180: }
181:
182: } else {
183: retstr = null;
184: rettp = 0;
185: }
186:
187: return new AToken(rettp, retstr);
188:
189: } catch (IOException ioe) {
190: ioe.printStackTrace();
191: }
192:
193: return null;
194: }
195:
196: public class AToken {
197:
198: public int type;
199: public String value;
200:
201: public AToken(int tp, String val) {
202: type = tp;
203: value = val;
204: }
205:
206: }
207:
208: public static void main(String[] args) {
209:
210: //AttribTokenizer at = new AttribTokenizer("(!((NOT(amos)and(jouda OR smoula AND not prd)||(trouba))))");
211: //AToken atok = at.getNextToken();
212: //while ( atok.type != EOF ) {
213: //
214: // switch ( atok.type ) {
215: // case LOG_AND: System.out.println( "LOG_AND: " + atok.value ); break;
216: // case LOG_OR: System.out.println( "LOG_OR: " + atok.value ); break;
217: // case LOG_NOT: System.out.println( "LOG_NOT: " + atok.value ); break;
218: // case OP_PAR: System.out.println( "OP_PAR: " + atok.value ); break;
219: // case CL_PAR: System.out.println( "CL_PAR: " + atok.value ); break;
220: // case ATTR: System.out.println( "ATTR: " + atok.value ); break;
221: // case ERR: System.out.println( "ERR: " + atok.value ); break;
222: // case EOF: System.out.println( "EOF: " + atok.value ); break;
223: // }
224: //
225: // atok = at.getNextToken();
226: //
227: //}
228: }
229:
230: }
|