001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.utils.regex;
059:
060: import java.util.Vector;
061:
062: /**
063: */
064: class Op {
065: static final int DOT = 0;
066: static final int CHAR = 1; // Single character
067: static final int RANGE = 3; // [a-zA-Z]
068: static final int NRANGE = 4; // [^a-zA-Z]
069: static final int ANCHOR = 5; // ^ $ ...
070: static final int STRING = 6; // literal String
071: static final int CLOSURE = 7; // X*
072: static final int NONGREEDYCLOSURE = 8; // X*?
073: static final int QUESTION = 9; // X?
074: static final int NONGREEDYQUESTION = 10; // X??
075: static final int UNION = 11; // X|Y
076: static final int CAPTURE = 15; // ( and )
077: static final int BACKREFERENCE = 16; // \1 \2 ...
078: static final int LOOKAHEAD = 20; // (?=...)
079: static final int NEGATIVELOOKAHEAD = 21; // (?!...)
080: static final int LOOKBEHIND = 22; // (?<=...)
081: static final int NEGATIVELOOKBEHIND = 23; // (?<!...)
082: static final int INDEPENDENT = 24; // (?>...)
083: static final int MODIFIER = 25; // (?ims-ims:...)
084: static final int CONDITION = 26; // (?(..)yes|no)
085:
086: static int nofinstances = 0;
087: static final boolean COUNT = false;
088:
089: static Op createDot() {
090: if (Op.COUNT)
091: Op.nofinstances++;
092: return new Op(Op.DOT);
093: }
094:
095: static CharOp createChar(int data) {
096: if (Op.COUNT)
097: Op.nofinstances++;
098: return new CharOp(Op.CHAR, data);
099: }
100:
101: static CharOp createAnchor(int data) {
102: if (Op.COUNT)
103: Op.nofinstances++;
104: return new CharOp(Op.ANCHOR, data);
105: }
106:
107: static CharOp createCapture(int number, Op next) {
108: if (Op.COUNT)
109: Op.nofinstances++;
110: CharOp op = new CharOp(Op.CAPTURE, number);
111: op.next = next;
112: return op;
113: }
114:
115: static UnionOp createUnion(int size) {
116: if (Op.COUNT)
117: Op.nofinstances++;
118: //System.err.println("Creates UnionOp");
119: return new UnionOp(Op.UNION, size);
120: }
121:
122: static ChildOp createClosure(int id) {
123: if (Op.COUNT)
124: Op.nofinstances++;
125: return new ModifierOp(Op.CLOSURE, id, -1);
126: }
127:
128: static ChildOp createNonGreedyClosure() {
129: if (Op.COUNT)
130: Op.nofinstances++;
131: return new ChildOp(Op.NONGREEDYCLOSURE);
132: }
133:
134: static ChildOp createQuestion(boolean nongreedy) {
135: if (Op.COUNT)
136: Op.nofinstances++;
137: return new ChildOp(nongreedy ? Op.NONGREEDYQUESTION
138: : Op.QUESTION);
139: }
140:
141: static RangeOp createRange(Token tok) {
142: if (Op.COUNT)
143: Op.nofinstances++;
144: return new RangeOp(Op.RANGE, tok);
145: }
146:
147: static ChildOp createLook(int type, Op next, Op branch) {
148: if (Op.COUNT)
149: Op.nofinstances++;
150: ChildOp op = new ChildOp(type);
151: op.setChild(branch);
152: op.next = next;
153: return op;
154: }
155:
156: static CharOp createBackReference(int refno) {
157: if (Op.COUNT)
158: Op.nofinstances++;
159: return new CharOp(Op.BACKREFERENCE, refno);
160: }
161:
162: static StringOp createString(String literal) {
163: if (Op.COUNT)
164: Op.nofinstances++;
165: return new StringOp(Op.STRING, literal);
166: }
167:
168: static ChildOp createIndependent(Op next, Op branch) {
169: if (Op.COUNT)
170: Op.nofinstances++;
171: ChildOp op = new ChildOp(Op.INDEPENDENT);
172: op.setChild(branch);
173: op.next = next;
174: return op;
175: }
176:
177: static ModifierOp createModifier(Op next, Op branch, int add,
178: int mask) {
179: if (Op.COUNT)
180: Op.nofinstances++;
181: ModifierOp op = new ModifierOp(Op.MODIFIER, add, mask);
182: op.setChild(branch);
183: op.next = next;
184: return op;
185: }
186:
187: static ConditionOp createCondition(Op next, int ref,
188: Op conditionflow, Op yesflow, Op noflow) {
189: if (Op.COUNT)
190: Op.nofinstances++;
191: ConditionOp op = new ConditionOp(Op.CONDITION, ref,
192: conditionflow, yesflow, noflow);
193: op.next = next;
194: return op;
195: }
196:
197: int type;
198: Op next = null;
199:
200: protected Op(int type) {
201: this .type = type;
202: }
203:
204: int size() { // for UNION
205: return 0;
206: }
207:
208: Op elementAt(int index) { // for UNIoN
209: throw new RuntimeException("Internal Error: type=" + this .type);
210: }
211:
212: Op getChild() { // for CLOSURE, QUESTION
213: throw new RuntimeException("Internal Error: type=" + this .type);
214: }
215:
216: // ModifierOp
217: int getData() { // CharOp for CHAR, BACKREFERENCE, CAPTURE, ANCHOR,
218: throw new RuntimeException("Internal Error: type=" + this .type);
219: }
220:
221: int getData2() { // ModifierOp
222: throw new RuntimeException("Internal Error: type=" + this .type);
223: }
224:
225: RangeToken getToken() { // RANGE, NRANGE
226: throw new RuntimeException("Internal Error: type=" + this .type);
227: }
228:
229: String getString() { // STRING
230: throw new RuntimeException("Internal Error: type=" + this .type);
231: }
232:
233: // ================================================================
234: static class CharOp extends Op {
235: int charData;
236:
237: CharOp(int type, int data) {
238: super (type);
239: this .charData = data;
240: }
241:
242: int getData() {
243: return this .charData;
244: }
245: }
246:
247: // ================================================================
248: static class UnionOp extends Op {
249: Vector branches;
250:
251: UnionOp(int type, int size) {
252: super (type);
253: this .branches = new Vector(size);
254: }
255:
256: void addElement(Op op) {
257: this .branches.addElement(op);
258: }
259:
260: int size() {
261: return this .branches.size();
262: }
263:
264: Op elementAt(int index) {
265: return (Op) this .branches.elementAt(index);
266: }
267: }
268:
269: // ================================================================
270: static class ChildOp extends Op {
271: Op child;
272:
273: ChildOp(int type) {
274: super (type);
275: }
276:
277: void setChild(Op child) {
278: this .child = child;
279: }
280:
281: Op getChild() {
282: return this .child;
283: }
284: }
285:
286: // ================================================================
287: static class ModifierOp extends ChildOp {
288: int v1;
289: int v2;
290:
291: ModifierOp(int type, int v1, int v2) {
292: super (type);
293: this .v1 = v1;
294: this .v2 = v2;
295: }
296:
297: int getData() {
298: return this .v1;
299: }
300:
301: int getData2() {
302: return this .v2;
303: }
304: }
305:
306: // ================================================================
307: static class RangeOp extends Op {
308: Token tok;
309:
310: RangeOp(int type, Token tok) {
311: super (type);
312: this .tok = tok;
313: }
314:
315: RangeToken getToken() {
316: return (RangeToken) this .tok;
317: }
318: }
319:
320: // ================================================================
321: static class StringOp extends Op {
322: String string;
323:
324: StringOp(int type, String literal) {
325: super (type);
326: this .string = literal;
327: }
328:
329: String getString() {
330: return this .string;
331: }
332: }
333:
334: // ================================================================
335: static class ConditionOp extends Op {
336: int refNumber;
337: Op condition;
338: Op yes;
339: Op no;
340:
341: ConditionOp(int type, int refno, Op conditionflow, Op yesflow,
342: Op noflow) {
343: super(type);
344: this.refNumber = refno;
345: this.condition = conditionflow;
346: this.yes = yesflow;
347: this.no = noflow;
348: }
349: }
350: }
|