Example usage for java.io StreamTokenizer nextToken

List of usage examples for java.io StreamTokenizer nextToken

Introduction

In this page you can find the example usage for java.io StreamTokenizer nextToken.

Prototype

public int nextToken() throws IOException 

Source Link

Document

Parses the next token from the input stream of this tokenizer.

Usage

From source file:org.xwiki.properties.converter.collection.AbstractCollectionConverter.java

/**
 * <p>//from   www.j  av  a  2 s.  co m
 * Parse an incoming String of the form similar to an array initializer in the Java language into a
 * <code>List</code> individual Strings for each element, according to the following rules.
 * </p>
 * <ul>
 * <li>The string is expected to be a comma-separated list of values.</li>
 * <li>The string may optionally have matching '{' and '}' delimiters around the list.</li>
 * <li>Whitespace before and after each element is stripped.</li>
 * <li>Elements in the list may be delimited by single or double quotes. Within a quoted elements, the normal Java
 * escape sequences are valid.</li>
 * </ul>
 *
 * @param <G> the type in which the provided value has to be converted
 * @param targetType Data type to which this value should be converted.
 * @param value String value to be parsed
 * @param elementType the generic type
 * @return List of parsed elements.
 * @throws ConversionException if the syntax of <code>value</code> is not syntactically valid
 * @throws NullPointerException if <code>value</code> is <code>null</code>
 */
protected <G extends T> G parseElements(Type targetType, String value, Type elementType) {
    String cleanedValue = cleanValue(value);

    try {
        // Set up a StreamTokenizer on the characters in this String
        StreamTokenizer st = createStreamTokenizer(cleanedValue);

        // Split comma-delimited tokens into a List
        T collection = newCollection(targetType);
        while (true) {
            int ttype = st.nextToken();
            if (ttype == StreamTokenizer.TT_WORD || ttype > 0) {
                if (st.sval != null) {
                    Object objValue = st.sval;
                    if (elementType != null && elementType != String.class) {
                        objValue = this.converterManager.convert(elementType, objValue);
                    }

                    collection.add(objValue);
                }
            } else if (ttype == StreamTokenizer.TT_EOF) {
                break;
            } else {
                throw new ConversionException("Encountered token of type " + ttype + " parsing elements.");
            }
        }

        // Return the completed list
        return (G) collection;
    } catch (IOException e) {
        throw new ConversionException("Error converting from String: " + e.getMessage(), e);
    }
}

From source file:org.yestech.jmlnitrate.transformation.inbound.BaseInboundTransformation.java

/**
 *  Parses a Parameter Request String and return a array of the tokens in the
 *  string. The default delimeters are ^ and ;
 *
 * @param  rawRequest the Parameter Request
 * @return  Array of the tokens/*w  w w  . ja  v  a 2 s .co  m*/
 * @throws  Exception if an error happens
 */
private Object[] parseParameterRequest(String rawRequest) throws Exception {
    StreamTokenizer tokenStream = new StreamTokenizer(
            new BufferedReader(new StringReader(rawRequest), BUFFER_SIZE));
    //reset the stream
    tokenStream.resetSyntax();
    //configure tokens
    tokenStream.lowerCaseMode(false);
    tokenStream.eolIsSignificant(false);

    //add word chars
    tokenStream.wordChars(32, 58);
    tokenStream.wordChars(60, 93);
    tokenStream.wordChars(95, 126);

    //add <CR>\r <LF>\n
    tokenStream.wordChars(10, 10);
    tokenStream.wordChars(13, 13);

    //set ^ AND ; as delimeters to string tokens
    tokenStream.quoteChar(94);

    //removed ;
    //tokenStream.quoteChar(59);

    //set up the temp arraylist
    ArrayList tokens = new ArrayList();

    int result = tokenStream.ttype;
    while (result != StreamTokenizer.TT_EOF) {
        result = tokenStream.nextToken();
        switch (result) {
        case StreamTokenizer.TT_EOF:
            break;
        default:
            tokens.add(tokenStream.sval);
            break;
        }
    }
    return tokens.toArray();
}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]// w w  w.  ja v a  2s .  co m
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param fileName
 *            The path to the text file to read the data from
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(String fileName, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    try (Reader r = new BufferedReader(new FileReader(fileName))) {

        StreamTokenizer stok = new StreamTokenizer(r);
        stok.parseNumbers();
        int currentColumn = 0;
        double range0 = 0;
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from file " + fileName + ": " + stok.sval,
                        LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException("Could not find file " + fileName + " from which to read histogram", e);
    } catch (IOException e) {
        printStack(e);
    }

}

From source file:pipeline.misc_util.Utils.java

