Example usage for org.apache.commons.io LineIterator hasNext

List of usage examples for org.apache.commons.io LineIterator hasNext

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator hasNext.

Prototype

public boolean hasNext() 

Source Link

Document

Indicates whether the Reader has more lines.

Usage

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref countries./*from   ww  w.j a  v  a2s.  c o  m*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefCountries(String path) throws Exception {
    refCountries = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_COUNTRIES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <commonCountryName>|akaCountryName1|akaCountryName2|...
            // For example: United States|the U.S.|the United States
            //    All three will match as a valid country, and "United States" will always be used as the common country name
            String[] countries = it.nextLine().split("[|]");
            String commonCountryName = countries[0].trim();
            refCountries.put(commonCountryName, commonCountryName);
            for (int i = 1; i < countries.length; i++)
                refCountries.put(countries[i].trim(), commonCountryName);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:ke.co.tawi.babblesms.server.servlet.upload.UploadUtil.java

/**
 * Checks that an uploaded Contact file is in proper order.
 * //from  w  w  w . j  av  a2  s  .c o m
 * @param file
 * @return the feedback of having inspected the file, whether it was proper
 */
protected String inspectContactFile(File file) {
    String feedback = ContactUpload.UPLOAD_SUCCESS;
    int count = 1;

    LineIterator lineIterator = null;
    try {
        lineIterator = FileUtils.lineIterator(file, "UTF-8");

        String line;
        String[] rowTokens, phoneTokens, networkTokens;
        String network;
        while (lineIterator.hasNext()) {
            line = lineIterator.nextLine();

            rowTokens = StringUtils.split(line, ',');

            if (rowTokens.length != 3 && line.length() > 0) {// Every row must have 3 columns
                return ("Invalid format on line " + count + ": " + line);
            }

            phoneTokens = StringUtils.split(rowTokens[1], ';');
            networkTokens = StringUtils.split(rowTokens[2], ';');

            // Check that the number of phone numbers and number of networks match
            if (phoneTokens.length != networkTokens.length) {
                return ("Invalid format on line " + count + ": " + line);
            }

            // Check that the phone numbers contain only numbers or spaces
            for (String phone : phoneTokens) {
                if (!StringUtils.isNumericSpace(phone)) {
                    return ("Invalid number on line " + count + ": " + line);
                }
            }

            // Check to see that only valid networks have been provided
            for (String s : networkTokens) {
                network = StringUtils.lowerCase(StringUtils.trimToEmpty(s));
                if (!networkList.contains(network)) {
                    return ("Invalid network on line " + count + ": " + line);
                }
            }

            count++;
        }

    } catch (IOException e) {
        logger.error("IOException when inspecting: " + file);
        logger.error(e);

    } finally {
        if (lineIterator != null) {
            lineIterator.close();
        }
    }

    return feedback;
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref us cities states./*  w ww . ja v  a2 s  .co  m*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefUSCitiesStates(String path) throws Exception {
    refCitiesByStates = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_US_CITIES_STATES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <cities>|<AA-BB-CC>
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            fields[0] = fields[0].trim();
            fields[1] = fields[1].trim();
            refCitiesByStates.put(fields[1], fields[0]);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref us states./*from  w w w.  j  a v a2 s  .c o  m*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefUSStates(String path) throws Exception {
    refStateAbbrevsByNames = new HashMap<String, String>();
    refValidStateAbbrevs = new HashMap<String, Object>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_US_STATE_NAMES_ABBREVS), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <stateName>|<stateAbbrev>
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            fields[0] = fields[0].trim();
            fields[1] = fields[1].trim();
            refStateAbbrevsByNames.put(fields[0], fields[1]);
            refValidStateAbbrevs.put(fields[1], null);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref currencies countries.
 *
 * @param path the path/*from w  w  w . jav a  2s .  c  o m*/
 * @throws Exception the exception
 */
private void createRefCurrenciesCountries(String path) throws Exception {
    refCountriesByCurrencies = new HashMap<String, String>();
    refCurrenciesByCountries = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_CURRENCIES_COUNTRIES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <countryName>|<primaryCurrencyName>|<akaCurrencyName1>|<akaCurrencyName2>|...
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            for (int i = 0; i < fields.length; i++)
                fields[i] = fields[i].trim();
            fields[1] = fields[1].trim();
            // When looking up by country, always return the primary currency
            refCurrenciesByCountries.put(fields[0], fields[1]);
            for (int i = 1; i < fields.length; i++)
                refCountriesByCurrencies.put(fields[i], fields[0]);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:$.MessageLogParser.java

/**
     * Gets lines which corresponds with specified correlation ID from the specified log file.
     */*from  w  w  w  .  ja  va  2  s  .  c o  m*/
     * @param logFile the log file
     * @param correlationId the correlation ID
     * @return log lines
     * @throws IOException when error occurred during file reading
     */
    private List<String> getLogLines(File logFile, String correlationId) throws IOException {
        List<String> logLines = new ArrayList<String>();

        Log.debug("Go through the following log file: " + logFile);

        int year = Calendar.getInstance().get(Calendar.YEAR);
        String[] possibleYears = new String[] { String.valueOf(year - 1), String.valueOf(year) };

        InputStream stream = null;
        try {
            if (logFile.getName().endsWith(GZIP_FILE_EXTENSION)) {
                stream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(logFile)));
            } else {
                stream = new BufferedInputStream(new FileInputStream(logFile));
            }

            LineIterator it = IOUtils.lineIterator(stream, Charset.defaultCharset());

            String requestId = null;
            boolean lastCorrectLine = false; // if previous log line belongs to requestId
            while (it.hasNext()) {
                String line = it.nextLine();

                if (requestId == null) {
                    if (StringUtils.contains(line, correlationId)) {
                        logLines.add(formatLogLine(line));

                        // finds requestID
                        requestId = getRequestId(line);

                        if (requestId != null) {
                            Log.debug("correlationId (" + correlationId + ") => requestId (" + requestId + ")");
                        }
                    }
                } else {
                    // adds lines with requestID and lines that belongs to previous log record (e.g. XML request)
                    //  it's better to check also correlationID because it's not one request ID
                    //  for all repeated scheduled jobs for processing of partly failed messages

                    // 2013-05-23 20:22:36,754 [MACHINE_IS_UNDEFINED, ajp-bio-8009-exec-19, /esb/ws/account/v1, ...
                    // <checkCustomerCreditRequest xmlns="cleverbus.org/ws/AccountService-v1">
                    //    <firstName>csd</firstName>
                    //    <lastName>acs</lastName>
                    //    <birthNumber>111111/1111</birthNumber>
                    // </checkCustomerCreditRequest>

                    if (StringUtils.contains(line, requestId) || (StringUtils.contains(line, correlationId))
                            || (lastCorrectLine && !StringUtils.startsWithAny(line, possibleYears))) {
                        logLines.add(formatLogLine(line));
                        lastCorrectLine = true;
                    } else {
                        lastCorrectLine = false;
                    }
                }
            }
        } finally {
            IOUtils.closeQuietly(stream);
        }

        return logLines;
    }

