Example usage for com.google.common.io LineProcessor getResult

List of usage examples for com.google.common.io LineProcessor getResult

Introduction

In this page you can find the example usage for com.google.common.io LineProcessor getResult.

Prototype

T getResult();

Source Link

Document

Return the result of processing all the lines.

Usage

From source file:org.sonar.plugins.web.checks.header.HeaderCheck.java

@Override
public void startDocument(List<Node> nodes) {
    LineProcessor<Boolean> processor = new HeaderLinesProcessor(expectedLines);
    try {/*  w  w w  .ja  va 2s .c om*/
        Files.readLines(getWebSourceCode().getFile(), charset, processor);
    } catch (IOException e) {
        throw new SonarException(e);
    }
    if (!processor.getResult()) {
        createViolation(0, MESSAGE);
    }
}

From source file:com.davidbracewell.io.resource.Resource.java

/**
 * <p> Reads in the resource line by line passing each line through the given procedure. </p>
 *
 * @param processor A <code>LineProcessor</code> to apply to each line in the resource.
 * @return the t/*www.  ja  v a2  s.  com*/
 * @throws java.io.IOException An error happened when reading the file.
 */
public final <T> T read(LineProcessor<T> processor) throws IOException {
    if (!exists()) {
        throw new IOException(resourceDescriptor() + " does not exist!");
    }
    try (BufferedReader bufferedReader = new BufferedReader(openReader())) {
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (!processor.processLine(line)) {
                break;
            }
        }
    }
    return processor.getResult();
}

From source file:com.j2swift.util.ProGuardUsageParser.java

public static DeadCodeMap parse(CharSource listing) throws IOException {
    LineProcessor<DeadCodeMap> processor = new LineProcessor<DeadCodeMap>() {
        DeadCodeMap.Builder dead = DeadCodeMap.builder();
        String lastClass;/*from   w w w .j a v a2s .  c  o m*/

        @Override
        public DeadCodeMap getResult() {
            return dead.build();
        }

        private void handleClass(String line) {
            if (line.endsWith(":")) {
                // Class, but not completely dead; save to read its dead methods
                lastClass = line.substring(0, line.length() - 1);
            } else {
                dead.addDeadClass(line);
            }
        }

        private void handleMethod(String line) throws IOException {
            Matcher methodMatcher = proGuardMethodPattern.matcher(line);
            if (!methodMatcher.matches()) {
                throw new AssertionError("Line doesn't match expected ProGuard format!");
            }
            if (lastClass == null) {
                throw new IOException("Bad listing format: method not attached to a class");
            }
            String returnType = methodMatcher.group(5);
            String methodName = methodMatcher.group(6);
            String arguments = methodMatcher.group(7);
            String signature = buildMethodSignature(returnType, arguments);
            dead.addDeadMethod(lastClass, methodName, signature);
        }

        private void handleField(String line) throws IOException {
            String name = line.substring(line.lastIndexOf(" ") + 1);
            dead.addDeadField(lastClass, name);
        }

        @Override
        public boolean processLine(String line) throws IOException {
            if (line.startsWith("ProGuard, version") || line.startsWith("Reading ")) {
                // ignore output header
            } else if (!line.startsWith("    ")) {
                handleClass(line);
            } else if (line.startsWith("    ") && !line.contains("(")) {
                handleField(line);
            } else {
                handleMethod(line);
            }
            return true;
        }
    };

    // TODO(cgdecker): Just use listing.readLines(processor) once guava_jdk5 is upgraded to a newer
    // version.
    Closer closer = Closer.create();
    try {
        BufferedReader reader = closer.register(listing.openBufferedStream());
        String line;
        while ((line = reader.readLine()) != null) {
            processor.processLine(line);
        }
        return processor.getResult();
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}