FileDocCategorySizeDatePackage
AbstractParser.javaAPI DocphoneME MR2 API (J2ME)6479Wed May 02 18:00:36 BST 2007com.sun.perseus.parser

AbstractParser

public abstract class AbstractParser extends Object
AbstractParser is the base class for parsers found in this package.
All parsers work on a String and the AbstractParser keeps a reference to that string along with the current position (@see #currentPos) and current character (@see current).
The key methods for this class are read which reads the next character in the parsed string, setString which sets the string to be parsed, and the utility methods skipCommaSpaces, skipSpaces and skipSpacesCommaSpaces which can be used by descendants to skip common separators.
For an implementation example, see {@link TransformListParser}.
version
$Id: AbstractParser.java,v 1.2 2006/04/21 06:40:21 st125089 Exp $

Fields Summary
protected int
currentPos
The current position in the string
protected String
s
The String being parsed
protected int
current
The current character being parsed This is accessible by sub-classes
Constructors Summary
Methods Summary
protected final booleancurrentStartsWith(java.lang.String str)
Tests if the current substring (i.e. the substring beginning at the current position) starts with the specified prefix. If the current substring starts with the specified prefix, the current character will be updated to point to the character immediately following the last character in the prefix; otherwise, the currentPos will not be affected. For example, if the string being parsed is "timingAttr", and the current character is 'A':
currentStartsWith("Att") returns true, current == 'r'
currentStartsWith("Attr") returns true, current == -1
currentStartsWith("Attx") returns false, current == 'A'

param
str the prefix to be tested
return
true if the current substring starts with the specified prefix. The result is false if currentPos is non-positive, or if the current substring does not start with the specified prefix.

        if (currentPos <= 0) {
            return false;
        }
        if (s.startsWith(str, currentPos - 1)) {
            currentPos += str.length() - 1;
            current = read();
            return true;
        }
        return false;
    
protected final intread()

return
the next character. Returns -1 when the end of the String has been reached.

        if (currentPos < s.length()) {
            return s.charAt(currentPos++);
        }
        return -1;
    
protected final voidsetString(java.lang.String str)
Sets this parser's String. This also resets the current position to 0

param
str the string this parser should parse. Should not be null.

        if (str == null) {
            throw new IllegalArgumentException();
        }

        this.s = str;
        this.currentPos = 0;
        this.current = -1;
    
protected final voidskipCommaSpaces()
Skips the whitespaces and an optional comma.

        skipSepSpaces(',");
    
protected final voidskipSepSpaces(char sep)
Skips the whitespaces and an optional comma.

param
sep seperator to skip in addition to spaces.

        wsp1: for (;;) {
            switch (current) {
            default:
                break wsp1;
            case 0x20:
            case 0x9:
            case 0xD:
            case 0xA:
            }
            current = read();
        }
        if (current == sep) {
            wsp2: for (;;) {
                switch (current = read()) {
                default:
                    break wsp2;
                case 0x20:
                case 0x9:
                case 0xD:
                case 0xA:
                }
            }
        }
    
protected final voidskipSpaces()
Skips the whitespaces in the current reader.

        for (;;) {
            switch (current) {
            default:
                return;
            case 0x20:
            case 0x09:
            case 0x0D:
            case 0x0A:
            }
            current = read();
        }
    
protected final voidskipSpacesCommaSpaces()
Skips wsp*,wsp* and throws an IllegalArgumentException if no comma is found.

        skipSpaces();

        if (current != ',") {
            throw new IllegalArgumentException();
        }

        current = read();
        skipSpaces();