Google Guava Resources getResource(Class<?> contextClass, String resourceName)

PreviousNext

Google Guava Resources getResource(Class<?> contextClass, String resourceName) Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.

Syntax

The method getResource() from Resources is declared as:

@CanIgnoreReturnValue 
public static URL getResource(Class<?> contextClass, String resourceName)

Parameter

The method getResource() has the following parameter:

  • Class contextClass -
  • String resourceName -

Return

The method getResource() returns

Exception

The method getResource() throws the following exceptions:

  • IllegalArgumentException - if the resource is not found

Example

The following code shows how to use Google Guava Resources getResource(Class<?> contextClass, String resourceName)

Example 1

import java.io.ByteArrayOutputStream;
import java.util.Map;

import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;

import com.google.common.io.Resources;

public class XContentTests {
    public static void main(String[] args) throws Exception {
        byte[] jsonResource = Resources
                .toByteArray(Resources.getResource(XContentTests.class, "post_cluster_by_url.json"));

        XContentParser parser = XContentFactory.xContent(jsonResource).createParser(jsonResource);
        Map<String, Object> mapOrderedAndClose = parser.mapOrderedAndClose();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XContentFactory.smileBuilder(baos).map(mapOrderedAndClose).close();

        System.out.println("Out:");
        System.out.println(new String(baos.toByteArray()));
    }//    w w w   .  d e  m   o  2   s . c   o   m
}

Example 2

import com.google.common.io.Resources;
import io.immutables.lang.SyntaxProductions;
import io.immutables.lang.SyntaxTerms;
import io.immutables.lang.SyntaxTrees.Unit;
import java.nio.charset.StandardCharsets;

public class Imc {
    public static void main(String... args) throws Exception {
        String sourceName = "/io/immutables/lang/fixture/debug.im";

        String content = Resources.toString(Resources.getResource(Imc.class, sourceName), StandardCharsets.UTF_8);

        SyntaxTerms terms = SyntaxTerms.from(content.toCharArray());
        SyntaxProductions<Unit> productions = SyntaxProductions.unit(terms);
        System.out.println(productions.show());
        if (productions.ok()) {
            System.out.println(productions.construct());
        } else {//   w  w  w  . d  e    m  o2   s   .  c o   m
            System.out.println(productions.messageForFile(sourceName));
            //System.out.println(productions.show());
        }
    }
}

Example 3

import java.io.ByteArrayOutputStream;

import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import com.google.common.io.Resources;

public class XContentTests {
    public static void main(String[] args) throws Exception {
        byte[] jsonResource = Resources
                .toByteArray(Resources.getResource(XContentTests.class, "post_cluster_by_url.json"));

        XContent xcontent = XContentFactory.xContent(jsonResource);
        XContentType type = XContentType.YAML; // xcontent.type();
        XContentParser parser = xcontent.createParser(jsonResource);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XContentBuilder builder = XContentFactory.contentBuilder(type, baos).copyCurrentStructure(parser);
        builder.close();/* w   w w .   d  e m   o  2   s  .c    o m */

        System.out.println("Out:");
        System.out.println(new String(baos.toByteArray()));
    }
}
PreviousNext

Related