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.facebook.buck.apple.project_generator.NewNativeTargetProjectMutator.java

private String generateXcodeShellScript(TargetNode<?, ?> targetNode) {
    Preconditions.checkArgument(targetNode.getConstructorArg() instanceof ReactNativeLibraryArgs);

    ST template;//from ww  w  .  j a v a  2s  .  co  m
    try {
        template = new ST(Resources.toString(
                Resources.getResource(NewNativeTargetProjectMutator.class, REACT_NATIVE_PACKAGE_TEMPLATE),
                Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading 'rn_package.st' template", e);
    }

    ReactNativeLibraryArgs args = (ReactNativeLibraryArgs) targetNode.getConstructorArg();

    template.add("bundle_name", args.bundleName);

    ProjectFilesystem filesystem = targetNode.getFilesystem();
    BuildTarget buildTarget = targetNode.getBuildTarget();
    Path jsOutput = ReactNativeBundle.getPathToJSBundleDir(buildTarget, filesystem).resolve(args.bundleName);
    template.add("built_bundle_path", filesystem.resolve(jsOutput));

    Path resourceOutput = ReactNativeBundle.getPathToResources(buildTarget, filesystem);
    template.add("built_resources_path", filesystem.resolve(resourceOutput));

    Path sourceMap = ReactNativeBundle.getPathToSourceMap(buildTarget, filesystem);
    template.add("built_source_map_path", filesystem.resolve(sourceMap));

    return template.render();
}

From source file:com.facebook.buck.apple.ProjectGenerator.java

private String getCodesignShellScript(TargetNode<?> targetNode) {
    ST template;//w ww .  ja  va  2  s.co  m
    try {
        template = new ST(Resources.toString(Resources.getResource(ProjectGenerator.class, CODESIGN_TEMPLATE),
                Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading '" + CODESIGN_TEMPLATE + "' template", e);
    }

    Optional<String> productName = getProductNameForTargetNode(targetNode);
    String binaryName = AppleBundle.getBinaryName(targetToBuildWithBuck.get(), productName);
    Path bundleDestination = getScratchPathForAppBundle(targetToBuildWithBuck.get(), binaryName);
    Path resolvedBundleDestination = projectFilesystem.resolve(bundleDestination);

    template.add("root_path", projectFilesystem.getRootPath());
    template.add("path_to_codesign_script", getCodesignScriptPath(projectFilesystem));
    template.add("app_bundle_path", resolvedBundleDestination);

    return template.render();
}

From source file:com.facebook.buck.apple.project_generator.ProjectGenerator.java

private String getCodesignShellScript(TargetNode<?, ?> targetNode) {
    ST template;//from w  w w  . j a  va  2 s  . c  om
    try {
        template = new ST(Resources.toString(Resources.getResource(ProjectGenerator.class, CODESIGN_TEMPLATE),
                Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading '" + CODESIGN_TEMPLATE + "' template", e);
    }

    Optional<String> productName = getProductNameForTargetNode(targetNode);
    String binaryName = AppleBundle.getBinaryName(targetNode.getBuildTarget(), productName);
    Path bundleDestination = getScratchPathForAppBundle(projectFilesystem, targetNode.getBuildTarget(),
            binaryName);
    Path resolvedBundleDestination = projectFilesystem.resolve(bundleDestination);

    template.add("root_path", projectFilesystem.getRootPath());
    template.add("path_to_codesign_script", getCodesignScriptPath(projectFilesystem));
    template.add("app_bundle_path", resolvedBundleDestination);

    return template.render();
}

From source file:com.facebook.buck.features.apple.project.NewNativeTargetProjectMutator.java

private String generateXcodeShellScriptForJsBundle(TargetNode<?> targetNode,
        Function<? super TargetNode<?>, BuildRuleResolver> buildRuleResolverForNode) {
    Preconditions.checkArgument(targetNode.getDescription() instanceof JsBundleOutputsDescription);

    ST template;/* ww w.  j av  a 2s.co  m*/
    try {
        template = new ST(Resources.toString(
                Resources.getResource(NewNativeTargetProjectMutator.class, JS_BUNDLE_TEMPLATE),
                Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException(
                String.format("There was an error loading '%s' template", JS_BUNDLE_TEMPLATE), e);
    }

    BuildRuleResolver resolver = buildRuleResolverForNode.apply(targetNode);
    BuildRule rule = resolver.getRule(targetNode.getBuildTarget());

    Preconditions.checkState(rule instanceof JsBundleOutputs);
    JsBundleOutputs bundle = (JsBundleOutputs) rule;

    SourcePath jsOutput = bundle.getSourcePathToOutput();
    SourcePath resOutput = bundle.getSourcePathToResources();
    SourcePathResolver sourcePathResolver = DefaultSourcePathResolver.from(new SourcePathRuleFinder(resolver));

    template.add("built_bundle_path", sourcePathResolver.getAbsolutePath(jsOutput));
    template.add("built_resources_path", sourcePathResolver.getAbsolutePath(resOutput));

    return template.render();
}

From source file:eu.citadel.converter.transform.CitadelJsonTransform.java

/**
 * Validate the JSON/* ww w  . j a  v a 2s  . com*/
 * @param json the provided JSON to validate using the predefined schema
 * @throws TransformException if not valid, an exception will be thrown
 */
private static void validateJson(String json) throws TransformException {
    Logger logger = LoggerFactory.getLogger(CitadelJsonTransform.class.getName() + ".validateJson");
    logger.trace("validateJson(String) - start");
    try {
        URL url = CitadelJsonTransform.class.getResource("/data/datatype/citadel_general_schema.json");
        final JsonNode schemaJson = JsonLoader.fromString(Resources.toString(url, Charsets.UTF_8));
        final JsonNode generatedJson = JsonLoader.fromString(json);
        final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        final JsonSchema schema = factory.getJsonSchema(schemaJson);

        ProcessingReport report = schema.validate(generatedJson);
        if (!report.isSuccess()) {
            String message = report.toString();
            for (Iterator<ProcessingMessage> i = report.iterator(); i.hasNext();) {
                ProcessingMessage processingMessage = i.next();
                JsonNode jsonMessage = processingMessage.asJson();
                message = processingMessage.getMessage();
                if (jsonMessage.get("instance") != null) {
                    if (jsonMessage.get("instance").get("pointer") != null) {
                        message = message + " in " + jsonMessage.get("instance").get("pointer").toString();
                    } else {
                        message = message + " in " + jsonMessage.get("instance").toString();
                    }
                }

            }
            logger.error("validateJson(String) - not valid: {}", message);
            throw new TransformException(message);
        }
        logger.debug("validateJson(String) - end");
    } catch (IOException e) {
        logger.error("validateJson(String) - IOException: {}", e);
        throw new TransformException(e.getMessage());
    } catch (ProcessingException e) {
        logger.error("validateJson(String) - ProcessingException: {}", e);
        throw new TransformException(e.getMessage());
    }
}

From source file:models.ModelHome.java

/**
 * General factory method for creating Sharp Java instance of a particular class. Loads the JSON
 * from a file with a name corresponding to the Class name (e.g., for ParameterList, would load file
 * /public/data/ParameterList.json./*from w  w  w .  j ava 2  s  .c  o m*/
 *
 * Currently only files for TemplateList and ParameterList.
 */
public static <M> M createJavaInstanceFromJsonFile(final Class<M> aClass)
        throws ModelDataFileNotFoundException, ConvertJsonToJavaException {
    String jsonDataResourcePath = jsonFileForClass(aClass);
    System.out.println("jsonDataResourcePath = " + jsonDataResourcePath);

    final URL urlParameters = Resources.getResource(jsonDataResourcePath);

    final String jsonText;
    try {
        jsonText = Resources.toString(urlParameters, Charsets.UTF_8);
    } catch (IOException e) {
        e.printStackTrace();
        throw new ModelDataFileNotFoundException(e);
    }
    try {
        JsonNode jsonNode = Json.parse(jsonText);
        //            System.out.println( "fetched value (json) = |" + jsonValue + "|" );

        Object o;
        o = Json.fromJson(jsonNode, aClass);

        return (M) o;
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ConvertJsonToJavaException(ex);
    }
}

From source file:com.google.appinventor.buildserver.Compiler.java

private void loadJsonInfo(ConcurrentMap<String, Set<String>> infoMap, String targetInfo)
        throws IOException, JSONException {
    synchronized (infoMap) {
        if (infoMap.isEmpty()) {
            String buildInfoJson = Resources.toString(Compiler.class.getResource(COMPONENT_BUILD_INFO),
                    Charsets.UTF_8);//from  w w w  .j  a  v  a2 s  . c  o m

            JSONArray componentsArray = new JSONArray(buildInfoJson);
            int componentsLength = componentsArray.length();
            for (int componentsIndex = 0; componentsIndex < componentsLength; componentsIndex++) {
                JSONObject componentObject = componentsArray.getJSONObject(componentsIndex);
                String name = componentObject.getString("name");

                Set<String> infoGroupForThisComponent = Sets.newHashSet();

                JSONArray infoArray = componentObject.getJSONArray(targetInfo);
                int infoLength = infoArray.length();
                for (int infoIndex = 0; infoIndex < infoLength; infoIndex++) {
                    String info = infoArray.getString(infoIndex);
                    infoGroupForThisComponent.add(info);
                }

                infoMap.put(name, infoGroupForThisComponent);
            }
        }
    }
}

From source file:blusunrize.immersiveengineering.common.util.Utils.java

public static LootTable loadBuiltinLootTable(ResourceLocation resource, LootTableManager lootTableManager) {
    URL url = Utils.class
            .getResource("/assets/" + resource.getNamespace() + "/loot_tables/" + resource.getPath() + ".json");
    if (url == null)
        return LootTable.EMPTY_LOOT_TABLE;
    else {//from   w  w w .  j  a  va2  s .  c  o m
        String s;
        try {
            s = Resources.toString(url, Charsets.UTF_8);
        } catch (IOException ioexception) {
            //            IELogger.warn(("Failed to load loot table " + resource.toString() + " from " + url.toString()));
            ioexception.printStackTrace();
            return LootTable.EMPTY_LOOT_TABLE;
        }

        try {
            return net.minecraftforge.common.ForgeHooks.loadLootTable(GSON_INSTANCE, resource, s, false,
                    lootTableManager);
        } catch (JsonParseException jsonparseexception) {
            //            IELogger.error(("Failed to load loot table " + resource.toString() + " from " + url.toString()));
            jsonparseexception.printStackTrace();
            return LootTable.EMPTY_LOOT_TABLE;
        }
    }
}