KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * StringElement.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.PrintWriter JavaDoc;
26
27 import net.percederberg.grammatica.parser.LookAheadReader;
28
29 /**
30  * A regular expression string element. This element only matches an
31  * exact string. Once created, the string element is immutable.
32  *
33  * @author Per Cederberg, <per at percederberg dot net>
34  * @version 1.5
35  */

36 class StringElement extends Element {
37
38     /**
39      * The string to match with.
40      */

41     private String JavaDoc value = null;
42
43     /**
44      * Creates a new string element.
45      *
46      * @param c the character to match with
47      */

48     public StringElement(char c) {
49         this(String.valueOf(c));
50     }
51
52     /**
53      * Creates a new string element.
54      *
55      * @param str the string to match with
56      */

57     public StringElement(String JavaDoc str) {
58         value = str;
59     }
60
61     /**
62      * Returns the string to be matched.
63      *
64      * @return the string to be matched
65      */

66     public String JavaDoc getString() {
67         return value;
68     }
69
70     /**
71      * Returns this element as it is immutable.
72      *
73      * @return this string element
74      */

75     public Object JavaDoc clone() {
76         return this;
77     }
78
79     /**
80      * Returns the length of a matching string starting at the
81      * specified position. The number of matches to skip can also be
82      * specified, but numbers higher than zero (0) cause a failed
83      * match for any element that doesn't attempt to combine other
84      * elements.
85      *
86      * @param m the matcher being used
87      * @param input the input character stream to match
88      * @param start the starting position
89      * @param skip the number of matches to skip
90      *
91      * @return the length of the longest matching string, or
92      * -1 if no match was found
93      *
94      * @throws IOException if a I/O error occurred
95      */

96     public int match(Matcher m, LookAheadReader input, int start, int skip)
97         throws IOException JavaDoc {
98
99         int c;
100
101         if (skip != 0) {
102             return -1;
103         }
104         for (int i = 0; i < value.length(); i++) {
105             c = input.peek(start + i);
106             if (c < 0) {
107                 m.setReadEndOfString();
108                 return -1;
109             }
110             if (m.isCaseInsensitive()) {
111                 c = Character.toLowerCase((char) c);
112             }
113             if (c != value.charAt(i)) {
114                 return -1;
115             }
116         }
117         return value.length();
118     }
119
120     /**
121      * Prints this element to the specified output stream.
122      *
123      * @param output the output stream to use
124      * @param indent the current indentation
125      */

126     public void printTo(PrintWriter JavaDoc output, String JavaDoc indent) {
127         output.println(indent + "'" + value + "'");
128     }
129
130 }
131
Popular Tags