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(Class<?> contextClass, String resourceName) 

Source Link

Document

Given a resourceName that is relative to contextClass , returns a URL pointing to the named resource.

Usage

From source file:se.softhouse.common.testlib.ResourceLoader.java

/**
 * Loads a file and reads it as an {@link com.google.common.base.Charsets#UTF_8 UTF_8} file.
 * {@code resourceName} must start with a "/"
 * //www  . jav a 2  s  .c  o  m
 * @return the file as a string
 * @throws IllegalArgumentException if {@code resourceName} can't be found/loaded
 */
public static String get(String resourceName) {
    URL resourcePath = Resources.getResource(ResourceLoader.class, resourceName);
    String fileContent = null;
    try {
        fileContent = Resources.toString(resourcePath, UTF_8);
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to load: " + resourcePath, e);
    }
    return fileContent;
}

From source file:org.apache.isis.viewer.restfulobjects.applib.JsonFixture.java

public static JsonNode readJson(final String resourceName)
        throws JsonParseException, JsonMappingException, IOException {
    return JsonMapper.instance().read(
            Resources.toString(Resources.getResource(JsonFixture.class, resourceName), Charsets.UTF_8),
            JsonNode.class);
}

From source file:org.apache.provisionr.core.Mustache.java

public static String toString(Class<?> contextClass, String resource, Map<String, ?> scopes)
        throws IOException {
    return toString(Resources.getResource(contextClass, resource), scopes);
}

From source file:google.registry.rde.RdeTestData.java

private static URL getUrl(String filename) {
    return Resources.getResource(RdeTestData.class, "testdata/" + filename);
}

From source file:google.registry.rde.imports.RdeImportsTestData.java

private static URL getUrl(String filename) {
    return Resources.getResource(RdeImportsTestData.class, "testdata/" + filename);
}

From source file:com.google.errorprone.bugpatterns.apidiff.Java7ApiChecker.java

private static ApiDiff loadApiDiff() {
    try {/*from  w  w w .j  a  v  a 2 s. c  o  m*/
        ApiDiffProto.Diff.Builder diffBuilder = ApiDiffProto.Diff.newBuilder();
        byte[] diffData = Resources
                .toByteArray(Resources.getResource(Java7ApiChecker.class, "7to8diff.binarypb"));
        diffBuilder.mergeFrom(diffData)
                .addClassDiff(ApiDiffProto.ClassDiff.newBuilder()
                        .setMemberDiff(ApiDiffProto.MemberDiff.newBuilder()
                                .setClassName("com/google/common/base/Predicate")
                                .addMember(ApiDiffProto.ClassMember.newBuilder().setIdentifier("test")
                                        .setMemberDescriptor("(Ljava/lang/Object;)Z"))))
                .addClassDiff(ApiDiffProto.ClassDiff.newBuilder()
                        .setMemberDiff(ApiDiffProto.MemberDiff.newBuilder()
                                .setClassName("com/google/common/base/BinaryPredicate")
                                .addMember(ApiDiffProto.ClassMember.newBuilder().setIdentifier("test")
                                        .setMemberDescriptor("(Ljava/lang/Object;Ljava/lang/Object;)Z"))));
        return ApiDiff.fromProto(diffBuilder.build());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:kmworks.dsltools.util.xml.XSLTransformerFactory.java

public static XSLTransform getTransform(String transformName) {
    XSLTransform result = INSTANCE.cache.get(transformName);
    if (result == null) {
        String xslResource = transformName + ".xsl";
        try (final InputStream stream = Resources.getResource(XSLTransformerFactory.class, xslResource)
                .openStream()) {/*from w w w  .j  a v  a 2s  .  c o  m*/
            Builder builder = new Builder();
            try {
                Document stylesheet = builder.build(stream);
                result = new XSLTransform(stylesheet);
            } catch (ParsingException | IOException | XSLException ex) {
                throw new RuntimeException("Error building XSLTransformer: " + ex.getMessage());
            }
        } catch (IOException ex) {
            throw new RuntimeException("XSLTransform-source not found: " + xslResource);
        }
        INSTANCE.cache.put(transformName, result);
    }
    return result;
}

From source file:org.openqa.selenium.server.browserlaunchers.ResourceExtractor.java

public static File extractResourcePath(Class cl, String resourcePath, File dest) throws IOException {
    boolean alwaysExtract = true;

    URL url = Resources.getResource(cl, resourcePath);
    if ("jar".equalsIgnoreCase(url.getProtocol())) {
        File jarFile = getJarFileFromUrl(url);
        extractResourcePathFromJar(cl, jarFile, resourcePath, dest);
    } else {// www.j a  v  a 2s  .  c o  m
        try {
            File resourceFile = new File(new URI(url.toExternalForm()));
            if (!alwaysExtract) {
                return resourceFile;
            }
            if (resourceFile.isDirectory()) {
                LauncherUtils.copyDirectory(resourceFile, dest);
            } else {
                FileHandler.copy(resourceFile, dest);
            }
        } catch (URISyntaxException e) {
            throw new RuntimeException("Couldn't convert URL to File:" + url, e);
        }
    }
    return dest;
}

From source file:uk.ac.susx.tag.method51.text.StopwordService.java

/**
 * load stop words from specified source
 *
 *///from   w ww .j ava 2s . c  o m
private static ImmutableSet<String> load(String source) {
    Set<String> set = new HashSet<>();
    source = "stopwords/" + source;
    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(Resources.getResource(StopwordService.class, source).openStream()));) {
        String line;
        while ((line = reader.readLine()) != null) {
            set.add(line.trim());
        }
        return new ImmutableSet<>(set);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.sonar.server.issue.actionplan.ActionPlanWs.java

private static void defineSearchAction(NewController controller) {
    WebService.NewAction action = controller.createAction("search")
            .setDescription("Get a list of action plans. Requires Browse permission on project").setSince("3.6")
            .setHandler(RailsHandler.INSTANCE)
            .setResponseExample(Resources.getResource(ActionPlanWs.class, "example-search.json"));
    addProjectParam(action);// w  ww  .  j a va  2s  .c om
    addFormatParam(action);
}