Example usage for java.io StreamTokenizer eolIsSignificant

List of usage examples for java.io StreamTokenizer eolIsSignificant

Introduction

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

Prototype

public void eolIsSignificant(boolean flag) 

Source Link

Document

Determines whether or not ends of line are treated as tokens.

Usage

From source file:Matrix.java

/**
 * Read a matrix from a stream. The format is the same the print method, so
 * printed matrices can be read back in (provided they were printed using US
 * Locale). Elements are separated by whitespace, all the elements for each
 * row appear on a single line, the last row is followed by a blank line.
 * // ww  w  . j a v  a  2s .c o m
 * @param input
 *            the input stream.
 */

public static Matrix read(BufferedReader input) throws java.io.IOException {
    StreamTokenizer tokenizer = new StreamTokenizer(input);

    // Although StreamTokenizer will parse numbers, it doesn't recognize
    // scientific notation (E or D); however, Double.valueOf does.
    // The strategy here is to disable StreamTokenizer's number parsing.
    // We'll only get whitespace delimited words, EOL's and EOF's.
    // These words should all be numbers, for Double.valueOf to parse.

    tokenizer.resetSyntax();
    tokenizer.wordChars(0, 255);
    tokenizer.whitespaceChars(0, ' ');
    tokenizer.eolIsSignificant(true);
    java.util.Vector v = new java.util.Vector();

    // Ignore initial empty lines
    while (tokenizer.nextToken() == StreamTokenizer.TT_EOL)
        ;
    if (tokenizer.ttype == StreamTokenizer.TT_EOF)
        throw new java.io.IOException("Unexpected EOF on matrix read.");
    do {
        v.addElement(Double.valueOf(tokenizer.sval)); // Read & store 1st
        // row.
    } while (tokenizer.nextToken() == StreamTokenizer.TT_WORD);

    int n = v.size(); // Now we've got the number of columns!
    double row[] = new double[n];
    for (int j = 0; j < n; j++)
        // extract the elements of the 1st row.
        row[j] = ((Double) v.elementAt(j)).doubleValue();
    v.removeAllElements();
    v.addElement(row); // Start storing rows instead of columns.
    while (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
        // While non-empty lines
        v.addElement(row = new double[n]);
        int j = 0;
        do {
            if (j >= n)
                throw new java.io.IOException("Row " + v.size() + " is too long.");
            row[j++] = Double.valueOf(tokenizer.sval).doubleValue();
        } while (tokenizer.nextToken() == StreamTokenizer.TT_WORD);
        if (j < n)
            throw new java.io.IOException("Row " + v.size() + " is too short.");
    }
    int m = v.size(); // Now we've got the number of rows.
    double[][] A = new double[m][];
    v.copyInto(A); // copy the rows out of the vector
    return new Matrix(A);
}

From source file:com.redskyit.scriptDriver.RunTests.java

private void parseBlock(ExecutionContext script, StreamTokenizer tokenizer, BlockHandler handler)
        throws Exception {
    tokenizer.nextToken();/*w  ww .  ja v a 2  s .co m*/
    if (tokenizer.ttype == '{') {
        int braceLevel = 1;
        System.out.print(" {");
        tokenizer.eolIsSignificant(true);
        tokenizer.nextToken();
        while (tokenizer.ttype == StreamTokenizer.TT_WORD || tokenizer.ttype == '"'
                || tokenizer.ttype == StreamTokenizer.TT_NUMBER || tokenizer.ttype == '\n'
                || tokenizer.ttype == '{' || tokenizer.ttype == '}' || tokenizer.ttype == ','
                || tokenizer.ttype == '*' || tokenizer.ttype == ':') {
            System.out.print(' ');
            String arg;
            switch (tokenizer.ttype) {
            case '{':
                braceLevel++;
                arg = "{";
                break;
            case '}':
                if (--braceLevel == 0) {
                    System.out.print("}");
                    tokenizer.eolIsSignificant(false);
                    return;
                }
                arg = "}";
                break;
            case StreamTokenizer.TT_NUMBER:
                arg = String.valueOf(tokenizer.nval);
                break;
            case StreamTokenizer.TT_EOL:
                arg = "\n";
                break;
            case '"':
                // 5 backslashed required in replace string because its processed once
                // as a string (yielding \\") and then again by the replace method
                // of the regular expression (so \\" becomes \")
                arg = '"' + script.getExpandedString(tokenizer).replaceAll("\"", "\\\\\"") + '"';
                break;
            case ',':
                arg = ",";
                break;
            case ':':
                arg = ":";
                break;
            case '*':
                arg = "*";
                break;
            default:
                arg = script.getExpandedString(tokenizer);
            }
            System.out.print(arg);
            handler.parseToken(tokenizer, arg);
            tokenizer.nextToken();
        }
        System.out.println();
        throw new Exception("args unexpectd token " + tokenizer.ttype);
    }
    tokenizer.pushBack(); // no arguments
}

