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

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

Introduction

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

Prototype

public LineIterator(final Reader reader) throws IllegalArgumentException 

Source Link

Document

Constructs an iterator of the lines for a Reader.

Usage

From source file:cn.org.once.cstack.maven.plugin.handler.ResponseErrorHandler.java

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    int status = response.getStatusLine().getStatusCode();

    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {/*w ww.j  av a2  s.  co  m*/
        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            throw new ClientProtocolException(jsonStringBuilder.toString());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            throw new ClientProtocolException(
                    "Status 404 - The server can treat the request, please contact an admin");
        default:
            throw new ClientProtocolException("Cloudunit server does not response. Please contact an admin");
        }
    }

}

From source file:de.tudarmstadt.lt.nlkg.EvaluateArgs.java

static void evaluate(String file) throws IllegalArgumentException, FileNotFoundException {
    DT dt = new DT() {
        {//from   ww w .  j av  a 2  s  . co m
            _mysql_host = "localhost";
            _mysql_dbname = "nlkg_1";
        }
    };
    LineIterator iter = new LineIterator(new FileReader(file));
    iter.nextLine(); // skip first line
    int lineno = 1;
    double tp = 0d, tn = 0d, fp = 0d, fn = 0d;
    while (iter.hasNext() && (lineno < 100 || true)) {
        lineno++;
        String line = iter.nextLine();
        if (line.trim().isEmpty())
            continue;

        String[] splits = line.split("\t");
        //         String context = splits[0].trim();
        String arg_l = splits[0].trim();
        String arg_r = splits[1].trim();
        boolean entailing_trueclass = Boolean.valueOf(splits[2].trim());

        //         _CONTEXT.add(context);
        _ARG_L.add(arg_l);
        _ARG_R.add(arg_r);
        _ENTAILING.add(entailing_trueclass);

        boolean entailing_predicted = predictEntailing(dt, arg_l, arg_r);
        if (lineno % 100 == 0)
            Evaluate.log_progress();

        if (entailing_predicted && entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tp", arg_l, arg_r,
                    entailing_trueclass));
            tp++;
        }
        if (!entailing_predicted && !entailing_trueclass) {
            Evaluate.log_true(String.format("%d %-10s %-30s %-30s %b %n", lineno, "tn", arg_l, arg_r,
                    entailing_trueclass));
            tn++;
        }
        if (entailing_predicted && !entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fp", arg_l, arg_r,
                    entailing_trueclass));
            fp++;
        }
        if (!entailing_predicted && entailing_trueclass) {
            Evaluate.log_false(String.format("%d %-10s %-30s %-30s %b %n", lineno, "fn", arg_l, arg_r,
                    entailing_trueclass));
            fn++;
        }

    }

    dt.disconnect();

    System.out.format("%ntp: %d; fp: %d; fn: %d; tn: %d; %n", (int) tp, (int) fp, (int) fn, (int) tn);
    System.out.println("Precision = " + (tp / (tp + fp)));
    System.out.println("Recall    = " + (tp / (tp + fn)));
    System.out.println("F1        = " + ((2 * tp) / ((2 * tp) + fn + fp)));
}

From source file:cn.org.once.cstack.cli.exception.CustomResponseErrorHandler.java

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    int status = response.getStatusLine().getStatusCode();

    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {/*from  w w w .ja va2s .  c  o m*/

        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            HttpErrorServer error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        default:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        }
    }

}

From source file:fr.treeptik.cloudunit.cli.exception.CustomResponseErrorHandler.java

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    int status = response.getStatusLine().getStatusCode();

    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {/*w w  w .jav  a 2s.c o  m*/

        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            JsonResponseError error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        default:
            reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            lineIterator = new LineIterator(reader);
            jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            error = JsonConverter.getError(jsonStringBuilder.toString());
            throw new ClientProtocolException(error.getMessage());
        }
    }

}

From source file:eu.eexcess.domaindetection.wordnet.WordnetDomainsReader.java

