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:org.klab.com.etl.Movies.java

public void readFile(String file, BufferedWriter bw) {
    int _n_ = 0;//from  w w w  .jav a 2  s  . c  o  m
    int _r_ = 0;
    LineIterator it = null;
    String FILE_DATE = movieFileDate(file);
    try {
        it = FileUtils.lineIterator(new File(file), "UTF-8");
        while (it.hasNext()) {
            String line = it.nextLine();

            int amountOfTabsLine = StringUtils.countMatches(line, SEPARATOR);
            //This will decide the structure
            String fixedLine = fixLine(line);
            // do something with line
            // System.out.println("N:\t"+amountOfTabsLine+ "\t"+line);
            if (!fixedLine.isEmpty() && _n_ > 0) {
                String output = _n_ + "\t" + FILE_DATE + "\t" + amountOfTabsLine + "\t" + fixedLine;
                bw.write(output);
                bw.newLine();
                _r_++;
            }

            _n_++;
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        System.out.println("io > [" + _n_ + "][" + _r_ + "] > out");
        LineIterator.closeQuietly(it);
    }
}

From source file:org.klab.com.etl.Movies.java

public void readinHeader(File file) {
    LineIterator it = null;
    try {//from w  ww .j a  v a 2  s . com
        it = FileUtils.lineIterator(file, "UTF-8");
        if (it.hasNext()) {
            String line = it.nextLine();
            getHeaderStructure(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:org.kuali.rice.krad.demo.uif.components.ComponentLibraryView.java

/**
 * Process xml source code to be consumed by the exhibit component
 *
 * @param sourceCode list of sourceCode to be filled in, in order the group exhibit examples appear
 *//*from ww w .j  a va  2  s .c  o  m*/
private void processXmlSource(List<String> sourceCode) {
    Map<String, String> idSourceMap = new HashMap<String, String>();
    if (xmlFilePath != null) {
        try {
            //Get the source file
            URL fileUrl = ComponentLibraryView.class.getClassLoader().getResource(xmlFilePath);
            File file = new File(fileUrl.toURI());
            Pattern examplePattern = Pattern.compile("ex:(.*?)(\\s|(-->))");

            boolean readingSource = false;
            String currentSource = "";
            String currentId = "";

            LineIterator lineIt = FileUtils.lineIterator(file);
            while (lineIt.hasNext()) {
                String line = lineIt.next();
                if (line.contains("ex:") && !readingSource) {
                    //found a ex: tag and are not already reading source
                    readingSource = true;

                    Matcher matcher = examplePattern.matcher(line);
                    if (matcher.find()) {
                        currentId = matcher.group(1);
                    }

                    currentSource = idSourceMap.get(currentId) != null ? idSourceMap.get(currentId) : "";

                    if (!currentSource.isEmpty()) {
                        currentSource = currentSource + "\n";
                    }
                } else if (line.contains("ex:") && readingSource) {
                    //stop reading source on second ex tag
                    readingSource = false;
                    idSourceMap.put(currentId, currentSource);
                } else if (readingSource) {
                    //when reading source just continue to add it
                    currentSource = currentSource + line + "\n";
                }

            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "file not found or error while reading: " + xmlFilePath + " for source reading", e);
        }
    }

    for (Group demoGroup : demoGroups) {
        //add source to the source list by order that demo groups appear
        String groupId = demoGroup.getId();
        String source = idSourceMap.get(groupId);
        if (source != null) {
            //translate the source to something that can be displayed
            sourceCode.add(translateSource(source));
        }
    }
}

From source file:org.limy.eclipse.qalab.task.Java2HtmlTask.java

/**
 * javahtml?B/*from   w  w  w.  j a  v  a2s. c o  m*/
 * @param cmd java->htmlR}h
 * @param lines Java
 * @param fileName t@C
 * @return HTML
 */
private String convertHtml(JavaToHtml cmd, String lines, String fileName) {
    LineIterator iterator = new LineIterator(new StringReader(lines));

    StringBuilder buff = new StringBuilder();
    appendLine(buff, "<head>");
    appendLine(buff, "<meta http-equiv=\"Content-Type\" content=\"text/html;" + " charset=UTF-8\"/>");
    buff.append("<title>").append(fileName).append("</title>");
    appendLine(buff, "<style type=\"text/css\">");
    appendLine(buff, "<!-- pre.src { margin-top: 0px; margin-bottom: 0px; } -->");
    appendLine(buff, "<!-- td.numLine { background: #f0f0f0; border-right: #dcdcdc 1px solid;"
            + " padding-right: 3px; text-align: right; } -->");
    appendLine(buff, "<!-- table.src {  border: #dcdcdc 1px solid; font-size: 16px; } -->");
    appendLine(buff, "<!-- td.src { width: 100%; } -->");
    appendLine(buff, "<!-- span.comment { color: #b22222; font-style: italic; } -->");
    appendLine(buff, "<!-- span.keyword { color: #2020bf; font-weight: bold; } -->");
    appendLine(buff, "<!-- span.string { color: #2a00ff; } -->");
    appendLine(buff, "<!-- span.text_italic { font-size: 12px; font-style: italic; } -->");
    appendLine(buff, "<!-- body { font-family: verdana, arial, helvetica; } -->");
    appendLine(buff, "<!-- div.separator { height: 10px; } -->");
    appendLine(buff, "<!-- table tr td, table tr th { font-size: 75%; } -->");
    appendLine(buff, "<!-- h1, h2, h3, h4, h5, h6 { margin-bottom: 0.5em; } -->");
    appendLine(buff, "<!-- h5 { margin-top: 0.5em; } -->");
    appendLine(buff, "</style>");
    //        appendLine(buff, "<link title=\"Style\" type=\"text/css\" rel=\"stylesheet\" href=\"css/main.css\"/>");
    appendLine(buff, "</head>");

    if (enableLineAnchor) {
        appendLine(buff, "<script>");
        appendLine(buff,
                "function funcInit() { " + "var pos = window.location.href.lastIndexOf(\"#\"); "
                        + "if (pos >= 0) { var number = window.location.href.substring(pos + 1);"
                        + " document.getElementById(number).style.backgroundColor = \"yellow\"; }}");
        appendLine(buff, "</script>");
        appendLine(buff, "<body onload=\"funcInit()\">");
    } else {
        appendLine(buff, "<body>");
    }
    buff.append("<h5>").append(fileName).append("</h5>");
    appendLine(buff, "<div class=\"separator\">&nbsp;</div>");
    appendLine(buff, "<table cellspacing=\"0\" cellpadding=\"0\" class=\"src\">");

    int number = 1;
    while (iterator.hasNext()) {
        String line = cmd.process(iterator.nextLine());
        buff.append("<tr id=\"").append(number).append("\">");
        buff.append("<td class=\"numLine\">&nbsp;").append(number).append("</td>");
        buff.append("<td class=\"src\"><pre class=\"src\">&nbsp;").append(line);
        buff.append("</pre></td>");
        appendLine(buff, "</tr>");

        ++number;
    }

    appendLine(buff, "</table>");
    appendLine(buff, "</body>");

    return buff.toString();
}

From source file:org.limy.eclipse.qalab.task.TodoReport.java

public void parseJavaSource(String fileName, String contents) {
    TodoBean bean = new TodoBean();
    bean.setName(fileName);//from   ww w .  ja v a 2  s  .co  m
    LineIterator iterator = new LineIterator(new StringReader(contents));
    int lineNumber = 1;
    while (iterator.hasNext()) {
        String line = iterator.nextLine();
        Matcher matcher = PATTERN_TODO.matcher(line);
        if (matcher.matches()) {
            String message = matcher.group(3);
            bean.addError(new TodoError(lineNumber, message));
        }
        ++lineNumber;
    }
    beans.add(bean);
}

From source file:org.m1theo.apt.repo.signing.PGPSigner.java

/**
 * Creates a clear sign signature over the input data. (Not detached)
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *///from  ww  w. ja  v a2 s .  c  om
public void clearSign(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);
    armoredOutput.beginClearText(digest);

    LineIterator iterator = new LineIterator(new InputStreamReader(input));

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

        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        armoredOutput.write(data);
        armoredOutput.write(EOL);

        signatureGenerator.update(data);
        if (iterator.hasNext()) {
            signatureGenerator.update(EOL);
        }
    }

    armoredOutput.endClearText();

    PGPSignature signature = signatureGenerator.generate();
    signature.encode(new BCPGOutputStream(armoredOutput));

    armoredOutput.close();
}

From source file:org.m1theo.apt.repo.signing.PGPSigner.java

/**
 * Creates a detached clear sign signature over the input data.
 *
 * @param input      the content to be signed
 * @param output     the output destination of the signature
 *//*from  w ww  . j a v a  2s  . c  om*/
public void clearSignDetached(InputStream input, OutputStream output)
        throws IOException, PGPException, GeneralSecurityException {

    PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(
            new BcPGPContentSignerBuilder(privateKey.getPublicKeyPacket().getAlgorithm(), digest));
    signatureGenerator.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, privateKey);

    ArmoredOutputStream armoredOutput = new ArmoredOutputStream(output);

    LineIterator iterator = new LineIterator(new InputStreamReader(input));

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

        // trailing spaces must be removed for signature calculation (see http://tools.ietf.org/html/rfc4880#section-7.1)
        byte[] data = trim(line).getBytes("UTF-8");

        signatureGenerator.update(data);
        if (iterator.hasNext()) {
            signatureGenerator.update(EOL);
        }
    }

    PGPSignature signature = signatureGenerator.generate();
    signature.encode(new BCPGOutputStream(armoredOutput));

    armoredOutput.close();
}