/**
 * Reads a text file formatted by the GSL histogram outputting functions:
 * range[0] range[1] bin[0]/*from   w w w .j  a  v a  2  s.  c  om*/
 * range[1] range[2] bin[1]
 * range[2] range[3] bin[2]
 * ....
 * range[n-1] range[n] bin[n-1]
 * 
 * Writes the results as a pair of XY values into an XYSeries object from FreeChart,
 * taking the lower end of the range for each pair
 * 
 * @param plot
 *            The plot produced by a plugin
 * @param series
 *            The series object to write the results into
 */
public static void readHistogramIntoXYSeries(PluginIOCells plot, XYSeries series) {
    final int nColumns = 3;
    // From http://www.java2s.com/Code/Java/File-Input-Output/ReadingNumbersfromaTextFile.htm
    byte[] protobufArray = (byte[]) plot.getProperty("Protobuf");
    if (protobufArray == null)
        return;
    Reader r = new StringReader(new String(protobufArray));
    StreamTokenizer stok = new StreamTokenizer(r);
    stok.parseNumbers();
    int currentColumn = 0;
    double range0 = 0;
    try {
        stok.nextToken();

        while (stok.ttype != StreamTokenizer.TT_EOF) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER) {
                if (currentColumn == 0) {
                    range0 = stok.nval;
                } else if (currentColumn == 2) {
                    series.add(range0, stok.nval);
                }
            } else
                Utils.log("Nonnumber while reading histogram from string " + plot.getProperty("Protobuf") + ": "
                        + stok.sval, LogLevel.ERROR);
            stok.nextToken();
            currentColumn++;
            if (currentColumn == nColumns)
                currentColumn = 0;
        }
    } catch (IOException e) {
        printStack(e);
        return;
    }

}

From source file:uk.ac.leeds.ccg.andyt.projects.fluvialglacial.SlopeAreaAnalysis.java