From source file:FourByFour.java

public void actionPerformed(ActionEvent event) {

    Object target = event.getSource();

    // Process the button events.
    if (target == skill_return_button) {
        skill_panel.setVisible(false);//from w  ww.ja v  a2s.c  o  m
        skill_return_button.setVisible(false);
        c_container.setVisible(true);
        b_container.setVisible(true);
        newGame();
    } else if (target == winner_return_button) {
        if (winner_flag) {
            String name = winner_name.getText();
            String tmp_name = new String("");
            int tmp_score = 0;
            boolean insert_flag = false;
            winner_flag = false;
            for (int i = 0; i < 20; i++) {
                if (insert_flag) {
                    name = names[i];
                    score = scores[i];
                    names[i] = tmp_name;
                    scores[i] = tmp_score;
                    tmp_name = name;
                    tmp_score = score;
                }
                if (!insert_flag && score > scores[i]) {
                    tmp_name = names[i];
                    tmp_score = scores[i];
                    scores[i] = score;
                    names[i] = name;
                    insert_flag = true;
                }
                high_names[i].setText(names[i]);
                high_scores[i].setText(Integer.toString(scores[i]));
            }
            scoresString = new String("");
            int place;
            for (int i = 0; i < 20; i++) {
                place = (int) places[i];
                scoresString += Integer.toString(place);
                scoresString += "\t";
                scoresString += names[i];
                scoresString += "   ";
                scoresString += Integer.toString(scores[i]);
                scoresString += "\n";
            }

            if (appletFlag) {
                // Use this section of code when writing the high
                // scores file back to a server. Requires the use
                // of a deamon on the server to receive the socket
                // connection.
                //
                // Create the output stream.
                // try {
                //    Socket socket = new Socket(host, port);
                //    outStream = new BufferedOutputStream
                //       (socket.getOutputStream(), 8192);
                // }
                // catch(IOException ioe) {
                //    System.out.println("Error: " + ioe.toString());
                // }
                // System.out.println("Output stream opened");
                //
                // Write the scores to the file back on the server.
                // outText = scoresString.getBytes();
                // try {
                //    outStream.write(outText);
                //    outStream.flush();
                //    outStream.close();
                //    outStream = null;
                // }
                // catch (IOException ioe) {
                //    System.out.println("Error: " + ioe.toString());
                // }
                // System.out.println("Output stream written");

                try {
                    OutputStreamWriter outFile = new OutputStreamWriter(new FileOutputStream("scores.txt"));
                    outFile.write(scoresString);
                    outFile.flush();
                    outFile.close();
                    outFile = null;
                } catch (IOException ioe) {
                    System.out.println("Error: " + ioe.toString());
                } catch (Exception e) {
                    System.out.println("Error: " + e.toString());
                }
            } else {

                try {
                    OutputStreamWriter outFile = new OutputStreamWriter(new FileOutputStream("scores.txt"));
                    outFile.write(scoresString);
                    outFile.flush();
                    outFile.close();
                    outFile = null;
                } catch (IOException ioe) {
                    System.out.println("Error: " + ioe.toString());
                }
            }
        }
        winner_panel.setVisible(false);
        winner_return_button.setVisible(false);
        winner_label.setVisible(false);
        winner_score_label.setVisible(false);
        winner_name_label.setVisible(false);
        winner_top_label.setVisible(false);
        winner_name.setVisible(false);
        c_container.setVisible(true);
        b_container.setVisible(true);
    } else if (target == high_return_button) {
        high_return_button.setVisible(false);
        high_panel.setVisible(false);
        c_container.setVisible(true);
        b_container.setVisible(true);
    } else if (target == instruct_return_button) {
        instruct_text.setVisible(false);
        instruct_return_button.setVisible(false);
        instruct_text.repaint();
        c_container.setVisible(true);
        b_container.setVisible(true);
    } else if (target == undo_button) {
        board.undo_move();
        canvas2D.repaint();
    } else if (target == instruct_button) {
        c_container.setVisible(false);
        b_container.setVisible(false);
        instruct_text.setVisible(true);
        instruct_return_button.setVisible(true);
    } else if (target == new_button) {
        newGame();
    } else if (target == skill_button) {
        c_container.setVisible(false);
        b_container.setVisible(false);
        skill_panel.setVisible(true);
        skill_return_button.setVisible(true);
    } else if (target == high_button) {
        // Read the high scores file.
        if (appletFlag) {
            try {
                inStream = new BufferedInputStream(new URL(getCodeBase(), "scores.txt").openStream(), 8192);
                Reader read = new BufferedReader(new InputStreamReader(inStream));
                StreamTokenizer st = new StreamTokenizer(read);
                st.whitespaceChars(32, 44);
                st.eolIsSignificant(false);

                int count = 0;
                int token = st.nextToken();
                boolean scoreFlag = true;
                String string;
                while (count < 20) {
                    places[count] = (int) st.nval;
                    string = new String("");
                    token = st.nextToken();
                    while (token == StreamTokenizer.TT_WORD) {
                        string += st.sval;
                        string += " ";
                        token = st.nextToken();
                    }
                    names[count] = string;
                    scores[count] = (int) st.nval;
                    token = st.nextToken();
                    count++;
                }
                inStream.close();
            } catch (Exception ioe) {
                System.out.println("Error: " + ioe.toString());
            }
        } else {
            try {
                inStream = new BufferedInputStream(new FileInputStream("scores.txt"));
                Reader read = new BufferedReader(new InputStreamReader(inStream));
                StreamTokenizer st = new StreamTokenizer(read);
                st.whitespaceChars(32, 44);
                st.eolIsSignificant(false);

                int count = 0;
                int token = st.nextToken();
                boolean scoreFlag = true;
                String string;
                while (count < 20) {
                    places[count] = (int) st.nval;
                    string = new String("");
                    token = st.nextToken();
                    while (token == StreamTokenizer.TT_WORD) {
                        string += st.sval;
                        string += " ";
                        token = st.nextToken();
                    }
                    names[count] = string;
                    scores[count] = (int) st.nval;
                    token = st.nextToken();
                    count++;
                }
                inStream.close();
            } catch (Exception ioe) {
                System.out.println("Error: " + ioe.toString());
            }
        }
        c_container.setVisible(false);
        b_container.setVisible(false);
        high_panel.setVisible(true);
        high_return_button.setVisible(true);
    }

    Checkbox box = group.getSelectedCheckbox();
    String label = box.getLabel();
    if (label.equals("Babe in the Woods        ")) {
        board.set_skill_level(0);
    } else if (label.equals("Walk and Chew Gum        ")) {
        board.set_skill_level(1);
    } else if (label.equals("Jeopardy Contestant      ")) {
        board.set_skill_level(2);
    } else if (label.equals("Rocket Scientist         ")) {
        board.set_skill_level(3);
    } else if (label.equals("Be afraid, be very afraid")) {
        board.set_skill_level(4);
    }
}

