Example usage for org.apache.commons.csv CSVParser CSVParser

List of usage examples for org.apache.commons.csv CSVParser CSVParser

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser CSVParser.

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:com.marklogic.contentpump.CompressedDelimitedTextReader.java

private boolean nextKeyValueInZip() throws IOException, InterruptedException {
    ByteArrayOutputStream baos;/*w ww. j a v a  2  s.  c om*/
    ZipInputStream zis = (ZipInputStream) zipIn;
    while (true) {
        currZipEntry = zis.getNextEntry();
        if (currZipEntry == null) {
            break;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("ZipEntry: " + currZipEntry.getName());
        }
        if (currZipEntry.getSize() == 0) {
            continue;
        }
        subId = currZipEntry.getName();
        long size = currZipEntry.getSize();
        if (size == -1) {
            baos = new ByteArrayOutputStream();
        } else {
            baos = new ByteArrayOutputStream((int) size);
        }
        int nb;
        while ((nb = zis.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, nb);
        }
        if (encoding == null) {
            instream = new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()));
        } else {
            instream = new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()), encoding);
        }
        baos.close();
        parser = new CSVParser(instream, CSVParserFormatter.getFormat(delimiter, encapsulator, true, true));
        parserIterator = parser.iterator();
        // clear metadata
        fields = null;
        if (super.nextKeyValue()) {
            // current delim txt has next
            return true;
        }
        // continue read next zip entry if any
    }
    // end of zip
    if (iterator != null && iterator.hasNext()) {
        close();
        initStream(iterator.next());
        return nextKeyValueInZip();
    } else {
        hasNext = false;
        return false;
    }
}

From source file:edu.harvard.mcz.imagecapture.loader.JobVerbatimFieldLoad.java