From source file:com.sangupta.murmur.MurmurEnglishTest.java

/**
 * The main core logic for all testing.//from   ww  w.  jav  a  2  s  . c  om
 * 
 * @param outputFileName
 * @param function
 * @throws IOException
 */
private void testHashes(String outputFileName, StringHashFunction function) throws IOException {
    LineIterator iterator = FileUtils.lineIterator(new File(BASE_PATH + "/english-wordlist.txt"));
    LineIterator results = FileUtils.lineIterator(new File(BASE_PATH + "/" + outputFileName));

    int matched = 0;
    int total = 0;

    while (iterator.hasNext()) {
        String line = iterator.next();

        byte[] bytes = line.getBytes();
        String computed = function.getHash(bytes);
        String actual = results.next();

        if (actual.contains(",")) {
            // result has multiple values
            String[] act = actual.split(",");
            String[] com = computed.split(",");
            if (act.length == com.length) {
                boolean allMatch = true;
                for (int index = 0; index < act.length; index++) {
                    allMatch = allMatch & bigMatch(act[index], com[index]);
                }

                if (allMatch) {
                    matched++;
                }
            }
        } else {
            // result has only a single value
            if (actual.equals(computed)) {
                matched++;
            } else {
                if (bigMatch(actual, computed)) {
                    matched++;
                }
            }
        }

        total++;
    }

    Assert.assertEquals("Total number of hashes did not match", total, matched);
}

From source file:fr.ericlab.mabed.structure.Corpus.java

