Example usage for com.google.gwt.resources.ext ResourceContext getGeneratorContext

List of usage examples for com.google.gwt.resources.ext ResourceContext getGeneratorContext

Introduction

In this page you can find the example usage for com.google.gwt.resources.ext ResourceContext getGeneratorContext.

Prototype

GeneratorContext getGeneratorContext();

Source Link

Document

Return the GeneratorContext in which the overall resource generation framework is being run.

Usage

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  a2 s .c om
        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:cc.alcina.framework.entity.gen.SimpleCssResourceGenerator.java

License:Apache License

private String replaceWithDataUrls(ResourceContext context, String toWrite) throws Exception {
    Pattern urlPat = Pattern.compile("url\\s*\\((?!'?data:)(?!http:)(.+?)\\)");
    Matcher m = urlPat.matcher(toWrite);
    while (m.find()) {
        String url = m.group(1);// www  . j a v a 2 s  . co m
        int qIdx = url.indexOf('?');
        if (qIdx != -1) {
            url = url.substring(0, qIdx);
        }
        // url = url.replaceFirst("(.+?)\\?.*", "$1");
        url = url.replace("'", "").replace("\"", "");
        StandardGeneratorContext generatorContext = (StandardGeneratorContext) context.getGeneratorContext();
        Field compilerContextField = StandardGeneratorContext.class.getDeclaredField("compilerContext");
        compilerContextField.setAccessible(true);
        CompilerContext compilerContext = (CompilerContext) compilerContextField.get(generatorContext);
        Field moduleField = CompilerContext.class.getDeclaredField("module");
        moduleField.setAccessible(true);
        ModuleDef module = (ModuleDef) moduleField.get(compilerContext);
        Resource resource = module.findPublicFile(url);
        if (resource == null) {
            resource = module.findPublicFile("gwt/standard/" + url);
        }
        if (resource == null) {
            if (url.contains("://")) {
                continue;
            } else {
                if (logMissingUrlResources) {
                    String[] pub = getAllPublicFiles(module);
                    // System.out.println("missing url resource - " + url);
                    for (String path : pub) {
                        if (path.contains(url)) {
                            System.out.format("Maybe - %s : %s\n", url, path);
                        }
                    }
                }
                continue;
            }
        }
        InputStream contents = resource.openContents();
        byte[] bytes = ResourceUtilities.readStreamToByteArray(contents);
        String out = Base64.encodeBytes(bytes);
        String fileName = url.replaceFirst(".+/", "");
        String extension = fileName.replaceFirst(".+\\.", "");
        String mimeType = null;
        if (extension.toLowerCase().equals("gif")) {
            mimeType = "image/gif";
        } else if (extension.toLowerCase().equals("jpeg")) {
            mimeType = "image/jpeg";
        } else if (extension.toLowerCase().equals("jpg")) {
            mimeType = "image/jpeg";
        } else if (extension.toLowerCase().equals("png")) {
            mimeType = "image/png";
        }
        if (mimeType != null) {
            String encoded = String.format("url(data:%s;base64,%s)", mimeType, out.replace("\n", ""));
            if (encoded.length() > 5000) {
                // System.out.println("warn - large css sprite - " + url);
            }
            if (encoded.length() < MAX_DATA_URL_LENGTH) {
                toWrite = m.replaceFirst(encoded);
                m = urlPat.matcher(toWrite);
            }
        } else {
            System.out.println("unable to resolve mime type - " + url);
            // continue on
        }
    }
    return toWrite;
}

From source file:com.googlecode.mgwt.ui.server.util.MGWTCssResourceGenerator.java

License:Apache License

@Override
public void init(TreeLogger logger, ResourceContext context) throws UnableToCompleteException {
    super.init(logger, context);

    try {/*w w  w .j  a v  a2 s. c  o  m*/
        // get module base url config value
        ConfigurationProperty property = context.getGeneratorContext().getPropertyOracle()
                .getConfigurationProperty("mgwt.css.inject");
        injectAtStart = "start".equals(property.getValues().get(0));
    } catch (BadPropertyValueException e) {
        // if we can`t find it die
        logger.log(TreeLogger.ERROR,
                "Configuration property mgwt.css.inject is not defined. Is UI.gwt.xml inherited?");
        throw new UnableToCompleteException();
    }
}

From source file:org.cruxframework.crux.plugin.bootstrap.rebind.font.FontResourceGenerator.java

License:Apache License

private void writeGetText(TreeLogger logger, ResourceContext context, JMethod method, SourceWriter sw)
        throws UnableToCompleteException {
    sw.println("public String getText() {");
    sw.indent();//from www  . j a  va  2 s  . co m

    URL[] urls = ResourceGeneratorUtil.findResources(logger, context, method);

    sw.print("return \"");
    sw.print("@font-face{");

    sw.print("font-family:'");
    sw.print(getFontName(method));
    sw.print("';");

    sw.print("font-style:normal;");
    sw.print("font-weight:400;");

    try {
        String agent = context.getGeneratorContext().getPropertyOracle()
                .getSelectionProperty(logger, "user.agent").getCurrentValue();
        if (agent.equals("ie6") || agent.equals("ie8"))
            writeSrcIE(logger, context, method, sw, urls);
        else
            writeSrc(logger, context, method, sw, urls);
    } catch (BadPropertyValueException e) {
        throw new UnableToCompleteException();
    }
    sw.print("}");

    sw.println("\";");

    sw.outdent();
    sw.println("}");

}

From source file:org.vectomatic.dev.svg.impl.gen.ExternalSVGResourceGenerator.java

License:Apache License

@Override
public void createFields(TreeLogger logger, ResourceContext context, ClientBundleFields fields)
        throws UnableToCompleteException {
    data.append(']');

    urlExpression = context.deploy(/* w w  w. j  a va 2  s  .  c o  m*/
            context.getClientBundleType().getQualifiedSourceName().replace('.', '_') + "_jsonbundle.txt",
            "text/plain", data.toString().getBytes(), true);

    TypeOracle typeOracle = context.getGeneratorContext().getTypeOracle();
    JClassType stringType = typeOracle.findType(String.class.getName());
    assert stringType != null;

    externalSVGUrlIdent = fields.define(stringType, "externalSVGUrl", urlExpression, true, true);

    JClassType textResourceType = typeOracle.findType(SVGResource.class.getName());
    assert textResourceType != null;
    JType textResourceArrayType = typeOracle.getArrayType(textResourceType);

    externalSVGCacheIdent = fields.define(textResourceArrayType, "externalSVGCache",
            "new " + SVGResource.class.getName() + "[" + currentIndex + "]", true, true);
}