com.itemanalysis.jmetrik.file.JmetrikOutputWriter.java Source code

Java tutorial

Introduction

Here is the source code for com.itemanalysis.jmetrik.file.JmetrikOutputWriter.java

Source

/**
 * Copyright 2015 J. Patrick Meyer
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itemanalysis.jmetrik.file;

import com.itemanalysis.psychometrics.data.VariableAttributes;
import com.itemanalysis.psychometrics.data.VariableName;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * A class designed to create a delimited output file. It will create either a *.jmetrik formatted file
 * or a CSV file.
 *
 */
public class JmetrikOutputWriter {

    public JmetrikOutputWriter() {

    }

    public void saveFile(File outputFile, Outputter outputter, boolean asCsv) throws IOException {
        if (asCsv) {
            saveCsvFile(outputFile, outputter);
        } else {
            saveJmetrikFile(outputFile, outputter);
        }
    }

    public void saveJmetrikFile(File outputFile, Outputter outputter) throws IOException {

        ArrayList<VariableAttributes> variables = outputter.getColumnAttributes();
        LinkedHashMap<VariableName, VariableAttributes> variableAttributeMap = new LinkedHashMap<VariableName, VariableAttributes>();
        for (VariableAttributes v : variables) {
            variableAttributeMap.put(v.getName(), v);
        }

        //Ensure that file is a jMetrik file.
        String fname = FilenameUtils.removeExtension(outputFile.getAbsolutePath());
        outputFile = new File(fname + ".jmetrik");

        JmetrikFileWriter writer = new JmetrikFileWriter(outputFile, variableAttributeMap);

        try {
            writer.openConnection();
            writer.writeHeader(outputter.getNumberOfOutputRows());

            Iterator<Object[][]> iter = outputter.iterator();
            Object[][] outputChunk = null;

            while (iter.hasNext()) {
                outputChunk = iter.next();
                for (int i = 0; i < outputChunk.length; i++) {
                    for (int j = 0; j < outputChunk[0].length; j++) {
                        VariableAttributes v = variables.get(j);
                        if (outputChunk[i][j] != null) {
                            writer.writeValue(v.getName(), outputChunk[i][j].toString());
                        }

                    }
                    writer.updateRow();
                }

            }

        } catch (IOException ex) {
            throw ex;
        } finally {
            writer.close();
        }

    }

    private void saveCsvFile(File outputFile, Outputter outputter) throws IOException {

        ArrayList<VariableAttributes> variables = outputter.getColumnAttributes();
        LinkedHashMap<VariableName, VariableAttributes> variableAttributeMap = new LinkedHashMap<VariableName, VariableAttributes>();
        String[] header = new String[variables.size()];
        int hIndex = 0;
        for (VariableAttributes v : variables) {
            variableAttributeMap.put(v.getName(), v);
            header[hIndex] = v.getName().toString();
            hIndex++;
        }

        Writer writer = null;
        CSVPrinter printer = null;

        try {
            //Ensure that file is a csv file.
            String fname = FilenameUtils.removeExtension(outputFile.getAbsolutePath());
            outputFile = new File(fname + ".csv");

            writer = new OutputStreamWriter(new FileOutputStream(outputFile));
            printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#').withHeader(header));

            Iterator<Object[][]> iter = outputter.iterator();
            Object[][] outputChunk = null;

            while (iter.hasNext()) {
                outputChunk = iter.next();

                for (int i = 0; i < outputChunk.length; i++) {
                    printer.printRecord(outputChunk[i]);
                }

            }

        } catch (IOException ex) {
            throw ex;
        } finally {
            printer.close();
        }

    }

    public String printTextOutput(Outputter outputter) {

        //TODO create a generic method that will display a formatted chunk of output. Would be great to figure this out.

        return "";
    }

}