public void prepareCorpus() {
    System.out.println(Util.getDate() + " Preparing corpus...");
    String[] fileArray = new File("input/").list();
    nbTimeSlices = 0;//from  ww  w.j av  a  2  s .c  o  m
    NumberFormat formatter = new DecimalFormat("00000000");
    ArrayList<Integer> list = new ArrayList<>();
    for (String filename : fileArray) {
        if (filename.endsWith(".text")) {
            try {
                list.add(formatter.parse(filename.substring(0, 8)).intValue());
            } catch (ParseException ex) {
                Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
            }
            nbTimeSlices++;
        }
    }
    int a = Collections.min(list), b = Collections.max(list);
    LineIterator it = null;
    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
        it = FileUtils.lineIterator(new File("input/" + formatter.format(a) + ".time"), "UTF-8");
        if (it.hasNext()) {
            Date parsedDate = dateFormat.parse(it.nextLine());
            startTimestamp = new java.sql.Timestamp(parsedDate.getTime());
        }
        it = FileUtils.lineIterator(new File("input/" + formatter.format(b) + ".time"), "UTF-8");
        String lastLine = "";
        while (it.hasNext()) {
            lastLine = it.nextLine();
        }
        Date parsedDate = dateFormat.parse(lastLine);
        endTimestamp = new java.sql.Timestamp(parsedDate.getTime());
    } catch (IOException | ParseException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        LineIterator.closeQuietly(it);
    }
    System.out.print("   - Computing word frequencies");
    GlobalIndexer indexer = new GlobalIndexer(configuration.numberOfThreads, false);
    try {
        indexer.index("input/", configuration.stopwords);
    } catch (InterruptedException | IOException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    }
    indexer = new GlobalIndexer(configuration.numberOfThreads, true);
    try {
        indexer.index("input/", configuration.stopwords);
    } catch (InterruptedException | IOException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(", 100% done.");
}

From source file:com.okmich.hackerday.client.tool.ClientSimulator.java

@Override
public void run() {
    //read file content
    LineIterator lIt;
    try {//from w  w w  .  j  av a2 s.  c o m
        LOG.log(Level.INFO, "Reading the content of file");
        lIt = FileUtils.lineIterator(this.file);
    } catch (IOException ex) {
        Logger.getLogger(ClientSimulator.class.getName()).log(Level.SEVERE, null, ex);
        throw new RuntimeException(ex);
    }
    String line;
    LOG.log(Level.INFO, "Sending file content to kafka topic - {0}", this.topic);
    while (lIt.hasNext()) {
        line = lIt.nextLine();
        //send message to kafka
        this.kafkaMessageProducer.send(this.topic, line);
        try {
            Thread.currentThread().sleep(this.random.nextInt(1000));
        } catch (InterruptedException ex) {
            Logger.getLogger(ClientSimulator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.automation.util.AutomationUtil.java

public static void addTabSepTrainDocument(MiraTemplate aTemplate, RepositoryService aRepository,
        AutomationService aAutomationService)
        throws IOException, UIMAException, ClassNotFoundException, AutomationException {
    File miraDir = aAutomationService.getMiraDir(aTemplate.getTrainFeature());
    if (!miraDir.exists()) {
        FileUtils.forceMkdir(miraDir);/*from   w ww  .  j a v a  2  s  .  co m*/
    }

    AutomationStatus status = aAutomationService.getAutomationStatus(aTemplate);

    boolean documentChanged = false;
    for (SourceDocument document : aAutomationService
            .listTabSepDocuments(aTemplate.getTrainFeature().getProject())) {
        if (!document.isProcessed()) {
            documentChanged = true;
            break;
        }
    }
    if (!documentChanged) {
        return;
    }

    for (SourceDocument sourceDocument : aAutomationService
            .listTabSepDocuments(aTemplate.getTrainFeature().getProject())) {
        if (sourceDocument.getFeature() != null) { // This is a target layer train document
            continue;
        }
        File trainFile = new File(miraDir,
                sourceDocument.getId() + sourceDocument.getProject().getId() + ".train");
        BufferedWriter trainOut = new BufferedWriter(new FileWriter(trainFile));
        File tabSepFile = new File(aRepository.getDocumentFolder(sourceDocument), sourceDocument.getName());
        LineIterator it = IOUtils.lineIterator(new FileReader(tabSepFile));
        while (it.hasNext()) {
            String line = it.next();
            if (line.trim().equals("")) {
                trainOut.append("\n");
            } else {
                StringTokenizer st = new StringTokenizer(line, "\t");
                if (st.countTokens() != 2) {
                    trainOut.close();
                    throw new AutomationException("This is not a valid TAB-SEP document");
                }
                trainOut.append(getMiraLineForTabSep(st.nextToken(), st.nextToken()));
            }
        }
        sourceDocument.setProcessed(false);
        status.setTrainDocs(status.getTrainDocs() - 1);
        trainOut.close();
    }

}