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

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

Introduction

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

Prototype

public Object next() 

Source Link

Document

Returns the next line in the wrapped Reader.

Usage

From source file:org.duracloud.mill.ltp.PathFilterManager.java

private List<String> loadPatterns(File file, String type) {
    try (Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
        LineIterator it = new LineIterator(r);
        List<String> patterns = new ArrayList<>();
        while (it.hasNext()) {
            String pattern = it.next();
            if (!pattern.startsWith("#")) {
                addPattern(type, patterns, pattern);
            }/*from  w ww .  j a v a2s.co  m*/
        }

        return patterns;
    } catch (Exception ex) {
        throw new DuraCloudRuntimeException(ex);
    }
}

From source file:org.eclipse.gyrex.jobs.internal.externalprocess.AsyncLoggingInputStreamReader.java

@Override
public void run() {
    try {//from w ww.  j  ava2s .  c o  m
        final LineIterator li = IOUtils.lineIterator(in, null);
        while (!closed && li.hasNext()) {
            final String line = lastLine = li.next();
            switch (level) {
            case ERROR:
                log.error(line);
                break;

            case INFO:
            default:
                log.info(line);
                break;
            }
        }
    } catch (final Exception | LinkageError | AssertionError e) {
        log.error("Unable to read from stream: {}", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.jasig.ssp.web.api.reports.AbstractReportControllerIntegrationTest.java

protected void expectReportBodyLines(List<String> expectedReportBodyLines, MockHttpServletResponse response,
        Predicate<String> firstBodyRowRule) throws UnsupportedEncodingException {
    final List<String> actualReportBodyLines = new ArrayList<String>();
    final String csvReport = response.getContentAsString();
    final LineIterator lineIterator = IOUtils.lineIterator(new CharSequenceReader(csvReport));
    boolean accumulatingActualBodyLines = false;
    if (firstBodyRowRule == null)
        accumulatingActualBodyLines = true;
    while (lineIterator.hasNext()) {
        String line = lineIterator.next();
        if (accumulatingActualBodyLines || firstBodyRowRule.apply(line)) {
            accumulatingActualBodyLines = true;
            actualReportBodyLines.add(line);
        }/*w  ww  . j  ava  2  s.c  o m*/
    }

    assertStringCollectionsEqual(expectedReportBodyLines, actualReportBodyLines);
}

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
 *//*w  w  w .j av a 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.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 .j  av a  2 s.  com*/

    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.neo4art.sentiment.service.DictionaryBasicService.java

/**
 * @see org.neo4art.sentiment.service.DictionaryService#saveDictionary()
 *//*from  w w  w  .  j a v  a  2  s.co m*/
@Override
public void saveDictionary() throws IOException {
    String alphabet[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q",
            "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    for (int i = 0; i < alphabet.length; i++) {
        String dictionaryFile = "dictionary" + File.separator + this.locale.getLanguage() + File.separator
                + alphabet[i] + " Words.txt";

        logger.info("Importing dictionary file: " + dictionaryFile);

        LineIterator dictionaryFileIterator = IOUtils.lineIterator(
                getClass().getClassLoader().getResourceAsStream(dictionaryFile), Charset.defaultCharset());

        while (dictionaryFileIterator.hasNext()) {
            this.mergeWordWithoutFlushing(dictionaryFileIterator.next());
        }
    }

    Neo4ArtBatchInserterSingleton.flushLegacyNodeIndex(NLPLegacyIndex.WORD_LEGACY_INDEX);
}

From source file:org.nomt.agent.nmon.job.ParserJob.java

@Override
public void perform() {
    logger.info("ParserJob.perform()");
    String filename = NmonDataFiles.getNextFile();
    File nmonDataFile = new File(filename);
    logger.debug("Found nmon file {}, start to parser it.", filename);
    try {//from  w w w.  j  a va2 s .c o  m
        LineIterator lineIterator = FileUtils.lineIterator(nmonDataFile, "utf-8");
        Aaa aaa = new Aaa();
        CpuX86 cpuX86 = new CpuX86();
        CpuAll cpuAll = new CpuAll();
        Mem mem = new Mem();
        Net net = new Net();
        NetPacket netPacket = new NetPacket();
        DiskBusy diskBusy = new DiskBusy();
        DiskBsize diskBsize = new DiskBsize();
        DiskRead diskRead = new DiskRead();
        DiskWrite diskWrite = new DiskWrite();
        DiskXfer diskXfer = new DiskXfer();
        JfsInode jfsInode = new JfsInode();
        Proc proc = new Proc();

        String nextLine;
        String[] lineParts;
        String part0, part1;
        String[] netTitleLineParts = null;
        String[] netPacketTitleLineParts = null;
        String[] jfsLineParts = null;
        String[] diskBusyLineParts = null;
        String[] diskReadLineParts = null;
        String[] diskWriteLineParts = null;
        String[] diskBsizeLineParts = null;
        String[] diskXferLineParts = null;

        while (lineIterator.hasNext()) {
            nextLine = lineIterator.next();
            lineParts = nextLine.split(",");
            part0 = lineParts[0];
            part1 = lineParts[1];
            if ("AAA".equalsIgnoreCase(part0)) {
                if (lineParts.length == 3) {
                    NmonLineParser.parseAaa(aaa, lineParts);
                } else if (lineParts.length == 4) {
                    NmonLineParser.parseCpuX86(cpuX86, lineParts);
                }
            } else if ("T0001".equalsIgnoreCase(part1)) {
                if ("CPU_ALL".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseCpuAll(cpuAll, lineParts);
                } else if ("MEM".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseMem(mem, lineParts);
                } else if ("NET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNet(net, netTitleLineParts, lineParts);
                } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNetPacket(netPacket, netPacketTitleLineParts, lineParts);
                } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseJfsInode(jfsInode, jfsLineParts, lineParts);
                } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBusy(diskBusy, diskBusyLineParts, lineParts);
                } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskRead(diskRead, diskReadLineParts, lineParts);
                } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskWrite(diskWrite, diskWriteLineParts, lineParts);
                } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskXfer(diskXfer, diskXferLineParts, lineParts);
                } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBsize(diskBsize, diskBsizeLineParts, lineParts);
                } else if ("PROC".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseProc(proc, lineParts);
                }
            } else if ("NET".equalsIgnoreCase(part0)) {
                netTitleLineParts = lineParts;
            } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                netPacketTitleLineParts = lineParts;
            } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                jfsLineParts = lineParts;
            } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                diskBusyLineParts = lineParts;
            } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                diskReadLineParts = lineParts;
            } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                diskWriteLineParts = lineParts;
            } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                diskXferLineParts = lineParts;
            } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                diskBsizeLineParts = lineParts;
            }

        }

        NmonFileInfo nmonFileInfo = new NmonFileInfo();
        nmonFileInfo.setAaa(aaa);
        nmonFileInfo.setX86(cpuX86);
        nmonFileInfo.setCpuAll(cpuAll);
        nmonFileInfo.setNet(net);
        nmonFileInfo.setNetPacket(netPacket);
        nmonFileInfo.setMem(mem);
        nmonFileInfo.setJfsInode(jfsInode);
        nmonFileInfo.setDiskBusy(diskBusy);
        nmonFileInfo.setDiskRead(diskRead);
        nmonFileInfo.setDiskWrite(diskWrite);
        nmonFileInfo.setDiskXfer(diskXfer);
        nmonFileInfo.setBsize(diskBsize);
        NmonFileInfoRepo.setContent(nmonFileInfo);
        logger.debug("Parse Nmon File {} successfully.", filename);
    } catch (IOException e) {
        logger.info("IOException while parsering {}.", filename);
    }

    FileUtils.deleteQuietly(nmonDataFile);
    logger.debug("After parsering, delete file {}.", filename);
}

