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

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

Introduction

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

Prototype

LineProcessor

Source Link

Usage

From source file:com.facebook.buck.android.relinker.Symbols.java

public static Symbols getSymbols(ProcessExecutor executor, Tool objdump, SourcePathResolver resolver, Path lib)
        throws IOException, InterruptedException {
    final ImmutableSet.Builder<String> undefined = ImmutableSet.builder();
    final ImmutableSet.Builder<String> global = ImmutableSet.builder();
    final ImmutableSet.Builder<String> all = ImmutableSet.builder();

    runObjdump(executor, objdump, resolver, lib, ImmutableList.of("-T"), new LineProcessor<Void>() {
        @Override//from w  ww .  j  a v  a2  s  . c o m
        public boolean processLine(String line) throws IOException {
            SymbolInfo si = extractSymbolInfo(line);
            if (si == null) {
                return true;
            }
            if (si.isUndefined) {
                undefined.add(si.symbol);
            } else if (si.isGlobal) {
                global.add(si.symbol);
            }
            all.add(si.symbol);
            return true;
        }

        @Override
        public Void getResult() {
            return null;
        }
    });

    return new Symbols(undefined.build(), global.build(), all.build());
}

From source file:net.minecraftforge.fml.common.asm.transformers.MarkerTransformer.java

private void readMapFile(String rulesFile) throws IOException {
    File file = new File(rulesFile);
    URL rulesResource;/*from   w w w  .j av  a2  s.  c  o  m*/
    if (file.exists()) {
        rulesResource = file.toURI().toURL();
    } else {
        rulesResource = Resources.getResource(rulesFile);
    }
    Resources.readLines(rulesResource, Charsets.UTF_8, new LineProcessor<Void>() {
        @Override
        public Void getResult() {
            return null;
        }

        @Override
        public boolean processLine(String input) throws IOException {
            String line = Iterables.getFirst(Splitter.on('#').limit(2).split(input), "").trim();
            if (line.length() == 0) {
                return true;
            }
            List<String> parts = Lists.newArrayList(Splitter.on(" ").trimResults().split(line));
            if (parts.size() != 2) {
                throw new RuntimeException("Invalid config file line " + input);
            }
            List<String> markerInterfaces = Lists
                    .newArrayList(Splitter.on(",").trimResults().split(parts.get(1)));
            for (String marker : markerInterfaces) {
                markers.put(parts.get(0), marker);
            }
            return true;
        }
    });
}

From source file:com.mapr.synth.distributions.WordGenerator.java

public WordGenerator(String seed, String others) {
    // read the common words
    if (seed != null) {
        try {//from w w  w  .j a  va 2s  .  co m
            Resources.readLines(Resources.getResource(seed), Charsets.UTF_8, new LineProcessor<Object>() {
                private boolean header = true;
                private final Splitter onTabs = Splitter.on("\t");

                public boolean processLine(String s) throws IOException {
                    if (!s.startsWith("#")) {
                        if (!header) {
                            Iterator<String> fields = onTabs.split(s).iterator();
                            fields.next();
                            String word = fields.next();
                            words.add(word);
                            int count = (int) Math.rint(Double.parseDouble(fields.next()));
                            baseWeights.put(word, count);
                        } else {
                            header = false;
                        }
                    }
                    return true;
                }

                public Object getResult() {
                    return null;
                }
            });
        } catch (IOException e) {
            log.error("Can't read resource \"{}\", will continue without realistic words", seed);
        }
    }

    try {
        wordReader = new BufferedReader(
                Resources.newReaderSupplier(Resources.getResource(others), Charsets.UTF_8).getInput());
    } catch (IOException e) {
        log.error("Can't read resource \"{}\", will continue without realistic words", others);
        wordReader = null;
    }

}

From source file:com.android.tools.idea.rendering.RDotTxtParser.java

/**
 * For styleable array entries.//from  ww w.j  a  va  2  s .c  om
 * <p>
 * Search R.txt file, {@code r}, for the styleable with {@code styleableName} and return the
 * array of attribute ids, in the order specified by the list {@code attrs}. Returns null if the
 * styleable is not found.
 */
