KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xpath > regex > Op


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xpath.regex;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * @xerces.internal
23  *
24  * @version $Id: Op.java,v 1.5 2004/10/04 22:07:40 mrglavas Exp $
25  */

26 class Op {
27     static final int DOT = 0;
28     static final int CHAR = 1; // Single character
29
static final int RANGE = 3; // [a-zA-Z]
30
static final int NRANGE = 4; // [^a-zA-Z]
31
static final int ANCHOR = 5; // ^ $ ...
32
static final int STRING = 6; // literal String
33
static final int CLOSURE = 7; // X*
34
static final int NONGREEDYCLOSURE = 8; // X*?
35
static final int QUESTION = 9; // X?
36
static final int NONGREEDYQUESTION = 10; // X??
37
static final int UNION = 11; // X|Y
38
static final int CAPTURE = 15; // ( and )
39
static final int BACKREFERENCE = 16; // \1 \2 ...
40
static final int LOOKAHEAD = 20; // (?=...)
41
static final int NEGATIVELOOKAHEAD = 21; // (?!...)
42
static final int LOOKBEHIND = 22; // (?<=...)
43
static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
44
static final int INDEPENDENT = 24; // (?>...)
45
static final int MODIFIER = 25; // (?ims-ims:...)
46
static final int CONDITION = 26; // (?(..)yes|no)
47

48     static int nofinstances = 0;
49     static final boolean COUNT = false;
50
51     static Op createDot() {
52         if (Op.COUNT) Op.nofinstances ++;
53         return new Op(Op.DOT);
54     }
55     static CharOp createChar(int data) {
56         if (Op.COUNT) Op.nofinstances ++;
57         return new CharOp(Op.CHAR, data);
58     }
59     static CharOp createAnchor(int data) {
60         if (Op.COUNT) Op.nofinstances ++;
61         return new CharOp(Op.ANCHOR, data);
62     }
63     static CharOp createCapture(int number, Op next) {
64         if (Op.COUNT) Op.nofinstances ++;
65         CharOp op = new CharOp(Op.CAPTURE, number);
66         op.next = next;
67         return op;
68     }
69     static UnionOp createUnion(int size) {
70         if (Op.COUNT) Op.nofinstances ++;
71         //System.err.println("Creates UnionOp");
72
return new UnionOp(Op.UNION, size);
73     }
74     static ChildOp createClosure(int id) {
75         if (Op.COUNT) Op.nofinstances ++;
76         return new ModifierOp(Op.CLOSURE, id, -1);
77     }
78     static ChildOp createNonGreedyClosure() {
79         if (Op.COUNT) Op.nofinstances ++;
80         return new ChildOp(Op.NONGREEDYCLOSURE);
81     }
82     static ChildOp createQuestion(boolean nongreedy) {
83         if (Op.COUNT) Op.nofinstances ++;
84         return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION : Op.QUESTION);
85     }
86     static RangeOp createRange(Token tok) {
87         if (Op.COUNT) Op.nofinstances ++;
88         return new RangeOp(Op.RANGE, tok);
89     }
90     static ChildOp createLook(int type, Op next, Op branch) {
91         if (Op.COUNT) Op.nofinstances ++;
92         ChildOp op = new ChildOp(type);
93         op.setChild(branch);
94         op.next = next;
95         return op;
96     }
97     static CharOp createBackReference(int refno) {
98         if (Op.COUNT) Op.nofinstances ++;
99         return new CharOp(Op.BACKREFERENCE, refno);
100     }
101     static StringOp createString(String JavaDoc literal) {
102         if (Op.COUNT) Op.nofinstances ++;
103         return new StringOp(Op.STRING, literal);
104     }
105     static ChildOp createIndependent(Op next, Op branch) {
106         if (Op.COUNT) Op.nofinstances ++;
107         ChildOp op = new ChildOp(Op.INDEPENDENT);
108         op.setChild(branch);
109         op.next = next;
110         return op;
111     }
112     static ModifierOp createModifier(Op next, Op branch, int add, int mask) {
113         if (Op.COUNT) Op.nofinstances ++;
114         ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
115         op.setChild(branch);
116         op.next = next;
117         return op;
118     }
119     static ConditionOp createCondition(Op next, int ref, Op conditionflow, Op yesflow, Op noflow) {
120         if (Op.COUNT) Op.nofinstances ++;
121         ConditionOp op = new ConditionOp(Op.CONDITION, ref, conditionflow, yesflow, noflow);
122         op.next = next;
123         return op;
124     }
125
126     int type;
127     Op next = null;
128
129     protected Op(int type) {
130         this.type = type;
131     }
132
133     int size() { // for UNION
134
return 0;
135     }
136     Op elementAt(int index) { // for UNIoN
137
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
138     }
139     Op getChild() { // for CLOSURE, QUESTION
140
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
141     }
142                                                 // ModifierOp
143
int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR,
144
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
145     }
146     int getData2() { // ModifierOp
147
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
148     }
149     RangeToken getToken() { // RANGE, NRANGE
150
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
151     }
152     String JavaDoc getString() { // STRING
153
throw new RuntimeException JavaDoc("Internal Error: type="+this.type);
154     }
155
156     // ================================================================
157
static class CharOp extends Op {
158         int charData;
159         CharOp(int type, int data) {
160             super(type);
161             this.charData = data;
162         }
163         int getData() {
164             return this.charData;
165         }
166     }
167
168     // ================================================================
169
static class UnionOp extends Op {
170         Vector JavaDoc branches;
171         UnionOp(int type, int size) {
172             super(type);
173             this.branches = new Vector JavaDoc(size);
174         }
175         void addElement(Op op) {
176             this.branches.addElement(op);
177         }
178         int size() {
179             return this.branches.size();
180         }
181         Op elementAt(int index) {
182             return (Op)this.branches.elementAt(index);
183         }
184     }
185
186     // ================================================================
187
static class ChildOp extends Op {
188         Op child;
189         ChildOp(int type) {
190             super(type);
191         }
192         void setChild(Op child) {
193             this.child = child;
194         }
195         Op getChild() {
196             return this.child;
197         }
198     }
199     // ================================================================
200
static class ModifierOp extends ChildOp {
201         int v1;
202         int v2;
203         ModifierOp(int type, int v1, int v2) {
204             super(type);
205             this.v1 = v1;
206             this.v2 = v2;
207         }
208         int getData() {
209             return this.v1;
210         }
211         int getData2() {
212             return this.v2;
213         }
214     }
215     // ================================================================
216
static class RangeOp extends Op {
217         Token tok;
218         RangeOp(int type, Token tok) {
219             super(type);
220             this.tok = tok;
221         }
222         RangeToken getToken() {
223             return (RangeToken)this.tok;
224         }
225     }
226     // ================================================================
227
static class StringOp extends Op {
228         String JavaDoc string;
229         StringOp(int type, String JavaDoc literal) {
230             super(type);
231             this.string = literal;
232         }
233         String JavaDoc getString() {
234             return this.string;
235         }
236     }
237     // ================================================================
238
static class ConditionOp extends Op {
239         int refNumber;
240         Op condition;
241         Op yes;
242         Op no;
243         ConditionOp(int type, int refno, Op conditionflow, Op yesflow, Op noflow) {
244             super(type);
245             this.refNumber = refno;
246             this.condition = conditionflow;
247             this.yes = yesflow;
248             this.no = noflow;
249         }
250     }
251 }
252
Popular Tags