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

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

Introduction

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

Prototype

public static String toString(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all characters from a URL into a String , using the given character set.

Usage

From source file:com.redspr.redrobot.WebDriverRobot.java

public void init(WebDriver webDriver2) {
    this.webDriver = webDriver2;

    URL url = getClass().getResource("/redRobotCore.js");

    try {//from   ww w .  j a va  2s .c o  m
        SCRIPT = Resources.toString(url, Charsets.UTF_8);
    } catch (IOException ex) {
        throw new RuntimeException("Unable to read script", ex);
    }
}

From source file:com.danhaywood.java.testsupport.dbunit.DbUnitRule.java

public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {

    return new Statement() {

        @Override//  ww w . j  av  a2s .c  o m
        public void evaluate() throws Throwable {

            try {
                Ddl ddl = method.getAnnotation(Ddl.class);
                if (ddl != null) {
                    String[] values = ddl.value();
                    for (String value : values) {
                        executeUpdate(
                                Resources.toString(resourceBase.getResource(value), Charset.defaultCharset()));
                    }
                }

                JsonData data = method.getAnnotation(JsonData.class);
                if (data != null) {
                    IDataSet ds = new JsonDataSet(resourceBase.getResourceAsStream(data.value()));
                    databaseTester.setDataSet(ds);
                }

                databaseTester.onSetup();

                base.evaluate();
            } finally {
                databaseTester.onTearDown();
            }
        }
    };
}

From source file:fg.sonar.plugins.secaudit.rules.SecAuditRulesDefinition.java

private static String readRuleMetadata(String fileName) {
    URL resource = SecAuditRulesDefinition.class.getResource(RULE_RESOURCE_PATH + "/" + fileName);
    if (resource == null) {
        return null;
    }/*from ww w. j a  v a 2s  .c  o  m*/
    try {
        return Resources.toString(resource, Charsets.UTF_8);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to read: " + resource, e);
    }
}

From source file:ro.cosu.vampires.server.resources.ec2.EC2Resource.java

@Override
public void onStart() throws Exception {

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest();

    amazonEC2Client.setEndpoint(String.format("ec2.%1s.amazonaws.com", parameters.region()));

    URL cloudInitResource = Resources.getResource("cloud_init.yaml");
    String cloudInit = Resources.toString(cloudInitResource, Charsets.UTF_8);

    String command = parameters.command() + " " + parameters.serverId() + " " + parameters().id();

    cloudInit = cloudInit.replace("$command", command);
    LOG.debug("EC2command:  {}", command);

    RunInstancesRequest request = runInstancesRequest.withImageId(parameters.imageId())
            .withInstanceType(parameters.instanceType()).withMinCount(1).withMaxCount(1)
            .withKeyName(parameters.keyName()).withSecurityGroups(parameters.securityGroup())
            .withUserData(Base64.encodeBase64String(cloudInit.getBytes(Charsets.UTF_8)));

    instanceId = amazonEC2Client.runInstances(request).getReservation().getInstances().get(0).getInstanceId();

    LOG.debug("Started amazon instance {}", instanceId);

    CreateTagsRequest createTagsRequest = new CreateTagsRequest();

    createTagsRequest.withResources(instanceId).withTags(new Tag("providerType", "vampires"));

    amazonEC2Client.createTags(createTagsRequest);

    DescribeInstancesRequest describeRequest = new DescribeInstancesRequest();

    describeRequest.withInstanceIds(instanceId);

    String publicDnsName = getPublicDnsName(describeRequest);

    LOG.info("instance {} : {}", instanceId, publicDnsName);
}

From source file:no.ssb.vtl.test.junit.GrammarRule.java

private void before() {
    try {//from  w  w w . j a v a  2  s.c  o  m
        if (grammarURL == null) {
            try {
                Class<?> vtlParserClass = ClassLoader.getSystemClassLoader()
                        .loadClass("no.ssb.vtl.parser.VTLParser");
                grammarURL = Resources.getResource(vtlParserClass, "VTL.g4");
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(
                        "could not find no.ssb.vtl.parser.VTLParser.class, did you compile antlr project?");
            }
        }
        String grammarString = Resources.toString(grammarURL, Charset.defaultCharset());
        grammar = new Grammar(grammarString);
    } catch (IOException | RecognitionException e) {
        throw new IllegalArgumentException("could not create grammar", e);
    }
}

From source file:org.jsfr.json.BenchmarkParseLongText.java

@Setup
public void setup() throws Exception {
    gsonSurfer = JsonSurfer.gson();//from w w w.  j  ava 2  s  .  co  m
    jacksonSurfer = JsonSurfer.jackson();
    simpleSurfer = JsonSurfer.simple();
    collectOneListener = new CollectOneListener(true);
    surfingConfiguration = SurfingConfiguration.builder().bind("$.findMe", collectOneListener).build();
    gson = new GsonBuilder().create();
    om = new ObjectMapper();
    json = Resources.toString(Resources.getResource("longText.json"), StandardCharsets.UTF_8);
}

From source file:io.fabric8.partition.internal.repositories.ProfileWorkItemRepository.java

@Override
public String readContent(String location) {
    try {/* ww  w . j a  v a  2 s.c om*/
        return Resources.toString(new URL(ProfileWorkItemRepositoryFactory.SCHME + ":" + location),
                Charsets.UTF_8);
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    }
}

From source file:com.shazam.fork.summary.HtmlSummaryPrinter.java

private void generateCssFromLess() {
    try {/*from w  ww  . jav  a 2  s .co  m*/
        LessCompiler compiler = new LessCompiler();
        String less = Resources.toString(getClass().getResource("/spoon.less"), UTF_8);
        String css = compiler.compile(less);
        File cssFile = new File(staticOutput, "spoon.css");
        writeStringToFile(cssFile, css);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.sk89q.worldedit.world.registry.LegacyMapper.java

/**
 * Attempt to load the data from file.//from   w  w  w  . j av  a 2  s .  c om
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    URL url = LegacyMapper.class.getResource("legacy.json");
    if (url == null) {
        throw new IOException("Could not find legacy.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
    }.getType());

    ParserContext parserContext = new ParserContext();
    parserContext.setPreferringWildcard(false);
    parserContext.setRestricted(false);
    parserContext.setTryLegacy(false); // This is legacy. Don't match itself.

    for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
        try {
            String id = blockEntry.getKey();
            BlockState state = WorldEdit.getInstance().getBlockFactory()
                    .parseFromInput(blockEntry.getValue(), parserContext).toImmutableState();
            blockToStringMap.put(state, id);
            stringToBlockMap.put(id, state);
        } catch (Exception e) {
            log.warn("Unknown block: " + blockEntry.getValue());
        }
    }

    for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
        try {
            String id = itemEntry.getKey();
            ItemType type = ItemTypes.get(itemEntry.getValue());
            checkNotNull(type);
            itemToStringMap.put(type, id);
            stringToItemMap.put(id, type);
        } catch (Exception e) {
            log.warn("Unknown item: " + itemEntry.getValue());
        }
    }
}

From source file:com.sk89q.worldedit.world.registry.BundledBlockData.java

/**
 * Attempt to load the data from file.//from   w ww . j  a  v a2s  .c  o  m
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector.class, new VectorAdapter());
    Gson gson = gsonBuilder.create();
    URL url = BundledBlockData.class.getResource("blocks.json");
    if (url == null) {
        throw new IOException("Could not find blocks.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {
    }.getType());

    for (BlockEntry entry : entries) {
        entry.postDeserialization();
        idMap.put(entry.id, entry);
        legacyMap.put(entry.legacyId, entry);
    }
}