@Override
public void start() {
    startDateTime = new Date();
    Singleton.getSingletonInstance().getJobList().addJob((RunnableJob) this);
    runStatus = RunStatus.STATUS_RUNNING;

    String selectedFilename = "";

    if (file == null) {
        final JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        if (Singleton.getSingletonInstance().getProperties().getProperties()
                .getProperty(ImageCaptureProperties.KEY_LASTLOADPATH) != null) {
            fileChooser.setCurrentDirectory(new File(Singleton.getSingletonInstance().getProperties()
                    .getProperties().getProperty(ImageCaptureProperties.KEY_LASTLOADPATH)));
        }/*from   www  .ja va 2s . c  o  m*/

        int returnValue = fileChooser.showOpenDialog(Singleton.getSingletonInstance().getMainFrame());
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            file = fileChooser.getSelectedFile();
        }
    }

    if (file != null) {
        log.debug("Selected file to load: " + file.getName() + ".");

        if (file.exists() && file.isFile() && file.canRead()) {
            // Save location
            Singleton.getSingletonInstance().getProperties().getProperties()
                    .setProperty(ImageCaptureProperties.KEY_LASTLOADPATH, file.getPath());
            selectedFilename = file.getName();

            String[] headers = new String[] {};

            CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers);
            int rows = 0;
            try {
                rows = readRows(file, csvFormat);
            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                        "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found",
                        JOptionPane.OK_OPTION);
                errors.append("File not found ").append(e.getMessage()).append("\n");
                log.error(e.getMessage(), e);
            } catch (IOException e) {
                errors.append("Error loading csv format, trying tab delimited: ").append(e.getMessage())
                        .append("\n");
                log.debug(e.getMessage());
                try {
                    // try reading as tab delimited format, if successful, use that format.
                    CSVFormat tabFormat = CSVFormat.newFormat('\t').withIgnoreSurroundingSpaces(true)
                            .withHeader(headers).withQuote('"');
                    rows = readRows(file, tabFormat);
                    csvFormat = tabFormat;
                } catch (IOException e1) {
                    errors.append("Error Loading data: ").append(e1.getMessage()).append("\n");
                    log.error(e.getMessage(), e1);
                }
            }

            try {
                Reader reader = new FileReader(file);

                CSVParser csvParser = new CSVParser(reader, csvFormat);

                Map<String, Integer> csvHeader = csvParser.getHeaderMap();
                headers = new String[csvHeader.size()];
                int i = 0;
                for (String header : csvHeader.keySet()) {
                    headers[i++] = header;
                    log.debug(header);
                }

                boolean okToRun = true;
                //TODO: Work picking/checking responsibility into a FieldLoaderWizard
                List<String> headerList = Arrays.asList(headers);
                if (!headerList.contains("barcode")) {
                    log.error("Input file " + file.getName()
                            + " header does not contain required field 'barcode'.");
                    // no barcode field, we can't match the input to specimen records.
                    errors.append("Field \"barcode\" not found in csv file headers.  Unable to load data.")
                            .append("\n");
                    okToRun = false;
                }

                if (okToRun) {

                    Iterator<CSVRecord> iterator = csvParser.iterator();

                    FieldLoader fl = new FieldLoader();

                    if (headerList.size() == 3 && headerList.contains("verbatimUnclassifiedText")
                            && headerList.contains("questions") && headerList.contains("barcode")) {
                        log.debug("Input file matches case 1: Unclassified text only.");
                        // Allowed case 1a: unclassified text only

                        int confirm = JOptionPane.showConfirmDialog(
                                Singleton.getSingletonInstance().getMainFrame(),
                                "Confirm load from file " + selectedFilename + " (" + rows
                                        + " rows) with just barcode and verbatimUnclassifiedText",
                                "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION);
                        if (confirm == JOptionPane.OK_OPTION) {
                            String barcode = "";
                            int lineNumber = 0;
                            while (iterator.hasNext()) {
                                lineNumber++;
                                counter.incrementSpecimens();
                                CSVRecord record = iterator.next();
                                try {
                                    String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText");
                                    barcode = record.get("barcode");
                                    String questions = record.get("questions");

                                    fl.load(barcode, verbatimUnclassifiedText, questions, true);
                                    counter.incrementSpecimensUpdated();
                                } catch (IllegalArgumentException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                } catch (LoadException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                }
                                percentComplete = (int) ((lineNumber * 100f) / rows);
                                this.setPercentComplete(percentComplete);
                            }
                        } else {
                            errors.append("Load canceled by user.").append("\n");
                        }
                    } else if (headerList.size() == 4 && headerList.contains("verbatimUnclassifiedText")
                            && headerList.contains("questions") && headerList.contains("barcode")
                            && headerList.contains("verbatimClusterIdentifier")) {
                        log.debug(
                                "Input file matches case 1: Unclassified text only (with cluster identifier).");
                        // Allowed case 1b: unclassified text only (including cluster identifier)

                        int confirm = JOptionPane.showConfirmDialog(
                                Singleton.getSingletonInstance().getMainFrame(),
                                "Confirm load from file " + selectedFilename + " (" + rows
                                        + " rows) with just barcode and verbatimUnclassifiedText",
                                "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION);
                        if (confirm == JOptionPane.OK_OPTION) {
                            String barcode = "";
                            int lineNumber = 0;
                            while (iterator.hasNext()) {
                                lineNumber++;
                                counter.incrementSpecimens();
                                CSVRecord record = iterator.next();
                                try {
                                    String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText");
                                    String verbatimClusterIdentifier = record.get("verbatimClusterIdentifier");
                                    barcode = record.get("barcode");
                                    String questions = record.get("questions");

                                    fl.load(barcode, verbatimUnclassifiedText, verbatimClusterIdentifier,
                                            questions, true);
                                    counter.incrementSpecimensUpdated();
                                } catch (IllegalArgumentException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                } catch (LoadException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                }
                                percentComplete = (int) ((lineNumber * 100f) / rows);
                                this.setPercentComplete(percentComplete);
                            }
                        } else {
                            errors.append("Load canceled by user.").append("\n");
                        }

                    } else if (headerList.size() == 8 && headerList.contains("verbatimUnclassifiedText")
                            && headerList.contains("questions") && headerList.contains("barcode")
                            && headerList.contains("verbatimLocality") && headerList.contains("verbatimDate")
                            && headerList.contains("verbatimNumbers")
                            && headerList.contains("verbatimCollector")
                            && headerList.contains("verbatimCollection")) {
                        // Allowed case two, transcription into verbatim fields, must be exact list of all
                        // verbatim fields, not including cluster identifier or other metadata.
                        log.debug("Input file matches case 2: Full list of verbatim fields.");

                        int confirm = JOptionPane.showConfirmDialog(
                                Singleton.getSingletonInstance().getMainFrame(),
                                "Confirm load from file " + selectedFilename + " (" + rows
                                        + " rows) with just barcode and verbatim fields.",
                                "Verbatim Fields found for load", JOptionPane.OK_CANCEL_OPTION);
                        if (confirm == JOptionPane.OK_OPTION) {

                            String barcode = "";
                            int lineNumber = 0;
                            while (iterator.hasNext()) {
                                lineNumber++;
                                counter.incrementSpecimens();
                                CSVRecord record = iterator.next();
                                try {
                                    String verbatimLocality = record.get("verbatimLocality");
                                    String verbatimDate = record.get("verbatimDate");
                                    String verbatimCollector = record.get("verbatimCollector");
                                    String verbatimCollection = record.get("verbatimCollection");
                                    String verbatimNumbers = record.get("verbatimNumbers");
                                    String verbatimUnclasifiedText = record.get("verbatimUnclassifiedText");
                                    barcode = record.get("barcode");
                                    String questions = record.get("questions");

                                    fl.load(barcode, verbatimLocality, verbatimDate, verbatimCollector,
                                            verbatimCollection, verbatimNumbers, verbatimUnclasifiedText,
                                            questions);
                                    counter.incrementSpecimensUpdated();
                                } catch (IllegalArgumentException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                } catch (LoadException e) {
                                    RunnableJobError error = new RunnableJobError(file.getName(), barcode,
                                            Integer.toString(lineNumber), e.getClass().getSimpleName(), e,
                                            RunnableJobError.TYPE_LOAD_FAILED);
                                    counter.appendError(error);
                                    log.error(e.getMessage(), e);
                                }
                                percentComplete = (int) ((lineNumber * 100f) / rows);
                                this.setPercentComplete(percentComplete);
                            }
                        } else {
                            errors.append("Load canceled by user.").append("\n");
                        }

                    } else {
                        // allowed case three, transcription into arbitrary sets verbatim or other fields
                        log.debug("Input file case 3: Arbitrary set of fields.");

                        // Check column headers before starting run.
                        boolean headersOK = false;

                        try {
                            HeaderCheckResult headerCheck = fl.checkHeaderList(headerList);
                            if (headerCheck.isResult()) {
                                int confirm = JOptionPane.showConfirmDialog(
                                        Singleton.getSingletonInstance().getMainFrame(),
                                        "Confirm load from file " + selectedFilename + " (" + rows
                                                + " rows) with headers: \n"
                                                + headerCheck.getMessage().replaceAll(":", ":\n"),
                                        "Fields found for load", JOptionPane.OK_CANCEL_OPTION);
                                if (confirm == JOptionPane.OK_OPTION) {
                                    headersOK = true;
                                } else {
                                    errors.append("Load canceled by user.").append("\n");
                                }
                            } else {
                                int confirm = JOptionPane.showConfirmDialog(
                                        Singleton.getSingletonInstance().getMainFrame(),
                                        "Problem found with headers in file, try to load anyway?\nHeaders: \n"
                                                + headerCheck.getMessage().replaceAll(":", ":\n"),
                                        "Problem in fields for load", JOptionPane.OK_CANCEL_OPTION);
                                if (confirm == JOptionPane.OK_OPTION) {
                                    headersOK = true;
                                } else {
                                    errors.append("Load canceled by user.").append("\n");
                                }
                            }
                        } catch (LoadException e) {
                            errors.append("Error loading data: \n").append(e.getMessage()).append("\n");
                            JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                                    e.getMessage().replaceAll(":", ":\n"), "Error Loading Data: Problem Fields",
                                    JOptionPane.ERROR_MESSAGE);

                            log.error(e.getMessage(), e);
                        }

                        if (headersOK) {
                            int lineNumber = 0;
                            while (iterator.hasNext()) {
                                lineNumber++;
                                Map<String, String> data = new HashMap<String, String>();
                                CSVRecord record = iterator.next();
                                String barcode = record.get("barcode");
                                Iterator<String> hi = headerList.iterator();
                                boolean containsNonVerbatim = false;
                                while (hi.hasNext()) {
                                    String header = hi.next();
                                    // Skip any fields prefixed by the underscore character _
                                    if (!header.equals("barcode") && !header.startsWith("_")) {
                                        data.put(header, record.get(header));
                                        if (!header.equals("questions")
                                                && MetadataRetriever.isFieldExternallyUpdatable(Specimen.class,
                                                        header)
                                                && MetadataRetriever.isFieldVerbatim(Specimen.class, header)) {
                                            containsNonVerbatim = true;
                                        }
                                    }
                                }
                                if (data.size() > 0) {
                                    try {
                                        boolean updated = false;
                                        if (containsNonVerbatim) {
                                            updated = fl.loadFromMap(barcode, data,
                                                    WorkFlowStatus.STAGE_CLASSIFIED, true);
                                        } else {
                                            updated = fl.loadFromMap(barcode, data,
                                                    WorkFlowStatus.STAGE_VERBATIM, true);
                                        }
                                        counter.incrementSpecimens();
                                        if (updated) {
                                            counter.incrementSpecimensUpdated();
                                        }
                                    } catch (HibernateException e1) {
                                        // Catch (should just be development) problems with the underlying query 
                                        StringBuilder message = new StringBuilder();
                                        message.append("Query Error loading row (").append(lineNumber)
                                                .append(")[").append(barcode).append("]")
                                                .append(e1.getMessage());
                                        RunnableJobError err = new RunnableJobError(selectedFilename, barcode,
                                                Integer.toString(lineNumber), e1.getMessage(), e1,
                                                RunnableJobError.TYPE_LOAD_FAILED);
                                        counter.appendError(err);
                                        log.error(e1.getMessage(), e1);

                                    } catch (LoadException e) {
                                        StringBuilder message = new StringBuilder();
                                        message.append("Error loading row (").append(lineNumber).append(")[")
                                                .append(barcode).append("]").append(e.getMessage());

                                        RunnableJobError err = new RunnableJobError(selectedFilename, barcode,
                                                Integer.toString(lineNumber), e.getMessage(), e,
                                                RunnableJobError.TYPE_LOAD_FAILED);

                                        counter.appendError(err);
                                        // errors.append(message.append("\n").toString());
                                        log.error(e.getMessage(), e);
                                    }
                                }
                                percentComplete = (int) ((lineNumber * 100f) / rows);
                                this.setPercentComplete(percentComplete);
                            }
                        } else {
                            String message = "Can't load data, problem with headers.";
                            errors.append(message).append("\n");
                            log.error(message);
                        }
                    }
                }
                csvParser.close();
                reader.close();
            } catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(),
                        "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found",
                        JOptionPane.OK_OPTION);
                errors.append("File not found ").append(e.getMessage()).append("\n");
                log.error(e.getMessage(), e);
            } catch (IOException e) {
                errors.append("Error Loading data: ").append(e.getMessage()).append("\n");
                log.error(e.getMessage(), e);
            }
        }

    } else {
        //TODO: handle error condition
        log.error("File selection cancelled by user.");
    }

    report(selectedFilename);
    done();
}

