001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xpath.regex;
019:
020: import java.util.Vector;
021:
022: /**
023: * @xerces.internal
024: *
025: * @version $Id: Op.java 572108 2007-09-02 18:48:31Z mrglavas $
026: */
027: class Op {
028: static final int DOT = 0;
029: static final int CHAR = 1; // Single character
030: static final int RANGE = 3; // [a-zA-Z]
031: static final int NRANGE = 4; // [^a-zA-Z]
032: static final int ANCHOR = 5; // ^ $ ...
033: static final int STRING = 6; // literal String
034: static final int CLOSURE = 7; // X*
035: static final int NONGREEDYCLOSURE = 8; // X*?
036: static final int QUESTION = 9; // X?
037: static final int NONGREEDYQUESTION = 10; // X??
038: static final int UNION = 11; // X|Y
039: static final int CAPTURE = 15; // ( and )
040: static final int BACKREFERENCE = 16; // \1 \2 ...
041: static final int LOOKAHEAD = 20; // (?=...)
042: static final int NEGATIVELOOKAHEAD = 21; // (?!...)
043: static final int LOOKBEHIND = 22; // (?<=...)
044: static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
045: static final int INDEPENDENT = 24; // (?>...)
046: static final int MODIFIER = 25; // (?ims-ims:...)
047: static final int CONDITION = 26; // (?(..)yes|no)
048:
049: static int nofinstances = 0;
050: static final boolean COUNT = false;
051:
052: static Op createDot() {
053: if (Op.COUNT)
054: Op.nofinstances++;
055: return new Op(Op.DOT);
056: }
057:
058: static CharOp createChar(int data) {
059: if (Op.COUNT)
060: Op.nofinstances++;
061: return new CharOp(Op.CHAR, data);
062: }
063:
064: static CharOp createAnchor(int data) {
065: if (Op.COUNT)
066: Op.nofinstances++;
067: return new CharOp(Op.ANCHOR, data);
068: }
069:
070: static CharOp createCapture(int number, Op next) {
071: if (Op.COUNT)
072: Op.nofinstances++;
073: CharOp op = new CharOp(Op.CAPTURE, number);
074: op.next = next;
075: return op;
076: }
077:
078: static UnionOp createUnion(int size) {
079: if (Op.COUNT)
080: Op.nofinstances++;
081: //System.err.println("Creates UnionOp");
082: return new UnionOp(Op.UNION, size);
083: }
084:
085: static ChildOp createClosure(int id) {
086: if (Op.COUNT)
087: Op.nofinstances++;
088: return new ModifierOp(Op.CLOSURE, id, -1);
089: }
090:
091: static ChildOp createNonGreedyClosure() {
092: if (Op.COUNT)
093: Op.nofinstances++;
094: return new ChildOp(Op.NONGREEDYCLOSURE);
095: }
096:
097: static ChildOp createQuestion(boolean nongreedy) {
098: if (Op.COUNT)
099: Op.nofinstances++;
100: return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION
101: : Op.QUESTION);
102: }
103:
104: static RangeOp createRange(Token tok) {
105: if (Op.COUNT)
106: Op.nofinstances++;
107: return new RangeOp(Op.RANGE, tok);
108: }
109:
110: static ChildOp createLook(int type, Op next, Op branch) {
111: if (Op.COUNT)
112: Op.nofinstances++;
113: ChildOp op = new ChildOp(type);
114: op.setChild(branch);
115: op.next = next;
116: return op;
117: }
118:
119: static CharOp createBackReference(int refno) {
120: if (Op.COUNT)
121: Op.nofinstances++;
122: return new CharOp(Op.BACKREFERENCE, refno);
123: }
124:
125: static StringOp createString(String literal) {
126: if (Op.COUNT)
127: Op.nofinstances++;
128: return new StringOp(Op.STRING, literal);
129: }
130:
131: static ChildOp createIndependent(Op next, Op branch) {
132: if (Op.COUNT)
133: Op.nofinstances++;
134: ChildOp op = new ChildOp(Op.INDEPENDENT);
135: op.setChild(branch);
136: op.next = next;
137: return op;
138: }
139:
140: static ModifierOp createModifier(Op next, Op branch, int add,
141: int mask) {
142: if (Op.COUNT)
143: Op.nofinstances++;
144: ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
145: op.setChild(branch);
146: op.next = next;
147: return op;
148: }
149:
150: static ConditionOp createCondition(Op next, int ref,
151: Op conditionflow, Op yesflow, Op noflow) {
152: if (Op.COUNT)
153: Op.nofinstances++;
154: ConditionOp op = new ConditionOp(Op.CONDITION, ref,
155: conditionflow, yesflow, noflow);
156: op.next = next;
157: return op;
158: }
159:
160: final int type;
161: Op next = null;
162:
163: protected Op(int type) {
164: this .type = type;
165: }
166:
167: int size() { // for UNION
168: return 0;
169: }
170:
171: Op elementAt(int index) { // for UNIoN
172: throw new RuntimeException("Internal Error: type=" + this .type);
173: }
174:
175: Op getChild() { // for CLOSURE, QUESTION
176: throw new RuntimeException("Internal Error: type=" + this .type);
177: }
178:
179: // ModifierOp
180: int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR,
181: throw new RuntimeException("Internal Error: type=" + this .type);
182: }
183:
184: int getData2() { // ModifierOp
185: throw new RuntimeException("Internal Error: type=" + this .type);
186: }
187:
188: RangeToken getToken() { // RANGE, NRANGE
189: throw new RuntimeException("Internal Error: type=" + this .type);
190: }
191:
192: String getString() { // STRING
193: throw new RuntimeException("Internal Error: type=" + this .type);
194: }
195:
196: // ================================================================
197: static class CharOp extends Op {
198: final int charData;
199:
200: CharOp(int type, int data) {
201: super (type);
202: this .charData = data;
203: }
204:
205: int getData() {
206: return this .charData;
207: }
208: }
209:
210: // ================================================================
211: static class UnionOp extends Op {
212: final Vector branches;
213:
214: UnionOp(int type, int size) {
215: super (type);
216: this .branches = new Vector(size);
217: }
218:
219: void addElement(Op op) {
220: this .branches.addElement(op);
221: }
222:
223: int size() {
224: return this .branches.size();
225: }
226:
227: Op elementAt(int index) {
228: return (Op) this .branches.elementAt(index);
229: }
230: }
231:
232: // ================================================================
233: static class ChildOp extends Op {
234: Op child;
235:
236: ChildOp(int type) {
237: super (type);
238: }
239:
240: void setChild(Op child) {
241: this .child = child;
242: }
243:
244: Op getChild() {
245: return this .child;
246: }
247: }
248:
249: // ================================================================
250: static class ModifierOp extends ChildOp {
251: final int v1;
252: final int v2;
253:
254: ModifierOp(int type, int v1, int v2) {
255: super (type);
256: this .v1 = v1;
257: this .v2 = v2;
258: }
259:
260: int getData() {
261: return this .v1;
262: }
263:
264: int getData2() {
265: return this .v2;
266: }
267: }
268:
269: // ================================================================
270: static class RangeOp extends Op {
271: final Token tok;
272:
273: RangeOp(int type, Token tok) {
274: super (type);
275: this .tok = tok;
276: }
277:
278: RangeToken getToken() {
279: return (RangeToken) this .tok;
280: }
281: }
282:
283: // ================================================================
284: static class StringOp extends Op {
285: final String string;
286:
287: StringOp(int type, String literal) {
288: super (type);
289: this .string = literal;
290: }
291:
292: String getString() {
293: return this .string;
294: }
295: }
296:
297: // ================================================================
298: static class ConditionOp extends Op {
299: final int refNumber;
300: final Op condition;
301: final Op yes;
302: final Op no;
303:
304: ConditionOp(int type, int refno, Op conditionflow, Op yesflow,
305: Op noflow) {
306: super(type);
307: this.refNumber = refno;
308: this.condition = conditionflow;
309: this.yes = yesflow;
310: this.no = noflow;
311: }
312: }
313: }
|