Example usage for org.apache.commons.csv CSVRecord size

List of usage examples for org.apache.commons.csv CSVRecord size

Introduction

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

Prototype

public int size() 

Source Link

Document

Returns the number of values in this record.

Usage

From source file:com.github.r351574nc3.amex.assignment1.csv.DefaultInterpreter.java

protected EmailNotificationTestData toTestData(final CSVRecord record) {
    final EmailNotificationTestData retval = new EmailNotificationTestData();

    retval.setName(record.get(0));//w  w  w .java  2s .co  m
    retval.setMemberSince(Integer.parseInt(record.get(1)));
    retval.setAnnualSpend(new BigDecimal(record.get(2)));
    retval.setEmailAddress(record.get(3));
    retval.setSubject(record.get(4));

    if (record.size() > 6) {
        for (int idx = 6; idx < record.size(); idx++) {
            retval.getAdditionalFields().add(record.get(idx));
        }
    }

    retval.setContent(replaceTokens(record.get(5), retval.getAdditionalFields()));

    return retval;
}

From source file:com.denimgroup.threadfix.service.waflog.RiverbedWebAppFirewallLogParser.java

/**
 * @param entryBuffer//  w w w .j  a v a  2  s .c  o m
 * @return
 */
@Override
public SecurityEvent getSecurityEvent(String entry) {
    if (entry == null || entry.isEmpty() || entry.startsWith("#")) {
        return null;
    }

    // a logline is a csv encoded line with the following columns
    //  * [0] a timestamp: YYYYMMDD-HHMMSS in local time
    //  * [1] an internal session id or "default"
    //  * [2] internal cluster node id
    //  * [3] host header
    //  * [4] client ip
    //  * [5] HTTP method
    //  * [6] URL
    //  * [7] HTTP protocol version
    //  * [8] internal ruleset / rule id
    //  * [9] action
    //  * [10] protection or detection mode
    //  * [11] request or response
    //  * [12] handlerName - we only care for the THREADFIX_HANDLER_NAME here
    //  * [13] component which reject the request
    //  * [14] value which rejects the request
    //  * [16] error id (use this together with the timetamp to be unique)
    //  * [17] free text field
    //  * ... aditional stuff

    try {
        // we are using an iterator here because this
        // is the interface of this CSV parser 
        // however, we always feed only one line into
        // this parser so it is ok to return from this
        // loop and never continue
        Iterable<CSVRecord> parser = CSVFormat.DEFAULT.parse(new StringReader(entry));
        for (CSVRecord record : parser) {

            // We access elements 0 .. 17 later, so this has to have at least 18 elements
            if (record.size() < 18) {
                log.error("can't parse logline: " + entry);
                return null;
            }
            String csvTimestamp = record.get(0); // 20140131-172342
            String csvClientIP = record.get(4); // 10.17.23.41
            String csvRulesetMode = record.get(10); // P or D
            String csvHandlerName = record.get(12); // ThreadfixHandler
            String csvComponentName = record.get(13); // protection_ruleset
            String csvComponentValue = record.get(14); // threadfix:100042 or 100042
            String csvErrorId = record.get(16); // 1234567
            String csvFreeText = record.get(17); // free text which describe the action

            if (csvTimestamp == null || csvClientIP == null || csvHandlerName == null || csvRulesetMode == null
                    || csvComponentName == null || csvComponentValue == null || csvErrorId == null
                    || csvFreeText == null) {

                log.error("can't parse logline: " + entry);
                return null;
            }

            // we only care for THREADFIX_HANDLER_NAME here ... ignore all other stuff
            if (!csvHandlerName.equals(THREADFIX_HANDLER_NAME)) {
                log.debug("ignore unknown handler: " + csvHandlerName);
                return null;
            }

            // while the error id act more or less as
            // a unique id for rejected requests, this id
            // is too short to be really unique over a
            // long time. So we combine it here with the
            // timestamp to get a better native id
            String nativeId = csvTimestamp + "-" + csvErrorId;

            log.debug("native id: " + nativeId);

            if (securityEventDao.retrieveByNativeIdAndWafId(nativeId, wafId) != null) {
                return null;
            }

            String wafRuleId = null;
            if (csvComponentName.equals(THREADFIX_HANDLER_COMPONENT)) {
                // allow threadfix:123456 and 123456
                if (csvComponentValue.contains(":")) {
                    wafRuleId = csvComponentValue.split(":", 2)[1];
                } else {
                    wafRuleId = csvComponentValue;
                }
            } else {
                log.debug("ignore unknown component: " + csvComponentName);
                return null;
            }

            log.debug("wafRuleId " + wafRuleId);

            WafRule rule = wafRuleDao.retrieveByWafAndNativeId(wafId, wafRuleId);
            if (rule == null) {
                log.debug("wafRule not found");
                return null;
            }

            Calendar calendar = parseDate(csvTimestamp);

            if (calendar == null) {
                log.error("can't parse logline (timestamp): " + entry);
                return null;
            }

            SecurityEvent event = new SecurityEvent();

            event.setWafRule(rule);
            event.setImportTime(calendar);
            event.setLogText(csvFreeText);

            event.setAttackType("deny");
            //if (csvRulesetMode == WAF_LOG_MODE_PROTECTION)
            //{
            //    event.setAttackType("deny");
            //} else {
            //    event.setAttackType("log"); 
            //}
            event.setNativeId(nativeId);
            event.setAttackerIP(csvClientIP);

            return event;
        }
    } catch (IOException e) {
        return null;
    }
    return null;

}

