975dd3d7fedea80948e1389b7fb919342aea6a74
[mir.git] / source / mir / util / SimpleParser.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License, 
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
23  * (or with modified versions of the above that use the same license as the above), 
24  * and distribute linked combinations including the two.  You must obey the 
25  * GNU General Public License in all respects for all of the code used other than 
26  * the above mentioned libraries.  If you modify this file, you may extend this 
27  * exception to your version of the file, but you are not obligated to do so.  
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mir.util;
32
33 import gnu.regexp.RE;
34 import gnu.regexp.REException;
35 import gnu.regexp.REMatch;
36 import multex.Exc;
37 import multex.Failure;
38
39 /**
40  * a class to do some basic, regexp based parsing of character data
41  *
42  * <p>Title: </p>
43  * <p>Description: </p>
44  * <p>Copyright: Copyright (c) 2003</p>
45  * <p>Company: </p>
46  * @author not attributable
47  * @version 1.0
48  */
49
50 public class SimpleParser {
51   private String data;
52   private int position;
53
54   /**
55    *
56    * @param aData
57    */
58
59   public SimpleParser(String aData) {
60     data=aData;
61     position=0;
62   }
63
64   /**
65    *
66    * @param aRegularExpression
67    * @return
68    * @throws SimpleParserExc
69    */
70
71   public boolean parses(RE aRegularExpression) throws SimpleParserExc {
72     REMatch match = aRegularExpression.getMatch(data, position);
73
74     return (match!=null && match.getStartIndex()==position) ;
75   }
76
77   /**
78    *
79    * @param aRegularExpression
80    * @param aMessage
81    * @return
82    * @throws SimpleParserExc
83    */
84
85   public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
86     REMatch match = aRegularExpression.getMatch(data, position);
87
88     if (match==null || match.getStartIndex()!=position)
89       throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
90
91     position=match.getEndIndex();
92
93     return match.toString();
94   }
95
96   /**
97    *
98    * @param aRegularExpression
99    * @return
100    * @throws SimpleParserExc
101    */
102
103   public String parse(RE aRegularExpression) throws SimpleParserExc {
104     return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");
105   }
106
107   /**
108    *
109    * @param aRegularExpression
110    * @throws SimpleParserExc
111    */
112
113   public void skip(RE aRegularExpression) throws SimpleParserExc {
114     REMatch match = aRegularExpression.getMatch(data, position);
115
116     if (match!=null && match.getStartIndex()==position)
117       position=match.getEndIndex();
118   }
119
120   /**
121    *
122    * @param anExpression
123    * @return
124    * @throws SimpleParserExc
125    */
126
127   public boolean parses(String anExpression) throws SimpleParserExc {
128     try {
129       return parses(new RE(anExpression));
130     }
131     catch (SimpleParserExc e) {
132       throw e;
133     }
134     catch (REException e) {
135       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
136     }
137     catch (Throwable t) {
138       throw new SimpleParserFailure( t );
139     }
140   }
141
142   /**
143    *
144    * @param anExpression
145    * @return
146    * @throws SimpleParserExc
147    * @throws SimpleParserFailure
148    */
149
150   public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
151     try {
152       return parse(new RE(anExpression));
153     }
154     catch (SimpleParserExc e) {
155       throw e;
156     }
157     catch (REException e) {
158       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
159     }
160     catch (Throwable t) {
161       throw new SimpleParserFailure( t );
162     }
163   }
164
165   /**
166    *
167    * @param anExpression
168    * @param aMessage
169    * @return
170    * @throws SimpleParserExc
171    * @throws SimpleParserFailure
172    */
173
174   public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {
175     try {
176       return parse(new RE(anExpression), aMessage);
177     }
178     catch (SimpleParserExc e) {
179       throw e;
180     }
181     catch (REException e) {
182       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
183     }
184     catch (Throwable t) {
185       throw new SimpleParserFailure( t );
186     }
187   }
188
189   /**
190    *
191    * @param anExpression
192    * @throws SimpleParserExc
193    * @throws SimpleParserFailure
194    */
195
196   public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
197     try {
198       skip(new RE(anExpression));
199     }
200     catch (SimpleParserExc e) {
201       throw e;
202     }
203     catch (REException e) {
204       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
205     }
206     catch (Throwable t) {
207       throw new SimpleParserFailure( t );
208     }
209   }
210
211   /**
212    *
213    * @return true if the parser is at the end of the data
214    */
215
216   public boolean isAtEnd() {
217     return position>=data.length();
218   }
219
220   /**
221    *
222    * @return
223    */
224   public String remainingData() {
225     return data.substring(position);
226   }
227
228   /**
229    *
230    * <p>Title: </p>
231    * <p>Description: </p>
232    * <p>Copyright: Copyright (c) 2003</p>
233    * <p>Company: </p>
234    * @author not attributable
235    * @version 1.0
236    */
237
238   public static class SimpleParserFailure extends Failure {
239     public SimpleParserFailure(Throwable aThrowable) {
240       super(aThrowable.getMessage(), aThrowable);
241     }
242
243     public SimpleParserFailure(String aMessage, Throwable aThrowable) {
244       super(aMessage, aThrowable);
245     }
246   }
247
248   public static class SimpleParserExc extends Exc {
249     public SimpleParserExc(String aMessage) {
250       super(aMessage);
251     }
252   }
253 }