Code example for StreamTokenizer

Methods: nextToken, pushBack, toString

0
     *             if unexpected or unknown token encountered 
     */ 
    protected KeystoreEntry readKeystoreEntry(StreamTokenizer st)
            throws IOException, InvalidFormatException { 
        KeystoreEntry ke = new KeystoreEntry();
        if (st.nextToken() == '"') {
            ke.url = st.sval;
            if ((st.nextToken() == '"')
                    || ((st.ttype == ',') && (st.nextToken() == '"'))) {
                ke.type = st.sval;
            } else { // handle token in the main loop 
                st.pushBack();
            } 
        } else { 
            handleUnexpectedToken(st, Messages.getString("security.8A")); //$NON-NLS-1$
        } 
        return ke;
    } 
 
    /** 
     * Tries to read <i>grant </i> clause. <br> 
     * First, it reads <i>codebase </i>, <i>signedby </i>, <i>principal </i> 
     * entries till the '{' (opening curly brace) symbol. Then it calls 
     * readPermissionEntries() method to read the permissions of this clause. 
     * <br> 
     * Principal entries (if any) are read by invoking readPrincipalEntry() 
     * method, obtained PrincipalEntries are accumulated. <br> 
     * The expected syntax is 
     *  
     * <pre> 
     *  
     *     [ [codebase &quot;url&quot;] | [signedby &quot;name1,...,nameN&quot;] |  
     *          principal ...] ]* { ... } 
     *   
     * </pre> 
     *  
     * @return successfully parsed GrantEntry 
     * @throws IOException 
     *             if stream reading failed 
     * @throws InvalidFormatException 
     *             if unexpected or unknown token encountered 
     */ 
    protected GrantEntry readGrantEntry(StreamTokenizer st) throws IOException,
            InvalidFormatException { 
        GrantEntry ge = new GrantEntry();
        parsing: while (true) { 
            switch (st.nextToken()) {
 
            case StreamTokenizer.TT_WORD:
                if (Util.equalsIgnoreCase("signedby", st.sval)) { //$NON-NLS-1$
                    if (st.nextToken() == '"') {
                        ge.signers = st.sval;
                    } else { 
                        handleUnexpectedToken(st, Messages.getString("security.8B")); //$NON-NLS-1$
                    } 
                } else if (Util.equalsIgnoreCase("codebase", st.sval)) { //$NON-NLS-1$
                    if (st.nextToken() == '"') {
                        ge.codebase = st.sval;
                    } else { 
                        handleUnexpectedToken(st, Messages.getString("security.8C")); //$NON-NLS-1$
                    } 
                } else if (Util.equalsIgnoreCase("principal", st.sval)) { //$NON-NLS-1$
                    ge.addPrincipal(readPrincipalEntry(st));
                } else { 
                    handleUnexpectedToken(st);
                } 
                break; 
 
            case ',': //just delimiter of entries 
                break; 
 
            case '{': 
                ge.permissions = readPermissionEntries(st);
                break parsing; 
 
            default: // handle token in the main loop 
                st.pushBack();
                break parsing; 
            } 
        } 
 
        return ge;
    } 
 
    /** 
     * Tries to read <i>Principal </i> entry fields. The expected syntax is 
     *  
     * <pre> 
     *  
     *     [ principal_class_name ] &quot;principal_name&quot; 
     *   
     * </pre> 
     *  
     * Both class and name may be wildcards, wildcard names should not 
     * surrounded by quotes. 
     *  
     * @return successfully parsed PrincipalEntry 
     * @throws IOException 
     *             if stream reading failed 
     * @throws InvalidFormatException 
     *             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, Messages.getString("security.8D")); //$NON-NLS-1$
        } 
        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)) { //$NON-NLS-1$
                    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)) { //$NON-NLS-1$
                            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, Messages.getString("security.8E")); //$NON-NLS-1$
                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. 
     *