List of usage examples for com.google.gwt.dev.util Util readURLAsString
public static String readURLAsString(URL url)
From source file:cc.alcina.framework.entity.gen.SimpleCssResourceGenerator.java
License:Apache License
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { try {//from w w w . j a v a 2s . com ConfigurationProperty cp = context.getGeneratorContext().getPropertyOracle() .getConfigurationProperty(IGNORE_DATA_URLS); logMissingUrlResources = !Boolean.valueOf(cp.getValues().get(0)); } catch (BadPropertyValueException e1) { e1.printStackTrace(); } URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null); throw new UnableToCompleteException(); } URL resource = resources[0]; SourceWriter sw = new StringSourceWriter(); // Write the expression to create the subtype. sw.println("new " + SimpleCssResource.class.getName() + "() {"); sw.indent(); if (!AbstractResourceGenerator.STRIP_COMMENTS) { // Convenience when examining the generated code. sw.println("// " + resource.toExternalForm()); } sw.println("public String getText() {"); sw.indent(); String toWrite = Util.readURLAsString(resource); if (context.supportsDataUrls()) { try { toWrite = replaceWithDataUrls(context, toWrite); } catch (Exception e) { logger.log(Type.ERROR, "css data url gen", e); throw new UnableToCompleteException(); } } if (toWrite.length() > MAX_STRING_CHUNK) { writeLongString(sw, toWrite); } else { sw.println("return \"" + Generator.escape(toWrite) + "\";"); } sw.outdent(); sw.println("}"); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); }
From source file:geogebra.vectomatic.SVGResourceGenerator.java
License:Open Source License
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { // Extract the SVG name from the @Source annotation URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null); throw new UnableToCompleteException(); }// w w w .j a v a 2s. c o m URL resource = resources[0]; // The SVGResource is implemented as an anonymous inner class // xxx = new SVGResource() { // public OMSVGSVGElement getSvg() { // return OMSVGParser.parse("..."); // } // }; String toWrite = Util.readURLAsString(resource); /* * if (getValidated(method)) { SVGValidator.validate(toWrite, * resource.toExternalForm(), logger, null); } */ SourceWriter sw = new StringSourceWriter(); sw.println("new " + SVGResource.class.getName() + "() {"); sw.indent(); sw.println("private String svg=\"" + Generator.escape(toWrite) + "\";"); // Convenience when examining the generated code. sw.println("// " + resource.toExternalForm()); sw.println("@Override"); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.println("@Override"); sw.println("public String getUrl() {"); sw.indent(); sw.println("return \"data:image/svg+xml;base64,\" + " + Browser.class.getName() + ".base64encode(svg);"); sw.outdent(); sw.println("}"); sw.println("@Override"); sw.println("public " + SafeUri.class.getName() + " getSafeUri() {"); sw.indent(); sw.println("return " + UriUtils.class.getName() + ".fromSafeConstant(\"data:image/svg+xml;base64,\" + " + Browser.class.getName() + ".base64encode(svg));"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); }
From source file:gwt.g3d.resources.ExternalMeshResourceGenerator.java
License:Apache License
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null); throw new UnableToCompleteException(); }/*from w w w . ja v a2 s . co m*/ MeshDataInfo meshDataInfo = getMeshDataInfo(method); String url = null; URL modelUrl = resources[0]; if (modelUrl.getFile().endsWith(".obj")) { try { String content = Util.readURLAsString(modelUrl); File outFile = File.createTempFile("temp", ".json"); BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); new ObjParser().parse(content).export(meshDataInfo, writer); writer.close(); url = context.deploy(outFile.toURI().toURL(), true); } catch (ParserException e) { e.printStackTrace(); throw new UnableToCompleteException(); } catch (IOException e) { e.printStackTrace(); throw new UnableToCompleteException(); } } else if (modelUrl.getFile().endsWith(".json")) { url = context.deploy(modelUrl, true); } else { // Unsupported format. throw new UnableToCompleteException(); } SourceWriter sw = new StringSourceWriter(); sw.println(String.format("new %s() {", AbstractExternalMeshResource.class.getName())); sw.indent(); // Creates method: void getMesh(ResourceCallback<MeshResource>); sw.println(String.format("public void getMesh(%s<%s> callback) {", ResourceCallback.class.getName(), MeshResource.class.getName())); sw.indent(); // Creates the MeshDataInfo. sw.println(String.format("%s meshDataInfo = new %s();", MeshDataInfo.class.getName(), MeshDataInfo.class.getName())); sw.println(String.format("meshDataInfo.setPositionDataType(%s.%s);", DataType.class.getName(), meshDataInfo.getPositionDataType())); sw.println(String.format("meshDataInfo.setPositionDataSize(%s);", meshDataInfo.getPositionDataSize())); sw.println(String.format("meshDataInfo.setNormalDataType(%s.%s);", DataType.class.getName(), meshDataInfo.getNormalDataType())); sw.println(String.format("meshDataInfo.setNormalDataSize(%s);", meshDataInfo.getNormalDataSize())); sw.println(String.format("meshDataInfo.setNormalized(%s);", meshDataInfo.isNormalized())); sw.println(String.format("meshDataInfo.setTexCoordDataType(%s.%s);", DataType.class.getName(), meshDataInfo.getTexCoordDataType())); sw.println(String.format("meshDataInfo.setTexCoordDataSize(%s);", meshDataInfo.getTexCoordDataSize())); sw.println(String.format("getMesh(%s, meshDataInfo, callback);", url)); sw.outdent(); sw.println("}"); // Creates method: String getName(); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); }
From source file:gwt.g3d.resources.ShaderResourceGenerator.java
License:Apache License
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 2) { logger.log(TreeLogger.ERROR, "Exactly two resources must be specified", null); throw new UnableToCompleteException(); }//from ww w. ja v a 2 s . com SourceWriter sw = new StringSourceWriter(); sw.println("new " + ShaderResource.class.getName() + "() {"); sw.indent(); sw.println("public String getVertexShaderSource() {"); sw.indent(); String vertexSource = Generator.escape(Util.readURLAsString(resources[0])); sw.println("return \"" + vertexSource + "\";"); sw.outdent(); sw.println("}"); sw.println("public String getFragmentShaderSource() {"); sw.indent(); String fragmentSource = Generator.escape(Util.readURLAsString(resources[1])); sw.println("return \"" + fragmentSource + "\";"); sw.outdent(); sw.println("}"); sw.println(String.format("public %s createShader(%s gl) throws %s {", AbstractShader.class.getName(), GL2.class.getName(), ShaderException.class.getName())); sw.indent(); sw.println(String.format("%s shader = %s;", AbstractShader.class.getName(), createAbstractShader(logger, context, method))); sw.println("shader.init(gl);"); sw.println("return shader;"); sw.outdent(); sw.println("}"); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); }
From source file:org.parallax3d.parallax.platforms.gwt.generator.SourceTextResourceGenerator.java
License:Open Source License
public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", (Throwable) null); throw new UnableToCompleteException(); } else {/*from ww w. j av a2s. c o m*/ URL resource = resources[0]; StringSourceWriter sw = new StringSourceWriter(); sw.println("new " + SourceTextResource.class.getName() + "() {"); sw.indent(); if (!AbstractResourceGenerator.STRIP_COMMENTS) { sw.println("// " + resource.toExternalForm()); } sw.println("public String getText() {"); sw.indent(); String toWrite = Util.readURLAsString(resource); if (toWrite.length() > 16383) { this.writeLongString(sw, toWrite); } else { sw.println("return \"" + Generator.escape(toWrite) + "\";"); } sw.outdent(); sw.println("}"); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); } }
From source file:org.vectomatic.dev.svg.impl.gen.ExternalSVGResourceGenerator.java
License:Apache License
@Override public void prepare(TreeLogger logger, ResourceContext context, ClientBundleRequirements requirements, JMethod method) throws UnableToCompleteException { URL[] urls = ResourceGeneratorUtil.findResources(logger, context, method); if (urls.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null); throw new UnableToCompleteException(); }/* w w w . j a va2 s .c o m*/ URL resource = urls[0]; String toWrite = Util.readURLAsString(resource); if (getValidated(method)) { SVGValidator.validate(toWrite, resource.toExternalForm(), logger, null); } // This de-duplicates strings in the bundle. if (!hashes.containsKey(toWrite)) { hashes.put(toWrite, currentIndex++); if (!first) { data.append(",\n"); } else { first = false; } data.append('"'); data.append(Generator.escape(toWrite)); data.append('"'); } // Store the (possibly n:1) mapping of resource function to bundle // index. offsets.put(method.getName(), hashes.get(toWrite)); }
From source file:org.vectomatic.dev.svg.impl.gen.SVGResourceGenerator.java
License:Open Source License
@Override public String createAssignment(TreeLogger logger, ResourceContext context, JMethod method) throws UnableToCompleteException { // Extract the SVG name from the @Source annotation URL[] resources = ResourceGeneratorUtil.findResources(logger, context, method); if (resources.length != 1) { logger.log(TreeLogger.ERROR, "Exactly one resource must be specified", null); throw new UnableToCompleteException(); }//from w w w . j a v a 2 s .c om URL resource = resources[0]; // The SVGResource is implemented as an anonymous inner class // xxx = new SVGResource() { // public OMSVGSVGElement getSvg() { // return OMSVGParser.parse("..."); // } // }; SourceWriter sw = new StringSourceWriter(); sw.println("new " + SVGResource.class.getName() + "() {"); sw.indent(); // Convenience when examining the generated code. sw.println("// " + resource.toExternalForm()); sw.println("public " + OMSVGSVGElement.class.getName() + " getSvg() {"); sw.indent(); String toWrite = Util.readURLAsString(resource); if (getValidated(method)) { SVGValidator.validate(toWrite, resource.toExternalForm(), logger, null); } sw.println("return " + OMSVGParser.class.getName() + ".parse(\"" + Generator.escape(toWrite) + "\");"); sw.outdent(); sw.println("}"); sw.println("public String getName() {"); sw.indent(); sw.println("return \"" + method.getName() + "\";"); sw.outdent(); sw.println("}"); sw.outdent(); sw.println("}"); return sw.toString(); }