Search in sources :

Example 1 with ResourcePrototype

use of com.google.gwt.resources.client.ResourcePrototype in project actor-platform by actorapp.

the class JsAssetsProvider method loadAsset.

@Override
public String loadAsset(String name) {
    name = name.replace('.', '_');
    Log.d("AssetsRuntime", "loadAsset: " + name);
    for (ClientBundleWithLookup b : bundles) {
        ResourcePrototype res = b.getResource(name);
        if (res != null) {
            Log.d("AssetsRuntime", "loadAsset " + res);
            return ((TextResource) res).getText();
        }
    }
    return null;
}
Also used : TextResource(com.google.gwt.resources.client.TextResource) ClientBundleWithLookup(com.google.gwt.resources.client.ClientBundleWithLookup) ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype)

Example 2 with ResourcePrototype

use of com.google.gwt.resources.client.ResourcePrototype in project onebusaway-application-modules by camsys.

the class ClientBundleFactory method processBundleMethods.

/**
 **
 * Private Methods
 *
 * @return
 ***
 */
private List<ResourcePrototypeImpl> processBundleMethods(Class<?> bundleType, ClientBundleImpl bundle) throws MalformedURLException {
    List<ResourcePrototypeImpl> resources = new ArrayList<ResourcePrototypeImpl>();
    for (Method method : bundleType.getMethods()) {
        String methodName = method.getName();
        WebappSource source = method.getAnnotation(WebappSource.class);
        if (source == null) {
            _log.warn("no Resource annotation found: " + methodName);
            continue;
        }
        String[] values = source.value();
        if (values == null || values.length != 1)
            throw new IllegalStateException("@WebappSource has no value: " + bundleType.getName() + "#" + methodName);
        String resourceName = values[0];
        Class<?> returnType = method.getReturnType();
        URL localURL = getBundleResourceAsLocalUrl(bundleType, resourceName);
        if (localURL == null) {
            _log.warn("could not find ClientBundle resource for " + bundleType.getName() + "#" + methodName + " source=" + resourceName);
            continue;
        }
        File localFile = getBundleResourceAsLocalFile(resourceName, localURL);
        String name = getMethodNameAsPropertyName(methodName);
        if (name == null) {
            _log.warn("invalid resource name: " + bundleType.getName() + "#" + methodName);
            continue;
        }
        if (CssResource.class.isAssignableFrom(returnType)) {
            CssResourceImpl resourceImpl = new CssResourceImpl(_context, bundle, methodName, localURL);
            if (localFile != null)
                resourceImpl.setLocalFile(localFile);
            resources.add(resourceImpl);
            ResourcePrototype resourceProxy = (ResourcePrototype) Proxy.newProxyInstance(bundleType.getClassLoader(), new Class[] { returnType, ResourceWithUrl.class }, resourceImpl);
            bundle.addResource(resourceProxy);
        } else if (ImageResource.class.isAssignableFrom(returnType)) {
            ImageResourceImpl resourceImpl = new ImageResourceImpl(_context, bundle, methodName, localURL);
            bundle.addResource(resourceImpl);
            resources.add(resourceImpl);
        }
    }
    return resources;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) URL(java.net.URL) ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype) ImageResource(com.google.gwt.resources.client.ImageResource) File(java.io.File) WebappSource(org.onebusaway.presentation.services.resources.WebappSource)

Example 3 with ResourcePrototype

use of com.google.gwt.resources.client.ResourcePrototype in project onebusaway-application-modules by camsys.

the class CssDocumentHandlerImpl method property.

