Search in sources :

Example 51 with StreamTokenizer

use of java.io.StreamTokenizer in project android_frameworks_base by DirtyUnicorns.

the class TypedProperties method parse.

/**
     * Parses the data in the reader.
     *
     * @param r The {@code Reader} containing input data to parse
     * @param map The {@code Map} to insert parameter values into
     * @throws ParseException if the input data is malformed
     * @throws IOException if there is a problem reading from the {@code Reader}
     */
static void parse(Reader r, Map<String, Object> map) throws ParseException, IOException {
    final StreamTokenizer st = initTokenizer(r);
    /* A property name must be a valid fully-qualified class + package name.
         * We don't support Unicode, though.
         */
    final String identifierPattern = "[a-zA-Z_$][0-9a-zA-Z_$]*";
    final Pattern propertyNamePattern = Pattern.compile("(" + identifierPattern + "\\.)*" + identifierPattern);
    while (true) {
        int token;
        // Read the next token, which is either the type or EOF.
        token = st.nextToken();
        if (token == StreamTokenizer.TT_EOF) {
            break;
        }
        if (token != StreamTokenizer.TT_WORD) {
            throw new ParseException(st, "type name");
        }
        final int type = interpretType(st.sval);
        if (type == TYPE_ERROR) {
            throw new ParseException(st, "valid type name");
        }
        st.sval = null;
        if (type == TYPE_UNSET) {
            // Expect '('.
            token = st.nextToken();
            if (token != '(') {
                throw new ParseException(st, "'('");
            }
        }
        // Read the property name.
        token = st.nextToken();
        if (token != StreamTokenizer.TT_WORD) {
            throw new ParseException(st, "property name");
        }
        final String propertyName = st.sval;
        if (!propertyNamePattern.matcher(propertyName).matches()) {
            throw new ParseException(st, "valid property name");
        }
        st.sval = null;
        if (type == TYPE_UNSET) {
            // Expect ')'.
            token = st.nextToken();
            if (token != ')') {
                throw new ParseException(st, "')'");
            }
            map.remove(propertyName);
        } else {
            // Expect '='.
            token = st.nextToken();
            if (token != '=') {
                throw new ParseException(st, "'='");
            }
            // Read a value of the appropriate type, and insert into the map.
            final Object value = parseValue(st, type);
            final Object oldValue = map.remove(propertyName);
            if (oldValue != null) {
                //       the same property is defined with a different type.
                if (value.getClass() != oldValue.getClass()) {
                    throw new ParseException(st, "(property previously declared as a different type)");
                }
            }
            map.put(propertyName, value);
        }
        // Expect ';'.
        token = st.nextToken();
        if (token != ';') {
            throw new ParseException(st, "';'");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) StreamTokenizer(java.io.StreamTokenizer)

Example 52 with StreamTokenizer

use of java.io.StreamTokenizer in project android_frameworks_base by AOSPA.

the class TypedProperties method parse.

/**
     * Parses the data in the reader.
     *
     * @param r The {@code Reader} containing input data to parse
     * @param map The {@code Map} to insert parameter values into
     * @throws ParseException if the input data is malformed
     * @throws IOException if there is a problem reading from the {@code Reader}
     */
static void parse(Reader r, Map<String, Object> map) throws ParseException, IOException {
    final StreamTokenizer st = initTokenizer(r);
    /* A property name must be a valid fully-qualified class + package name.
         * We don't support Unicode, though.
         */
    final String identifierPattern = "[a-zA-Z_$][0-9a-zA-Z_$]*";
    final Pattern propertyNamePattern = Pattern.compile("(" + identifierPattern + "\\.)*" + identifierPattern);
    while (true) {
        int token;
        // Read the next token, which is either the type or EOF.
        token = st.nextToken();
        if (token == StreamTokenizer.TT_EOF) {
            break;
        }
        if (token != StreamTokenizer.TT_WORD) {
            throw new ParseException(st, "type name");
        }
        final int type = interpretType(st.sval);
        if (type == TYPE_ERROR) {
            throw new ParseException(st, "valid type name");
        }
        st.sval = null;
        if (type == TYPE_UNSET) {
            // Expect '('.
            token = st.nextToken();
            if (token != '(') {
                throw new ParseException(st, "'('");
            }
        }
        // Read the property name.
        token = st.nextToken();
        if (token != StreamTokenizer.TT_WORD) {
            throw new ParseException(st, "property name");
        }
        final String propertyName = st.sval;
        if (!propertyNamePattern.matcher(propertyName).matches()) {
            throw new ParseException(st, "valid property name");
        }
        st.sval = null;
        if (type == TYPE_UNSET) {
            // Expect ')'.
            token = st.nextToken();
            if (token != ')') {
                throw new ParseException(st, "')'");
            }
            map.remove(propertyName);
        } else {
            // Expect '='.
            token = st.nextToken();
            if (token != '=') {
                throw new ParseException(st, "'='");
            }
            // Read a value of the appropriate type, and insert into the map.
            final Object value = parseValue(st, type);
            final Object oldValue = map.remove(propertyName);
            if (oldValue != null) {
                //       the same property is defined with a different type.
                if (value.getClass() != oldValue.getClass()) {
                    throw new ParseException(st, "(property previously declared as a different type)");
                }
            }
            map.put(propertyName, value);
        }
        // Expect ';'.
        token = st.nextToken();
        if (token != ';') {
            throw new ParseException(st, "';'");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) StreamTokenizer(java.io.StreamTokenizer)

Example 53 with StreamTokenizer

use of java.io.StreamTokenizer in project android_frameworks_base by ResurrectionRemix.

the class TypedProperties method initTokenizer.

/**
     * Instantiates a {@link java.io.StreamTokenizer} and sets its syntax tables
     * appropriately for the {@code TypedProperties} file format.
     *
     * @param r The {@code Reader} that the {@code StreamTokenizer} will read from
     * @return a newly-created and initialized {@code StreamTokenizer}
     */
static StreamTokenizer initTokenizer(Reader r) {
    StreamTokenizer st = new StreamTokenizer(r);
    // Treat everything we don't specify as "ordinary".
    st.resetSyntax();
    /* The only non-quoted-string words we'll be reading are:
         * - property names: [._$a-zA-Z0-9]
         * - type names: [a-zS]
         * - number literals: [-0-9.eExXA-Za-z]  ('x' for 0xNNN hex literals. "NaN", "Infinity")
         * - "true" or "false" (case insensitive): [a-zA-Z]
         */
    st.wordChars('0', '9');
    st.wordChars('A', 'Z');
    st.wordChars('a', 'z');
    st.wordChars('_', '_');
    st.wordChars('$', '$');
    st.wordChars('.', '.');
    st.wordChars('-', '-');
    st.wordChars('+', '+');
    // Single-character tokens
    st.ordinaryChar('=');
    // Other special characters
    st.whitespaceChars(' ', ' ');
    st.whitespaceChars('\t', '\t');
    st.whitespaceChars('\n', '\n');
    st.whitespaceChars('\r', '\r');
    st.quoteChar('"');
    // Java-style comments
    st.slashStarComments(true);
    st.slashSlashComments(true);
    return st;
}
Also used : StreamTokenizer(java.io.StreamTokenizer)

Example 54 with StreamTokenizer

use of java.io.StreamTokenizer in project ceylon-compiler by ceylon.

the class CommandLine method loadCmdFile.

private static void loadCmdFile(String name, ListBuffer<String> args) throws IOException {
    Reader r = new BufferedReader(new FileReader(name));
    StreamTokenizer st = new StreamTokenizer(r);
    st.resetSyntax();
    st.wordChars(' ', 255);
    st.whitespaceChars(0, ' ');
    st.commentChar('#');
    st.quoteChar('"');
    st.quoteChar('\'');
    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        args.append(st.sval);
    }
    r.close();
}
Also used : BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) FileReader(java.io.FileReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) StreamTokenizer(java.io.StreamTokenizer)

Example 55 with StreamTokenizer

use of java.io.StreamTokenizer in project cxf by apache.

the class HttpAuthHeader method parseHeader.

private Map<String, String> parseHeader() {
    Map<String, String> map = new HashMap<>();
    try {
        StreamTokenizer tok = new StreamTokenizer(new StringReader(this.fullContent));
        tok.quoteChar('"');
        tok.quoteChar('\'');
        tok.whitespaceChars('=', '=');
        tok.whitespaceChars(',', ',');
        while (tok.nextToken() != StreamTokenizer.TT_EOF) {
            String key = tok.sval;
            if (tok.nextToken() == StreamTokenizer.TT_EOF) {
                map.put(key, null);
                return map;
            }
            String value = null;
            if ("nc".equals(key)) {
                // nc is a 8 length HEX number so need get it as number
                value = String.valueOf(tok.nval);
                if (value.indexOf(".") > 0) {
                    value = value.substring(0, value.indexOf("."));
                }
                StringBuilder pad = new StringBuilder();
                pad.append("");
                for (int i = 0; i < 8 - value.length(); i++) {
                    pad.append("0");
                }
                value = pad.toString() + value;
            } else {
                value = tok.sval;
            }
            map.put(key, value);
        }
    } catch (IOException ex) {
    // ignore can't happen for StringReader
    }
    return map;
}
Also used : HashMap(java.util.HashMap) StringReader(java.io.StringReader) IOException(java.io.IOException) StreamTokenizer(java.io.StreamTokenizer)

Aggregations

StreamTokenizer (java.io.StreamTokenizer)58 IOException (java.io.IOException)22 StringReader (java.io.StringReader)16 BufferedReader (java.io.BufferedReader)9 FileReader (java.io.FileReader)8 Reader (java.io.Reader)7 ArrayList (java.util.ArrayList)7 Pattern (java.util.regex.Pattern)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStreamReader (java.io.InputStreamReader)4 Support_StringReader (tests.support.Support_StringReader)4 HashMap (java.util.HashMap)3 FileOutputStream (java.io.FileOutputStream)2 ParseException (java.text.ParseException)2 Locale (java.util.Locale)2 Attribute (smile.data.Attribute)2 DateAttribute (smile.data.DateAttribute)2 NominalAttribute (smile.data.NominalAttribute)2 NumericAttribute (smile.data.NumericAttribute)2 StringAttribute (smile.data.StringAttribute)2