Example usage for com.google.common.io Resources getResource

List of usage examples for com.google.common.io Resources getResource

Introduction

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

Prototype

public static URL getResource(String resourceName) 

Source Link

Document

Returns a URL pointing to resourceName if the resource is found using the Thread#getContextClassLoader() context class loader .

Usage

From source file:com.fractal.facebooksentiment.processor.FacebookSentimentCalculator.java

private FacebookSentimentCalculator() {
    commentSentimentMap = Maps.newTreeMap();
    afinnSentimentMap = Maps.newTreeMap();

    try {/*from ww  w .ja v  a  2s .  co  m*/
        final URL url = Resources.getResource(Constants.AFINN_SENTIMENT_FILE_NAME);
        final String text = Resources.toString(url, Charsets.UTF_8);
        final Iterable<String> lineSplit = Splitter.on("\n").trimResults().omitEmptyStrings().split(text);
        List<String> tabSplit;
        for (final String str : lineSplit) {
            tabSplit = Lists.newArrayList(Splitter.on("\t").trimResults().omitEmptyStrings().split(str));
            afinnSentimentMap.put(tabSplit.get(0), Integer.parseInt(tabSplit.get(1)));
        }
    } catch (final IOException ioException) {
        ioException.printStackTrace();
        // Should not occur. If it occurs, we cant continue. So, exiting at this point itself.
        // System.exit(1);
    }
}

From source file:com.streamsets.datacollector.dev.standalone.DevPipelineRunIT.java

@Override
protected String getPipelineJson() throws Exception {
    URI uri = Resources.getResource("dev_pipeline_run.json").toURI();
    return new String(Files.readAllBytes(Paths.get(uri)), StandardCharsets.UTF_8);
}

From source file:org.cloudifysource.cosmo.utils.ResourceExtractor.java

/**
 * Extracts `absoluteResourcePath` to `target`. This method assumes the JAR containing the package
 * is the same JAR this class belongs to
 * @param absoluteResourcePath The package to extract.
 * @param target The target to extract files to.
 */// w w w .j a va 2 s .  c o m
public static void extractResource(String absoluteResourcePath, final Path target) throws IOException {
    URL resource = Resources.getResource(absoluteResourcePath);
    extractResource(absoluteResourcePath, target, resource);
}

From source file:ec.util.jdbc.SqlKeywords.java

private static Set<String> loadWords(String resourceName) {
    try {//from   w w w .j  a  v  a  2  s.co m
        Set<String> result = new HashSet<>();
        for (String o : Resources.readLines(Resources.getResource(resourceName), StandardCharsets.UTF_8)) {
            result.add(o);
        }
        return Collections.unmodifiableSet(result);
    } catch (IOException ex) {
        throw new RuntimeException("Missing resource '" + resourceName + "'", ex);
    }
}

From source file:org.anarres.dblx.core.model.ModelLoader.java

@Nonnull
public Model load() throws IOException {
    Model model = new Model(modelName);

    Splitter splitter = Splitter.on(CharMatcher.BREAKING_WHITESPACE);

    NODES: {//from   www.j av  a2 s.c o m
        URL url = Resources.getResource("models/" + modelName + "/nodes.csv");
        CharSource source = Resources.asCharSource(url, StandardCharsets.UTF_8);
        try (Reader in = source.openBufferedStream()) {
            CSVReader reader = newSVReader(in, '\t', 1);
            for (String[] line : reader) {
                String name = line[0];
                long x = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[1]));
                long y = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[2]));
                long z = (long) LengthUnit.INCH.toMillimetres(Double.parseDouble(line[3]));
                List<String> tags = splitter.splitToList(line[4]);
                model.addNode(new Node(name, x, y, z, tags));
            }
        }
    }

    BARS: {
        URL url = Resources.getResource("models/" + modelName + "/bars.csv");
        CharSource source = Resources.asCharSource(url, StandardCharsets.UTF_8);
        try (Reader in = source.openBufferedStream()) {
            CSVReader reader = newSVReader(in, '\t', 1);
            for (String[] line : reader) {
                List<String> tags = splitter.splitToList(line[2]);
                Bar bar = new Bar(line[0], line[1], tags);
                model.addEdge(bar);
            }
        }
    }

    return model;
}

From source file:com.opengamma.collect.io.ResourceLocator.java

/**
 * Creates a resource from a string locator.
 * <p>/* w w w.ja  v  a2  s .c om*/
 * This accepts locators starting with 'classpath:' or 'file:'.
 * It also accepts unprefixed locators, treated as 'file:'.
 * 
 * @param locator  the string form of the resource locator
 * @return the resource locator
 */
@FromString
public static ResourceLocator of(String locator) {
    ArgChecker.notNull(locator, "locator");
    try {
        if (locator.startsWith(CLASSPATH_URL_PREFIX)) {
            String urlStr = locator.substring(CLASSPATH_URL_PREFIX.length());
            return ofClasspathUrl(Resources.getResource(urlStr));

        } else if (locator.startsWith(FILE_URL_PREFIX)) {
            String fileStr = locator.substring(FILE_URL_PREFIX.length());
            return ofFile(new File(fileStr));

        } else {
            return ofFile(new File(locator));
        }
    } catch (RuntimeException ex) {
        throw new IllegalArgumentException("Invalid resource locator: " + locator, ex);
    }
}

From source file:kmworks.dsltools.util.PropertiesManager.java

private PropertiesManager() {
    try (final InputStream stream = Resources.getResource("rrd.properties").openStream()) {
        Properties properties = new Properties();
        properties.load(stream);//  w w  w  . j ava2 s . c o m
        rrdOptions = PropertyMapConverter.fromProperties(properties);
    } catch (Exception ex) {
    }
}

From source file:org.springframework.data.cassandra.support.CqlDataSet.java

/**
 * Create a {@link CqlDataSet} from a class-path resource.
 *
 * @param resource/*  ww w.  ja  v a 2  s . c o  m*/
 * @return
 */
public static CqlDataSet fromClassPath(String resource) {

    URL url = Resources.getResource(resource);
    return new CqlDataSet(url, null);
}

From source file:fr.rjoakim.app.checklink.utils.FileResource.java

@Override
public String getContent(String filename) throws FileResourceServiceException {
    if (Strings.isNullOrEmpty(filename)) {
        throw new IllegalArgumentException("filename argument is required.");
    }/*w  w w .  j  av a  2 s  .c o m*/

    try {
        URL url = Resources.getResource(filename);
        return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        throw new FileResourceServiceException(e.getMessage(), e);
    }
}

From source file:io.scigraph.lucene.SynonymMapSupplier.java

@Override
public SynonymMap get() {
    try {/*from w w  w.  j  a v a  2 s. co  m*/
        return Resources.readLines(Resources.getResource("lemmatization.txt"), Charsets.UTF_8,
                new LineProcessor<SynonymMap>() {

                    SynonymMap.Builder builder = new SynonymMap.Builder(true);

                    @Override
                    public boolean processLine(String line) throws IOException {
                        List<String> synonyms = newArrayList(Splitter.on(',').trimResults().split(line));
                        for (String term : synonyms) {
                            for (String synonym : synonyms) {
                                if (!term.equals(synonym)) {
                                    builder.add(new CharsRef(term), new CharsRef(synonym), true);
                                }
                            }
                        }
                        return true;
                    }

                    @Override
                    public SynonymMap getResult() {
                        try {
                            return builder.build();
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to build synonym map", e);
        return null;
    }
}