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

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

Introduction

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

Prototype

public String nextLine() 

Source Link

Document

Returns the next line in the wrapped Reader.

Usage

From source file:mitm.common.postfix.PostfixQueueParser.java

private void parse(String queue, LineHandler lineHandler) {
    Check.notNull(lineHandler, "lineHandler");

    StringReader reader = new StringReader(queue);

    try {//from  w w w.ja  v  a2s. c  o  m
        LineIterator iterator = IOUtils.lineIterator(reader);

        /*
         * If the mail queue is empty the first line is "Mail queue is empty". If the mail queue is
         * not empty the first line should be the header. We should therefore skip the first line
         */
        if (iterator.hasNext()) {
            iterator.next();
        }

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

            if (line.startsWith("--")) {
                /*
                 * The last line starts with -- so we are finished
                 */
                break;
            }

            /*
             * We need to collect all lines that belong to one queue item. Queue items use multiple lines
             * which are separated by an empty line
             */
            while (iterator.hasNext()) {
                String otherLine = iterator.nextLine();

                if (otherLine.length() == 0) {
                    break;
                }

                line = line + " " + otherLine;
            }

            boolean match = true;

            if (searchPattern != null) {
                Matcher matcher = searchPattern.matcher(line);

                if (!matcher.find()) {
                    match = false;
                }
            }

            if (match && !lineHandler.lineFound(line)) {
                break;
            }
        }
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.jpetrak.gate.scala.ScalaScriptPR.java

public void tryCompileScript() {
    String scalaProgramSource;/*from   ww  w .  j  av  a2 s.c  om*/
    String className;
    if (classloader != null) {
        Gate.getClassLoader().forgetClassLoader(classloader);
    }
    classloader = Gate.getClassLoader().getDisposableClassLoader(
            //"C"+java.util.UUID.randomUUID().toString().replaceAll("-", ""), 
            scalaProgramUrl.toExternalForm() + System.currentTimeMillis(), this.getClass().getClassLoader(),
            true);
    try {
        className = "ScalaScriptClass" + getNextId();
        StringBuilder sb = new StringBuilder();
        scalaProgramLines = new ArrayList<String>();
        scalaProgramLines.add(fileProlog);
        scalaProgramLines.add(classProlog.replaceAll("THECLASSNAME", className));
        LineIterator it = FileUtils.lineIterator(scalaProgramFile, "UTF-8");
        try {
            while (it.hasNext()) {
                String line = it.nextLine();
                scalaProgramLines.add(line);
            }
        } finally {
            LineIterator.closeQuietly(it);
        }
        scalaProgramLines.add(scalaCompiler.getClassEpilog().replaceAll("THECLASSNAME", className));
        for (String line : scalaProgramLines) {
            sb.append(line);
            sb.append("\n");
        }
        scalaProgramSource = sb.toString();

        //System.out.println("Program Source: " + scalaProgramSource);
    } catch (IOException ex) {
        System.err.println("Problem reading program from " + scalaProgramUrl);
        ex.printStackTrace(System.err);
        return;
    }
    try {
        //System.out.println("Trying to compile ...");
        scalaProgramClass = scalaCompiler.compile(className, scalaProgramSource, classloader);
        //scalaProgramClass = (ScalaScript) Gate.getClassLoader().
        //        loadClass("scalascripting." + className).newInstance();
        scalaProgramClass.globalsForPr = globalsForPr;
        scalaProgramClass.lockForPr = new Object();
        if (registeredEditorVR != null) {
            registeredEditorVR.setCompilationOk();
        }
        scalaProgramClass.resource1 = resource1;
        scalaProgramClass.resource2 = resource2;
        scalaProgramClass.resource3 = resource3;
        isCompileError = false;
        scalaProgramClass.resetInitAll();
    } catch (Exception ex) {
        System.err.println("Problem compiling ScalaScript Class");
        printScalaProgram(System.err);
        ex.printStackTrace(System.err);
        if (classloader != null) {
            Gate.getClassLoader().forgetClassLoader(classloader);
            classloader = null;
        }
        isCompileError = true;
        scalaProgramClass = null;
        if (registeredEditorVR != null) {
            registeredEditorVR.setCompilationError();
        }
        return;
    }
}

From source file:net.mindengine.blogix.web.routes.DefaultRoutesParser.java

@Override
public List<Route> parseRoutes(File file) throws IOException {
    LineIterator it = FileUtils.lineIterator(file, "UTF-8");
    List<Route> routes = new LinkedList<Route>();

    Route currentRoute = null;/*  w  w  w  .j  a v  a 2s. co m*/
    while (it.hasNext()) {
        String line = it.nextLine();
        if (line.trim().isEmpty()) {
            currentRoute = null;
        } else if (line.startsWith("  ")) {
            loadModelEntryForRoute(currentRoute, line);
        } else {
            currentRoute = parseLine(line.trim());
            if (currentRoute != null) {
                routes.add(currentRoute);
            }
        }
    }
    return routes;
}

From source file:at.sti2.spark.streamer.SparkStreamer.java

private void stream(File fileToStream) {

    PrintWriter streamWriter = null;
    LineIterator lineIterator = null;

    long Counter = 0;

    try {/*w ww  . ja  va  2s  . com*/
        sock = new Socket("localhost", port);
    } catch (IOException e) {
        logger.debug("Cannot connect to server.");
        System.exit(1);
    }
    logger.info("Connected.");

    try {
        streamWriter = new PrintWriter(sock.getOutputStream());
        lineIterator = FileUtils.lineIterator(fileToStream, "UTF-8");

        logger.info("Beginning to stream.");
        Date startStreaming = new Date();
        String line = null;
        while (lineIterator.hasNext()) {

            line = lineIterator.nextLine();
            streamWriter.println(line);
            Counter++;

            //            if (tripleCounter%1000 == 0){
            //               long currentTimepoint = (new Date()).getTime();
            //               System.out.println("Processing " + (1000/(currentTimepoint - timepoint)) + " triples/sec.");
            //               timepoint = currentTimepoint;
            //               streamWriter.flush();
            //            }
        }

        streamWriter.flush();

        Date endStreaming = new Date();
        logger.info("End of streaming.");
        logger.info("Streamed " + Counter + " triples/lines.");
        logger.info("Total streaming time " + (endStreaming.getTime() - startStreaming.getTime()) + " ms.");

    } catch (IOException e) {
        logger.error(e.getMessage());
    } finally {
        IOUtils.closeQuietly(streamWriter);
        lineIterator.close();
        logger.info("Disconnected.");
    }
}

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

protected void saveContacts(File contactFile, Account account, ContactDAO contactDAO, PhoneDAO phoneDAO,
        List<String> groupUuids, ContactGroupDAO contactGroupDAO) {
    LineIterator lineIterator = null;
    Contact contact;// www. jav a 2 s  . c  om
    Phone phone;

    List<Group> groupList = new LinkedList<>();
    Group grp;
    for (String uuid : groupUuids) {
        grp = new Group();
        grp.setUuid(uuid);
        groupList.add(grp);
    }

    try {
        lineIterator = FileUtils.lineIterator(contactFile, "UTF-8");

        String line;
        String[] rowTokens, phoneTokens, networkTokens;

        while (lineIterator.hasNext()) {
            line = lineIterator.nextLine();

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

            // Extract the Contact and save
            contact = new Contact();
            contact.setAccountUuid(account.getUuid());
            contact.setName(rowTokens[0]);
            contact.setStatusUuid(Status.ACTIVE);
            contactDAO.putContact(contact);

            // Extract the phones and save
            phoneTokens = StringUtils.split(rowTokens[1], ';');
            networkTokens = StringUtils.split(rowTokens[2], ';');

            String network;

            for (int j = 0; j < phoneTokens.length; j++) {
                phone = new Phone();
                phone.setPhonenumber(StringUtils.trimToEmpty(phoneTokens[j]));
                phone.setPhonenumber(StringUtils.remove(phone.getPhonenumber(), ' '));
                phone.setContactUuid(contact.getUuid());
                phone.setStatusuuid(Status.ACTIVE);

                network = StringUtils.lowerCase(StringUtils.trimToEmpty(networkTokens[j]));
                phone.setNetworkuuid(networkUuidArray[networkList.indexOf(network)]);

                phoneDAO.putPhone(phone);
            }

            // Associate the Contact to the Groups
            for (Group group : groupList) {
                contactGroupDAO.putContact(contact, group);
            }

        } // end 'while (lineIterator.hasNext())'

    } catch (IOException e) {
        logger.error("IOException when storing: " + contactFile);
        logger.error(e);

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

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

/**
 * Creates the ref countries./*  www. 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:fr.ericlab.sondy.core.DataManipulation.java

public StopWords getStopwords(AppVariables appVariables, String name) {
    StopWords stopWords = new StopWords();
    LineIterator it = null;
    try {/*  w ww  .j ava2  s.c  o m*/
        it = FileUtils.lineIterator(new File(appVariables.configuration.getWorkspace() + "/stopwords/" + name),
                "UTF-8");
        while (it.hasNext()) {
            stopWords.add(it.nextLine());
        }
    } catch (IOException ex) {
        Logger.getLogger(DataManipulation.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        LineIterator.closeQuietly(it);
    }
    return stopWords;
}

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

/**
 * Read readme series id./*from   ww  w.  j  a  v a  2s  . co m*/
 *
 * @return the list
 * @throws Exception the exception
 */
public List<SeriesIdItem> readReadmeSeriesId() throws Exception {
    List<SeriesIdItem> seriesIdItems = new ArrayList<SeriesIdItem>();
    boolean isHeaderRows = true;
    boolean isFooterRows = false;
    LineIterator it = FileUtils.lineIterator(new File(inputPathFredData + "README_SERIES_ID_SORT.txt"),
            "UTF-8");
    int numLines = 0;
    try {
        while (it.hasNext()) {
            String line = it.nextLine();
            numLines++;
            if (isHeaderRows) {
                if (line.startsWith("File"))
                    isHeaderRows = false;
            } else if (isFooterRows) {

            } else {
                if (line.length() == 0) {
                    isFooterRows = true;
                    continue;
                }
                // Data row
                // File;Title; Units; Frequency; Seasonal Adjustment; Last Updated
                // Bypass all (DISCONTINUED SERIES) rows;
                if (line.indexOf("(DISCONTINUED SERIES)") > -1 || line.indexOf("(DISCONTINUED)") > -1
                        || line.indexOf("(Discontinued Series)") > -1)
                    continue;
                String[] fields = splitFields(line);

                seriesIdItems.add(new SeriesIdItem().setCsvFileName(fields[0])
                        .setTitle(fields[1].replace("", "")).setUnits(fields[2]).setFrequency(fields[3])
                        .setSeasonalAdj(fields[4]).setLastUpdated(fields[5]));
            }

            if ((numLines % 25000) == 0)
                log.info("readReadmeSeriesId: read lines: " + numLines);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
    return seriesIdItems;
}

From source file:egovframework.rte.fdl.filehandling.FilehandlingServiceTest.java

/**
 * @throws Exception/*w ww . ja va  2s  . com*/
 */
@Test
public void testLineIterator() throws Exception {

    String[] string = {
            "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
            "  xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">",
            "  <parent>", "     <groupId>egovframework.rte</groupId>",
            "     <artifactId>egovframework.rte.root</artifactId>", "     <version>1.0.0-SNAPSHOT</version>",
            "  </parent>", "  <modelVersion>4.0.0</modelVersion>", "  <groupId>egovframework.rte</groupId>",
            "  <artifactId>egovframework.rte.fdl.filehandling</artifactId>", "  <packaging>jar</packaging>",
            "  <version>1.0.0-SNAPSHOT</version>", "  <name>egovframework.rte.fdl.filehandling</name>",
            "  <url>http://maven.apache.org</url>", "  <dependencies>", "    <dependency>",
            "      <groupId>junit</groupId>", "      <artifactId>junit</artifactId>",
            "      <version>4.4</version>", "      <scope>test</scope>", "    </dependency>",
            "    <dependency>", "      <groupId>commons-vfs</groupId>",
            "      <artifactId>commons-vfs</artifactId>", "      <version>1.0</version>", "    </dependency>",
            "    <dependency>", "      <groupId>commons-io</groupId>",
            "      <artifactId>commons-io</artifactId>", "      <version>1.4</version>", "    </dependency>",
            "    <!-- egovframework.rte -->", "    <dependency>", "      <groupId>egovframework.rte</groupId>",
            "      <artifactId>egovframework.rte.fdl.string</artifactId>",
            "      <version>1.0.0-SNAPSHOT</version>", "    </dependency>", "  </dependencies>", "</project>" };

    try {
        File file = new File("pom.xml");

        LineIterator it = FileUtils.lineIterator(file, "UTF-8");

        try {
            log.debug("############################# LineIterator ###############################");

            for (int i = 0; it.hasNext(); i++) {
                String line = it.nextLine();
                log.info(line);

                assertEquals(string[i], line);
            }
        } finally {
            LineIterator.closeQuietly(it);
        }

    } catch (Exception e) {
        log.error(e.getCause());
    }
}

From source file:com.alexkli.osgi.troubleshoot.impl.TroubleshootServlet.java

private void includeResource(PrintWriter out, String path) {
    try {/*from  ww  w . jav  a  2s .  com*/
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        URL url = getClass().getResource(path);
        if (url == null) {
            // not found`
            return;
        }
        InputStream ins = url.openConnection().getInputStream();
        LineIterator lineIterator = IOUtils.lineIterator(ins, "UTF-8");

        boolean startComment = true;
        while (lineIterator.hasNext()) {
            String line = lineIterator.nextLine();
            if (startComment) {
                String trimmed = line.trim();
                if (!trimmed.isEmpty() && !trimmed.startsWith("/**") && !trimmed.startsWith("*")) {
                    startComment = false;
                }
            }
            if (!startComment) {
                out.println(line);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}