2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
34 import gnu.regexp.RE;
\r
35 import gnu.regexp.REException;
\r
36 import gnu.regexp.REMatch;
\r
38 import multex.Failure;
\r
41 * a class to do some basic, regexp based parsing of character data
\r
44 * <p>Description: </p>
\r
45 * <p>Copyright: Copyright (c) 2003</p>
\r
47 * @author not attributable
\r
51 public class SimpleParser {
\r
52 private String data;
\r
53 private int position;
\r
60 public SimpleParser(String aData) {
\r
67 * @param aRegularExpression
\r
69 * @throws SimpleParserExc
\r
72 public boolean parses(RE aRegularExpression) throws SimpleParserExc {
\r
73 REMatch match = aRegularExpression.getMatch(data, position);
\r
75 return (match!=null && match.getStartIndex()==position) ;
\r
80 * @param aRegularExpression
\r
83 * @throws SimpleParserExc
\r
86 public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
\r
87 REMatch match = aRegularExpression.getMatch(data, position);
\r
89 if (match==null || match.getStartIndex()!=position)
\r
90 throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
\r
92 position=match.getEndIndex();
\r
94 return match.toString();
\r
99 * @param aRegularExpression
\r
101 * @throws SimpleParserExc
\r
104 public String parse(RE aRegularExpression) throws SimpleParserExc {
\r
105 return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");
\r
110 * @param aRegularExpression
\r
111 * @throws SimpleParserExc
\r
114 public void skip(RE aRegularExpression) throws SimpleParserExc {
\r
115 REMatch match = aRegularExpression.getMatch(data, position);
\r
117 if (match!=null && match.getStartIndex()==position)
\r
118 position=match.getEndIndex();
\r
123 * @param anExpression
\r
125 * @throws SimpleParserExc
\r
128 public boolean parses(String anExpression) throws SimpleParserExc {
\r
130 return parses(new RE(anExpression));
\r
132 catch (SimpleParserExc e) {
\r
135 catch (REException e) {
\r
136 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
\r
138 catch (Throwable t) {
\r
139 throw new SimpleParserFailure( t );
\r
145 * @param anExpression
\r
147 * @throws SimpleParserExc
\r
148 * @throws SimpleParserFailure
\r
151 public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
\r
153 return parse(new RE(anExpression));
\r
155 catch (SimpleParserExc e) {
\r
158 catch (REException e) {
\r
159 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
\r
161 catch (Throwable t) {
\r
162 throw new SimpleParserFailure( t );
\r
168 * @param anExpression
\r
171 * @throws SimpleParserExc
\r
172 * @throws SimpleParserFailure
\r
175 public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {
\r
177 return parse(new RE(anExpression), aMessage);
\r
179 catch (SimpleParserExc e) {
\r
182 catch (REException e) {
\r
183 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
\r
185 catch (Throwable t) {
\r
186 throw new SimpleParserFailure( t );
\r
192 * @param anExpression
\r
193 * @throws SimpleParserExc
\r
194 * @throws SimpleParserFailure
\r
197 public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
\r
199 skip(new RE(anExpression));
\r
201 catch (SimpleParserExc e) {
\r
204 catch (REException e) {
\r
205 throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
\r
207 catch (Throwable t) {
\r
208 throw new SimpleParserFailure( t );
\r
214 * @return true if the parser is at the end of the data
\r
217 public boolean isAtEnd() {
\r
218 return position>=data.length();
\r
225 public String remainingData() {
\r
226 return data.substring(position);
\r
232 * <p>Description: </p>
\r
233 * <p>Copyright: Copyright (c) 2003</p>
\r
235 * @author not attributable
\r
239 public static class SimpleParserFailure extends Failure {
\r
240 public SimpleParserFailure(Throwable aThrowable) {
\r
241 super(aThrowable.getMessage(), aThrowable);
\r
244 public SimpleParserFailure(String aMessage, Throwable aThrowable) {
\r
245 super(aMessage, aThrowable);
\r
249 public static class SimpleParserExc extends Exc {
\r
250 public SimpleParserExc(String aMessage) {
\r