From source file:javalibs.CSVDataNormalizer.java

private void readCSV() {
    try {/*  w  ww . ja va2 s . co  m*/
        CSVParser parser = new CSVParser(Files.newBufferedReader(Paths.get(this.csvPath)),
                CSVFormat.DEFAULT.withHeader().withIgnoreHeaderCase().withTrim());

        // Get all headers in the CSV file so they can be used later when writing the file
        this.headerMap = parser.getHeaderMap();

        // Add them to the records list for later use
        this.allRecords = parser.getRecords();

        parser.close();

        reverseHeaderMap();
    } catch (IOException e) {
        log_.die(e);
    }
}

From source file:com.edu.duke.FileResource.java

/**
 * Returns a <code>CSVParser</code> object to access the contents of an open file, possibly
 * without a header row and a different data delimiter than a comma.
 * //w  w  w  .  j  a va 2s. co m
 * Each line of the file should be formatted as data separated by the delimiter passed as a
 * parameter and with/without a header row to describe the column names. This is useful if the
 * data is separated by some character other than a comma.
 * 
 * @param withHeader uses first row of data as a header row only if true
 * @param delimiter a single character that separates one field of data from another
 * @return a <code>CSVParser</code> that can provide access to the records in the file one at a
 *         time
 */
public CSVParser getCSVParser(boolean withHeader, String delimiter) {
    if (delimiter == null || delimiter.length() != 1) {
        throw new ResourceException("FileResource: CSV delimiter must be a single character: " + delimiter);
    }
    try {
        char delim = delimiter.charAt(0);
        Reader input = new StringReader(mySource);
        if (withHeader) {
            return new CSVParser(input, CSVFormat.EXCEL.withHeader().withDelimiter(delim));
        } else {
            return new CSVParser(input, CSVFormat.EXCEL.withDelimiter(delim));
        }
    } catch (Exception e) {
        throw new ResourceException("FileResource: cannot read " + myPath + " as a CSV file.");
    }
}