protected TreeMap<Integer, Object[]> readSwissData(File fileIn) {
    TreeMap<Integer, Object[]> result;
    result = new TreeMap<Integer, Object[]>();
    BufferedReader br;/*w ww  . ja  v a2  s  . c  o  m*/
    br = Generic_StaticIO.getBufferedReader(fileIn);
    StreamTokenizer st;
    st = new StreamTokenizer(br);
    Generic_StaticIO.setStreamTokenizerSyntax5(st);
    st.wordChars('(', '(');
    st.wordChars(')', ')');
    st.wordChars('%', '%');
    Generic_StaticIO.skipline(st);
    int token;
    String line = "";
    String[] fields;
    try {
        token = st.nextToken();
        int ID;
        //int pointID;
        while (token != StreamTokenizer.TT_EOF) {
            switch (token) {
            case StreamTokenizer.TT_EOL:
                //flowacc,area (km2),slope_25_(%),proglac_ID,COUNT
                //12.11111069,0.00756944,32.33880000000,0,250631
                fields = line.split(sComma);
                ID = Integer.valueOf(fields[3]);
                if (ID > 0) {
                    //BigDecimal flowacc;
                    BigDecimal area;
                    BigDecimal slope;
                    Object[] data;
                    BigDecimal maxx;
                    BigDecimal maxy;
                    BigDecimal minx;
                    BigDecimal miny;
                    data = result.get(ID);
                    ArrayList<Generic_XYNumericalData> theGeneric_XYNumericalData;
                    if (data == null) {
                        data = new Object[5];
                        theGeneric_XYNumericalData = new ArrayList<Generic_XYNumericalData>();
                        maxx = BigDecimal.ZERO;
                        maxy = BigDecimal.ZERO;
                        minx = BigDecimal.valueOf(Double.MAX_VALUE);
                        miny = BigDecimal.valueOf(Double.MAX_VALUE);
                        data[0] = theGeneric_XYNumericalData;
                        data[1] = maxx;
                        data[2] = minx;
                        data[3] = maxy;
                        data[4] = miny;
                        result.put(ID, data);
                    } else {
                        theGeneric_XYNumericalData = (ArrayList<Generic_XYNumericalData>) data[0];
                        maxx = (BigDecimal) data[1];
                        minx = (BigDecimal) data[2];
                        maxy = (BigDecimal) data[3];
                        miny = (BigDecimal) data[4];
                    }
                    //pointID = Integer.valueOf(fields[4]);
                    //flowacc = new BigDecimal(fields[0]);
                    area = new BigDecimal(fields[1]);
                    if (area.compareTo(BigDecimal.ZERO) == 1) {
                        area = Generic_BigDecimal.log(10, area, 10, RoundingMode.HALF_UP);
                    } else {
                        area = BigDecimal.ZERO;
                    }
                    slope = new BigDecimal(fields[2]);
                    if (slope.compareTo(BigDecimal.ZERO) == 1) {
                        slope = Generic_BigDecimal.log(10, slope, 10, RoundingMode.HALF_UP);
                    } else {
                        slope = BigDecimal.ZERO;
                    }
                    Generic_XYNumericalData point;
                    point = new Generic_XYNumericalData(slope, area);
                    theGeneric_XYNumericalData.add(point);
                    data[0] = theGeneric_XYNumericalData;
                    data[1] = maxx.max(slope);
                    data[2] = minx.min(slope);
                    data[3] = maxy.max(area);
                    data[4] = miny.min(area);
                }
                break;
            case StreamTokenizer.TT_WORD:
                line = st.sval;
                break;
            }
            token = st.nextToken();
        }
    } catch (IOException ex) {
        Logger.getLogger(SlopeAreaAnalysis.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}

From source file:uk.ac.leeds.ccg.andyt.projects.fluvialglacial.SlopeAreaAnalysis.java

protected TreeMap<Integer, Object[]> readAustriaData(File fileIn) {
    TreeMap<Integer, Object[]> result;
    result = new TreeMap<Integer, Object[]>();
    BufferedReader br;/*from www.  j  av a2s  .c o  m*/
    br = Generic_StaticIO.getBufferedReader(fileIn);
    StreamTokenizer st;
    st = new StreamTokenizer(br);
    Generic_StaticIO.setStreamTokenizerSyntax5(st);
    st.wordChars('(', '(');
    st.wordChars(')', ')');
    st.wordChars('%', '%');
    Generic_StaticIO.skipline(st);
    int token;
    String line = "";
    String[] fields;
    try {
        token = st.nextToken();
        int ID;
        //int pointID;
        while (token != StreamTokenizer.TT_EOF) {
            switch (token) {
            case StreamTokenizer.TT_EOL:
                //flowacc,area (km2),slope_25_(%),proglac_ID,COUNT
                //12.11111069,0.00756944,32.33880000000,0,250631
                fields = line.split(sComma);
                ID = Double.valueOf(fields[1]).intValue();
                if (ID > 0) {
                    //BigDecimal flowacc;
                    BigDecimal area;
                    BigDecimal slope;
                    Object[] data;
                    BigDecimal maxx;
                    BigDecimal maxy;
                    BigDecimal minx;
                    BigDecimal miny;
                    data = result.get(ID);
                    ArrayList<Generic_XYNumericalData> theGeneric_XYNumericalData;
                    if (data == null) {
                        data = new Object[5];
                        theGeneric_XYNumericalData = new ArrayList<Generic_XYNumericalData>();
                        maxx = BigDecimal.ZERO;
                        maxy = BigDecimal.ZERO;
                        minx = BigDecimal.valueOf(Double.MAX_VALUE);
                        miny = BigDecimal.valueOf(Double.MAX_VALUE);
                        data[0] = theGeneric_XYNumericalData;
                        data[1] = maxx;
                        data[2] = minx;
                        data[3] = maxy;
                        data[4] = miny;
                        result.put(ID, data);
                    } else {
                        theGeneric_XYNumericalData = (ArrayList<Generic_XYNumericalData>) data[0];
                        maxx = (BigDecimal) data[1];
                        minx = (BigDecimal) data[2];
                        maxy = (BigDecimal) data[3];
                        miny = (BigDecimal) data[4];
                    }
                    //pointID = Integer.valueOf(fields[4]);
                    //flowacc = new BigDecimal(fields[0]);
                    area = new BigDecimal(fields[3]);
                    if (area.compareTo(BigDecimal.ZERO) == 1) {
                        area = Generic_BigDecimal.log(10, area, 10, RoundingMode.HALF_UP);
                    } else {
                        area = BigDecimal.ZERO;
                    }
                    slope = new BigDecimal(fields[2]);
                    if (slope.compareTo(BigDecimal.ZERO) == 1) {
                        slope = Generic_BigDecimal.log(10, slope, 10, RoundingMode.HALF_UP);
                    } else {
                        slope = BigDecimal.ZERO;
                    }
                    Generic_XYNumericalData point;
                    point = new Generic_XYNumericalData(slope, area);
                    theGeneric_XYNumericalData.add(point);
                    data[0] = theGeneric_XYNumericalData;
                    data[1] = maxx.max(slope);
                    data[2] = minx.min(slope);
                    data[3] = maxy.max(area);
                    data[4] = miny.min(area);
                }
                break;
            case StreamTokenizer.TT_WORD:
                line = st.sval;
                break;
            }
            token = st.nextToken();
        }
    } catch (IOException ex) {
        Logger.getLogger(SlopeAreaAnalysis.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result;
}