KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > regexp > internal > REProgram


1 package com.sun.org.apache.regexp.internal;
2
3 /*
4  * ====================================================================
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 1999 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "The Jakarta Project", "Jakarta-Regexp", and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  *
58  */

59
60 import com.sun.org.apache.regexp.internal.RE;
61 import java.util.Hashtable JavaDoc;
62
63 /**
64  * A class that holds compiled regular expressions. This is exposed mainly
65  * for use by the recompile utility (which helps you produce precompiled
66  * REProgram objects). You should not otherwise need to work directly with
67  * this class.
68 *
69  * @see RE
70  * @see RECompiler
71  *
72  * @author <a HREF="mailto:jonl@muppetlabs.com">Jonathan Locke</a>
73  * @version $Id: REProgram.java,v 1.1 2000/04/27 01:22:33 jon Exp $
74  */

75 public class REProgram
76 {
77     static final int OPT_HASBACKREFS = 1;
78
79     char[] instruction; // The compiled regular expression 'program'
80
int lenInstruction; // The amount of the instruction buffer in use
81
char[] prefix; // Prefix string optimization
82
int flags; // Optimization flags (REProgram.OPT_*)
83

84     /**
85      * Constructs a program object from a character array
86      * @param instruction Character array with RE opcode instructions in it
87      */

88     public REProgram(char[] instruction)
89     {
90         this(instruction, instruction.length);
91     }
92
93     /**
94      * Constructs a program object from a character array
95      * @param instruction Character array with RE opcode instructions in it
96      * @param lenInstruction Amount of instruction array in use
97      */

98     public REProgram(char[] instruction, int lenInstruction)
99     {
100         setInstructions(instruction, lenInstruction);
101     }
102
103     /**
104      * Returns a copy of the current regular expression program in a character
105      * array that is exactly the right length to hold the program. If there is
106      * no program compiled yet, getInstructions() will return null.
107      * @return A copy of the current compiled RE program
108      */

109     public char[] getInstructions()
110     {
111         // Ensure program has been compiled!
112
if (lenInstruction != 0)
113         {
114             // Return copy of program
115
char[] ret = new char[lenInstruction];
116             System.arraycopy(instruction, 0, ret, 0, lenInstruction);
117             return ret;
118         }
119         return null;
120     }
121
122     /**
123      * Sets a new regular expression program to run. It is this method which
124      * performs any special compile-time search optimizations. Currently only
125      * two optimizations are in place - one which checks for backreferences
126      * (so that they can be lazily allocated) and another which attempts to
127      * find an prefix anchor string so that substantial amounts of input can
128      * potentially be skipped without running the actual program.
129      * @param instruction Program instruction buffer
130      * @param lenInstruction Length of instruction buffer in use
131      */

132     public void setInstructions(char[] instruction, int lenInstruction)
133     {
134         // Save reference to instruction array
135
this.instruction = instruction;
136         this.lenInstruction = lenInstruction;
137
138         // Initialize other program-related variables
139
flags = 0;
140         prefix = null;
141
142         // Try various compile-time optimizations if there's a program
143
if (instruction != null && lenInstruction != 0)
144         {
145             // If the first node is a branch
146
if (lenInstruction >= RE.nodeSize && instruction[0 + RE.offsetOpcode] == RE.OP_BRANCH)
147             {
148                 // to the end node
149
int next = instruction[0 + RE.offsetNext];
150                 if (instruction[next + RE.offsetOpcode] == RE.OP_END)
151                 {
152                     // and the branch starts with an atom
153
if (lenInstruction >= (RE.nodeSize * 2) && instruction[RE.nodeSize + RE.offsetOpcode] == RE.OP_ATOM)
154                     {
155                         // then get that atom as an prefix because there's no other choice
156
int lenAtom = instruction[RE.nodeSize + RE.offsetOpdata];
157                         prefix = new char[lenAtom];
158                         System.arraycopy(instruction, RE.nodeSize * 2, prefix, 0, lenAtom);
159                     }
160                 }
161             }
162
163             BackrefScanLoop:
164
165             // Check for backreferences
166
for (int i = 0; i < lenInstruction; i += RE.nodeSize)
167             {
168                 switch (instruction[i + RE.offsetOpcode])
169                 {
170                     case RE.OP_ANYOF:
171                         i += (instruction[i + RE.offsetOpdata] * 2);
172                         break;
173
174                     case RE.OP_ATOM:
175                         i += instruction[i + RE.offsetOpdata];
176                         break;
177
178                     case RE.OP_BACKREF:
179                         flags |= OPT_HASBACKREFS;
180                         break BackrefScanLoop;
181                 }
182             }
183         }
184     }
185 }
186
Popular Tags