that's exactly what we do.. we just named it "url", instead of "uri". :) :)

Geoffrey Wiseman wrote:
On Thu, Mar 26, 2009 at 1:40 PM, Dave Greggory <davegregg...@yahoo.com>wrote:

Just use RenderSupport.

I just built my own component named asset attacher for this purpose.


And if anyone's curious what the other approach might look like:

package com.tsg.tapestry;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Locale;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ioc.Resource;
import org.apache.tapestry5.services.AssetFactory;

public class UriAssetFactory implements AssetFactory {

    @Override
    public Asset createAsset(Resource resource) {
        return new UriAsset((UriResource) resource);
    }

    @Override
    public Resource getRootResource() {
        return new UriResource();
    }

    private static class UriResource implements Resource {

        private String path;
        private URI uri;

        public UriResource() {
            // root uri resource
            this.path = null;
            this.uri = null;
        }

        public UriResource(String path) {
            this.path = path;
            try {
                this.uri = new URI(path);
            } catch (URISyntaxException e) {
                uri = null;
            }
        }

        private boolean isRoot() {
            return path == null;
        }

        @Override
        public boolean exists() {
            if (uri != null) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public Resource forFile(String relativePath) {
            if (isRoot()) {
                return new UriResource(relativePath);
            } else {
                return new UriResource(this.getFolder() + '/' +
relativePath);
            }
        }

        /**
         * URI Resources can't be localized -- at least, not in any easy
manner.
         */
        @Override
        public Resource forLocale(Locale locale) {
            return this;
        }

        @Override
        public String getFile() {
            if (isRoot()) {
                return "";
            } else {
                int index = path.lastIndexOf('/');
                if (index == -1) {
                    return path;
                } else {
                    return path.substring(index + 1);
                }
            }
        }

        @Override
        public String getFolder() {
            if (isRoot()) {
                return "";
            } else {
                int index = path.lastIndexOf('/');
                if (index == -1) {
                    return "";
                } else {
                    return path.substring(0, index);
                }
            }
        }

        @Override
        public String getPath() {
            return path;
        }

        @Override
        public InputStream openStream() throws IOException {
            if (isRoot()) {
                throw new IOException("Cannot open stream for root uri.");
            } else if (uri == null) {
                throw new IOException("Cannot open stream for malformed
uri.");
            } else {
                return uri.toURL().openStream();
            }
        }

        @Override
        public URL toURL() {
            if (uri == null) {
                try {
                    return uri.toURL();
                } catch (MalformedURLException e) {
                    return null;
                }
            } else {
                return null;
            }
        }

        @Override
        public Resource withExtension(String extension) {
            if (isRoot()) {
                return this;
            } else {
                int index = path.lastIndexOf('.');
                if (index == -1) {
                    return new UriResource(path + '.' + extension);
                } else {
                    return new UriResource(path.substring(0, index + 1)
                            + extension);
                }
            }
        }

    }

    private static class UriAsset implements Asset {

        private UriResource resource;

        public UriAsset(UriResource resource) {
            this.resource = resource;
        }

        @Override
        public Resource getResource() {
            return resource;
        }

        @Override
        public String toClientURL() {
            return resource.getPath();
        }

    }

}


package com.tsg.crystal.web.services;

import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.services.AssetFactory;

import com.tsg.tapestry.UriAssetFactory;

public class TapestryModule {

    public static void contributeAssetSource(
            MappedConfiguration<String, AssetFactory> configuration) {
        configuration.add("uri", new UriAssetFactory());
    }
}



@IncludeStylesheet( {
        "uri:
http://yui.yahooapis.com/2.7.0/build/reset-fonts-grids/reset-fonts-grids.css
",
        "uri:http://yui.yahooapis.com/2.7.0/build/base/base-min.css"; })



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to