From source file:org.movsim.MovsimCoreMainWithExperiments.java

private static void appendFiles(File src, File dst, boolean writeFirstLine) throws IOException {

    LineIterator lIter = FileUtils.lineIterator(src);
    RandomAccessFile rFile = new RandomAccessFile(dst, "rw");

    rFile.seek(dst.length());//from  w w  w .ja va2s  . co  m

    long lineCount = 1;
    while (lIter.hasNext()) {
        String line = lIter.next();

        if (lineCount > 1 || writeFirstLine) {
            rFile.write((line + "\n").getBytes());
        }
        lineCount++;
    }
    lIter.close();
    rFile.close();
}

From source file:org.mskcc.cbio.importer.io.internal.FileUtilsImpl.java

/**
 * Reads the precomputed md5 digest out of a .md5 file (firehose).
  * Assume the file only contains one line wit checksum.
 *
 * @param file File//ww  w.j a va 2s . c  o  m
 * @return String
 * @throws Exception 
 */
@Override
public String getPrecomputedMD5Digest(File file) throws Exception {

    if (LOG.isInfoEnabled()) {
        LOG.info("getPrecomputedMD5Digest(): " + file.getCanonicalPath());
    }

    String toReturn = "";
    LineIterator it = org.apache.commons.io.FileUtils.lineIterator(file);
    try {
        while (it.hasNext()) {
            String content = it.nextLine();
            if (content.split(" ").length == 2) {
                toReturn = content.split(" ")[0].toUpperCase();
            }
        }
    } finally {
        LineIterator.closeQuietly(it);
    }

    // outta here
    return toReturn;
}