@Nullable
static Integer[] getDeclareStyleableArray(File r, final List<AttrResourceValue> attrs,
        final String styleableName) {
    try {
        return Files.readLines(r, Charsets.UTF_8, new LineProcessor<Integer[]>() {
            private static final String INT_STYLEABLE = "int styleable ";

            private final String myArrayStart = "int[] styleable " + styleableName + " { ";
            private final String myEntryStart = INT_STYLEABLE + styleableName + "_";

            private Integer[] myValuesList;
            private String[] myDeclaredAttrs;
            private int myAttrsFound;

            @Override
            public boolean processLine(@NotNull String line) throws IOException {
                if (line.startsWith(myArrayStart)) {
                    // line must be of the form "int[] styleable name { value1, value2, ..., valueN }"
                    // extract " value1, value2, ..., valueN "
                    String valuesList = line.substring(myArrayStart.length(), line.length() - 1);
                    myValuesList = new Integer[StringUtil.countChars(valuesList, ',') + 1];
                    myDeclaredAttrs = new String[myValuesList.length];
                    int idx = 0;

                    for (String s : COMMA_SPLITTER.split(valuesList)) {
                        myValuesList[idx++] = Integer.decode(s);
                    }
                    return true;
                }

                if (line.startsWith(myEntryStart)) {
                    assert myValuesList != null : "Entries for a declare-styleable should be after the array declaration.";
                    // line must be of the form "int styleable name value"
                    int lastSpace = line.lastIndexOf(' ');
                    String name = line.substring(INT_STYLEABLE.length(), lastSpace);
                    int index = Integer.parseInt(line.substring(lastSpace + 1));
                    myDeclaredAttrs[index] = name;
                    myAttrsFound++;
                    // return false if all entries have been found.
                    return myAttrsFound != myDeclaredAttrs.length;
                }

                // Not a line we care about, continue processing.
                return true;
            }

            @Override
            public Integer[] getResult() {
                if (myValuesList == null) {
                    return null;
                }
                // The order of entries in a declare-styleable in the source xml and in R.txt may be different.
                // It's essential that the order of entries match the order of attrs. So, we reorder the entries.
                int index = 0;
                for (AttrResourceValue attr : attrs) {
                    String name = AarResourceClassGenerator.getResourceName(styleableName, attr);
                    for (int i = index; i < myDeclaredAttrs.length; i++) {
                        String declaredAttr = myDeclaredAttrs[i];
                        if (declaredAttr.equals(name)) {
                            ArrayUtil.swap(myDeclaredAttrs, i, index);
                            ArrayUtil.swap(myValuesList, i, index);
                            break;
                        }
                    }
                    assert myDeclaredAttrs[index].equals(name) : name + " does not equal "
                            + myDeclaredAttrs[index];
                    index++;
                }
                return myValuesList;
            }
        });
    } catch (IOException e) {
        return null;
    }
}

From source file:com.android.tools.idea.res.RDotTxtParser.java

/**
 * For styleable array entries./*from   w ww.j  a  v  a 2  s.  c  om*/
 * <p>
 * Search R.txt file, {@code r}, for the styleable with {@code styleableName} and return the
 * array of attribute ids, in the order specified by the list {@code attrs}. Returns null if the
 * styleable is not found.
 */
@Nullable
static Integer[] getDeclareStyleableArray(File r, final List<AttrResourceValue> attrs,
        final String styleableName) {
    try {
        return Files.readLines(r, Charsets.UTF_8, new LineProcessor<Integer[]>() {
            private static final String INT_STYLEABLE = "int styleable ";

            private final String myArrayStart = "int[] styleable " + styleableName + " { ";
            private final String myEntryStart = INT_STYLEABLE + styleableName + "_";

            private Integer[] myValuesList;
            private String[] myDeclaredAttrs;
            private int myAttrsFound;

            @Override
            public boolean processLine(@NotNull String line) throws IOException {
                if (line.startsWith(myArrayStart)) {
                    // line must be of the form "int[] styleable name { value1, value2, ..., valueN }"
                    // extract " value1, value2, ..., valueN "
                    String valuesList = line.substring(myArrayStart.length(), line.length() - 1);
                    int valuesCount = StringUtil.countChars(valuesList, ',') + 1;

                    // The declared styleable doesn't match the size of this list of values so ignore this styleable declaration
                    if (valuesCount != attrs.size()) {
                        // Do not keep looking for the attr indexes
                        return false;
                    }
                    myValuesList = new Integer[valuesCount];
                    myDeclaredAttrs = new String[valuesCount];
                    int idx = 0;

                    for (String s : COMMA_SPLITTER.split(valuesList)) {
                        myValuesList[idx++] = Integer.decode(s);
                    }
                    return true;
                } else if (line.startsWith(myEntryStart)) {
                    assert myValuesList != null : "Entries for a declare-styleable should be after the array declaration.";
                    // line must be of the form "int styleable name value"
                    int lastSpace = line.lastIndexOf(' ');
                    String name = line.substring(INT_STYLEABLE.length(), lastSpace);
                    int index = Integer.parseInt(line.substring(lastSpace + 1));
                    myDeclaredAttrs[index] = name;
                    myAttrsFound++;
                    // return false if all entries have been found.
                    return myAttrsFound != myDeclaredAttrs.length;
                }

                // Not a line we care about, continue processing.
                return true;
            }

            @Override
            public Integer[] getResult() {
                if (myValuesList == null || myDeclaredAttrs == null) {
                    return null;
                }
                // The order of entries in a declare-styleable in the source xml and in R.txt may be different.
                // It's essential that the order of entries match the order of attrs. So, we reorder the entries.
                int index = 0;
                for (AttrResourceValue attr : attrs) {
                    String name = ResourceClassGenerator.getResourceName(styleableName, attr);
                    for (int i = index; i < myDeclaredAttrs.length; i++) {
                        String declaredAttr = myDeclaredAttrs[i];
                        if (declaredAttr.equals(name)) {
                            ArrayUtil.swap(myDeclaredAttrs, i, index);
                            ArrayUtil.swap(myValuesList, i, index);
                            break;
                        }
                    }
                    assert myDeclaredAttrs[index].equals(name) : name + " does not equal "
                            + myDeclaredAttrs[index];
                    index++;
                }
                return myValuesList;
            }
        });
    } catch (IOException e) {
        return null;
    }
}

