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: * AttribParser.java
044: *
045: * Created on October 23, 2001, 9:40 AM
046: */
047:
048: package org.netbeans.xtest.harness;
049:
050: import org.netbeans.xtest.harness.AttribTokenizer;
051: import org.netbeans.xtest.harness.AttribTokenizer.AToken;
052:
053: import java.util.Collection;
054:
055: /**
056: *
057: * @author mk97936
058: * @version
059: */
060: public class AttribParser implements AttribTypes {
061:
062: private AttribTokenizer tokenizer;
063: private AToken at;
064: private Collection attribs;
065:
066: /** Creates new AttribParser */
067: public AttribParser(String str, Collection attrs) {
068: tokenizer = new AttribTokenizer(str);
069: attribs = attrs;
070: }
071:
072: public boolean parse() {
073: return S();
074: }
075:
076: private boolean S() {
077:
078: boolean G_res = false;
079:
080: G_res = G();
081:
082: if (G_res && compareNext(EOF)) {
083: return true;
084: } else {
085: return false;
086: }
087: }
088:
089: private boolean T() {
090:
091: boolean G_res = false, T_res = false, ret = false;
092:
093: at = tokenizer.getPeakToken();
094: switch (at.type) {
095: case OP_PAR:
096: compareNext(OP_PAR);
097: G_res = G();
098: compareNext(CL_PAR);
099: ret = G_res;
100: break;
101: case ATTR:
102: String rattr = tokenizer.getNextToken().value;
103: if (attribs.contains(rattr)) {
104: ret = true;
105: } else {
106: ret = false;
107: }
108: break;
109: case LOG_NOT:
110: compareNext(LOG_NOT);
111: T_res = T();
112: ret = !T_res;
113: default:
114: ;
115: }
116:
117: return ret;
118:
119: }
120:
121: private boolean F() {
122:
123: boolean F_res = false, T_res = false, ret = false;
124:
125: T_res = T();
126: ret = T_res;
127:
128: at = tokenizer.getPeakToken();
129: switch (at.type) {
130: case LOG_AND:
131: compareNext(LOG_AND);
132: F_res = F();
133: if (T_res && F_res) {
134: ret = true;
135: } else {
136: ret = false;
137: }
138: break;
139: case LOG_OR:
140: compareNext(LOG_OR);
141: F_res = F();
142: if (T_res || F_res) {
143: ret = true;
144: } else {
145: ret = false;
146: }
147: break;
148: default:
149: ;
150: }
151:
152: return ret;
153:
154: }
155:
156: private boolean G() {
157:
158: boolean F_res = false, ret = false;
159:
160: at = tokenizer.getPeakToken();
161: switch (at.type) {
162: case LOG_NOT:
163: compareNext(LOG_NOT);
164: F_res = F();
165: ret = !F_res;
166: break;
167: case OP_PAR:
168: case ATTR:
169: default:
170: F_res = F();
171: ret = F_res;
172: }
173:
174: return ret;
175: }
176:
177: private boolean compareNext(int ct) {
178:
179: at = tokenizer.getNextToken();
180:
181: if (at.type == ct) {
182: return true;
183: } else {
184: return false;
185: }
186:
187: }
188:
189: public static void main(String[] args) {
190: //java.util.ArrayList attriblist = new java.util.ArrayList ();
191: //attriblist.add("stable");
192: //attriblist.add("code");
193: //attriblist.add("oracle");
194: //System.out.println("Result = " + new AttribParser( "(stable OR code) AND not oracle", attriblist ).parse () );
195: }
196:
197: }
|