From source file:ca.uhn.fhir.jpa.term.TerminologyLoaderSvc.java

private void iterateOverZipFile(List<byte[]> theZipBytes, String fileNamePart, IRecordHandler handler,
        char theDelimiter, QuoteMode theQuoteMode) {
    boolean found = false;

    for (byte[] nextZipBytes : theZipBytes) {
        ZipInputStream zis = new ZipInputStream(
                new BufferedInputStream(new ByteArrayInputStream(nextZipBytes)));
        try {//w w w . j a  v  a  2  s . co m
            for (ZipEntry nextEntry; (nextEntry = zis.getNextEntry()) != null;) {
                ZippedFileInputStream inputStream = new ZippedFileInputStream(zis);

                String nextFilename = nextEntry.getName();
                if (nextFilename.contains(fileNamePart)) {
                    ourLog.info("Processing file {}", nextFilename);
                    found = true;

                    Reader reader = null;
                    CSVParser parsed = null;
                    try {
                        reader = new InputStreamReader(zis, Charsets.UTF_8);
                        CSVFormat format = CSVFormat.newFormat(theDelimiter).withFirstRecordAsHeader();
                        if (theQuoteMode != null) {
                            format = format.withQuote('"').withQuoteMode(theQuoteMode);
                        }
                        parsed = new CSVParser(reader, format);
                        Iterator<CSVRecord> iter = parsed.iterator();
                        ourLog.debug("Header map: {}", parsed.getHeaderMap());

                        int count = 0;
                        int logIncrement = LOG_INCREMENT;
                        int nextLoggedCount = 0;
                        while (iter.hasNext()) {
                            CSVRecord nextRecord = iter.next();
                            handler.accept(nextRecord);
                            count++;
                            if (count >= nextLoggedCount) {
                                ourLog.info(" * Processed {} records in {}", count, nextFilename);
                                nextLoggedCount += logIncrement;
                            }
                        }

                    } catch (IOException e) {
                        throw new InternalErrorException(e);
                    }
                }
            }
        } catch (IOException e) {
            throw new InternalErrorException(e);
        } finally {
            IOUtils.closeQuietly(zis);
        }
    }

    // This should always be true, but just in case we've introduced a bug...
    Validate.isTrue(found);
}