/**
 * @param wordNetFile//from w w w . j  a  v a2s  .  c om
 * @throws FileNotFoundException 
 */
public void readDefinition(File wordNetFile) throws FileNotFoundException {
    File file = new File(wordNetFile.getParentFile(), "wn-domains-3.2-tree.csv");
    LineIterator iterator = new LineIterator(new FileReader(file));
    String[] currentParents = new String[4];
    while (iterator.hasNext()) {
        String line = iterator.nextLine();
        String[] tokens = line.split("[,]");
        int depth = -1;
        for (int i = 0; i < tokens.length; i++) {
            if (!tokens[i].trim().isEmpty()) {
                depth = i;
                break;
            }
        }
        String domain = tokens[depth].trim().toLowerCase(Locale.US);
        if (depth >= 0) {
            Map<String, Double> parentToWeight = domainToParentDomainToWeight.get(domain);
            if (parentToWeight == null) {
                parentToWeight = new LinkedHashMap<String, Double>();
                domainToParentDomainToWeight.put(domain, parentToWeight);
            }
            for (int i = 0; i < depth; i++) {
                double weight = 1.0 / ((depth - i + 1) * (depth - i + 1));
                String parent = currentParents[i];
                parentToWeight.put(parent, weight);
            }
            currentParents[depth] = domain;
            for (int i = depth + 1; i < 4; i++) {
                currentParents[i] = null;
            }
        } else {
            domainToParentDomainToWeight.put(domain, null);
        }
    }
    iterator.close();
}

From source file:eu.eexcess.domaindetection.wordnet.XwndReader.java

public void read(File file) throws IOException {
    String domain = FilenameUtils.getBaseName(file.getName());

    File cacheFile = new File(file.getPath() + ".cache");
    if (!cacheFile.exists()) {
        BinaryOutputStream bos = new BinaryOutputStream(new FileOutputStream(cacheFile));
        System.out.println("Read in the Extended WordNet Domains file: " + file);
        LineIterator iterator = new LineIterator(new FileReader(file));
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            String[] tokens = line.split("\t");
            String synset = tokens[0];
            double weight = Double.parseDouble(tokens[1]);
            String[] ssid = synset.split("-");
            int nr = Integer.parseInt(ssid[0]);
            POS pos = POS.getPOSForKey(ssid[1]);
            bos.writeInt(nr);/*from   w  ww .ja v  a 2s.  com*/
            bos.writeSmallInt(pos.getId());
            bos.writeInt(Float.floatToIntBits((float) weight));
        }
        iterator.close();
        bos.close();
    }

    System.out.println("Read in the Extended WordNet Domains cache file: " + file);
    FileInputStream fStream = new FileInputStream(cacheFile);
    BinaryInputStream bis = new BinaryInputStream(fStream);
    while (bis.available() > 0) {
        int nr = bis.readInt();
        int key = bis.readSmallInt();
        POS pos = POS.getPOSForId(key);
        String synset = String.format("%08d-%s", nr, pos.getKey());
        double weight = Float.intBitsToFloat(bis.readInt());
        DomainAssignment assignment = new DomainAssignment(domain, weight);
        Set<DomainAssignment> domains = synsetToDomains.get(synset);
        if (domains == null) {
            domains = new TreeSet<DomainAssignment>();
            synsetToDomains.put(synset, domains);
        }
        domains.add(assignment);
    }
    fStream.close();
    bis.close();
}

From source file:io.druid.data.input.impl.FileIteratingFirehoseTest.java