From source file:org.occiware.clouddesigner.occi.docker.connector.dockermachine.util.DockerUtil.java

public static String asString(final InputStream response) {
    final StringWriter logwriter = new StringWriter();
    try {//from   w w w  .  ja v  a 2  s  . co  m
        final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            {
                String line = itr.next();
                boolean _hasNext = itr.hasNext();
                if (_hasNext) {
                    logwriter.write((line + "\n"));
                } else {
                    logwriter.write((line + ""));
                }
            }
        }
        response.close();
        return logwriter.toString();
    } catch (final Throwable _t) {
        if (_t instanceof IOException) {
            final IOException e = (IOException) _t;
            throw new RuntimeException(e);
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    } finally {
        IOUtils.closeQuietly(response);
    }
}

From source file:org.occiware.clouddesigner.occi.hypervisor.connector.libvirt.util.DomainMarshaller.java

public String asString(final String uuid) {
    try {//from w w  w  .j a v a  2s  .  c  om
        final String filename = (("/" + uuid) + ".xml");
        final StringWriter logwriter = new StringWriter();
        final File tmpDir = this.createTempDir(this.xmlDirectory);
        String _plus = (tmpDir + "/");
        String _plus_1 = (_plus + filename);
        InputStream response = new FileInputStream(_plus_1);
        try {
            final LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
            while (itr.hasNext()) {
                {
                    String line = itr.next();
                    boolean _hasNext = itr.hasNext();
                    if (_hasNext) {
                        logwriter.write((line + "\n"));
                    } else {
                        logwriter.write((line + ""));
                    }
                }
            }
            response.close();
            return logwriter.toString();
        } catch (final Throwable _t) {
            if (_t instanceof IOException) {
                final IOException e = (IOException) _t;
                throw new RuntimeException(e);
            } else {
                throw Exceptions.sneakyThrow(_t);
            }
        } finally {
            IOUtils.closeQuietly(response);
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}

From source file:org.opennms.upgrade.implementations.JettyConfigMigratorOffline.java

@Override
public void execute() throws OnmsUpgradeException {
    String jettySSL = getMainProperties().getProperty("org.opennms.netmgt.jetty.https-port", null);
    String jettyAJP = getMainProperties().getProperty("org.opennms.netmgt.jetty.ajp-port", null);
    boolean sslWasFixed = false;
    boolean ajpWasFixed = false;
    try {/*from w  w  w  .  j  av  a  2 s .  c  o m*/
        log("SSL Enabled ? %s\n", jettySSL != null);
        log("AJP Enabled ? %s\n", jettyAJP != null);
        if (jettySSL != null || jettyAJP != null) {
            File jettyXmlExample = new File(getHomeDirectory(),
                    "etc" + File.separator + "examples" + File.separator + "jetty.xml");
            File jettyXml = new File(getHomeDirectory(), "etc" + File.separator + "jetty.xml");

            if (!jettyXml.exists() && !jettyXmlExample.exists()) {
                throw new FileNotFoundException("The required file doesn't exist: " + jettyXmlExample);
            }

            if (!jettyXml.exists()) {
                log("Copying %s into %s\n", jettyXmlExample, jettyXml);
                FileUtils.copyFile(jettyXmlExample, jettyXml);
            }

            log("Creating %s\n", jettyXml);
            File tempFile = new File(jettyXml.getAbsoluteFile() + ".tmp");
            FileWriter w = new FileWriter(tempFile);
            LineIterator it = FileUtils.lineIterator(jettyXmlExample);

            boolean startSsl = false;
            boolean startAjp = false;
            while (it.hasNext()) {
                String line = it.next();
                if (startAjp) {
                    if (line.matches("^\\s+[<][!]--\\s*$")) {
                        continue;
                    }
                    if (line.matches("^\\s+--[>]\\s*$")) {
                        startAjp = false;
                        ajpWasFixed = true;
                        continue;
                    }
                }
                if (startSsl) {
                    if (line.matches("^\\s+[<][!]--\\s*$")) {
                        continue;
                    }
                    if (line.matches("^\\s+--[>]\\s*$")) {
                        startSsl = false;
                        sslWasFixed = true;
                        continue;
                    }
                }
                w.write(line + "\n");
                if (startAjp == false && line.contains("<!-- Add AJP support -->") && jettyAJP != null) {
                    startAjp = true;
                    log("Enabling AjpConnector\n");
                }
                if (startSsl == false && line.contains("<!-- Add HTTPS support -->") && jettySSL != null) {
                    startSsl = true;
                    log("Enabling SslSelectChannelConnector\n");
                }
            }
            LineIterator.closeQuietly(it);
            w.close();
            FileUtils.copyFile(tempFile, jettyXml);
            FileUtils.deleteQuietly(tempFile);
        } else {
            log("Neither SSL nor AJP are enabled.\n");
        }
    } catch (Exception e) {
        throw new OnmsUpgradeException("Can't fix Jetty configuration because " + e.getMessage(), e);
    }
    if (jettyAJP != null && !ajpWasFixed) {
        throw new OnmsUpgradeException(
                "Can't enable APJ, please manually edit jetty.xml and uncomment the section where org.eclipse.jetty.ajp.Ajp13SocketConnector is defined.");
    }
    if (jettySSL != null && !sslWasFixed) {
        throw new OnmsUpgradeException(
                "Can't enable SSL, please manually edit jetty.xml and uncomment the section where org.eclipse.jetty.server.ssl.SslSelectChannelConnector is defined.");
    }
}