From source file:com.ge.research.semtk.load.dataset.CSVDataset.java

@Override
/**/* www  . j  a  v  a 2 s. c o m*/
 * Read the next set of rows from the CSV file
 */
public ArrayList<ArrayList<String>> getNextRecords(int numRecords) throws Exception {
    if (headers == null) {
        throw new Exception("Dataset headers are not available");
    }
    ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
    CSVRecord record;

    for (int i = 0; i < numRecords; i++) { // read the specified number of records
        try {
            ArrayList<String> currRow = new ArrayList<String>();
            record = this.recordIterator.next();
            if (record.size() == 1 && record.get(0).trim().isEmpty()) {
                System.out.println("Empty CSV row, continuing...");
                continue; // this is an empty line, skip it
            }

            for (int j = 0; j < headers.length; j++) {
                // add the next entry to the list. 
                try {
                    currRow.add(record.get(headers[j]));
                } catch (Exception eee) {
                    System.out.println("exception getting data for header");
                }
            }
            rows.add(currRow);
        } catch (NoSuchElementException e) {
            //            System.out.println("ran into an exception for a missing element when getting records.... out of rows.");
            break; // got to the end of the file
        }
    }

    // what is the count of Rows we want to return?
    //      System.out.println("number of CSV rows returned this run: " + rows.size());

    return rows;
}

From source file:citation_prediction.CitationCore.java

/**
 * Fix the citation data, which is in years by translating the timestamps and citations to be in days.
 * //w w w.  j a v a2  s .c  o  m
 * @param record The citation history in years.
 * @param limitToRows Limit the rows being processed.
 * @return The citation history in days.
 */
private static double[][] fixData(CSVRecord record, int limitToRows) {

    double[][] r = null;
    int citationCount = 0;
    int numberOfRowsToProcess = 0;

    Iterator<String> record_iterator = record.iterator();
    record_iterator.next(); //move pass paper id
    record_iterator.next(); //move pass paper publish year

    if (limitToRows != 0) {
        numberOfRowsToProcess = limitToRows;
        r = new double[numberOfRowsToProcess + 1][2];
    } else {
        numberOfRowsToProcess = record.size() - 2;
        r = new double[record.size() - 2][2];
    }

    for (int rowIndex = 0; record_iterator.hasNext() && rowIndex < numberOfRowsToProcess; rowIndex++) {

        String citations_forthis_year = record_iterator.next();

        r[rowIndex][0] = Double.valueOf(rowIndex); //timestamp
        r[rowIndex][1] = Double.valueOf(citations_forthis_year); //citation

        citationCount += r[rowIndex][1];
    }

    return fixData(r, citationCount);

}

From source file:com.raceup.fsae.test.TesterGui.java

/**
 * Parses data file, builds a Test//from w w w  . j  a  va  2  s. c  o  m
 * @param pathToDataFile path to data csv file
 */
