1810466efeb14d984ebea4d5af8361744a1d4e4a
[mir.git] / source / mir / util / SimpleParser.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 package mir.util;\r
33 \r
34 import gnu.regexp.RE;\r
35 import gnu.regexp.REException;\r
36 import gnu.regexp.REMatch;\r
37 import multex.Exc;\r
38 import multex.Failure;\r
39 \r
40 /**\r
41  * a class to do some basic, regexp based parsing of character data\r
42  *\r
43  * <p>Title: </p>\r
44  * <p>Description: </p>\r
45  * <p>Copyright: Copyright (c) 2003</p>\r
46  * <p>Company: </p>\r
47  * @author not attributable\r
48  * @version 1.0\r
49  */\r
50 \r
51 public class SimpleParser {\r
52   private String data;\r
53   private int position;\r
54 \r
55   /**\r
56    *\r
57    * @param aData\r
58    */\r
59 \r
60   public SimpleParser(String aData) {\r
61     data=aData;\r
62     position=0;\r
63   }\r
64 \r
65   /**\r
66    *\r
67    * @param aRegularExpression\r
68    * @return\r
69    * @throws SimpleParserExc\r
70    */\r
71 \r
72   public boolean parses(RE aRegularExpression) throws SimpleParserExc {\r
73     REMatch match = aRegularExpression.getMatch(data, position);\r
74 \r
75     return (match!=null && match.getStartIndex()==position) ;\r
76   }\r
77 \r
78   /**\r
79    *\r
80    * @param aRegularExpression\r
81    * @param aMessage\r
82    * @return\r
83    * @throws SimpleParserExc\r
84    */\r
85 \r
86   public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {\r
87     REMatch match = aRegularExpression.getMatch(data, position);\r
88 \r
89     if (match==null || match.getStartIndex()!=position)\r
90       throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");\r
91 \r
92     position=match.getEndIndex();\r
93 \r
94     return match.toString();\r
95   }\r
96 \r
97   /**\r
98    *\r
99    * @param aRegularExpression\r
100    * @return\r
101    * @throws SimpleParserExc\r
102    */\r
103 \r
104   public String parse(RE aRegularExpression) throws SimpleParserExc {\r
105     return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");\r
106   }\r
107 \r
108   /**\r
109    *\r
110    * @param aRegularExpression\r
111    * @throws SimpleParserExc\r
112    */\r
113 \r
114   public void skip(RE aRegularExpression) throws SimpleParserExc {\r
115     REMatch match = aRegularExpression.getMatch(data, position);\r
116 \r
117     if (match!=null && match.getStartIndex()==position)\r
118       position=match.getEndIndex();\r
119   }\r
120 \r
121   /**\r
122    *\r
123    * @param anExpression\r
124    * @return\r
125    * @throws SimpleParserExc\r
126    */\r
127 \r
128   public boolean parses(String anExpression) throws SimpleParserExc {\r
129     try {\r
130       return parses(new RE(anExpression));\r
131     }\r
132     catch (SimpleParserExc e) {\r
133       throw e;\r
134     }\r
135     catch (REException e) {\r
136       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);\r
137     }\r
138     catch (Throwable t) {\r
139       throw new SimpleParserFailure( t );\r
140     }\r
141   }\r
142 \r
143   /**\r
144    *\r
145    * @param anExpression\r
146    * @return\r
147    * @throws SimpleParserExc\r
148    * @throws SimpleParserFailure\r
149    */\r
150 \r
151   public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {\r
152     try {\r
153       return parse(new RE(anExpression));\r
154     }\r
155     catch (SimpleParserExc e) {\r
156       throw e;\r
157     }\r
158     catch (REException e) {\r
159       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);\r
160     }\r
161     catch (Throwable t) {\r
162       throw new SimpleParserFailure( t );\r
163     }\r
164   }\r
165 \r
166   /**\r
167    *\r
168    * @param anExpression\r
169    * @param aMessage\r
170    * @return\r
171    * @throws SimpleParserExc\r
172    * @throws SimpleParserFailure\r
173    */\r
174 \r
175   public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {\r
176     try {\r
177       return parse(new RE(anExpression), aMessage);\r
178     }\r
179     catch (SimpleParserExc e) {\r
180       throw e;\r
181     }\r
182     catch (REException e) {\r
183       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);\r
184     }\r
185     catch (Throwable t) {\r
186       throw new SimpleParserFailure( t );\r
187     }\r
188   }\r
189 \r
190   /**\r
191    *\r
192    * @param anExpression\r
193    * @throws SimpleParserExc\r
194    * @throws SimpleParserFailure\r
195    */\r
196 \r
197   public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {\r
198     try {\r
199       skip(new RE(anExpression));\r
200     }\r
201     catch (SimpleParserExc e) {\r
202       throw e;\r
203     }\r
204     catch (REException e) {\r
205       throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);\r
206     }\r
207     catch (Throwable t) {\r
208       throw new SimpleParserFailure( t );\r
209     }\r
210   }\r
211 \r
212   /**\r
213    *\r
214    * @return true if the parser is at the end of the data\r
215    */\r
216 \r
217   public boolean isAtEnd() {\r
218     return position>=data.length();\r
219   }\r
220 \r
221   /**\r
222    *\r
223    * @return\r
224    */\r
225   public String remainingData() {\r
226     return data.substring(position);\r
227   }\r
228 \r
229   /**\r
230    *\r
231    * <p>Title: </p>\r
232    * <p>Description: </p>\r
233    * <p>Copyright: Copyright (c) 2003</p>\r
234    * <p>Company: </p>\r
235    * @author not attributable\r
236    * @version 1.0\r
237    */\r
238 \r
239   public static class SimpleParserFailure extends Failure {\r
240     public SimpleParserFailure(Throwable aThrowable) {\r
241       super(aThrowable.getMessage(), aThrowable);\r
242     }\r
243 \r
244     public SimpleParserFailure(String aMessage, Throwable aThrowable) {\r
245       super(aMessage, aThrowable);\r
246     }\r
247   }\r
248 \r
249   public static class SimpleParserExc extends Exc {\r
250     public SimpleParserExc(String aMessage) {\r
251       super(aMessage);\r
252     }\r
253   }\r
254 }