public void property(String name, LexicalUnit unit, boolean important) throws CSSException {
    if (name.equals("gwt-image")) {
        String v = unit.toString();
        ResourcePrototype resource = _parentBundle.getResource(v);
        if (resource == null) {
            _log.warn("unknown resource: " + v);
            return;
        }
        if (!(resource instanceof ImageResource)) {
            _log.warn("expected ImageResource");
            return;
        }
        ImageResource img = (ImageResource) resource;
        _buffer.append("background-image:url(" + img.getURL() + ");");
    } else {
        _buffer.append(name);
        _buffer.append(':');
        int index = 0;
        while (unit != null) {
            if (index > 0)
                _buffer.append(" ");
            String v = unit.toString();
            v = substitute(v);
            _buffer.append(v);
            unit = unit.getNextLexicalUnit();
            index++;
        }
        _buffer.append(';');
    }
}
Also used : ImageResource(com.google.gwt.resources.client.ImageResource) ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype)

Example 4 with ResourcePrototype

use of com.google.gwt.resources.client.ResourcePrototype in project onebusaway-application-modules by camsys.

the class CssDocumentHandlerImpl method handleUrl.

/**
 *************************************************************************
 *
 *************************************************************************
 */
private void handleUrl(String name, String value) {
    ResourcePrototype resource = _parentBundle.getResource(value);
    if (resource == null)
        throw new IllegalStateException("unknown resource name=" + name + " value: " + value);
    if (!(resource instanceof ResourceWithUrl))
        throw new IllegalStateException("resource is not instance of ResourceWithUrl: name=" + value + " value=" + resource);
    ResourceWithUrl data = (ResourceWithUrl) resource;
    String url = data.getUrl();
    url = _context.addContext(url);
    _substitutions.put(name, "url(\"" + url + "\")");
}
Also used : ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype)

Example 5 with ResourcePrototype

use of com.google.gwt.resources.client.ResourcePrototype in project playn by threerings.

the class AutoClientBundleGenerator method generate.

