List of usage examples for com.google.common.io LineProcessor processLine
boolean processLine(String line) throws IOException;
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//from w ww . ja v a 2 s. c om * @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;// ww w. j av a 2 s . c om @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(); } }