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:uniol.apt.compiler.AbstractServiceProcessor.java

/**
 * Function that writes a resource list into the class output directory.
 * Existing entries are preserved. This means that this function only ever adds new entries.
 * @param resourceName Name of the file in which the resource list should be saved.
 * @param entries Entries that should be added to the list.
 * @throws IOException In case I/O errors occur.
 *///from   w ww. java 2  s. c  om
protected void writeResourceList(String resourceName, Collection<String> entries) throws IOException {
    entries = new TreeSet<>(entries);

    // read already listed services
    try {
        FileObject fo = this.filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
        try (InputStream is = fo.openInputStream()) {
            LineIterator lIter = IOUtils.lineIterator(is, "UTF-8");
            while (lIter.hasNext()) {
                String entry = lIter.next();
                entries.add(entry);
            }
        }
    } catch (IOException ex) {
        /* It's ok if the resource can't get found; we only skip reading it */
    }

    // write new list
    FileObject fo = this.filer.createResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
    Writer writer = fo.openWriter();
    for (String entry : entries) {
        writer.append(entry + "\n");
    }
    writer.close();
}

From source file:uniol.apt.io.parser.AbstractParsers.java

/**
 * Constructor// w  w  w  .  j  a  v a  2  s .co  m
 *
 * @param clazz Class object of the which the parsers should generate
 */
@SuppressWarnings("unchecked") // I hate type erasure and other java things ...
protected AbstractParsers(Class<T> clazz) {
    this.parsers = new HashMap<>();

    String className = clazz.getCanonicalName();

    ClassLoader cl = getClass().getClassLoader();
    try {
        Enumeration<URL> parserNames = cl.getResources(
                "META-INF/uniol/apt/compiler/" + Parser.class.getCanonicalName() + "/" + className);

        while (parserNames.hasMoreElements()) {
            try (InputStream is = parserNames.nextElement().openStream()) {
                LineIterator lIter = IOUtils.lineIterator(is, "UTF-8");
                while (lIter.hasNext()) {
                    String parserName = lIter.next();
                    Class<? extends Parser<T>> parserClass;
                    try {
                        parserClass = (Class<? extends Parser<T>>) cl.loadClass(parserName);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(String.format("Could not load class %s", parserName), ex);
                    }
                    Parser<T> parser;
                    try {
                        parser = parserClass.newInstance();
                    } catch (ClassCastException | IllegalAccessException | InstantiationException ex) {
                        throw new RuntimeException(String.format("Could not instantiate %s", parserName), ex);
                    }
                    String format = parser.getFormat();
                    if (format == null || format.equals("") || !format.equals(format.toLowerCase())) {
                        throw new RuntimeException(
                                String.format("Parser %s reports an invalid format: %s", parserName, format));
                    }
                    Parser<T> oldParser = this.parsers.get(format);
                    if (oldParser != null && !oldParser.getClass().equals(parserClass)) {
                        throw new RuntimeException(
                                String.format("Different parsers claim, to interpret format %s:" + " %s and %s",
                                        format, oldParser.getClass().getCanonicalName(), parserName));
                    }
                    this.parsers.put(format, parser);
                }
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("Failed to discover parsers", ex);
    }

}

From source file:uniol.apt.io.renderer.AbstractRenderers.java

/**
 * Constructor//from  w w  w  .  j a  va  2  s  .c  o m
 *
 * @param clazz Class object of the which the renderers should render
 */
@SuppressWarnings("unchecked") // I hate type erasure and other java things ...
protected AbstractRenderers(Class<T> clazz) {
    this.renderers = new HashMap<>();

    String className = clazz.getCanonicalName();

    ClassLoader cl = getClass().getClassLoader();
    try {
        Enumeration<URL> rendererNames = cl.getResources(
                "META-INF/uniol/apt/compiler/" + Renderer.class.getCanonicalName() + "/" + className);

        while (rendererNames.hasMoreElements()) {
            try (InputStream is = rendererNames.nextElement().openStream()) {
                LineIterator lIter = IOUtils.lineIterator(is, "UTF-8");
                while (lIter.hasNext()) {
                    String rendererName = lIter.next();
                    Class<? extends Renderer<T>> rendererClass;
                    try {
                        rendererClass = (Class<? extends Renderer<T>>) cl.loadClass(rendererName);
                    } catch (ClassNotFoundException ex) {
                        throw new RuntimeException(String.format("Could not load class %s", rendererName), ex);
                    }
                    Renderer<T> renderer;
                    try {
                        renderer = rendererClass.newInstance();
                    } catch (ClassCastException | IllegalAccessException | InstantiationException ex) {
                        throw new RuntimeException(String.format("Could not instantiate %s", rendererName), ex);
                    }
                    String format = renderer.getFormat();
                    if (format == null || format.equals("") || !format.equals(format.toLowerCase())) {
                        throw new RuntimeException(String.format("Renderer %s reports an invalid format: %s",
                                rendererName, format));
                    }
                    Renderer<T> oldRenderer = this.renderers.get(format);
                    if (oldRenderer != null && !oldRenderer.getClass().equals(rendererClass)) {
                        throw new RuntimeException(String.format(
                                "Different renderers claim, to interpret format %s:" + " %s and %s", format,
                                oldRenderer.getClass().getCanonicalName(), rendererName));
                    }
                    this.renderers.put(format, renderer);
                }
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("Failed to discover renderers", ex);
    }

}