From source file:org.apache.druid.data.input.MapPopulator.java

/**
 * Read through the `source` line by line and populate `map` with the data returned from the `parser`
 *
 * @param source The ByteSource to read lines from
 * @param map    The map to populate//from   w ww  .  j  a  v  a  2 s .  c o  m
 *
 * @return number of lines read and entries parsed
 *
 * @throws IOException
 */
public PopulateResult populate(final ByteSource source, final Map<K, V> map) throws IOException {
    return source.asCharSource(StandardCharsets.UTF_8).readLines(new LineProcessor<PopulateResult>() {
        private int lines = 0;
        private int entries = 0;

        @Override
        public boolean processLine(String line) {
            if (lines == Integer.MAX_VALUE) {
                throw new ISE("Cannot read more than %,d lines", Integer.MAX_VALUE);
            }
            final Map<K, V> kvMap = parser.parseToMap(line);
            map.putAll(kvMap);
            lines++;
            entries += kvMap.size();
            return true;
        }

        @Override
        public PopulateResult getResult() {
            return new PopulateResult(lines, entries);
        }
    });
}

From source file:us.eharning.atomun.mnemonic.spi.BidirectionalDictionary.java

/**
 * Utility method to convert a resource URL into a list of lines.
 *
 * @param resource/*from w w w. j a  v a  2  s  . c  o m*/
 *         location to generate the line list from.
 *
 * @return list of lines.
 *
 * @throws IOException
 *         on I/O error reading from the resource.
 */
@Nonnull
private static ImmutableList<String> resourceToLines(@Nonnull URL resource) throws IOException {
    LineProcessor<ImmutableList<String>> lineProcess = new LineProcessor<ImmutableList<String>>() {
        final ImmutableList.Builder<String> result = ImmutableList.builder();

        @Override
        public boolean processLine(@Nonnull String line) throws IOException {
            /* Skip comments and empty lines */
            if (line.startsWith("#") || line.isEmpty()) {
                return true;
            }
            /* Binding dictionary handling to NFKD normalization */
            line = Normalizer.normalize(line, Normalizer.Form.NFKD);
            result.add(line);
            return true;
        }

        @Override
        public ImmutableList<String> getResult() {
            return result.build();
        }
    };
    return Resources.readLines(resource, Charsets.UTF_8, lineProcess);
}

From source file:com.google.devtools.j2objc.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 ww .  ja  v  a  2  s. 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;
        }
    };

    return listing.readLines(processor);
}

From source file:com.github.steveash.jg2p.align.InputReader.java

public List<InputRecord> read(CharSource source) throws IOException {
    return source.readLines(new LineProcessor<List<InputRecord>>() {
        private final List<InputRecord> recs = Lists.newArrayList();

        @Override//from   w w  w  . ja  v a2s  .c o  m
        public boolean processLine(String line) throws IOException {
            if (isBlank(line)) {
                return true;
            }
            if (isComment(line)) {
                return true;
            }

            InputRecord maybe = reader.parse(line);
            if (maybe != null) {
                recs.add(maybe);
            }
            return true;
        }

        @Override
        public List<InputRecord> getResult() {
            return recs;
        }
    });
}

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;/* w  w  w  .  j  a  v 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();
    }
}