Code example for StreamTokenizer

Methods: nextToken, toString

0
     *             if unexpected or unknown token encountered 
     */ 
    protected PrincipalEntry readPrincipalEntry(StreamTokenizer st)
            throws IOException, InvalidFormatException { 
        PrincipalEntry pe = new PrincipalEntry();
        if (st.nextToken() == StreamTokenizer.TT_WORD) {
            pe.klass = st.sval;
            st.nextToken();
        } else if (st.ttype == '*') {
            pe.klass = PrincipalEntry.WILDCARD;
            st.nextToken();
        } 
        if (st.ttype == '"') {
            pe.name = st.sval;
        } else if (st.ttype == '*') {
            pe.name = PrincipalEntry.WILDCARD;
        } else { 
            handleUnexpectedToken(st, "Expected syntax is principal [class_name] \"principal_name\"");
        } 
        return pe;
    } 
 
    /** 
     * Tries to read a list of <i>permission </i> entries. The expected syntax 
     * is 
     * 
     * <pre> 
     * 
     *     permission permission_class_name 
     *          [ &quot;target_name&quot; ] [, &quot;action_list&quot;] 
     *          [, signedby &quot;name1,name2,...&quot;]; 
     * 
     * </pre> 
     * 
     * List is terminated by '}' (closing curly brace) symbol. 
     * 
     * @return collection of successfully parsed PermissionEntries 
     * @throws IOException 
     *             if stream reading failed 
     * @throws InvalidFormatException 
     *             if unexpected or unknown token encountered 
     */ 
    protected Collection<PermissionEntry> readPermissionEntries(
            StreamTokenizer st) throws IOException, InvalidFormatException {
        Collection<PermissionEntry> permissions = new HashSet<PermissionEntry>();
        parsing: while (true) { 
            switch (st.nextToken()) {
 
            case StreamTokenizer.TT_WORD:
                if (Util.equalsIgnoreCase("permission", st.sval)) {
                    PermissionEntry pe = new PermissionEntry();
                    if (st.nextToken() == StreamTokenizer.TT_WORD) {
                        pe.klass = st.sval;
                        if (st.nextToken() == '"') {
                            pe.name = st.sval;
                            st.nextToken();
                        } 
                        if (st.ttype == ',') {
                            st.nextToken();
                        } 
                        if (st.ttype == '"') {
                            pe.actions = st.sval;
                            if (st.nextToken() == ',') {
                                st.nextToken();
                            } 
                        } 
                        if (st.ttype == StreamTokenizer.TT_WORD
                                && Util.equalsIgnoreCase("signedby", st.sval)) {
                            if (st.nextToken() == '"') {
                                pe.signers = st.sval;
                            } else { 
                                handleUnexpectedToken(st);
                            } 
                        } else { // handle token in the next iteration 
                            st.pushBack();
                        } 
                        permissions.add(pe);
                        continue parsing; 
                    } 
                } 
                handleUnexpectedToken(st, "Expected syntax is permission permission_class_name [\"target_name\"] [, \"action_list\"] [, signedby \"name1,...,nameN\"]");
                break; 
 
            case ';': //just delimiter of entries 
                break; 
 
            case '}': //end of list 
                break parsing; 
 
            default: // invalid token 
                handleUnexpectedToken(st);
                break; 
            } 
        } 
 
        return permissions;
    } 
 
    /** 
     * Formats a detailed description of tokenizer status: current token, 
     * current line number, etc. 
     */ 
    protected String composeStatus(StreamTokenizer st) {
        return st.toString();
    } 
 
    /** 
     * Throws InvalidFormatException with detailed diagnostics. 
     *