Example usage for org.apache.commons.io IOUtils lineIterator

List of usage examples for org.apache.commons.io IOUtils lineIterator

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils lineIterator.

Prototype

public static LineIterator lineIterator(InputStream input, String encoding) throws IOException 

Source Link

Document

Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).

Usage

From source file:edu.smu.tspell.wordnet.impl.file.SenseIndexReader.java

/**
 * Reads the exceptions from a single file that correspond to the
 * exceptions for a particular synset type.
 * /*from  w  w  w .  j  a va  2  s . c o m*/
 * @param  fileName Name of the file to read.
 * @param  type Syntactic type associated with the file.
 * @throws RetrievalException An error occurred reading the exception data.
 */
private void loadSenseIndexEntries(String fileName) throws IOException {
    String dir = PropertyNames.databaseDirectory;
    InputStream file = getClass().getResourceAsStream(dir + fileName);
    LineIterator iterator = IOUtils.lineIterator(file, null);
    //  Loop through all lines in the file
    while (iterator.hasNext()) {
        String line = iterator.nextLine();
        //  Parse the index line
        SenseIndexEntry entry = parser.parse(line);
        String key = entry.getSenseKey().getFullSenseKeyText();
        entries.put(key, entry);

        // Deal with any adjective satellites
        if (entry.getSenseKey().getType() == SynsetType.ADJECTIVE_SATELLITE) {
            key = entry.getSenseKey().getPartialSenseKeyText();
            ArrayList<SenseIndexEntry> list = satelliteEntries.get(key);
            if (list == null) {
                // this is our first one for this key.
                list = new ArrayList<SenseIndexEntry>();
                satelliteEntries.put(key, list);
            }
            list.add(entry);
        }
    }
    file.close();
}

From source file:net.sf.logsaw.dialect.websphere.WebsphereDialect.java

