support for CAPTCHAs
[mir.git] / source / mir / util / SimpleParser.java
index e95d8ff..92a806b 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License, 
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
+ * (or with modified versions of the above that use the same license as the above), 
+ * and distribute linked combinations including the two.  You must obey the 
+ * GNU General Public License in all respects for all of the code used other than 
+ * the above mentioned libraries.  If you modify this file, you may extend this 
+ * exception to your version of the file, but you are not obligated to do so.  
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mir.util;
 
-import gnu.regexp.RE;
-import gnu.regexp.REException;
-import gnu.regexp.REMatch;
-import multex.Exc;
 import multex.Failure;
+import org.apache.oro.text.regex.*;
 
+/**
+ * Simple parser. Can be used to parse a <code>String</code> based using
+ * regular epxression.
+ */
 public class SimpleParser {
-  private String data;
   private int position;
 
-  public SimpleParser(String aData) {
-    data=aData;
-    position=0;
-  }
-
-  public boolean parses(RE aRegularExpression) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
-
-    return (match!=null && match.getStartIndex()==position) ;
-  }
-
-  public String parse(RE aRegularExpression, String aMessage) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
+  private Perl5Compiler compiler = new Perl5Compiler();
+  private Perl5Matcher matcher = new Perl5Matcher();
+  private PatternMatcherInput input;
 
-    if (match==null || match.getStartIndex()!=position)
-      throw new SimpleParserExc(aMessage+" at position "+position+" in '"+data+"'");
 
-    position=match.getEndIndex();
+  /**
+   * Initialize a new <code>SimpleParser</code>, with <code>someData</code>
+   * as the text to parse.
+   *
+   * @param someData the text to parse
+   */
 
-    return match.toString();
+  public SimpleParser(String someData) {
+    position = 0;
+    input = new PatternMatcherInput(someData);
   }
 