private void parseDataFileAndCreateTestOrFail(String pathToDataFile) {
    ArrayList<Question> questions = new ArrayList<>();
    CSVRecord[] rows = null;

    try {
        CSVParser parser = CSVFormat.DEFAULT.parse(new FileReader(pathToDataFile));
        rows = parser.getRecords().toArray(new CSVRecord[parser.getRecords().size()]);
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    for (CSVRecord row : rows) { // each row represent a question
        ArrayList<Answer> answers = new ArrayList<>(); // list of answers
        if (row.size() > 1) {
            for (int i = 1; i < row.size(); i++) {
                if (row.get(i).length() > 0) {
                    answers.add(new Answer(row.get(i)));
                }
            }

            Answer correctAnswer = answers.get(0); // the correct
            // answer is always the first one
            String questionText = row.get(0);
            questions.add(
                    new Question(questionText, answers.toArray(new Answer[answers.size()]), correctAnswer)); // add to list of questions
        }
    }
    test = new Test(questions.toArray(new Question[questions.size()]));
}

From source file:eu.fthevenet.binjr.data.codec.CsvDecoder.java

private List<String> parseColumnHeaders(CSVRecord record) throws IOException, DecodingDataFromAdapterException {
    try (Profiler ignored = Profiler.start("Getting hearders from csv data", logger::trace)) {
        if (record == null) {
            throw new DecodingDataFromAdapterException("CSV stream does not contains column header");
        }/*from   ww w. ja v  a  2s  .  c  o  m*/
        List<String> headerNames = new ArrayList<>();
        for (int i = 1; i < record.size(); i++) {
            headerNames.add(record.get(i));
        }
        return headerNames;
    }
}

From source file:com.archimatetool.csv.importer.CSVImporter.java

private boolean isElementsRecordCorrectSize(CSVRecord csvRecord) {
    return csvRecord.size() == MODEL_ELEMENTS_HEADER.length;
}

From source file:com.archimatetool.csv.importer.CSVImporter.java

private boolean isRelationsRecordCorrectSize(CSVRecord csvRecord) {
    return csvRecord.size() == RELATIONSHIPS_HEADER.length;
}

From source file:com.archimatetool.csv.importer.CSVImporter.java

private boolean isPropertiesRecordCorrectSize(CSVRecord csvRecord) {
    return csvRecord.size() == PROPERTIES_HEADER.length;
}

From source file:com.esri.geoevent.datastore.GeoEventDataStoreProxy.java

public GeoEventDataStoreProxy() {
    try (InputStream is = GeoEventDataStoreProxy.class.getResourceAsStream("/arcgisservers.properties")) {
        Properties props = new Properties();
        props.load(is);//from  www.j  ava2s.  c o  m
        StringReader csvServers = new StringReader(props.getProperty("servers", ""));
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(csvServers);
        ServerInfo currInfo;
        String currServerName = "<not initialized>";
        String username, password;
        int port;
        Iterator<CSVRecord> iterator = records.iterator();
        if (iterator.hasNext()) {
            CSVRecord record = iterator.next();
            int size = record.size();
            for (int i = 0; i < size; i++) {
                try {
                    currInfo = new ServerInfo();
                    currServerName = record.get(i);
                    currInfo.url = new URL(props.getProperty(currServerName + ".url", ""));
                    port = (currInfo.url.getPort() == -1) ? getDefaultPortForScheme(currInfo.url.getProtocol())
                            : currInfo.url.getPort();
                    currInfo.authscope = new AuthScope(currInfo.url.getHost(), port);
                    username = props.getProperty(currServerName + ".username", "");
                    password = props.getProperty(currServerName + ".password", "");
                    if (!StringUtils.isEmpty(username)) {
                        username = username.replace('\\', '/');
                        String encryptedPassword = Crypto.doEncrypt(password);
                        currInfo.credentials = new UsernameEncryptedPasswordCredentials(username,
                                encryptedPassword);
                        currInfo.ntCredentials = new NTCredentialsEncryptedPassword(
                                username + ":" + encryptedPassword);
                    }
                    currInfo.httpContext = createContextForServer(currInfo);
                    String tokenUrlKey = currServerName + ".tokenUrl";
                    String tokenUrl = props.getProperty(tokenUrlKey);
                    if (tokenUrl != null) {
                        currInfo.tokenUrl = new URL(tokenUrl);
                    }

                    username = props.getProperty(currServerName + ".gisTierUsername", "");
                    if (!StringUtils.isEmpty(username)) {
                        password = props.getProperty(currServerName + ".gisTierPassword", "");
                        currInfo.gisTierUsername = username;
                        currInfo.gisTierEncryptedPassword = Crypto.doEncrypt(password);
                    }
                    currInfo.name = currServerName;
                    serverInfos.put(currServerName, currInfo);
                } catch (Throwable t) {
                    LOG.log(Level.ALL, "Failed to parse properties for server " + currServerName, t);
                }
            }
        }
    } catch (Throwable t) {
        LOG.log(Level.SEVERE, "Unable to initialize.  Will not be able to proxy any requests.", t);
    }

}