@Test
public void testFirehose() throws Exception {
    for (Pair<String[], ImmutableList<String>> fixture : fixtures) {
        final List<LineIterator> lineIterators = Lists.transform(Arrays.asList(fixture.lhs),
                new Function<String, LineIterator>() {
                    @Override//from   w w  w  . j  ava2s .  c o  m
                    public LineIterator apply(String s) {
                        return new LineIterator(new StringReader(s));
                    }
                });

        final StringInputRowParser parser = new StringInputRowParser(
                new CSVParseSpec(new TimestampSpec("ts", "auto", null),
                        new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("x")), null, null),
                        ",", ImmutableList.of("ts", "x")),
                null);

        final FileIteratingFirehose firehose = new FileIteratingFirehose(lineIterators.iterator(), parser);
        final List<String> results = Lists.newArrayList();

        while (firehose.hasMore()) {
            results.add(Joiner.on("|").join(firehose.nextRow().getDimension("x")));
        }

        Assert.assertEquals(fixture.rhs, results);
    }
}

From source file:com.mewmew.fairy.v1.book.Xargs.java

public static void exec(Map<String, String> env, String cmd[], boolean redirectError, Output<String> output)
        throws IOException, InterruptedException {
    ProcessBuilder pb = new ProcessBuilder(cmd);
    if (env != null) {
        pb.environment().putAll(env);//from   w ww. java  2 s .  c om
    }
    if (redirectError) {
        pb.redirectErrorStream(true);
    }
    final Process p = pb.start();
    if (!redirectError) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    LineIterator err = new LineIterator(new InputStreamReader(p.getErrorStream()));
                    while (err.hasNext()) {
                        err.next();
                    }
                } finally {
                }
            }
        }).start();
    }
    LineIterator out = new LineIterator(new InputStreamReader(p.getInputStream()));
    while (out.hasNext()) {
        output.output(out.nextLine());
    }
    int code = p.waitFor();
    if (code != 0) {
        throw new RuntimeException(String.format("return != 0, code = %d", code));
    }
}

From source file:edu.cornell.med.icb.goby.reads.PicardFastaIndexedSequence.java

public PicardFastaIndexedSequence(final String filename) throws FileNotFoundException {
    delegate = new IndexedFastaSequenceFile(new File(filename));
    indexDelegate = new FastaSequenceIndex(new File(filename + ".fai"));
    final int numContigs = indexDelegate.size();
    if (!delegate.isIndexed())
        throw new FileNotFoundException("An fasta idx index must be found for filename " + filename);

    lengths = new int[numContigs];
    names = new String[numContigs];
    basesPerLine = new long[numContigs];

    final LineIterator lineIt = new LineIterator(new FileReader(filename + ".fai"));

    // collect the contig names by parsing the text fai file. For some bizarre reason neither the
    // IndexedFastaSequenceFile class nor the FastaSequenceIndex class expose the contig names, yet
    // contig name is the parameter expected to get data from the sequences!
    int index = 0;
    while (lineIt.hasNext()) {
        final String line = lineIt.nextLine();
        final String[] tokens = line.split("[\\s]");
        names[index] = tokens[0];//from  w w w .  j a v  a  2  s.  c  om
        namesToIndices.put(tokens[0], index);
        lengths[index] = Integer.parseInt(tokens[1]);
        basesPerLine[index] = Long.parseLong(tokens[2]);
        index++;
    }

}

From source file:eu.eexcess.diversityasurement.wikipedia.RDFCategoryExtractor.java

public void build() throws IOException {
    LineIterator categoryEntryIterator = new LineIterator(new FileReader(categoryListing));
    statistics.startTimeStamp = System.currentTimeMillis();
    statistics.linesInFile = getTotalNumberOfLines(categoryListing.getAbsoluteFile());

    while (categoryEntryIterator.hasNext()) {
        statistics.linesTotal++;// w  ww  .  j a  v  a  2 s .  c  om
        ParentChildCategoryGlue tuple = parseRelatedCategoryTuple(categoryEntryIterator.nextLine());
        if (null != tuple) {
            statistics.linesConsidered++;
            try {
                collector.takeTuple(tuple.parent, tuple.child);
            } catch (Throwable e) {
            }
        } else {
            statistics.linesSkipped++;
        }
        if (0 == (statistics.linesTotal % printStatsEvery)) {
            logStatistics();
        }
    }
    statistics.endTimeStamp = System.currentTimeMillis();
    logStatistics();
}