net.sf.jmatchparser.util
Class ExpectReader

java.lang.Object
  extended by net.sf.jmatchparser.util.ExpectReader

public class ExpectReader
extends Object

A fairly simple reader class, that can "expect" constant text and regular expressions, while still leaving full control to the programmer. This parser is fully deterministic; therefore, using it on more complex documents can be quite frustrating. Basically, this is what I used before I wrote JMatchParser. It is included because I still have old projects using it, and because it might be useful for simple projects.


Constructor Summary
ExpectReader(BufferedReader br)
          Create a new expect reader reading from the given BufferedReader.
ExpectReader(InputStream in, String charsetName)
          Create a new expect reader reading from the given InputStream.
ExpectReader(Reader r)
          Create a new expect reader reading from the given Reader.
 
Method Summary
 void close()
          Close the underlying reader.
 void ensureEndOfFile()
          Ensure that the end of file has been reached.
static List<Matcher> ensureMultiRegexMatch(String actual, Pattern pattern, Pattern terminalPattern, int restGroup)
          Perform a multi regex match.
static List<Matcher> ensureMultiRegexMatch(String actual, String regex, String terminalRegex, int restGroup)
          Perform a multi regex match.
 String ensureReadLine()
          Read a line from the reader, and return it.
 String ensureReadTrimmedLine()
          Read a line like ensureReadLine(), but trim the response and skip empty lines.
static Matcher ensureRegexMatch(String actual, Pattern pattern)
          Ensure that a string matches a pattern and returns a Matcher.
static Matcher ensureRegexMatch(String actual, String pattern)
          Ensure that a string matches a pattern and returns a Matcher.
static void ensureStringMatch(String actual, String expected)
          Ensure that two strings are the same.
 void expect(String... lines)
          Expect one ore more lines.
 void expectLine(String line)
          Expect a line.
 Matcher expectRegex(Pattern pattern)
          Expect a line that matches a regex.
 Matcher expectRegex(String regex)
          Expect a line that matches a regex.
 void expectTrimmed(String... lines)
          Expect one ore more trimmed lines.
 void expectTrimmedLine(String line)
          Expect a trimmed.
 Matcher expectTrimmedRegex(Pattern pattern)
          Expect a trimmed line that matches a regex.
 Matcher expectTrimmedRegex(String regex)
          Expect a trimmed line that matches a regex.
 BufferedReader getReader()
          Return the reader used.
 String readLine()
          Read a line.
static Matcher regexMatch(String actual, Pattern pattern)
          Try to match a string against pattern and returns a Matcher or null.
static Matcher regexMatch(String actual, String pattern)
          Try to match a string against pattern and returns a Matcher or null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpectReader

public ExpectReader(BufferedReader br)
Create a new expect reader reading from the given BufferedReader.


ExpectReader

public ExpectReader(Reader r)
Create a new expect reader reading from the given Reader.


ExpectReader

public ExpectReader(InputStream in,
                    String charsetName)
             throws UnsupportedEncodingException
Create a new expect reader reading from the given InputStream.

Throws:
UnsupportedEncodingException
Method Detail

getReader

public BufferedReader getReader()
Return the reader used.


ensureReadLine

public String ensureReadLine()
                      throws IOException
Read a line from the reader, and return it. Throw an exception if eof is reached before reading the line.

Throws:
IOException

ensureReadTrimmedLine

public String ensureReadTrimmedLine()
                             throws IOException
Read a line like ensureReadLine(), but trim the response and skip empty lines.

Throws:
IOException

readLine

public String readLine()
                throws IOException
Read a line.

Throws:
IOException
See Also:
BufferedReader.readLine()

ensureEndOfFile

public void ensureEndOfFile()
                     throws IOException
Ensure that the end of file has been reached.

Throws:
IOException

close

public void close()
           throws IOException
Close the underlying reader.

Throws:
IOException

expect

public void expect(String... lines)
            throws IOException
Expect one ore more lines.

Throws:
IOException
See Also:
expectLine(String)

expectTrimmed

public void expectTrimmed(String... lines)
                   throws IOException
Expect one ore more trimmed lines.

Throws:
IOException
See Also:
expectTrimmedLine(String)

expectLine

public void expectLine(String line)
                throws IOException
Expect a line. If the Reader's next line does not match the line given, an exception is thrown.

Throws:
IOException

expectTrimmedLine

public void expectTrimmedLine(String line)
                       throws IOException
Expect a trimmed. If the Reader's next line does not match the line given, after trimming, an exception is thrown. For symmetry reasons, calling this method with an empty string is a no-op.

Throws:
IOException

ensureStringMatch

public static void ensureStringMatch(String actual,
                                     String expected)
Ensure that two strings are the same.

Parameters:
actual - The actual string
expected - The expected string

expectRegex

public Matcher expectRegex(String regex)
                    throws IOException
Expect a line that matches a regex. A Matcher to read the groups is returned.

Throws:
IOException

expectRegex

public Matcher expectRegex(Pattern pattern)
                    throws IOException
Expect a line that matches a regex. A Matcher to read the groups is returned.

Throws:
IOException

expectTrimmedRegex

public Matcher expectTrimmedRegex(String regex)
                           throws IOException
Expect a trimmed line that matches a regex. A Matcher to read the groups is returned.

Throws:
IOException
See Also:
ensureReadTrimmedLine()

expectTrimmedRegex

public Matcher expectTrimmedRegex(Pattern pattern)
                           throws IOException
Expect a trimmed line that matches a regex. A Matcher to read the groups is returned.

Throws:
IOException
See Also:
ensureReadTrimmedLine()

ensureRegexMatch

public static Matcher ensureRegexMatch(String actual,
                                       String pattern)
Ensure that a string matches a pattern and returns a Matcher.

Parameters:
actual - The actual string
pattern - The pattern

ensureRegexMatch

public static Matcher ensureRegexMatch(String actual,
                                       Pattern pattern)
Ensure that a string matches a pattern and returns a Matcher.

Parameters:
actual - The actual string
pattern - The pattern

regexMatch

public static Matcher regexMatch(String actual,
                                 String pattern)
Try to match a string against pattern and returns a Matcher or null.

Parameters:
actual - The actual string
pattern - The pattern

regexMatch

public static Matcher regexMatch(String actual,
                                 Pattern pattern)
Try to match a string against pattern and returns a Matcher or null.

Parameters:
actual - The actual string
pattern - The pattern

ensureMultiRegexMatch

public static List<Matcher> ensureMultiRegexMatch(String actual,
                                                  String regex,
                                                  String terminalRegex,
                                                  int restGroup)
Perform a multi regex match.

See Also:
ensureMultiRegexMatch(String, Pattern, Pattern, int)

ensureMultiRegexMatch

public static List<Matcher> ensureMultiRegexMatch(String actual,
                                                  Pattern pattern,
                                                  Pattern terminalPattern,
                                                  int restGroup)
Perform a multi regex match.

If terminalPattern is non-null: Match actual against the terminal pattern. If it matches, the match is finished. If not, it is matched against the pattern and added to the result list (non-match is an error). The restGroup'th group of the matcher is then used as the actual value for the next round.

If terminalPattern is null: Terminate when it does not match any longer. Rest as above.



Copyright © 2011. All Rights Reserved.