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.mayocat.theme.internal.DefaultThemeFileResolver.java

@Override
public Template getGlobalTemplate(String name, Optional<Breakpoint> breakpoint)
        throws TemplateNotFoundException {
    try {//from   ww  w .j a  v a 2  s. c o  m
        return new Template(generateTemplateId(name, breakpoint),
                Resources.toString(Resources.getResource("templates/" + name), Charsets.UTF_8), true);
    } catch (IOException e) {
        throw new TemplateNotFoundException();
    }
}

From source file:org.apache.zeppelin.submarine.commons.SubmarineUI.java

public void createSubmarineUI(SubmarineCommand submarineCmd) {
    try {/*w  w  w .  ja v  a2  s .  c  om*/
        HashMap<String, Object> mapParams = new HashMap();
        mapParams.put(unifyKey(SubmarineConstants.PARAGRAPH_ID), intpContext.getParagraphId());
        mapParams.put(unifyKey(SubmarineConstants.COMMAND_TYPE), submarineCmd.getCommand());

        String templateName = "";
        switch (submarineCmd) {
        case USAGE:
            templateName = SUBMARINE_USAGE_JINJA;
            List<CommandlineOption> commandlineOptions = getCommandlineOptions();
            mapParams.put(SubmarineConstants.COMMANDLINE_OPTIONS, commandlineOptions);
            break;
        default:
            templateName = SUBMARINE_DASHBOARD_JINJA;
            break;
        }

        URL urlTemplate = Resources.getResource(templateName);
        String template = Resources.toString(urlTemplate, Charsets.UTF_8);
        Jinjava jinjava = new Jinjava();
        String submarineUsage = jinjava.render(template, mapParams);

        // UI
        InterpreterResultMessageOutput outputUI = intpContext.out.getOutputAt(0);
        outputUI.clear();
        outputUI.write(submarineUsage);
        outputUI.flush();

        // UI update, log needs to be cleaned at the same time
        InterpreterResultMessageOutput outputLOG = intpContext.out.getOutputAt(1);
        outputLOG.clear();
        outputLOG.flush();
    } catch (IOException e) {
        LOGGER.error("Can't print usage", e);
    }
}

From source file:org.eclipse.che.providers.DynaProviderGenerator.java

/**
 * Get the template for provider//  ww  w . j  av a 2s  .  c o m
 * @return the String Template
 */
protected ST getTemplate() {
    if (st == null) {
        URL url = Resources.getResource(DynaProviderGenerator.class, TEMPLATE_PATH);
        try {
            st = new ST(Resources.toString(url, UTF_8));
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to read template", e);
        }
    }
    return st;
}

From source file:org.fundacionjala.enforce.sonarqube.apex.rules.ApexRulesDefinition.java

/**
 * Reads the given resource if it can be, otherwise throws an exception
 *
 * @param resource to read from/*from w w  w.j  av  a  2 s.co m*/
 * @return a String value which results of reading resource.
 */
private static String readResource(URL resource) {
    try {
        return Resources.toString(resource, Charsets.UTF_8);
    } catch (IOException ioException) {
        throw new IllegalStateException(String.format("Failed to read: %s", resource), ioException);
    }
}

From source file:org.eclipse.che.plugin.typescript.dto.TypeScriptDtoGenerator.java

/**
 * Get the template for typescript//from  w w  w  .  j  a v  a  2  s.  c  om
 * @return the String Template
 */
protected ST getTemplate() {
    if (st == null) {
        URL url = Resources.getResource(TypeScriptDtoGenerator.class, TEMPLATE_NAME);
        try {
            st = new ST(Resources.toString(url, UTF_8));
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to read template", e);
        }
    }
    return st;
}

From source file:org.smartdeveloperhub.harvesters.it.frontend.controller.LocalBackendController.java

private LocalData loadLocalData() throws IOException {
    if (!Files.isRegularFile(this.path)) {
        LOGGER.error("Could not find local configuration file '{}'", this.path);
        throw new IOException("Could not find local configuration file '" + this.path + "'");
    }/*  w  w w  . j ava 2 s.  c o  m*/
    try {
        final String content = Resources.toString(this.path.toFile().toURI().toURL(), StandardCharsets.UTF_8);
        return Entities.unmarshallEntity(content, LocalData.class);
    } catch (final IOException e) {
        LOGGER.error("Could not load local configuration file '{}'. Full stacktrace follows", this.path, e);
        throw new IOException("Could not load local configuration file '" + this.path + "'", e);
    }
}

From source file:com.cognifide.qa.bb.aem.dialog.classic.field.image.AemImageSetterHelper.java

private static synchronized String getJavascriptToExecute() throws IOException {
    if (javascriptToExecute == null) {
        URL url = Resources.getResource(IMAGE_SETTER_JS_FILENAME);
        javascriptToExecute = Resources.toString(url, Charsets.UTF_8);
    }//from w w w  .ja  va  2 s  .c  o  m
    return javascriptToExecute;
}

From source file:io.viewserver.server.setup.BootstrapperBase.java

private void setupDatabaseObjects(Connection connection) {
    log.info("Creating database objects");
    ArrayList<String> scripts = new ArrayList<String>();
    try {/* ww w .  j a  v  a2 s  . co m*/
        scripts.add(Resources.toString(Resources.getResource(BootstrapperBase.class, "SetupDatabase.sql"),
                Charset.forName("utf-8")));
    } catch (IOException e) {
        throw new RuntimeException("Could not read database setup script", e);
    }
    scripts.addAll(getDatabaseSetupScripts());
    for (String script : scripts) {
        try (PreparedStatement statement = connection.prepareStatement(script)) {
            statement.execute();
        } catch (Throwable e) {
            throw new RuntimeException("Could not run database setup script", e);
        }
    }
}

From source file:espresso.CsCompiler.java

public static String load(URL url) {
    try {// w w w  .  j a v a2s . c o  m
        return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
        return null;
    }
}

From source file:com.google.api.codegen.config.PackagingConfig.java

public static PackagingConfig loadFromURL(URL url) throws IOException {
    String contents = Resources.toString(url, StandardCharsets.UTF_8);
    return createFromString(contents);
}