From source file:com.ibm.watson.app.qaclassifier.tools.PopulateAnswerStore.java

/**
 * Reads in the answer input file and creates a POJO for each answer it finds.  If the answer has no value
 * it is skipped./*  www . j  a v  a2 s. c o m*/
 * 
 * @return AnswerStore - full POJO of the answer store read from the file
 */
private static List<ManagedAnswer> readAnswerInput(String content) {
    List<ManagedAnswer> store = null;

    // read the CVS of label to canonical question first
    try (StringReader reader = new StringReader(content);
            CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL);) {
        // read in the csv file and get the records
        List<CSVRecord> records = parser.getRecords();

        // now we can create the answer store because we have read the records
        store = new ArrayList<ManagedAnswer>();
        for (CSVRecord r : records) {
            // order is: LabelId, CanonicalQuestion

            // create the answer pojo
            ManagedAnswer answer = new ManagedAnswer();
            answer.setClassName(r.get(0));
            answer.setCanonicalQuestion(r.get(1));
            answer.setType(TypeEnum.TEXT);

            // add to the managed answers list
            store.add(answer);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return store;
}

From source file:br.ufg.calendario.components.EventoBean.java

public void uploadEvento(FileUploadEvent event) {
    Map<String, Object> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    FacesMessage msg;// w ww . j a v  a  2s .  c o  m
    boolean saveStatus = false;
    UploadedFile arquivo = event.getFile();
    try {
        InputStream arquivoReader = arquivo.getInputstream();
        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();
        Reader reader = new InputStreamReader(arquivoReader, decoder);
        CSVParser parser = new CSVParser(reader,
                CSVFormat.DEFAULT.withHeader().withDelimiter(configBean.getDelimiter()));
        SimpleDateFormat dateFormatter = new SimpleDateFormat(configBean.getDateFormat());
        for (Entry<String, Integer> entry : parser.getHeaderMap().entrySet()) {
            System.out.format("header: %s - %d\n", entry.getKey(), entry.getValue());
        }
        Integer ano;
        Calendario cal = null;
        List<Regional> regionais = regionalDao.listar();
        List<Interessado> interessados = interessadoDao.listar();
        for (CSVRecord record : parser) {
            //adicionar entidade calendario (select box) na tela importar eventos.
            ano = Integer.parseInt(record.get(0));
            Date dataInicio = dateFormatter.parse(record.get(1));
            Date dataTermino = dateFormatter.parse(record.get(2));
            String assunto = record.get(3);
            String descricao = record.get(4);
            String[] interessadoArray = record.get(5).split(configBean.getRegexSplitter());
            String[] regionalArray = record.get(6).split(configBean.getRegexSplitter());
            boolean aprovado = record.get(7) != null && record.get(7).trim().toUpperCase().equals("S");
            if (cal == null) {
                //buscar apenas uma vez
                cal = calendarioDao.buscar(ano);
            }
            Set<Interessado> interessadoSet = new HashSet();
            for (String interessado : interessadoArray) {
                if (!interessado.isEmpty()) {
                    for (Interessado i : interessados) {
                        if (i.getNome().equals(interessado.trim())) {
                            interessadoSet.add(i);
                        }
                    }
                }
            }
            Set<Regional> regionalSet = new HashSet();
            for (String regional : regionalArray) {
                if (!regional.isEmpty()) {
                    for (Regional r : regionais) {
                        if (r.getNome().equals(regional.trim())) {
                            regionalSet.add(r);
                        }
                    }
                }
            }
            Evento evt = new Evento(assunto, dataInicio, dataTermino, descricao, cal, regionalSet,
                    interessadoSet, aprovado);
            eventosImportados.add(evt);
        }
    } catch (IOException | ParseException | ArrayIndexOutOfBoundsException | NullPointerException e) {
        System.out.println("erro: " + e.getMessage());
    }
    System.out.println("arquivo enviado: " + arquivo.getFileName());
    msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "info", LocaleBean.getMessage("arquivoEnviado"));
    FacesContext.getCurrentInstance().addMessage(null, msg);
    RequestContext.getCurrentInstance().addCallbackParam("resultado", saveStatus);
}

