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:org.fao.geonet.kernel.search.JSONLocCacheLoader.java

/**
 * Populate the cache by loading all JSON files.
 *///  ww w.j  a  v  a 2s.com
@SuppressWarnings("unchecked")
private void addJSONLocalizationFile(Map<String, String> translation, URL file)
        throws IOException, JDOMException {
    if (file != null) {
        try {
            JSONObject json = new JSONObject(Resources.toString(file, Constants.CHARSET));

            Iterator<String> keys = json.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                translation.put(key, json.getString(key));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

From source file:common.InitialData.java

static TakepublishedRecord take(DSLContext dsl, Time time, int user, String title, String factTitleSlug)
        throws IOException {
    TakepublishedRecord record = takeInternal(dsl, time, user, title);
    String jsonData = Resources.toString(
            Resources.getResource("initialdata/" + record.getTitleSlug() + ".json"), StandardCharsets.UTF_8);
    record.setBlocks(jsonData);/*w w w .  jav  a  2s  . com*/
    record.setImageUrl(factTitleSlug);
    record.insert();
    return record;
}

From source file:com.google.appinventor.components.scripts.LangDefXmlGenerator.java

private static String fileToString(String filename) throws IOException {
    return Resources.toString(LangDefXmlGenerator.class.getResource(TEMPLATE_PATH + filename),
            Charsets.US_ASCII);/* www .  java  2  s .c  om*/
}

From source file:org.novelang.outfit.shell.AgentFileInstaller.java

private AgentFileInstaller() throws IOException {

    final String versionOverride = System.getProperty(VERSIONOVERRIDE_SYSTEMPROPERTYNAME);
    if (versionOverride == null) {
        final URL versionResource = AgentFileInstaller.class.getResource(VERSION_RESOURCE_NAME);
        if (versionResource == null) {
            throw new IllegalStateException("Couldn't find resource for '" + VERSION_RESOURCE_NAME + "' "
                    + "(when building with Maven, this may be caused by Insider jar not present "
                    + "in the repository for current version)");
        }/*from   w  ww .j  a  v  a 2s. c o  m*/
        version = Resources.toString(versionResource, Charsets.UTF_8);
        LOGGER.info("Using version '", version, "' as found inside ", "'", VERSION_RESOURCE_NAME,
                "' resource.");

    } else {
        version = versionOverride;
        LOGGER.info("Using version override '", version, "' from system property ", "'",
                VERSIONOVERRIDE_SYSTEMPROPERTYNAME, "'.");
    }

    final String jarFileNameFromSystemProperty = System.getProperty(AGENTJARFILE_SYSTEMPROPERTYNAME);

    if (jarFileNameFromSystemProperty == null) {
        try {
            jarFile = File.createTempFile("Novelang-insider-agent", ".jar").getCanonicalFile();
            copyResourceToFile(JAR_RESOURCE_NAME_RADIX + version + ".jar", jarFile);
            LOGGER.info("Using jar file '", jarFile.getAbsolutePath(), "'.");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        jarFile = resolveWithVersion(jarFileNameFromSystemProperty);
        if (!jarFile.isFile()) {
            throw new IllegalArgumentException(
                    "Jar file '" + jarFile.getAbsolutePath() + "' doesn't exist as a file");
        }
        LOGGER.info("Using jar file '", jarFile.getAbsolutePath(), "' ", "set from system property '",
                AGENTJARFILE_SYSTEMPROPERTYNAME, "'.");
    }

}

From source file:com.optimizely.ab.event.internal.serializer.SerializerTestUtils.java

static String generateImpressionJson() throws IOException {
    String impressionJson = Resources.toString(Resources.getResource("serializer/impression.json"),
            Charsets.UTF_8);/*from w  ww .j  a va 2s .co m*/
    return impressionJson.replaceAll("\\s+", "");
}

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

@Override
public String readContent(String location) {
    try {/*from w  w  w  . j  av  a  2 s  .  co  m*/
        return Resources.toString(new URL(ZkWorkItemRepositoryFactory.SCHEME + ":" + location), Charsets.UTF_8);
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    }
}

From source file:com.nesscomputing.migratory.loader.JarLoader.java

/**
 * This method loads a file from a file:/... URI. This is probably not what you are looking for.
 *///from  w  ww  .ja v  a 2s  .  c o m
@Override
public String loadFile(final URI fileUri) throws IOException {
    return Resources.toString(fileUri.toURL(), charset);
}

From source file:com.twosigma.beaker.clojure.ClojureEvaluator.java

private String initScriptSource() throws IOException {
    URL url = this.getClass().getClassLoader().getResource("init_clojure_script.txt");
    return Resources.toString(url, Charsets.UTF_8);
}

From source file:co.jirm.core.util.ResourceUtils.java

private static String _getClasspathResourceAsString(String path) throws IOException {
    return Resources.toString(Resources.getResource(path), Charsets.UTF_8);
}

From source file:com.facebook.buck.java.intellij.IjProjectWriter.java

private static ST getST(StringTemplateFile file) throws IOException {
    URL templateUrl = Resources.getResource(IjProjectWriter.class, file.getFileName());
    String template = Resources.toString(templateUrl, StandardCharsets.UTF_8);
    return new ST(template, DELIMITER, DELIMITER);
}