Search in sources :

Example 26 with StreamTokenizer

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

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

use of java.io.StreamTokenizer in project jdk8u_jdk by JetBrains.

the class CommandLine method loadCmdFile.

private static void loadCmdFile(String name, List<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.add(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 28 with StreamTokenizer

use of java.io.StreamTokenizer in project jdk8u_jdk by JetBrains.

the class CommandLine method loadCmdFile.

private static void loadCmdFile(String name, List 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() != st.TT_EOF) {
        args.add(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 29 with StreamTokenizer

use of java.io.StreamTokenizer in project jdk8u_jdk by JetBrains.

the class X11FontManager method registerFontDir.

/* NOTE: this method needs to be executed in a privileged context.
     * The superclass constructor which is the primary caller of
     * this method executes entirely in such a context. Additionally
     * the loadFonts() method does too. So all should be well.

     */
@Override
protected void registerFontDir(String path) {
    /* fonts.dir file format looks like :-
         * 47
         * Arial.ttf -monotype-arial-regular-r-normal--0-0-0-0-p-0-iso8859-1
         * Arial-Bold.ttf -monotype-arial-bold-r-normal--0-0-0-0-p-0-iso8859-1
         * ...
         */
    if (FontUtilities.debugFonts()) {
        FontUtilities.getLogger().info("ParseFontDir " + path);
    }
    File fontsDotDir = new File(path + File.separator + "fonts.dir");
    FileReader fr = null;
    try {
        if (fontsDotDir.canRead()) {
            fr = new FileReader(fontsDotDir);
            BufferedReader br = new BufferedReader(fr, 8192);
            StreamTokenizer st = new StreamTokenizer(br);
            st.eolIsSignificant(true);
            int ttype = st.nextToken();
            if (ttype == StreamTokenizer.TT_NUMBER) {
                int numEntries = (int) st.nval;
                ttype = st.nextToken();
                if (ttype == StreamTokenizer.TT_EOL) {
                    st.resetSyntax();
                    st.wordChars(32, 127);
                    st.wordChars(128 + 32, 255);
                    st.whitespaceChars(0, 31);
                    for (int i = 0; i < numEntries; i++) {
                        ttype = st.nextToken();
                        if (ttype == StreamTokenizer.TT_EOF) {
                            break;
                        }
                        if (ttype != StreamTokenizer.TT_WORD) {
                            break;
                        }
                        int breakPos = st.sval.indexOf(' ');
                        if (breakPos <= 0) {
                            /* On TurboLinux 8.0 a fonts.dir file had
                                 * a line with integer value "24" which
                                 * appeared to be the number of remaining
                                 * entries in the file. This didn't add to
                                 * the value on the first line of the file.
                                 * Seemed like XFree86 didn't like this line
                                 * much either. It failed to parse the file.
                                 * Ignore lines like this completely, and
                                 * don't let them count as an entry.
                                 */
                            numEntries++;
                            ttype = st.nextToken();
                            if (ttype != StreamTokenizer.TT_EOL) {
                                break;
                            }
                            continue;
                        }
                        if (st.sval.charAt(0) == '!') {
                            /* TurboLinux 8.0 comment line: ignore.
                                 * can't use st.commentChar('!') to just
                                 * skip because this line mustn't count
                                 * against numEntries.
                                 */
                            numEntries++;
                            ttype = st.nextToken();
                            if (ttype != StreamTokenizer.TT_EOL) {
                                break;
                            }
                            continue;
                        }
                        String fileName = st.sval.substring(0, breakPos);
                        /* TurboLinux 8.0 uses some additional syntax to
                             * indicate algorithmic styling values.
                             * Ignore ':' separated files at the beginning
                             * of the fileName
                             */
                        int lastColon = fileName.lastIndexOf(':');
                        if (lastColon > 0) {
                            if (lastColon + 1 >= fileName.length()) {
                                continue;
                            }
                            fileName = fileName.substring(lastColon + 1);
                        }
                        String fontPart = st.sval.substring(breakPos + 1);
                        String fontID = specificFontIDForName(fontPart);
                        String sVal = (String) fontNameMap.get(fontID);
                        if (FontUtilities.debugFonts()) {
                            PlatformLogger logger = FontUtilities.getLogger();
                            logger.info("file=" + fileName + " xlfd=" + fontPart);
                            logger.info("fontID=" + fontID + " sVal=" + sVal);
                        }
                        String fullPath = null;
                        try {
                            File file = new File(path, fileName);
                            /* we may have a resolved symbolic link
                                 * this becomes important for an xlfd we
                                 * still need to know the location it was
                                 * found to update the X server font path
                                 * for use by AWT heavyweights - and when 2D
                                 * wants to use the native rasteriser.
                                 */
                            if (xFontDirsMap == null) {
                                xFontDirsMap = new HashMap();
                            }
                            xFontDirsMap.put(fontID, path);
                            fullPath = file.getCanonicalPath();
                        } catch (IOException e) {
                            fullPath = path + File.separator + fileName;
                        }
                        Vector xVal = (Vector) xlfdMap.get(fullPath);
                        if (FontUtilities.debugFonts()) {
                            FontUtilities.getLogger().info("fullPath=" + fullPath + " xVal=" + xVal);
                        }
                        if ((xVal == null || !xVal.contains(fontPart)) && (sVal == null) || !sVal.startsWith("/")) {
                            if (FontUtilities.debugFonts()) {
                                FontUtilities.getLogger().info("Map fontID:" + fontID + "to file:" + fullPath);
                            }
                            fontNameMap.put(fontID, fullPath);
                            if (xVal == null) {
                                xVal = new Vector();
                                xlfdMap.put(fullPath, xVal);
                            }
                            xVal.add(fontPart);
                        }
                        ttype = st.nextToken();
                        if (ttype != StreamTokenizer.TT_EOL) {
                            break;
                        }
                    }
                }
            }
            fr.close();
        }
    } catch (IOException ioe1) {
    } finally {
        if (fr != null) {
            try {
                fr.close();
            } catch (IOException ioe2) {
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) PlatformLogger(sun.util.logging.PlatformLogger) File(java.io.File) Vector(java.util.Vector) StreamTokenizer(java.io.StreamTokenizer)

Example 30 with StreamTokenizer

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

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)

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