From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java

private void convertFile() {
    CSVParser parser = null;/*from w  w  w.  j a v a  2s .  c o m*/
    Reader reader = null;
    CSVPrinter printer = null;
    Writer writer = null;

    try {
        if (outputFile.exists()) {
            if (!overwrite) {
                theException = new IOException("File already exists and overwrite==false");
                return;
            }
        } else {
            outputFile.createNewFile();
        }

        //For debugging
        //            System.out.println("CREATED: " + outputFile.getAbsolutePath());

        //Writer header to file
        writer = new OutputStreamWriter(new FileOutputStream(outputFile));
        printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#'));

        printer.printComment("VERSION");
        printer.printRecord(new String[] { "jmetrik1" });
        printer.printComment("METADATA");
        printer.printRecord(new String[] { Integer.valueOf(nrow).toString() });
        printer.printComment("ATTRIBUTES");
        for (VariableName v : variableAttributeMap.keySet()) {
            printer.printRecord(variableAttributeMap.get(v).getAttributeArray());
        }
        printer.printComment("DATA");

        //Write data to file
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8");
        parser = new CSVParser(reader, dataFileFormat);

        if (hasHeader) {
            parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true));
        } else {
            parser = new CSVParser(reader, dataFileFormat.withHeader(colNames));
        }

        Iterator<CSVRecord> iter = parser.iterator();
        CSVRecord csvRecord = null;
        VariableAttributes variableAttributes = null;
        DataType dataType = null;
        String temp = "";

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

            for (VariableName v : variableAttributeMap.keySet()) {
                temp = csvRecord.get(v.toString());
                variableAttributes = variableAttributeMap.get(v);
                dataType = variableAttributes.getDataType();
                if (!variableAttributes.isMissing(temp)) {
                    if (DataType.INTEGER == dataType) {
                        printer.print(Double.valueOf(Double.parseDouble(temp)).intValue());
                    } else if (DataType.DOUBLE == dataType) {
                        printer.print(Double.parseDouble(temp));
                    } else {
                        printer.print(temp);
                    }
                } else {
                    printer.print(temp);
                }

            }
            printer.println();
        }

    } catch (IOException ex) {
        theException = ex;
    } finally {
        try {
            if (parser != null)
                parser.close();
            if (reader != null)
                reader.close();
            if (printer != null)
                printer.close();
            if (writer != null)
                writer.close();
        } catch (IOException ex) {
            theException = ex;
            logger.fatal(ex);
        }
    }
}

