Example usage for java.io StreamTokenizer StreamTokenizer

List of usage examples for java.io StreamTokenizer StreamTokenizer

Introduction

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

Prototype

public StreamTokenizer(Reader r) 

Source Link

Document

Create a tokenizer that parses the given character stream.

Usage

From source file:com.bibisco.filters.FileFilter.java

/**
 * Example of how to get useful things with the uploaded file structure.
  * Generally speaking, this method should be overrided by framework's users.
 * //from  www  .ja  v  a  2 s . c  o  m
 * <p>Here we demostrate how to extract useful infos 
 * (<code>name, value, isInMemory, size, etc) </code>)
 * plus how to deal with memory- or disk-persisted cases.
 * 
 * <li><p>We pass the whole <code>FileItem</code> structure
 * to the next <code>jsp</code> page, which gains the ability to extract 
 * infos as well: via Request, under name: "file-" + fieldname
 * 
 * <li><p>The file content can be retrieved here or later, <code>FileItem</code>
 * object can use its data-getters in <code>.jsp</code>s! 
 * <p>In this code, we retrieve file content and pass it in Request 
 * for next uses under the a general format of 
 * array of bytes (<code>byte []</code>);  with name equal to "file-" + fieldname.  
 *  
 * @param pItem
 * @throws IOException
 */
protected void processUploadedFile(FileItem pItem, ServletRequest pRequest) throws IOException {
    String name = pItem.getFieldName();
    boolean isInMemory = pItem.isInMemory();
    pRequest.setAttribute("file-" + name, pItem);

    if (isInMemory) {
        mLog.debug("the file ", name, " is in memory under the request attribute file-content-", name);
        byte[] data = pItem.get();
        pRequest.setAttribute("file-content-" + name, data);
    } else {
        mLog.debug("the file ", name, " is in the file system and under the request attribute file-content-",
                name);
        InputStream uploadedStream = pItem.getInputStream();
        byte[] data = (new StreamTokenizer(new BufferedReader(new InputStreamReader(uploadedStream))))
                .toString().getBytes();
        uploadedStream.close();
        pRequest.setAttribute("file-content-" + name, data);
    }
}

From source file:com.enonic.cms.business.portal.datasource.expressionfunctions.ExpressionFunctions.java

/**
 * This method will take a freetext search string and create a valid query that can be used in the getContent* methods.  The search
 * string is spilt into tokens.  Using the operator, it may be specified whether the field must contain all or any of the words in the
 * search string.//from   w ww  .j  a v a 2  s  .  c o  m
 *
 * @param fieldName    The name of the field to search for the words in the search string.
 * @param searchString The words to search for.
 * @param operator     Must be either AND or OR.  Case doesn't matter.
 * @return A syntactically correct search that may be used as the query parameter in getContent* methods on the data source. With care,
 *         it may also be merged with other queries using AND or OR.
 * @throws IllegalArgumentException If any of the parameters are empty or the operator is not AND or OR.
 */