@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
    TypeOracle typeOracle = context.getTypeOracle();
    JClassType userType;
    try {
        userType = typeOracle.getType(typeName);
    } catch (NotFoundException e) {
        logger.log(TreeLogger.ERROR, "Unable to find metadata for type: " + typeName, e);
        throw new UnableToCompleteException();
    }
    String packageName = userType.getPackage().getName();
    String className = userType.getName();
    className = className.replace('.', '_');
    if (userType.isInterface() == null) {
        logger.log(TreeLogger.ERROR, userType.getQualifiedSourceName() + " is not an interface", null);
        throw new UnableToCompleteException();
    }
    ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, className + "Impl");
    composerFactory.addImplementedInterface(userType.getQualifiedSourceName());
    composerFactory.addImport(ClientBundleWithLookup.class.getName());
    composerFactory.addImport(DataResource.class.getName());
    composerFactory.addImport(GWT.class.getName());
    composerFactory.addImport(ImageResource.class.getName());
    composerFactory.addImport(ResourcePrototype.class.getName());
    composerFactory.addImport(TextResource.class.getName());
    File warDirectory = getWarDirectory(logger);
    assert warDirectory.isDirectory();
    File classesDirectory = new File(warDirectory, WEB_INF_CLASSES);
    assert classesDirectory.isDirectory();
    File resourcesDirectory = new File(classesDirectory, packageName.replace('.', '/'));
    assert resourcesDirectory.isDirectory();
    String baseClassesPath = classesDirectory.getPath();
    logger.log(TreeLogger.DEBUG, "baseClassesPath: " + baseClassesPath);
    Set<Resource> resources = preferMp3(getResources(context, userType, fileFilter));
    Set<String> methodNames = new HashSet<String>();
    PrintWriter pw = context.tryCreate(logger, packageName, className + "Impl");
    if (pw != null) {
        SourceWriter sw = composerFactory.createSourceWriter(context, pw);
        // write out jump methods
        sw.println("public ResourcePrototype[] getResources() {");
        sw.indent();
        sw.println("return MyBundle.INSTANCE.getResources();");
        sw.outdent();
        sw.println("}");
        sw.println("public ResourcePrototype getResource(String name) {");
        sw.indent();
        sw.println("return MyBundle.INSTANCE.getResource(name);");
        sw.outdent();
        sw.println("}");
        // write out static ClientBundle interface
        sw.println("static interface MyBundle extends ClientBundleWithLookup {");
        sw.indent();
        sw.println("MyBundle INSTANCE = GWT.create(MyBundle.class);");
        for (Resource resource : resources) {
            String relativePath = resource.getPath();
            String filename = resource.getPath().substring(resource.getPath().lastIndexOf('/') + 1);
            String contentType = getContentType(logger, resource);
            String methodName = stripExtension(filename);
            if (!isValidMethodName(methodName)) {
                logger.log(TreeLogger.WARN, "Skipping invalid method name (" + methodName + ") due to: " + relativePath);
                continue;
            }
            if (!methodNames.add(methodName)) {
                logger.log(TreeLogger.WARN, "Skipping duplicate method name due to: " + relativePath);
                continue;
            }
            logger.log(TreeLogger.DEBUG, "Generating method for: " + relativePath);
            Class<? extends ResourcePrototype> returnType = getResourcePrototype(contentType);
            // generate method
            sw.println();
            if (returnType == DataResource.class) {
                if (contentType.startsWith("audio/")) {
                    // Prevent the use of data URLs, which Flash won't play
                    sw.println("@DataResource.DoNotEmbed");
                } else {
                    // Specify an explicit MIME type, for use in the data URL
                    sw.println("@DataResource.MimeType(\"" + contentType + "\")");
                }
            }
            sw.println("@Source(\"" + relativePath + "\")");
            sw.println(returnType.getName() + " " + methodName + "();");
        }
        sw.outdent();
        sw.println("}");
        sw.commit(logger);
    }
    return composerFactory.getCreatedClassName();
}
Also used : TextResource(com.google.gwt.resources.client.TextResource) ClassSourceFileComposerFactory(com.google.gwt.user.rebind.ClassSourceFileComposerFactory) GWT(com.google.gwt.core.client.GWT) ClientBundleWithLookup(com.google.gwt.resources.client.ClientBundleWithLookup) ImageResource(com.google.gwt.resources.client.ImageResource) Resource(com.google.gwt.dev.resource.Resource) TextResource(com.google.gwt.resources.client.TextResource) DataResource(com.google.gwt.resources.client.DataResource) NotFoundException(com.google.gwt.core.ext.typeinfo.NotFoundException) DataResource(com.google.gwt.resources.client.DataResource) SourceWriter(com.google.gwt.user.rebind.SourceWriter) ResourcePrototype(com.google.gwt.resources.client.ResourcePrototype) JClassType(com.google.gwt.core.ext.typeinfo.JClassType) ImageResource(com.google.gwt.resources.client.ImageResource) TypeOracle(com.google.gwt.core.ext.typeinfo.TypeOracle) UnableToCompleteException(com.google.gwt.core.ext.UnableToCompleteException) File(java.io.File) HashSet(java.util.HashSet) PrintWriter(java.io.PrintWriter)

Aggregations

ResourcePrototype (com.google.gwt.resources.client.ResourcePrototype)5 ImageResource (com.google.gwt.resources.client.ImageResource)3 ClientBundleWithLookup (com.google.gwt.resources.client.ClientBundleWithLookup)2 TextResource (com.google.gwt.resources.client.TextResource)2 File (java.io.File)2 GWT (com.google.gwt.core.client.GWT)1 UnableToCompleteException (com.google.gwt.core.ext.UnableToCompleteException)1 JClassType (com.google.gwt.core.ext.typeinfo.JClassType)1 NotFoundException (com.google.gwt.core.ext.typeinfo.NotFoundException)1 TypeOracle (com.google.gwt.core.ext.typeinfo.TypeOracle)1 Resource (com.google.gwt.dev.resource.Resource)1 DataResource (com.google.gwt.resources.client.DataResource)1 ClassSourceFileComposerFactory (com.google.gwt.user.rebind.ClassSourceFileComposerFactory)1 SourceWriter (com.google.gwt.user.rebind.SourceWriter)1 PrintWriter (java.io.PrintWriter)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 WebappSource (org.onebusaway.presentation.services.resources.WebappSource)1