From source file:com.kdmanalytics.toif.ui.common.AdaptorConfiguration.java

/**
 * Load configuration data from the specified stream.
 * //w  ww.ja  va  2  s  . c o  m
 * @param is
 * @throws IOException
 */
private synchronized void load(InputStream is) throws IOException {
    if (!isEmpty()) {
        // If there is already data loaded, we want to merge the new data
        merge(is);
    } else {
        InputStreamReader in = null;
        CSVParser parser = null;
        try {
            in = new InputStreamReader(is);
            CSVFormat format = CSVFormat.EXCEL.withDelimiter(',').withIgnoreEmptyLines();

            parser = new CSVParser(in, format);

            // Set to false once the header is read
            boolean header = true;
            // Number of rows we have loaded so far
            int rcount = data.size();
            // Import all new rows
            for (CSVRecord record : parser) {
                if (header) {
                    parseHeader(record);
                    header = false;
                } else {
                    rcount = parseData(record, rcount);
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (parser != null) {
                parser.close();
            }
        }
    }
}

From source file:com.kdmanalytics.toif.report.internal.importWizard.TsvImportWizardPage.java

/**
 * Perform the actual load./*from   w  w w .j  a v  a 2 s  . c  o m*/
 * 
 * @return
 */
public boolean finish() {
    // Check source file
    final String name = editor.getStringValue();
    setErrorMessage("Importing " + name + " into " + project + "...");
    IPath location = new Path(name);
    File file = location.toFile();

    Reader in = null;
    CSVParser parser = null;
    try {
        in = new FileReader(file);
        CSVFormat format = CSVFormat.EXCEL.withDelimiter('\t').withIgnoreEmptyLines();

        parser = new CSVParser(in, format);

        System.err.println("FILE: " + name);

        Map<Integer, String> lookup = new HashMap<Integer, String>();
        boolean header = true;

        for (CSVRecord record : parser) {
            int size = record.size();

            IFile ifile = null;
            String tool = null;
            String description = null;
            int line = 0;
            int offset = 0;
            int trust = 0;
            Boolean status = null;
            int kdmLine = 0;
            String cwe = null;
            String sfp = null;

            // Read the header first
            if (header) {
                System.err.print("  ");
                for (int i = 0; i < size; i++) {
                    if (i > 0)
                        System.err.print(",");
                    String cell = record.get(i);
                    lookup.put(i, cell);
                    System.err.print(cell);
                }
                header = false;
                System.err.println();
                System.err.println("  ------------------------------------------");
            }

            // Otherwise this is a data row
            else {
                for (int i = 0; i < size; i++) {
                    String cell = record.get(i);
                    String colName = lookup.get(i);
                    if ("Resource".equals(colName)) {
                        IFileGroup group = new FileGroup(cell);
                        try {
                            IResource resource = MemberUtil.findMembers(project, group);
                            if (resource != null) {
                                ifile = (IFile) resource;
                            }
                        } catch (CoreException e) {
                            e.printStackTrace();
                        }
                    } else if ("SFP".equals(colName)) {
                        sfp = cell;
                    } else if ("CWE".equals(colName)) {
                        cwe = cell;
                    }
                    // Valid is *old* name for "Citing Status"
                    else if ("Valid".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            status = Boolean.parseBoolean(cell);
                        }
                    } else if ("Citing Status".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            status = Boolean.parseBoolean(cell);
                        }
                    } else if ("Trust".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            try {
                                trust = Integer.parseInt(cell);
                            } catch (NumberFormatException e) {
                            }
                        }
                    } else if ("Confidence".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            try {
                                trust = Integer.parseInt(cell);
                            } catch (NumberFormatException e) {
                            }
                        }
                    } else if ("Line Number".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            try {
                                line = Integer.parseInt(cell);
                            } catch (NumberFormatException e) {
                            }
                        }
                    } else if ("KDM Line Number".equals(colName)) {
                        if (cell != null && !cell.trim().isEmpty()) {
                            try {
                                kdmLine = Integer.parseInt(cell);
                            } catch (NumberFormatException e) {
                            }
                        }
                    }
                    // "Generator Tool" is *old* name for "SCA Tool"
                    else if ("Generator Tool".equals(colName)) {
                        tool = cell;
                    } else if ("SCA tool".equalsIgnoreCase(colName)) {
                        tool = cell;
                    } else if ("Weakness Description".equals(colName)) {
                        description = cell;
                    } else {
                        System.err.println("WARNING: Unknown column name '" + colName + "'");
                    }
                }

                System.err.print("  ");
                System.err.print(sfp);
                System.err.print(",");
                System.err.print(cwe);
                System.err.print(",");
                System.err.print(status);
                System.err.print(",");
                System.err.print(trust);
                System.err.print(",");
                System.err.print(ifile);
                System.err.print(",");
                System.err.print(line);
                System.err.print(",");
                System.err.print(kdmLine);
                System.err.print(",");
                System.err.print(tool);
                System.err.print(",");
                System.err.print(description);
                System.err.println();

                if (ifile != null) {
                    // Create an associated finding. This will allow us to
                    // set the citing status for the finding. If the
                    // finding does not actually exist in the database this information
                    // is still stored in case the finding exists in the future.
                    FindingData finding = new FindingData(ifile, tool, description, line, offset, cwe, sfp);
                    if (status != null) {
                        finding.cite(status);
                    }
                }
            }
        }

        try {
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            if (window != null) {
                IWorkbenchPage page = window.getActivePage();
                if (page != null) {
                    FindingView view = (FindingView) page.showView("com.kdmanalytics.toif.views.FindingView");
                    view.refresh();
                }

            }
        } catch (PartInitException e) {
            e.printStackTrace();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (parser != null) {
            try {
                parser.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable()
    // {
    // public void run()
    // {
    // final ToifReportImportJob job = new ToifReportImportJob("Import SFP/CWE Data", project,
    // name);
    // job.setUser(true);
    // job.setPriority(Job.BUILD);
    // job.setRule(project);
    // job.schedule();
    // }
    // });

    return true;
}