From source file:org.mskcc.cbio.importer.io.internal.FileUtilsImpl.java

/**
 * Get the case list from the staging file.
 *
 * @param caseIDs CaseIDs;/*from  w w  w .j  av  a 2s.  c  o  m*/
  * @param portalMetadata PortalMetadata
 * @param cancerStudyMetadata CancerStudyMetadata
 * @param stagingFilename String
 * @return List<String>
 * @throws Exception
 */
@Override
public List<String> getCaseListFromStagingFile(CaseIDs caseIDs, PortalMetadata portalMetadata,
        CancerStudyMetadata cancerStudyMetadata, String stagingFilename) throws Exception {

    if (LOG.isInfoEnabled()) {
        LOG.info("getCaseListFromStagingFile(): " + stagingFilename);
    }

    // we use set here
    HashSet<String> caseSet = new HashSet<String>();

    // staging file
    File stagingFile = org.apache.commons.io.FileUtils.getFile(portalMetadata.getStagingDirectory(),
            cancerStudyMetadata.getStudyPath(), stagingFilename);
    // sanity check
    if (!stagingFile.exists()) {
        return new ArrayList<String>();
    }

    // iterate over all rows in file
    org.apache.commons.io.LineIterator it = org.apache.commons.io.FileUtils.lineIterator(stagingFile);
    try {
        int mafCaseIDColumnIndex = 0;
        boolean processHeader = true;
        while (it.hasNext()) {
            // create a string list from row in file
            List<String> thisRow = Arrays.asList(it.nextLine().split(Converter.VALUE_DELIMITER));
            // is this the header file?
            if (processHeader) {
                // look for MAF file case id column header
                mafCaseIDColumnIndex = thisRow.indexOf(Converter.MUTATION_CASE_ID_COLUMN_HEADER);
                // this is not a MAF file, header contains the case ids, return here
                if (mafCaseIDColumnIndex == -1) {
                    for (String potentialCaseID : thisRow) {
                        if (caseIDs.isTumorCaseID(potentialCaseID)) {
                            caseSet.add(caseIDs.convertCaseID(potentialCaseID));
                        }
                    }
                    break;
                }
                processHeader = false;
                continue;
            }
            // we want to add the value at mafCaseIDColumnIndex into return set - this is a case ID
            String potentialCaseID = thisRow.get(mafCaseIDColumnIndex);
            if (caseIDs.isTumorCaseID(potentialCaseID)) {
                caseSet.add(caseIDs.convertCaseID(potentialCaseID));
            }
        }
    } finally {
        it.close();
    }

    // outta here
    return new ArrayList<String>(caseSet);
}