@Override
public void parse(ILogResource log, InputStream input, ILogEntryCollector collector) throws CoreException {
    Assert.isNotNull(log, "log"); //$NON-NLS-1$
    Assert.isNotNull(input, "input"); //$NON-NLS-1$
    Assert.isNotNull(collector, "collector"); //$NON-NLS-1$
    Assert.isTrue(isConfigured(), "Dialect should be configured by now"); //$NON-NLS-1$
    try {/*from w w w  .j  a  v a2s .c o m*/
        LogEntry currentEntry = null;
        IHasEncoding enc = (IHasEncoding) log.getAdapter(IHasEncoding.class);
        IHasLocale loc = (IHasLocale) log.getAdapter(IHasLocale.class);
        // WebSphere Dialect doesn't need to care about the timezone, because it is encoded in the log messages
        DateFormat df = getDateFormat(loc.getLocale());
        LineIterator iter = IOUtils.lineIterator(input, enc.getEncoding());
        int lineNo = 0;
        try {
            while (iter.hasNext()) {
                // Error handling
                lineNo++;
                List<IStatus> statuses = null;
                boolean fatal = false; // determines whether to interrupt parsing

                String line = iter.nextLine();
                Matcher m = getInternalPattern().matcher(line);
                if (m.find()) {
                    // The next line matches, so flush the previous entry and continue
                    if (currentEntry != null) {
                        collector.collect(currentEntry);
                        currentEntry = null;
                    }
                    currentEntry = new LogEntry();
                    for (int i = 0; i < m.groupCount(); i++) {
                        try {
                            extractField(currentEntry, i + 1, m.group(i + 1), df);
                        } catch (CoreException e) {
                            // Mark for interruption
                            fatal = fatal || e.getStatus().matches(IStatus.ERROR);

                            // Messages will be displayed later
                            if (statuses == null) {
                                statuses = new ArrayList<IStatus>();
                            }
                            if (e.getStatus().isMultiStatus()) {
                                Collections.addAll(statuses, e.getStatus().getChildren());
                            } else {
                                statuses.add(e.getStatus());
                            }
                        }
                    }

                    // We encountered errors or warnings
                    if (statuses != null && !statuses.isEmpty()) {
                        currentEntry = null; // Stop propagation
                        IStatus status = new MultiStatus(WebsphereDialectPlugin.PLUGIN_ID, 0,
                                statuses.toArray(new IStatus[statuses.size()]),
                                NLS.bind(Messages.WebsphereDialect_error_failedToParseLine, lineNo), null);
                        if (fatal) {
                            // Interrupt parsing in case of error
                            throw new CoreException(status);
                        } else {
                            collector.addMessage(status);
                        }
                    }
                } else if (currentEntry != null) {
                    // Append to message
                    String msg = currentEntry.get(getFieldProvider().getMessageField());
                    StringWriter strWriter = new StringWriter();
                    PrintWriter printWriter = new PrintWriter(strWriter);
                    printWriter.print(msg);
                    printWriter.println();
                    printWriter.print(line);
                    currentEntry.put(getFieldProvider().getMessageField(), strWriter.toString());
                }

                if (collector.isCanceled()) {
                    // Cancel parsing
                    break;
                }
            }

            if (currentEntry != null) {
                // Collect left over entry
                collector.collect(currentEntry);
            }
        } finally {
            LineIterator.closeQuietly(iter);
        }
    } catch (Exception e) {
        throw new CoreException(new Status(IStatus.ERROR, WebsphereDialectPlugin.PLUGIN_ID,
                NLS.bind(Messages.WebsphereDialect_error_failedToParseFile,
                        new Object[] { log.getName(), e.getLocalizedMessage() }),
                e));
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpEngine.java

public static List<CqpMacro> getMacros() {
    List<CqpMacro> macros = new ArrayList<CqpMacro>();

    boolean open = false;
    CqpMacro currentMacro = null;/*from   w ww. j a  va2s  .c om*/
    String lastComment = "";

    InputStream is = null;
    try {
        is = ResourceUtils.resolveLocation(macrosLocation, null, null).openStream();
        for (LineIterator li = IOUtils.lineIterator(is, "UTF-8"); li.hasNext();) {

            String line = li.next();
            String n = line.toLowerCase().trim();

            // comment
            if (n.startsWith("#") && !open) {
                lastComment = line;
                continue;
            }

            if (n.startsWith("macro") && !open) {
                currentMacro = new CqpMacro();
                Pattern p = Pattern.compile("MACRO\\s+(\\w+)\\s*\\((\\d+)\\)");
                Matcher m = p.matcher(line.trim());
                if (m.matches() && m.groupCount() >= 2) {
                    currentMacro.setName(m.group(1));
                    currentMacro.setParamCount(Integer.parseInt(m.group(2)));
                    currentMacro.setComment(lastComment);
                    currentMacro.setBody(new ArrayList<String>());
                } else {
                    // throw new
                }
                continue;
            }

            if (n.startsWith("(") && !open) {
                open = true;
                continue;
            }

            if (n.startsWith(")") && open) {
                if (n.startsWith(");") || (li.hasNext() && li.next().trim().startsWith(";"))) {
                    open = false;
                    macros.add(currentMacro);
                    continue;
                }
            }

            if (open) {
                currentMacro.getBody().add(line.trim());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeQuietly(is);
    }

    return macros;
}

From source file:com.zenika.doclipser.api.DockerClientJavaApi.java

@Override
public void defaultBuildCommand(String eclipseProjectName, String dockerBuildContext) {
    File baseDir = new File(dockerBuildContext);
    InputStream response = dockerClient.buildImageCmd(baseDir).exec();
    StringWriter logwriter = new StringWriter();
    messageConsole.getDockerConsoleOut()
            .println(">>> Building " + dockerBuildContext + "/Dockerfile with default options");
    messageConsole.getDockerConsoleOut().println("");

    try {/*from   www .java  2 s  . c  o m*/
        messageConsole.getDockerConsoleOut().flush();
        LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            String line = itr.next();
            logwriter.write(line);
            messageConsole.getDockerConsoleOut().println(line);
            messageConsole.getDockerConsoleOut().flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(response);
    }

    messageConsole.getDockerConsoleOut().println("");
    messageConsole.getDockerConsoleOut().println("<<< Build ended");
}

From source file:fr.treeptik.cloudunit.docker.JSONClient.java

public JsonResponse sendGet(URI uri) throws IOException {
    StringBuilder builder = new StringBuilder();
    CloseableHttpClient httpclient = build();
    HttpGet httpGet = new HttpGet(uri);
    HttpResponse response = httpclient.execute(httpGet);
    LineIterator iterator = IOUtils.lineIterator(response.getEntity().getContent(), "UTF-8");
    while (iterator.hasNext()) {
        builder.append(iterator.nextLine());
    }//  w ww. j a  v a  2  s .c  o m
    JsonResponse jsonResponse = new JsonResponse(response.getStatusLine().getStatusCode(), builder.toString(),
            null);
    return jsonResponse;
}

From source file:de.cebitec.guava.dockertest.Docker.java

public int run() throws Exception {

    if (repo.equals("")) {
        throw new Exception("No container defined.");
    }/*ww  w.  ja  va  2 s .co  m*/
    if (!isImageAvailable(repo)) {
        pullImage(repo);
    }

    String[] command = cmd.toArray(new String[cmd.size()]);
    ContainerCreateResponse containerResp = dockerClient.createContainerCmd(repo).withCmd(command).exec();

    if (binds.isEmpty()) {
        dockerClient.startContainerCmd(containerResp.getId()).exec();
    } else {

        dockerClient.startContainerCmd(containerResp.getId()).withBinds(binds.toArray(new Bind[binds.size()]))
                .exec();
    }

    int exit = dockerClient.waitContainerCmd(containerResp.getId()).exec();

    if (exit != 0) {

        ClientResponse resp = dockerClient.logContainerCmd(containerResp.getId()).withStdErr().exec();
        LineIterator itr = IOUtils.lineIterator(resp.getEntityInputStream(), "US-ASCII");
        while (itr.hasNext()) {
            String line = itr.next();
            //ugly hack because of java -docker bug
            System.out.println(line.replaceAll("[^\\x20-\\x7e]", ""));
            System.out.println(itr.hasNext() ? "\n" : "");
        }

        throw new Exception("Command Exited with code " + exit);
    }
    if (!this.outputFile.isEmpty()) {
        new File(new File(this.outputFile).getParent()).mkdirs();
        PrintWriter writer = new PrintWriter(this.outputFile, "UTF-8");
        ClientResponse resp = dockerClient.logContainerCmd(containerResp.getId()).withStdOut().exec();

        try {
            LineIterator itr = IOUtils.lineIterator(resp.getEntityInputStream(), "UTF-8");
            while (itr.hasNext()) {
                String out = itr.next();
                //ugly hack because of java -docker bug
                out = out.replaceAll("[\\p{Cc}&&[^\t]]", "");
                writer.write(out);
                writer.write(itr.hasNext() ? "\n" : "");
            }
        } finally {
            writer.close();
            IOUtils.closeQuietly(resp.getEntityInputStream());
        }
    }
    dockerClient.removeContainerCmd(containerResp.getId()).exec();

    return exit;
}

From source file:com.qualogy.qafe.bind.orm.jibx.ORMBinder.java

@SuppressWarnings("unchecked")
private static String docToString(InputStream stream) throws IOException {
    StringBuffer buf = new StringBuffer();

    int i = 1;//from   w  w w.  ja  v  a2 s . c  om
    buf.append("\n");
    for (Iterator iter = IOUtils.lineIterator(stream, ENCODING_TYPE); iter.hasNext(); i++) {
        String lnr = "" + i;
        for (int j = 0; lnr.length() < 4 && j < 4; j++) {
            lnr = " " + lnr;
        }
        buf.append(lnr + (String) iter.next());
    }
    return buf.toString();
}

From source file:com.github.scizeron.jidr.maven.plugin.JidrPackageApp.java

@Override
public void execute() throws MojoExecutionException {
    InputStream input = null;//from www  . j a v  a2 s.  c o m
    FileOutputStream output = null;

    if ("pom".equals(this.project.getPackaging())) {
        getLog().info("Skip execute on " + this.project.getPackaging() + " project.");
        return;
    }

    final String outputDirname = this.project.getBuild().getDirectory() + File.separator + "distrib";

    final String libOutputDir = outputDirname + File.separator + "lib";
    final String appOutputDir = outputDirname + File.separator + "app";
    final String confOutputDir = outputDirname + File.separator + "conf";
    final String binOutputDir = outputDirname + File.separator + "bin";

    try {
        new File(libOutputDir).mkdirs();
        new File(appOutputDir).mkdirs();
        new File(binOutputDir).mkdirs();
        new File(confOutputDir).mkdirs();

        File outputFile = new File(binOutputDir + File.separator + APP_SH_FILE);
        output = new FileOutputStream(outputFile);

        input = JidrPackageApp.class.getClassLoader().getResourceAsStream(APP_SH_FILE);
        final LineIterator lineIterator = IOUtils.lineIterator(input, Charsets.UTF_8);
        while (lineIterator.hasNext()) {
            output.write((lineIterator.nextLine() + "\n").getBytes());
        }
        output.close();

        getLog().info(String.format("Create \"%s\" in %s.", APP_SH_FILE, binOutputDir));

        outputFile = new File(confOutputDir + File.separator + APP_CFG_FILE);
        output = new FileOutputStream(outputFile);

        output.write(new String("APP_ARTIFACT=" + project.getArtifactId() + "\n").getBytes());
        output.write(new String("APP_PACKAGING=" + project.getPackaging() + "\n").getBytes());
        output.write(new String("APP_VERSION=" + project.getVersion()).getBytes());

        getLog().info(String.format("Create \"%s\" in %s.", APP_CFG_FILE, confOutputDir));

        // si un repertoire src/main/bin est present dans le projet, le contenu
        // sera copie dans binOutputDir
        addExtraFiles(this.project.getBasedir().getAbsolutePath() + "/src/main/bin", binOutputDir);

        // si un repertoire src/main/conf est present dans le projet, le contenu
        // sera copie dans confOutputDir
        addExtraFiles(this.project.getBasedir().getAbsolutePath() + "/src/main/conf", confOutputDir);

        final String artifactFilename = this.project.getBuild().getFinalName() + "."
                + this.project.getPackaging();

        FileUtils.copyFileToDirectory(
                new File(this.project.getBuild().getDirectory() + File.separator + artifactFilename),
                new File(appOutputDir));

        getLog().info(String.format("Copy \"%s\" to %s.", artifactFilename, appOutputDir));

        String distribFilename = this.project.getArtifactId() + "-" + this.project.getVersion() + "-"
                + classifier + "." + DISTRIB_TYPE;

        ZipFile distrib = new ZipFile(
                this.project.getBuild().getDirectory() + File.separator + distribFilename);
        ZipParameters zipParameters = new ZipParameters();
        distrib.addFolder(new File(binOutputDir), zipParameters);
        distrib.addFolder(new File(appOutputDir), zipParameters);
        distrib.addFolder(new File(confOutputDir), zipParameters);

        getLog().info(
                String.format("Create \"%s\" to %s.", distribFilename, this.project.getBuild().getDirectory()));

        this.mavenProjectHelper.attachArtifact(this.project, DISTRIB_TYPE, classifier, distrib.getFile());
        getLog().info(String.format("Attach \"%s\".", distribFilename));

    } catch (Exception exception) {
        getLog().error(exception);

    } finally {
        try {
            if (output != null) {
                output.close();
            }
            if (input != null) {
                input.close();
            }
        } catch (IOException e) {
            getLog().error(e);
        }
    }
}

From source file:com.norconex.collector.fs.crawler.FilesystemCrawler.java

private void queueStartPaths(ICrawlDataStore crawlDataStore) {
    // Queue regular start urls
    String[] startPaths = getCrawlerConfig().getStartPaths();
    if (startPaths != null) {
        for (int i = 0; i < startPaths.length; i++) {
            String startPath = startPaths[i];
            executeQueuePipeline(new BaseCrawlData(startPath), crawlDataStore);
        }/* ww w. jav  a 2 s . c om*/
    }
    // Queue start urls define in one or more seed files
    String[] pathsFiles = getCrawlerConfig().getPathsFiles();
    if (pathsFiles != null) {
        for (int i = 0; i < pathsFiles.length; i++) {
            String pathsFile = pathsFiles[i];
            LineIterator it = null;
            try {
                it = IOUtils.lineIterator(new FileInputStream(pathsFile), CharEncoding.UTF_8);
                while (it.hasNext()) {
                    String startPath = it.nextLine();
                    executeQueuePipeline(new BaseCrawlData(startPath), crawlDataStore);
                }
            } catch (IOException e) {
                throw new CollectorException("Could not process paths file: " + pathsFile, e);
            } finally {
                LineIterator.closeQuietly(it);
                ;
            }
        }
    }
}

From source file:cn.org.once.cstack.utils.JSONClient.java

public DockerResponse sendGet(URI uri) throws JSONClientException {

    if (logger.isDebugEnabled()) {
        logger.debug("Send a get request to : " + uri);
    }//  w w w .  ja  v a2  s.c  om
    StringBuilder builder = new StringBuilder();

    HttpGet httpGet = new HttpGet(uri);
    HttpResponse response = null;
    try {
        CloseableHttpClient httpclient = buildSecureHttpClient();
        response = httpclient.execute(httpGet);
        LineIterator iterator = IOUtils.lineIterator(response.getEntity().getContent(), "UTF-8");
        while (iterator.hasNext()) {
            builder.append(iterator.nextLine());
        }
    } catch (IOException e) {
        throw new JSONClientException("Error in sendGet method due to : " + e.getMessage(), e);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Status code : " + response.getStatusLine().getStatusCode());
        logger.debug("Server response : " + builder.toString());
    }

    return new DockerResponse(response.getStatusLine().getStatusCode(), builder.toString());

}