From source file:FourByFour.java

/**
 * Initialization/* www. ja  v  a 2  s .  co  m*/
 */
public void init() {

    // Set the port number.
    port = 4111;

    // Set the graphics window size.
    width = 350;
    height = 350;

    // Set the weighting factors used for scoring.
    level_weight = 1311;
    move_weight = 111;
    time_weight = 1000;

    // Create the "base" color for the AWT components.
    setBackground(new Color(200, 200, 200));

    // Read the instructions file.
    if (appletFlag) {

        // Get the host from which this applet came.
        host = getCodeBase().getHost();

        try {
            inStream = new BufferedInputStream(new URL(getCodeBase(), "instructions.txt").openStream(), 8192);
            text = new byte[5000];
            int character = inStream.read();
            int count = 0;
            while (character != -1) {
                text[count++] = (byte) character;
                character = inStream.read();
            }
            textString = new String(text);
            inStream.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    } else {

        try {
            inStream = new BufferedInputStream(new FileInputStream("instructions.txt"));
            text = new byte[5000];
            int character = inStream.read();
            int count = 0;
            while (character != -1) {
                text[count++] = (byte) character;
                character = inStream.read();
            }
            textString = new String(text);
            inStream.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    }

    // Read the high-scores file.
    places = new int[20];
    scores = new int[20];
    names = new String[20];
    if (appletFlag) {
        try {
            inStream = new BufferedInputStream(new URL(getCodeBase(), "scores.txt").openStream(), 8192);
            Reader read = new BufferedReader(new InputStreamReader(inStream));
            StreamTokenizer st = new StreamTokenizer(read);
            st.whitespaceChars(32, 44);
            st.eolIsSignificant(false);

            int count = 0;
            int token = st.nextToken();
            boolean scoreFlag = true;
            String string;
            while (count < 20) {
                places[count] = (int) st.nval;
                string = new String("");
                token = st.nextToken();
                while (token == StreamTokenizer.TT_WORD) {
                    string += st.sval;
                    string += " ";
                    token = st.nextToken();
                }
                names[count] = string;
                scores[count] = (int) st.nval;
                token = st.nextToken();
                count++;
            }
            inStream.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    } else {
        try {
            inStream = new BufferedInputStream(new FileInputStream("scores.txt"));
            Reader read = new BufferedReader(new InputStreamReader(inStream));
            StreamTokenizer st = new StreamTokenizer(read);
            st.whitespaceChars(32, 44);
            st.eolIsSignificant(false);

            int count = 0;
            int token = st.nextToken();
            boolean scoreFlag = true;
            String string;
            while (count < 20) {
                places[count] = (int) st.nval;
                string = new String("");
                token = st.nextToken();
                while (token == StreamTokenizer.TT_WORD) {
                    string += st.sval;
                    string += " ";
                    token = st.nextToken();
                }
                names[count] = string;
                scores[count] = (int) st.nval;
                token = st.nextToken();
                count++;
            }
            inStream.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.toString());
        }
    }

    // The positions object sets up the switch nodes which
    // control the rendering of the player's positions.
    positions = new Positions();

    // Create the game board object which is responsible
    // for keeping track of the moves on the game board
    // and determining what move the computer should make.
    board = new Board(this, positions, width, height);
    positions.setBoard(board);

    // Create a 2D graphics canvas.
    canvas2D = new Canvas2D(board);
    canvas2D.setSize(width, height);
    canvas2D.setLocation(width + 10, 5);
    canvas2D.addMouseListener(canvas2D);
    board.setCanvas(canvas2D);

    // Create the 2D backbuffer
    backbuffer2D = createImage(width, height);
    canvas2D.setBuffer(backbuffer2D);

    // Create a 3D graphics canvas.
    canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    canvas3D.setSize(width, height);
    canvas3D.setLocation(5, 5);

    // Create the scene branchgroup.
    BranchGroup scene3D = createScene3D();

    // Create a universe with the Java3D universe utility.
    universe = new SimpleUniverse(canvas3D);
    universe.addBranchGraph(scene3D);

    // Use parallel projection.
    View view = universe.getViewer().getView();
    view.setProjectionPolicy(View.PARALLEL_PROJECTION);

    // Set the universe Transform3D object.
    TransformGroup tg = universe.getViewingPlatform().getViewPlatformTransform();
    Transform3D transform = new Transform3D();
    transform.set(65.f, new Vector3f(0.0f, 0.0f, 400.0f));
    tg.setTransform(transform);

    // Create the canvas container.
    c_container = new Panel();
    c_container.setSize(720, 360);
    c_container.setLocation(0, 0);
    c_container.setVisible(true);
    c_container.setLayout(null);
    add(c_container);

    // Add the 2D and 3D canvases to the container.
    c_container.add(canvas2D);
    c_container.add(canvas3D);

    // Turn off the layout manager, widgets will be sized
    // and positioned explicitly.
    setLayout(null);

    // Create the button container.
    b_container = new Panel();
    b_container.setSize(720, 70);
    b_container.setLocation(0, 360);
    b_container.setVisible(true);
    b_container.setLayout(null);

    // Create the buttons.
    instruct_button = new Button("Instructions");
    instruct_button.setSize(135, 25);
    instruct_button.setLocation(10, 10);
    instruct_button.setVisible(true);
    instruct_button.addActionListener(this);

    new_button = new Button("New Game");
    new_button.setSize(135, 25);
    new_button.setLocation(150, 10);
    new_button.setVisible(true);
    new_button.addActionListener(this);

    undo_button = new Button("Undo Move");
    undo_button.setSize(135, 25);
    undo_button.setLocation(290, 10);
    undo_button.setVisible(true);
    undo_button.addActionListener(this);

    skill_button = new Button("Skill Level");
    skill_button.setSize(135, 25);
    skill_button.setLocation(430, 10);
    skill_button.setVisible(true);
    skill_button.addActionListener(this);

    high_button = new Button("High Scores");
    high_button.setSize(135, 25);
    high_button.setLocation(570, 10);
    high_button.setVisible(true);
    high_button.addActionListener(this);

    b_container.add(new_button);
    b_container.add(undo_button);
    b_container.add(skill_button);
    b_container.add(high_button);
    b_container.add(instruct_button);

    // Add the button container to the applet.
    add(b_container);

    // Create the "Skill Level" dialog box.
    skill_panel = new Panel();
    skill_panel.setSize(400, 300);
    skill_panel.setLocation(200, 20);
    skill_panel.setLayout(null);

    skill_label = new Label("Pick your skill level:");
    skill_label.setSize(200, 25);
    skill_label.setLocation(25, 20);
    skill_label.setVisible(true);
    skill_panel.add(skill_label);

    group = new CheckboxGroup();
    Checkbox skill_1 = new Checkbox("Babe in the Woods        ", group, false);
    Checkbox skill_2 = new Checkbox("Walk and Chew Gum        ", group, false);
    Checkbox skill_3 = new Checkbox("Jeopardy Contestant      ", group, false);
    Checkbox skill_4 = new Checkbox("Rocket Scientist         ", group, false);
    Checkbox skill_5 = new Checkbox("Be afraid, be very afraid", group, true);
    skill_1.setSize(170, 25);
    skill_1.setLocation(80, 60);
    skill_1.setVisible(true);
    skill_2.setSize(170, 25);
    skill_2.setLocation(80, 100);
    skill_2.setVisible(true);
    skill_3.setSize(170, 25);
    skill_3.setLocation(80, 140);
    skill_3.setVisible(true);
    skill_4.setSize(170, 25);
    skill_4.setLocation(80, 180);
    skill_4.setVisible(true);
    skill_5.setSize(170, 25);
    skill_5.setLocation(80, 220);
    skill_5.setVisible(true);
    skill_return_button = new Button("Return");
    skill_return_button.setSize(120, 25);
    skill_return_button.setLocation(300, 370);
    skill_return_button.setVisible(false);
    skill_return_button.addActionListener(this);
    skill_panel.add(skill_1);
    skill_panel.add(skill_2);
    skill_panel.add(skill_3);
    skill_panel.add(skill_4);
    skill_panel.add(skill_5);
    skill_panel.setVisible(false);
    add(skill_return_button);
    add(skill_panel);

    // Create the "Instructions" panel.
    instruct_return_button = new Button("Return");
    instruct_return_button.setLocation(300, 370);
    instruct_return_button.setSize(120, 25);
    instruct_return_button.setVisible(false);
    instruct_return_button.addActionListener(this);
    instruct_text = new TextArea(textString, 100, 200, TextArea.SCROLLBARS_VERTICAL_ONLY);
    instruct_text.setSize(715, 350);
    instruct_text.setLocation(0, 0);
    instruct_text.setVisible(false);
    add(instruct_text);

    add(instruct_return_button);

    high_panel = new Panel();
    high_panel.setSize(715, 350);
    high_panel.setLocation(0, 0);
    high_panel.setVisible(false);
    high_panel.setLayout(null);

    high_label = new Label("High Scores");
    high_label.setLocation(330, 5);
    high_label.setSize(200, 30);
    high_label.setVisible(true);
    high_panel.add(high_label);

    high_places = new Label[20];
    high_names = new Label[20];
    high_scores = new Label[20];
    for (int i = 0; i < 20; i++) {
        high_places[i] = new Label(Integer.toString(i + 1));
        high_places[i].setSize(20, 30);
        high_places[i].setVisible(true);
        high_names[i] = new Label(names[i]);
        high_names[i].setSize(150, 30);
        high_names[i].setVisible(true);
        high_scores[i] = new Label(Integer.toString(scores[i]));
        high_scores[i].setSize(150, 30);
        high_scores[i].setVisible(true);
        if (i < 10) {
            high_places[i].setLocation(70, i * 30 + 40);
            high_names[i].setLocation(100, i * 30 + 40);
            high_scores[i].setLocation(260, i * 30 + 40);
        } else {
            high_places[i].setLocation(425, (i - 10) * 30 + 40);
            high_names[i].setLocation(455, (i - 10) * 30 + 40);
            high_scores[i].setLocation(615, (i - 10) * 30 + 40);
        }
        high_panel.add(high_places[i]);
        high_panel.add(high_names[i]);
        high_panel.add(high_scores[i]);
    }
    high_return_button = new Button("Return");
    high_return_button.setSize(120, 25);
    high_return_button.setLocation(300, 370);
    high_return_button.setVisible(false);
    high_return_button.addActionListener(this);
    add(high_return_button);
    add(high_panel);

    // Create the "Winner" dialog box
    winner_panel = new Panel();
    winner_panel.setLayout(null);
    winner_panel.setSize(600, 500);
    winner_panel.setLocation(0, 0);
    winner_return_button = new Button("Return");
    winner_return_button.setSize(120, 25);
    winner_return_button.setLocation(300, 360);
    winner_return_button.addActionListener(this);
    winner_panel.add(winner_return_button);
    winner_label = new Label("");
    winner_label.setSize(200, 30);
    winner_label.setLocation(270, 110);
    winner_score_label = new Label("");
    winner_score_label.setSize(200, 30);
    winner_top_label = new Label("You have a score in the top 20.");
    winner_top_label.setSize(200, 25);
    winner_top_label.setLocation(260, 185);
    winner_top_label.setVisible(false);
    winner_name_label = new Label("Enter your name here:");
    winner_name_label.setSize(150, 25);
    winner_name_label.setLocation(260, 210);
    winner_name_label.setVisible(false);
    winner_name = new TextField("");
    winner_name.setSize(200, 30);
    winner_name.setLocation(260, 240);
    winner_name.setVisible(false);
    winner_panel.add(winner_label);
    winner_panel.add(winner_score_label);
    winner_panel.add(winner_top_label);
    winner_panel.add(winner_name_label);
    winner_panel.add(winner_name);
    winner_panel.setVisible(false);
    add(winner_panel);
}

From source file:org.apache.wiki.plugin.DefaultPluginManager.java

/**
 *  Parses plugin arguments.  Handles quotes and all other kewl stuff.
 *
 *  <h3>Special parameters</h3>
 *  The plugin body is put into a special parameter defined by {@link #PARAM_BODY};
 *  the plugin's command line into a parameter defined by {@link #PARAM_CMDLINE};
 *  and the bounds of the plugin within the wiki page text by a parameter defined
 *  by {@link #PARAM_BOUNDS}, whose value is stored as a two-element int[] array,
 *  i.e., <tt>[start,end]</tt>.
 *
 * @param argstring The argument string to the plugin.  This is
 *  typically a list of key-value pairs, using "'" to escape
 *  spaces in strings, followed by an empty line and then the
 *  plugin body.  In case the parameter is null, will return an
 *  empty parameter list./* w w  w.ja va 2 s .c o  m*/
 *
 * @return A parsed list of parameters.
 *
 * @throws IOException If the parsing fails.
 */
public Map<String, String> parseArgs(String argstring) throws IOException {
    Map<String, String> arglist = new HashMap<String, String>();

    //
    //  Protection against funny users.
    //
    if (argstring == null)
        return arglist;

    arglist.put(PARAM_CMDLINE, argstring);

    StringReader in = new StringReader(argstring);
    StreamTokenizer tok = new StreamTokenizer(in);
    int type;

    String param = null;
    String value = null;

    tok.eolIsSignificant(true);

    boolean potentialEmptyLine = false;
    boolean quit = false;

    while (!quit) {
        String s;

        type = tok.nextToken();

        switch (type) {
        case StreamTokenizer.TT_EOF:
            quit = true;
            s = null;
            break;

        case StreamTokenizer.TT_WORD:
            s = tok.sval;
            potentialEmptyLine = false;
            break;

        case StreamTokenizer.TT_EOL:
            quit = potentialEmptyLine;
            potentialEmptyLine = true;
            s = null;
            break;

        case StreamTokenizer.TT_NUMBER:
            s = Integer.toString((int) tok.nval);
            potentialEmptyLine = false;
            break;

        case '\'':
            s = tok.sval;
            break;

        default:
            s = null;
        }

        //
        //  Assume that alternate words on the line are
        //  parameter and value, respectively.
        //
        if (s != null) {
            if (param == null) {
                param = s;
            } else {
                value = s;

                arglist.put(param, value);

                // log.debug("ARG: "+param+"="+value);
                param = null;
            }
        }
    }

    //
    //  Now, we'll check the body.
    //

    if (potentialEmptyLine) {
        StringWriter out = new StringWriter();
        FileUtil.copyContents(in, out);

        String bodyContent = out.toString();

        if (bodyContent != null) {
            arglist.put(PARAM_BODY, bodyContent);
        }
    }

    return arglist;
}

From source file:org.opencms.main.CmsShell.java

/**
 * Executes all commands read from the given input stream.<p>
 * // ww w  .ja  v  a2  s.  co  m
 * @param fileInputStream a file input stream from which the commands are read
 */
private void executeCommands(FileInputStream fileInputStream) {

    try {
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(fileInputStream));
        while (!m_exitCalled) {
            printPrompt();
            String line = lnr.readLine();
            if (line == null) {
                // if null the file has been read to the end
                try {
                    Thread.sleep(500);
                } catch (Throwable t) {
                    // noop
                }
                break;
            }
            if (line.trim().startsWith("#")) {
                System.out.println(line);
                continue;
            }
            StringReader reader = new StringReader(line);
            StreamTokenizer st = new StreamTokenizer(reader);
            st.eolIsSignificant(true);

            // put all tokens into a List
            List<String> parameters = new ArrayList<String>();
            while (st.nextToken() != StreamTokenizer.TT_EOF) {
                if (st.ttype == StreamTokenizer.TT_NUMBER) {
                    parameters.add(Integer.toString(new Double(st.nval).intValue()));
                } else {
                    parameters.add(st.sval);
                }
            }
            reader.close();

            // extract command and arguments
            if (parameters.size() == 0) {
                if (m_echo) {
                    System.out.println();
                }
                continue;
            }
            String command = parameters.get(0);
            parameters = parameters.subList(1, parameters.size());

            // execute the command
            executeCommand(command, parameters);
        }
    } catch (Throwable t) {
        t.printStackTrace(System.err);
    }
}

From source file:org.structr.core.function.Functions.java

public static Object evaluate(final ActionContext actionContext, final GraphObject entity,
        final String expression) throws FrameworkException {

    final String expressionWithoutNewlines = expression.replace('\n', ' ');
    final StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(expressionWithoutNewlines));
    tokenizer.eolIsSignificant(true);
    tokenizer.ordinaryChar('.');
    tokenizer.wordChars('_', '_');
    tokenizer.wordChars('.', '.');
    tokenizer.wordChars('!', '!');

    Expression root = new RootExpression();
    Expression current = root;//from  w w  w  . j  a va 2s .  co  m
    Expression next = null;
    String lastToken = null;
    int token = 0;
    int level = 0;

    while (token != StreamTokenizer.TT_EOF) {

        token = nextToken(tokenizer);

        switch (token) {

        case StreamTokenizer.TT_EOF:
            break;

        case StreamTokenizer.TT_EOL:
            break;

        case StreamTokenizer.TT_NUMBER:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before NUMBER");
            }
            next = new ConstantExpression(tokenizer.nval);
            current.add(next);
            lastToken += "NUMBER";
            break;

        case StreamTokenizer.TT_WORD:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
            }
            next = checkReservedWords(tokenizer.sval);
            Expression previousExpression = current.getPrevious();
            if (tokenizer.sval.startsWith(".") && previousExpression != null
                    && previousExpression instanceof FunctionExpression && next instanceof ValueExpression) {

                final FunctionExpression previousFunctionExpression = (FunctionExpression) previousExpression;
                final ValueExpression valueExpression = (ValueExpression) next;

                current.replacePrevious(
                        new FunctionValueExpression(previousFunctionExpression, valueExpression));
            } else {
                current.add(next);
            }
            lastToken = tokenizer.sval;
            break;

        case '(':
            if (((current == null || current instanceof RootExpression) && next == null) || current == next) {

                // an additional bracket without a new function,
                // this can only be an execution group.
                next = new GroupExpression();
                current.add(next);
            }

            current = next;
            lastToken += "(";
            level++;
            break;

        case ')':
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + lastToken);
            }
            current = current.getParent();
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket after " + lastToken);
            }
            lastToken += ")";
            level--;
            break;

        case '[':
            // bind directly to the previous expression
            next = new ArrayExpression();
            current.add(next);
            current = next;
            lastToken += "[";
            level++;
            break;

        case ']':
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket before " + lastToken);
            }
            current = current.getParent();
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched closing bracket after " + lastToken);
            }
            lastToken += "]";
            level--;
            break;

        case ';':
            next = null;
            lastToken += ";";
            break;

        case ',':
            next = current;
            lastToken += ",";
            break;

        default:
            if (current == null) {
                throw new FrameworkException(422,
                        "Invalid expression: mismatched opening bracket before " + tokenizer.sval);
            }
            current.add(new ConstantExpression(tokenizer.sval));
            lastToken = tokenizer.sval;

        }
    }

    if (level > 0) {
        throw new FrameworkException(422, "Invalid expression: mismatched closing bracket after " + lastToken);
    }

    return root.evaluate(actionContext, entity);
}

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/*ww w . j  a v a 2  s  .c  om*/
 * @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();
}