-  public String parse(RE aRegularExpression) throws SimpleParserExc {
-    return parse( aRegularExpression, "No match found for '"+aRegularExpression.toString()+"'");
+  /**
+   * Parses a regular expression. Uses suppled <code>aMessage</code> parameter
+   * in case of an error.
+   *
+   * @param aRegularExpression  the expression to find at the cursor position
+   * @param aMessage the error message to apply if the regular expression couldn't be found
+   *
+   * @return the text that matched the regular expression
+   *
+   * @throws mir.util.SimpleParser.UnExpectedTokenExc
+   *     if the token wasn't found. <code>aMessage</code> will be used in the
+   *     exception's message 
+   */
+  public String parse(Pattern aRegularExpression, String aMessage) throws UnExpectedTokenExc {
+    input.setCurrentOffset(position);
+    if ( matcher.contains(input, aRegularExpression) && input.getMatchBeginOffset() == position) {
+        position = input.getMatchEndOffset();
+
+        return input.match();
+    }
+    else {
+      throw new UnExpectedTokenExc(aMessage + " at position " +
+              position + " in '" + input.toString() + "'");
+    }
   }
 
-  public void skip(RE aRegularExpression) throws SimpleParserExc {
-    REMatch match = aRegularExpression.getMatch(data, position);
+  /**
+   * Attempts to match the data right after the parsing position to
+   * a regular expression
+   *
+   * @throws UnExpectedTokenExc if no match could be found for the given regular
+   * expression.
+   */
+  public String parse(Pattern aRegularExpression) throws UnExpectedTokenExc {
+    return parse( aRegularExpression, "No match found for '" + aRegularExpression.getPattern() + "'");
+  }
 
-    if (match!=null && match.getStartIndex()==position)
-      position=match.getEndIndex();
+  /**
+   * Attempts to skip data at the parsing position matching the supplied regular expression.
+   * If no match is found, the method will simply return 
+   *
+   * @param aRegularExpression the expression to find and skip at the parsing position.
+   */
+
+  public void skip(Pattern aRegularExpression) {
+      input.setCurrentOffset(position);
+    
+      if (matcher.contains(input, aRegularExpression) &&
+          input.getMatchBeginOffset() == position) {
+          position = input.getMatchEndOffset();
+      }
   }
 
-  public boolean parses(String anExpression) throws SimpleParserExc {
+  /**
+   * Returns <code>true</code> if the data at the parsing position matches the given
+   * regular expression.
+   */
+  public boolean parses(String anExpression) throws MalformedRegularExpressionExc {
     try {
-      return parses(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
-    }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
+      return parses(compiler.compile(anExpression));
     }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
-  public String parse(String anExpression) throws SimpleParserExc, SimpleParserFailure {
+  /**
+   * Tests whether the data at the parsing position matches a given
+   * regular expression.
+   *
+   * @param aRegularExpression the pattern to look for
+   * @return returns <code>true</code> if the regular expression is found at the
+   *    parsing position
+   */
+  public boolean parses(Pattern aRegularExpression) {
+     input.setCurrentOffset(position);
+
+     return matcher.contains(input, aRegularExpression) && input.getMatchBeginOffset() == position;
+  }
+
+  /**
+   * Attempts to match the data right after the parsing position to
+   * a regular expression
+   *
+   * @throws SimpleParserExc if no match could be found for the given regular
+   * expression.
+   */
+  public String parse(String anExpression) throws MalformedRegularExpressionExc, UnExpectedTokenExc {
     try {
-      return parse(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
+      return parse(compiler.compile(anExpression));
     }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
-    }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
-  public String parse(String anExpression, String aMessage) throws SimpleParserExc, SimpleParserFailure {
+
+  /**
+   * Parses text at the current parsing position matching the given <code>anExpression</code>.
+   * Will apply the supplied message to an exception if no match could be found.
+   */
+  public String parse(String anExpression, String aMessage) throws MalformedRegularExpressionExc, UnExpectedTokenExc {
     try {
-      return parse(new RE(anExpression), aMessage);
+      return parse(compiler.compile(anExpression), aMessage);
     }
-    catch (SimpleParserExc e) {
-      throw e;
-    }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
-    }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
 
-  public void skip(String anExpression) throws SimpleParserExc, SimpleParserFailure {
+  /**
+   * Skips (i.e. discards) text matching the supplied regular expression
+   */
+
+  public void skip(String anExpression) throws MalformedRegularExpressionExc {
     try {
-      skip(new RE(anExpression));
-    }
-    catch (SimpleParserExc e) {
-      throw e;
+      skip(compiler.compile(anExpression));
     }
-    catch (REException e) {
-      throw new SimpleParserFailure( "Error compiling regular expression '" + anExpression + "': " + e.getMessage(), e);
-    }
-    catch (Throwable t) {
-      throw new SimpleParserFailure( t );
+    catch (MalformedPatternException e) {
+        throw new MalformedRegularExpressionExc(e);
     }
   }
+
+  /**
+   * returns <code>true</code> if the parser is at the end of the data
+   */
   public boolean isAtEnd() {
-    return position>=data.length();
+    return position >=input.getEndOffset();
   }
 
+  /**
+   * Returns the "remaining" data: the data after the current position
+   */
+  public String remainingData() {
+    return input.toString().substring(input.getEndOffset());
+  }
+
+  /**
+   * Unchecked exception thrown from <code>SimpleParser</code>
+   */
+
   public static class SimpleParserFailure extends Failure {
     public SimpleParserFailure(Throwable aThrowable) {
       super(aThrowable.getMessage(), aThrowable);
@@ -147,9 +208,37 @@ public class SimpleParser {
     }
   }
 
-  public static class SimpleParserExc extends Exc {
+  /**
+   * Checked exception thrown from <code>SimpleParser</code>
+   */
+  public static class SimpleParserExc extends Exception {
+    protected SimpleParserExc(String aMessage, Throwable aCause) {
+      super(aMessage, aCause);
+    }
+
+    public SimpleParserExc(Throwable aCause) {
+      super(aCause);
+    }
+
     public SimpleParserExc(String aMessage) {
       super(aMessage);
     }
   }
+
+  public static class MalformedRegularExpressionExc extends SimpleParserExc {
+    public MalformedRegularExpressionExc(String aMessage, Throwable aCause) {
+      super(aMessage, aCause);
+    }
+
+    public MalformedRegularExpressionExc(Throwable aCause) {
+      super(aCause);
+    }
+  }
+
+  public static class UnExpectedTokenExc extends SimpleParserExc {
+    public UnExpectedTokenExc(String aMessage) {
+      super(aMessage);
+    }
+  }
+
 }
\ No newline at end of file