KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > parser > re > Element


1 /*
2  * Element.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica.parser.re;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27
28 import net.percederberg.grammatica.parser.LookAheadReader;
29
30 /**
31  * A regular expression element. This is the common base class for all
32  * regular expression elements, i.e. the parts of the regular
33  * expression.
34  *
35  * @author Per Cederberg, <per at percederberg dot net>
36  * @version 1.5
37  */

38 abstract class Element implements Cloneable JavaDoc {
39
40     /**
41      * Creates a copy of this element. The copy will be an instance
42      * of the same class matching the same strings. Copies of elements
43      * are necessary to allow elements to cache intermediate results
44      * while matching strings without interfering with other threads.
45      *
46      * @return a copy of this element
47      */

48     public abstract Object JavaDoc clone();
49
50     /**
51      * Returns the length of a matching string starting at the
52      * specified position. The number of matches to skip can also be
53      * specified, but numbers higher than zero (0) cause a failed
54      * match for any element that doesn't attempt to combine other
55      * elements.
56      *
57      * @param m the matcher being used
58      * @param input the input character stream to match
59      * @param start the starting position
60      * @param skip the number of matches to skip
61      *
62      * @return the length of the longest matching string, or
63      * -1 if no match was found
64      *
65      * @throws IOException if an I/O error occurred
66      */

67     public abstract int match(Matcher m,
68                               LookAheadReader input,
69                               int start,
70                               int skip)
71         throws IOException JavaDoc;
72
73     /**
74      * Prints this element to the specified output stream.
75      *
76      * @param output the output stream to use
77      * @param indent the current indentation
78      */

79     public final void printTo(PrintStream JavaDoc output, String JavaDoc indent) {
80         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(output);
81
82         printTo(writer, indent);
83         writer.flush();
84     }
85
86     /**
87      * Prints this element to the specified output stream.
88      *
89      * @param output the output stream to use
90      * @param indent the current indentation
91      */

92     public abstract void printTo(PrintWriter JavaDoc output, String JavaDoc indent);
93
94 }
95
Popular Tags