KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > oro > text > awk > AwkPattern


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

59
60 import java.io.Serializable JavaDoc;
61 import java.util.*;
62
63 import org.apache.oro.text.regex.*;
64
65
66 final class DFAState {
67   int _stateNumber;
68   BitSet _state;
69
70   DFAState(BitSet s, int num){
71     _state = s;
72     _stateNumber = num;
73   }
74 }
75
76 /**
77  * An implementation of the Pattern interface for Awk regular expressions.
78  * This class is compatible with the AwkCompiler and AwkMatcher
79  * classes. When an AwkCompiler instance compiles a regular expression
80  * pattern, it produces an AwkPattern instance containing internal
81  * data structures used by AwkMatcher to perform pattern matches.
82  * This class cannot be subclassed and cannot be directly instantiated
83  * by the programmer as it would not make sense. It is however serializable
84  * so that pre-compiled patterns may be saved to disk and re-read at a later
85  * time. AwkPattern instances should only be created through calls to an
86  * AwkCompiler instance's compile() methods
87
88  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
89  @version $Id: AwkPattern.java,v 1.1.1.1 2000/07/23 23:08:50 jon Exp $
90
91  * @see AwkCompiler
92  * @see AwkMatcher
93  */

94 public final class AwkPattern implements Pattern, Serializable JavaDoc {
95   final static int _INVALID_STATE = -1, _START_STATE = 1;
96
97   int _numStates, _endPosition, _options;
98   String JavaDoc _expression;
99   Vector _Dtrans, _nodeList[], _stateList;
100   BitSet _U, _emptySet, _followSet[], _endStates;
101   Hashtable _stateMap;
102   boolean _matchesNullString, _fastMap[];
103   boolean _hasBeginAnchor = false, _hasEndAnchor = false;
104
105   AwkPattern(String JavaDoc expression, SyntaxTree tree){
106     int token, node, tstateArray[];
107     DFAState dfaState;
108
109     _expression = expression;
110
111     // Assume endPosition always occurs at end of parse.
112
_endPosition = tree._positions - 1;
113     _followSet = tree._followSet;
114
115     _Dtrans = new Vector();
116     _stateList = new Vector();
117     _endStates = new BitSet();
118
119     _U = new BitSet(tree._positions);
120     _U.or(tree._root._firstPosition());
121
122     tstateArray = new int[LeafNode._NUM_TOKENS];
123     _Dtrans.addElement(tstateArray); // this is a dummy entry because we
124
// number our states starting from 1
125
_Dtrans.addElement(tstateArray);
126
127     _numStates = _START_STATE;
128     if(_U.get(_endPosition))
129       _endStates.set(_numStates);
130     dfaState = new DFAState((BitSet)_U.clone(), _numStates);
131     _stateMap = new Hashtable();
132     _stateMap.put(dfaState._state, dfaState);
133     _stateList.addElement(dfaState); // this is a dummy entry because we
134
// number our states starting from 1
135
_stateList.addElement(dfaState);
136     _numStates++;
137
138     _U.xor(_U); // clear bits
139
_emptySet = new BitSet(tree._positions);
140
141     _nodeList = new Vector[LeafNode._NUM_TOKENS];
142     for(token = 0; token < LeafNode._NUM_TOKENS; token++){
143       _nodeList[token] = new Vector();
144       for(node=0; node < tree._positions; node++)
145     if(tree._nodes[node]._matches((char)token))
146       _nodeList[token].addElement(tree._nodes[node]);
147     }
148
149     _fastMap = tree.createFastMap();
150     _matchesNullString = _endStates.get(_START_STATE);
151   }
152
153   // tstateArray is assumed to have been set before calling this method
154
void _createNewState(int current, int token, int[] tstateArray) {
155     int node, pos;
156     DFAState T, dfaState;
157
158     T = (DFAState)_stateList.elementAt(current);
159     node = _nodeList[token].size();
160     _U.xor(_U); // clear bits
161
while(node-- > 0){
162       pos = ((LeafNode)_nodeList[token].elementAt(node))._position;
163       if(T._state.get(pos))
164     _U.or(_followSet[pos]);
165     }
166
167     if(!_stateMap.containsKey(_U)){
168       dfaState = new DFAState((BitSet)_U.clone(), _numStates++);
169       _stateList.addElement(dfaState);
170       _stateMap.put(dfaState._state, dfaState);
171       _Dtrans.addElement(new int[LeafNode._NUM_TOKENS]);
172
173       if(!_U.equals(_emptySet)){
174     tstateArray[token] = _numStates - 1;
175
176     if(_U.get(_endPosition))
177       _endStates.set(_numStates - 1);
178       } else
179     tstateArray[token] = _INVALID_STATE;
180     } else {
181       if(_U.equals(_emptySet))
182     tstateArray[token] = _INVALID_STATE;
183       else
184     tstateArray[token] = ((DFAState)_stateMap.get(_U))._stateNumber;
185     }
186   }
187
188   int[] _getStateArray(int state) { return ((int[])_Dtrans.elementAt(state)); }
189
190
191   /**
192    * This method returns the string representation of the pattern.
193    * <p>
194    * @return The original string representation of the regular expression
195    * pattern.
196    */

197   public String JavaDoc getPattern() { return _expression; }
198
199
200   /**
201    * This method returns an integer containing the compilation options used
202    * to compile this pattern.
203    * <p>
204    * @return The compilation options used to compile the pattern.
205    */

206   public int getOptions() { return _options; }
207 }
208
209
Popular Tags