public String buildFreetextQuery(String fieldName, String searchString, String operator) {
    if (searchString == null || searchString.trim().equals("")) {
        return "";
    }
    if (fieldName == null || fieldName.trim().equals("")) {
        throw new IllegalArgumentException("fieldName can not be empty.");
    }

    String op = "";
    if (operator != null) {
        op = operator.trim().toUpperCase();
    }
    if (!(op.equals("AND") || op.equals("OR"))) {
        throw new IllegalArgumentException("Illegal operator: " + operator);
    }

    boolean first = true;
    StringBuffer queryTokens = new StringBuffer();
    Reader searchStringReader = new StringReader(searchString);
    StreamTokenizer searchStringTokens = new StreamTokenizer(searchStringReader);
    searchStringTokens.slashSlashComments(false);
    searchStringTokens.slashStarComments(false);
    searchStringTokens.eolIsSignificant(false);
    searchStringTokens.ordinaryChar('!');
    searchStringTokens.ordinaryChars('#', '}');
    searchStringTokens.wordChars('!', '!');
    searchStringTokens.wordChars('#', '}');

    try {
        while (searchStringTokens.nextToken() != StreamTokenizer.TT_EOF) {
            String token = searchStringTokens.sval;
            addQueryPart(queryTokens, first, fieldName, token, op);
            if (first) {
                first = false;
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException("This should never happen, since the IO class is wrapping a string!");
    }

    return queryTokens.toString();
}

From source file:com.enonic.cms.core.portal.datasource.el.ExpressionFunctions.java

/**
 * This method will take a freetext search string and create a valid query that can be used in the getContent* methods.  The search
 * string is spilt into tokens.  Using the operator, it may be specified whether the field must contain all or any of the words in the
 * search string.//from  w ww  .  ja  va 2  s . c o  m
 *
 * @param fieldName    The name of the field to search for the words in the search string.
 * @param searchString The words to search for.
 * @param operator     Must be either AND or OR.  Case doesn't matter.
 * @return A syntactically correct search that may be used as the query parameter in getContent* methods on the data source. With care,
 *         it may also be merged with other queries using AND or OR.
 * @throws IllegalArgumentException If any of the parameters are empty or the operator is not AND or OR.
 */
public String buildFreetextQuery(String fieldName, String searchString, String operator) {
    if (searchString == null || searchString.trim().equals("")) {
        return "";
    }
    if (fieldName == null || fieldName.trim().equals("")) {
        throw new IllegalArgumentException("fieldName can not be empty.");
    }

    String op = "";
    if (operator != null) {
        op = operator.trim().toUpperCase();
    }
    if (!(op.equals("AND") || op.equals("OR"))) {
        throw new IllegalArgumentException("Illegal operator: " + operator);
    }

    boolean first = true;
    StringBuilder queryTokens = new StringBuilder();
    Reader searchStringReader = new StringReader(searchString);
    StreamTokenizer searchStringTokens = new StreamTokenizer(searchStringReader);
    searchStringTokens.slashSlashComments(false);
    searchStringTokens.slashStarComments(false);
    searchStringTokens.eolIsSignificant(false);
    searchStringTokens.ordinaryChar('!');
    searchStringTokens.ordinaryChars('#', '}');
    searchStringTokens.wordChars('!', '!');
    searchStringTokens.wordChars('#', '}');

    try {
        while (searchStringTokens.nextToken() != StreamTokenizer.TT_EOF) {
            String token = searchStringTokens.sval;
            addQueryPart(queryTokens, first, fieldName, token, op);
            if (first) {
                first = false;
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException("This should never happen, since the IO class is wrapping a string!");
    }

    return queryTokens.toString();
}

From source file:edu.buffalo.cse.pigout.parser.PigOutMacro.java

void validate() throws IOException {
    if (rets.isEmpty()) {
        return;//w  ww  . j a v  a2 s . c o  m
    }

    HashSet<String> testSet = new HashSet<String>();

    StreamTokenizer st = new StreamTokenizer(new StringReader(body));

    st.wordChars('.', '.');
    st.wordChars('0', '9');
    st.wordChars('_', '_');
    st.wordChars('$', '$');
    st.lowerCaseMode(false);
    st.ordinaryChar('/');
    st.slashStarComments(true);

    while (st.nextToken() != StreamTokenizer.TT_EOF) {
        if (matchWord(st, "define", false) && matchDollarAlias(st, true)) {
            testSet.add(st.sval.substring(1));
        } else if (matchDollarAlias(st, false)) {
            String prevWord = st.sval;
            if (matchWord(st, "if", true) || matchWord(st, "otherwise", true)) {
                testSet.add(prevWord.substring(1));
            } else if (matchChar(st, '=', true) && !matchChar(st, '=', true)) {
                testSet.add(prevWord.substring(1));
            } else if (matchChar(st, ',', true)) {
                // possible mult-alias inlining of a macro
                ArrayList<String> mlist = new ArrayList<String>();
                mlist.add(prevWord);
                if (isMultiValueReturn(st, mlist, true)) {
                    for (String s : mlist) {
                        testSet.add(s.substring(1));
                    }
                }
            }
        } else if (matchChar(st, '-', false) && matchChar(st, '-', true)) {
            skipSingleLineComment(st);
        }
    }

    for (String s : rets) {
        if (!testSet.contains(s)) {
            throw new IOException("Macro '" + name + "' missing return alias: " + s);
        }
    }
}

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

private StreamTokenizer openScript(File file) throws IOException {
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF8"));
    StreamTokenizer tokenizer = new StreamTokenizer(in);
    initTokenizer(tokenizer);/* w ww . j a v  a2s .c  o  m*/
    return tokenizer;
}

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

private StreamTokenizer openString(String code) {
    // StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(code.getBytes()))));
    StreamTokenizer tokenizer = new StreamTokenizer(new CharArrayReader(code.toCharArray()));
    initTokenizer(tokenizer);/*from   w ww  . j  a v  a2s . co m*/
    return tokenizer;
}

From source file:com.ecyrd.jspwiki.plugin.PluginManager.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./*from ww w . j ava  2 s. com*/
 *
 * @return A parsed list of parameters.
 *
 * @throws IOException If the parsing fails.
 */
public Map parseArgs(String argstring) throws IOException {
    HashMap<String, Object> arglist = new HashMap<String, Object>();

    //
    //  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:FourByFour.java

/**
 * Initialization//from   ww w.ja  v  a2s. c  o  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:uk.ac.leeds.ccg.andyt.projects.moses.process.RegressionReport_UK1.java

public void writeAggregateStatisticsForOptimisationConstraints_ISARHP_ISARCEP(String a_OutputDir_String)
        throws Exception {
    HashMap a_ID_RecordID_HashMap = _ISARDataHandler.get_ID_RecordID_HashMap();
    File optimisationConstraints_SARs = new File(a_OutputDir_String, "OptimisationConstraints_SARs.csv");
    FileOutputStream a_FileOutputStream = new FileOutputStream(optimisationConstraints_SARs);
    OutputDataHandler_OptimisationConstraints.writeHSARHP_ISARCEPHeader(a_FileOutputStream);
    a_FileOutputStream.flush();/*  ww w  .  j  a  va2 s. c o m*/
    Object[] fitnessCounts;
    HashMap<String, Integer> a_SARCounts = null;
    TreeSet<String> a_LADCodes_TreeSet = _CASDataHandler.getLADCodes_TreeSet();
    String s2;
    String s1;
    Iterator<String> a_Iterator_String = a_LADCodes_TreeSet.iterator();
    while (a_Iterator_String.hasNext()) {
        // Need to reorder data for each LAD as OAs not necessarily returned
        // in any order and an ordered result is wanted
        TreeMap<String, HashMap<String, Integer>> resultsForLAD = new TreeMap<String, HashMap<String, Integer>>();
        boolean setPrevious_OA_String = true;
        s1 = a_Iterator_String.next();
        s2 = s1.substring(0, 3);
        File resultsFile = new File(a_OutputDir_String + s2 + "/" + s1 + "/population.csv");
        // A few results are missing
        if (resultsFile.exists()) {
            System.out.println(resultsFile.toString() + " exists");
            String previous_OA_String = "";
            BufferedReader aBufferedReader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(resultsFile)));
            StreamTokenizer aStreamTokenizer = new StreamTokenizer(aBufferedReader);
            Generic_StaticIO.setStreamTokenizerSyntax1(aStreamTokenizer);
            String line = "";
            int tokenType = aStreamTokenizer.nextToken();
            while (tokenType != StreamTokenizer.TT_EOF) {
                switch (tokenType) {
                case StreamTokenizer.TT_EOL:
                    //System.out.println(line);
                    String[] lineFields = line.split(",");
                    String a_OA_String = lineFields[0];
                    if (previous_OA_String.equalsIgnoreCase(a_OA_String)) {
                        if (lineFields[1].equalsIgnoreCase("HP")) {
                            //System.out.println("HP");
                            long a_ISARRecordID = (Long) a_ID_RecordID_HashMap.get(new Long(lineFields[2]));
                            ISARDataRecord a_ISARDataRecord = _ISARDataHandler
                                    .getISARDataRecord(a_ISARRecordID);
                            GeneticAlgorithm_ISARHP_ISARCEP.addToCountsHP(a_ISARDataRecord, a_SARCounts,
                                    _Random);
                            //System.out.println(a_HSARDataRecord.toString());
                        } else {
                            //System.out.println("CEP");
                            // From the id of the ISARDataRecord get the
                            // ISARRecordID.
                            long a_ISARRecordID = (Long) a_ID_RecordID_HashMap.get(new Long(lineFields[2]));
                            ISARDataRecord a_ISARDataRecord = _ISARDataHandler
                                    .getISARDataRecord(a_ISARRecordID);
                            GeneticAlgorithm_ISARHP_ISARCEP.addToCountsCEP(a_ISARDataRecord, a_SARCounts,
                                    _Random);
                        }
                    } else {
                        // Store result
                        if (setPrevious_OA_String) {
                            previous_OA_String = a_OA_String;
                            setPrevious_OA_String = false;
                        } else {
                            // Store
                            resultsForLAD.put(previous_OA_String, a_SARCounts);
                        }
                        // Initialise/Re-initialise
                        CASDataRecord a_CASDataRecord = (CASDataRecord) _CASDataHandler
                                .getDataRecord(a_OA_String);
                        fitnessCounts = GeneticAlgorithm_ISARHP_ISARCEP.getFitnessCounts(a_CASDataRecord);
                        a_SARCounts = (HashMap<String, Integer>) fitnessCounts[1];
                        // Start a new aggregation
                        if (lineFields[1].equalsIgnoreCase("HP")) {
                            //System.out.println("HP");
                            long a_ISARRecordID = (Long) a_ID_RecordID_HashMap.get(new Long(lineFields[2]));
                            ISARDataRecord a_ISARDataRecord = _ISARDataHandler
                                    .getISARDataRecord(a_ISARRecordID);
                            GeneticAlgorithm_ISARHP_ISARCEP.addToCountsHP(a_ISARDataRecord, a_SARCounts,
                                    _Random);
                            //System.out.println(a_HSARDataRecord.toString());
                        } else {
                            //System.out.println("CEP");
                            // From the id of the ISARDataRecord get the
                            // ISARRecordID.
                            long a_ISARRecordID = (Long) a_ID_RecordID_HashMap.get(new Long(lineFields[2]));
                            ISARDataRecord a_ISARDataRecord = _ISARDataHandler
                                    .getISARDataRecord(a_ISARRecordID);
                            GeneticAlgorithm_ISARHP_ISARCEP.addToCountsCEP(a_ISARDataRecord, a_SARCounts,
                                    _Random);
                            //System.out.println(a_ISARDataRecord.toString());
                        }
                        //a_OA_String = lineFields[0];
                    }
                    previous_OA_String = a_OA_String;
                    break;
                case StreamTokenizer.TT_WORD:
                    line = aStreamTokenizer.sval;
                    break;
                }
                tokenType = aStreamTokenizer.nextToken();
            }
        } else {
            System.out.println(resultsFile.toString() + " !exists");
        }
        Iterator<String> string_Iterator = resultsForLAD.keySet().iterator();
        while (string_Iterator.hasNext()) {
            String oa_Code = string_Iterator.next();
            a_SARCounts = resultsForLAD.get(oa_Code);
            //GeneticAlgorithm_ISARHP_ISARCEP.addToCountsCEP(null, a_ID_RecordID_HashMap, _Random)
            OutputDataHandler_OptimisationConstraints.writeISARHP_ISARCEP(a_SARCounts, oa_Code,
                    a_FileOutputStream);
        }
    }
    a_FileOutputStream.close();
}

From source file:uk.ac.leeds.ccg.andyt.projects.moses.process.Comparison.java

/**
 * Aim is to produce an aggregated data set for comparison totalling males
 * and females by MSOA to compare with CASUV003DataRecord
 *///from w w  w .  j  a v a2s  .  c  o  m
private void run3() throws IOException {
    boolean aggregateToMSOA = true;
    // boolean aggregateToMSOA = false;
    ToyModelDataHandler tToyModelDataHandler = new ToyModelDataHandler();
    String startOfFilename = "C:/Work/Projects/MoSeS/Workspace/Leeds/ToyModel_SWR_OA_HSARHP_ISARCEP_0_5_5000_3_30_12_20";
    // String startOfFilename = new String(
    // "C:/Work/Projects/MoSeS/Workspace/Leeds/ToyModel_SWR_OA_HSARHP_ISARCEP_0_5_1000_3_30_12_20"
    // );
    // String startOfFilename = new String(
    // "C:/Work/Projects/MoSeS/Workspace/Leeds/ToyModel_SWR_OA_ISARHP_ISARCEP_0_5_200_3_30_12_20"
    // );
    File tToyModelDataRecord2CSVFile = new File(startOfFilename + ".csv");
    File tToyModelDataRecordMaleFemaleComparisonFile;
    if (aggregateToMSOA) {
        tToyModelDataRecordMaleFemaleComparisonFile = new File(
                startOfFilename + "_MSOAMaleFemaleComparison.csv");
    } else {
        tToyModelDataRecordMaleFemaleComparisonFile = new File(startOfFilename + "_OAMaleFemaleComparison.csv");
    }
    if (!tToyModelDataRecordMaleFemaleComparisonFile.exists()) {
        tToyModelDataRecordMaleFemaleComparisonFile.createNewFile();
    }
    PrintWriter tToyModelDataRecordMaleFemaleComparisonFilePrintWriter = new PrintWriter(
            tToyModelDataRecordMaleFemaleComparisonFile);
    // CASUV003DataHandler tCASUV003DataHandler = new CASUV003DataHandler(
    // new File(
    // "C:/Work/Projects/MoSeS/Workspace/Leeds/CASUV003DataRecordsMSOA.dat"
    // ) );
    CASUV003DataHandler tCASUV003DataHandler;
    CAS001DataHandler tCAS001DataHandler;
    if (aggregateToMSOA) {
        tCASUV003DataHandler = new CASUV003DataHandler(
                new File("C:/Work/Projects/MoSeS/Workspace/Leeds/CASUV003DataRecordsMSOA.dat"));
        tCAS001DataHandler = new CAS001DataHandler(
                new File("C:/Work/Projects/MoSeS/Workspace/Leeds/CAS001DataRecordsMSOA.dat"));
    } else {
        tCASUV003DataHandler = new CASUV003DataHandler(
                new File("C:/Work/Projects/MoSeS/Workspace/CASUV003DataRecords.dat"));
        tCAS001DataHandler = new CAS001DataHandler(
                new File("C:/Work/Projects/MoSeS/Workspace/CAS001DataRecords.dat"));
    }
    CASUV003DataRecord aCASUV003DataRecord;
    CAS001DataRecord aCAS001DataRecord;
    BufferedReader tBufferedReader = new BufferedReader(
            new InputStreamReader(new FileInputStream(tToyModelDataRecord2CSVFile)));
    StreamTokenizer tStreamTokenizer = new StreamTokenizer(tBufferedReader);
    Generic_StaticIO.setStreamTokenizerSyntax1(tStreamTokenizer);
    // Initialise
    int tMaleCount;
    int tFemaleCount;
    int tMaleCEPCount;
    int tMaleHPCount;
    int tFemaleCEPCount;
    int tFemaleHPCount;
    int tokenType = tStreamTokenizer.nextToken();
    ToyModelDataRecord_2 aToyModelDataRecord2;
    String aZoneCode;
    HashMap tLookUpMSOAfromOAHashMap = null;
    CASDataHandler tCASDataHandler = new CASDataHandler();
    if (aggregateToMSOA) {
        tLookUpMSOAfromOAHashMap = tCASDataHandler.get_LookUpMSOAfromOAHashMap();
    }
    Counts aCounts;
    tToyModelDataRecordMaleFemaleComparisonFilePrintWriter.println(
            "ZoneCode,CAS001HPFemales,CAS001CEPFemales,CAS001Females,CASUV003Females,ToyModelFemales,ToyModelHPFemales,ToyModelCEPFemales,CAS001HPMales,CAS001CEPMales,CAS001Males,CASUV003Males,ToyModelMales,ToyModelHPMales,ToyModelCEPMales");
    TreeMap result = new TreeMap();
    while (tokenType != StreamTokenizer.TT_EOF) {
        switch (tokenType) {
        case StreamTokenizer.TT_WORD:
            aToyModelDataRecord2 = new ToyModelDataRecord_2(tToyModelDataHandler, tStreamTokenizer.sval);
            if (aggregateToMSOA) {
                aZoneCode = (String) tLookUpMSOAfromOAHashMap
                        .get(new String(aToyModelDataRecord2.getZone_Code()));
            } else {
                aZoneCode = String.valueOf(aToyModelDataRecord2.getZone_Code());
            }
            if (aToyModelDataRecord2.SEX == 0) {
                tFemaleCount = 1;
                if (aToyModelDataRecord2.tHouseholdID != -9) {
                    tFemaleHPCount = 1;
                    tFemaleCEPCount = 0;
                } else {
                    tFemaleHPCount = 0;
                    tFemaleCEPCount = 1;
                }
                tMaleCount = 0;
                tMaleHPCount = 0;
                tMaleCEPCount = 0;
            } else {
                tMaleCount = 1;
                if (aToyModelDataRecord2.tHouseholdID != -9) {
                    tMaleHPCount = 1;
                    tMaleCEPCount = 0;
                } else {
                    tMaleHPCount = 0;
                    tMaleCEPCount = 1;
                }
                tFemaleCount = 0;
                tFemaleHPCount = 0;
                tFemaleCEPCount = 0;
            }
            if (result.containsKey(aZoneCode)) {
                aCounts = (Counts) result.get(aZoneCode);
                result.remove(aZoneCode);
                aCounts.addToCounts(tMaleCount, tMaleCEPCount, tMaleHPCount, tFemaleCount, tFemaleCEPCount,
                        tFemaleHPCount);
                result.put(aZoneCode, aCounts);
            } else {
                aCounts = new Counts();
                aCounts.addToCounts(tMaleCount, tMaleCEPCount, tMaleHPCount, tFemaleCount, tFemaleCEPCount,
                        tFemaleHPCount);
                result.put(aZoneCode, aCounts);
            }
        }
        tokenType = tStreamTokenizer.nextToken();
    }
    Iterator aIterator = result.keySet().iterator();
    Object key;
    while (aIterator.hasNext()) {
        key = aIterator.next();
        aCounts = (Counts) result.get(key);
        aZoneCode = (String) key;
        aCASUV003DataRecord = (CASUV003DataRecord) tCASUV003DataHandler.getDataRecord(aZoneCode);
        aCAS001DataRecord = (CAS001DataRecord) tCAS001DataHandler.getDataRecord(aZoneCode);
        tToyModelDataRecordMaleFemaleComparisonFilePrintWriter.println("" + aZoneCode + ", "
                + aCAS001DataRecord.getHouseholdResidentsFemales() + ", "
                + aCAS001DataRecord.getCommunalEstablishmentResidentsFemales() + ", "
                + (aCAS001DataRecord.getHouseholdResidentsFemales()
                        + aCAS001DataRecord.getCommunalEstablishmentResidentsFemales())
                + ", " + aCASUV003DataRecord.getFemales() + ", " + aCounts.tFemaleCount + ", "
                + aCounts.tFemaleHPCount + ", " + aCounts.tFemaleCEPCount + ", "
                + aCAS001DataRecord.getHouseholdResidentsMales() + ", "
                + aCAS001DataRecord.getCommunalEstablishmentResidentsMales() + ", "
                + (aCAS001DataRecord.getHouseholdResidentsMales()
                        + aCAS001DataRecord.getCommunalEstablishmentResidentsMales())
                + ", " + aCASUV003DataRecord.getMales() + ", " + aCounts.tMaleCount + ", "
                + aCounts.tMaleHPCount + ", " + aCounts.tMaleCEPCount);
    }
    tBufferedReader.close();
    tToyModelDataRecordMaleFemaleComparisonFilePrintWriter.close();
}