Search in sources :

Example 6 with StreamTokenizer

use of java.io.StreamTokenizer in project gephi by gephi.

the class ImporterDOT method importData.

private void importData(LineNumberReader reader) throws Exception {
    Progress.start(progressTicket);
    StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
    setSyntax(streamTokenizer);
    graph(streamTokenizer);
}
Also used : StreamTokenizer(java.io.StreamTokenizer)

Example 7 with StreamTokenizer

use of java.io.StreamTokenizer in project XobotOS by xamarin.

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 8 with StreamTokenizer

use of java.io.StreamTokenizer in project platform_frameworks_base by android.

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 9 with StreamTokenizer

use of java.io.StreamTokenizer in project robovm by robovm.

the class OldStreamTokenizerTest method test_harmonyRegressionTest.

public void test_harmonyRegressionTest() {
    byte[] data = new byte[] { (byte) '-' };
    StreamTokenizer tokenizer = new StreamTokenizer(new ByteArrayInputStream(data));
    try {
        tokenizer.nextToken();
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    String result = tokenizer.toString();
    Assert.assertEquals("Token['-'], line 1", result);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StreamTokenizer(java.io.StreamTokenizer) IOException(java.io.IOException)

Example 10 with StreamTokenizer

use of java.io.StreamTokenizer in project robovm by robovm.

the class OldStreamTokenizerTest method test_nextToken.

public void test_nextToken() throws IOException {
    st = new StreamTokenizer(new Support_StringReader("\n \r\n#"));
    // make \n ordinary
    st.ordinaryChar('\n');
    st.eolIsSignificant(true);
    assertTrue("Wrong token 2,1", st.nextToken() == '\n');
    assertTrue("Wrong token 2,2", st.nextToken() == '\n');
    assertEquals("Wrong token 2,3", '#', st.nextToken());
    Support_ASimpleInputStream sis = new Support_ASimpleInputStream();
    sis.throwExceptionOnNextUse = true;
    st = new StreamTokenizer(sis);
    try {
        st.nextToken();
        fail("IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : Support_ASimpleInputStream(tests.support.Support_ASimpleInputStream) IOException(java.io.IOException) StreamTokenizer(java.io.StreamTokenizer) Support_StringReader(tests.support.Support_StringReader)

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