Search in sources :

Example 26 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class LessResourceTransformer method invokeLessCompiler.

private BytestreamCache invokeLessCompiler(Resource source, ResourceDependencies dependencies) throws IOException {
    try {
        LessSource lessSource = new ResourceLessSource(source, dependencies);
        LessCompiler.CompilationResult compilationResult = compile(compiler, lessSource);
        return new BytestreamCache(compilationResult.getCss().getBytes("utf-8"));
    } catch (Less4jException ex) {
        throw new IOException(ex);
    } catch (UnsupportedEncodingException ex) {
        throw new IOException(ex);
    }
}
Also used : LessSource(com.github.sommeri.less4j.LessSource) BytestreamCache(org.apache.tapestry5.internal.services.assets.BytestreamCache) LessCompiler(com.github.sommeri.less4j.LessCompiler) DefaultLessCompiler(com.github.sommeri.less4j.core.DefaultLessCompiler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Less4jException(com.github.sommeri.less4j.Less4jException)

Example 27 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class AssetSourceImpl method getAssetForResource.

private Asset getAssetForResource(Resource resource) {
    try {
        acquireReadLock();
        Asset result = TapestryInternalUtils.getAndDeref(cache, resource);
        if (result == null) {
            result = createAssetFromResource(resource);
            cache.put(resource, new SoftReference(result));
        }
        return result;
    } finally {
        releaseReadLock();
    }
}
Also used : SoftReference(java.lang.ref.SoftReference) Asset(org.apache.tapestry5.Asset)

Example 28 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class AbstractAssetFactory method createAsset.

protected Asset createAsset(final Resource resource, final String folder, final String resourcePath) {
    assert resource != null;
    assert InternalUtils.isNonBlank(folder);
    assert InternalUtils.isNonBlank(resourcePath);
    return new AbstractAsset(false) {

        public String toClientURL() {
            try {
                // Get the uncompressed version, so that we can identify its content type (and remember, the extension is not enough,
                // as some types get translated to new content types by the SRS).
                StreamableResource uncompressed = streamableResourceSource.getStreamableResource(resource, StreamableResourceProcessing.COMPRESSION_DISABLED, resourceChangeTracker);
                StreamableResource forRequest = isCompressable(uncompressed) ? streamableResourceSource.getStreamableResource(resource, StreamableResourceProcessing.COMPRESSION_ENABLED, resourceChangeTracker) : uncompressed;
                return assetPathConstructor.constructAssetPath(folder, resourcePath, forRequest);
            } catch (IOException ex) {
                throw new RuntimeException(String.format("Unable to construct asset path for %s: %s", resource, ExceptionUtils.toMessage(ex)), ex);
            }
        }

        public Resource getResource() {
            return resource;
        }
    };
}
Also used : StreamableResource(org.apache.tapestry5.services.assets.StreamableResource) IOException(java.io.IOException)

Example 29 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class TemplateParserImplTest method namespaced_element.

@Test
public void namespaced_element() {
    Resource resource = getResource("namespaced_element.tml");
    ComponentTemplate template = getParser().parseTemplate(resource);
    assertSame(template.getResource(), resource);
    List<TemplateToken> tokens = template.getTokens();
    // They add up quick ...
    assertEquals(tokens.size(), 8);
    StartElementToken t0 = get(tokens, 0);
    String expectedURI = "http://foo.com";
    assertEquals(t0.namespaceURI, expectedURI);
    assertEquals(t0.name, "bar");
    DefineNamespacePrefixToken t1 = get(tokens, 1);
    assertEquals(t1.namespacePrefix, "foo");
    assertEquals(t1.namespaceURI, expectedURI);
    AttributeToken t2 = get(tokens, 2);
    assertEquals(t2.name, "biff");
    assertEquals(t2.value, "baz");
    assertEquals(t2.namespaceURI, expectedURI);
    StartElementToken t4 = get(tokens, 4);
    assertEquals(t4.namespaceURI, "");
    assertEquals(t4.name, "gnip");
// The rest are close tokens
}
Also used : Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) AbstractResource(org.apache.tapestry5.ioc.internal.util.AbstractResource) Test(org.testng.annotations.Test)

Example 30 with Resource

use of org.apache.tapestry5.commons.Resource in project tapestry-5 by apache.

the class TemplateParserImplTest method resource_that_throws_exception.

@Test
public // TAP5-2516
void resource_that_throws_exception() throws Exception {
    Resource resource = new AbstractResource("throwfoo") {

        @Override
        public URL toURL() {
            return null;
        }

        @Override
        public boolean exists() {
            return true;
        }

        @Override
        protected Resource newResource(String path) {
            return null;
        }

        @Override
        public InputStream openStream() throws IOException {
            throw new IOException("foo");
        }
    };
    try {
        getParser().parseTemplate(resource);
        unreachable();
    } catch (RuntimeException ex) {
        if (ex.getCause() instanceof TapestryException && ex.getCause().getCause() instanceof IOException) {
            assertMessageContains(ex, "foo");
        } else {
            throw ex;
        }
    }
}
Also used : Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) AbstractResource(org.apache.tapestry5.ioc.internal.util.AbstractResource) AbstractResource(org.apache.tapestry5.ioc.internal.util.AbstractResource) IOException(java.io.IOException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) Test(org.testng.annotations.Test)

Aggregations

Resource (org.apache.tapestry5.commons.Resource)78 Test (org.testng.annotations.Test)62 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)38 Logger (org.slf4j.Logger)38 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)16 Asset (org.apache.tapestry5.Asset)14 ComponentModel (org.apache.tapestry5.model.ComponentModel)10 MutableEmbeddedComponentModel (org.apache.tapestry5.model.MutableEmbeddedComponentModel)10 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)6 AssetFactory (org.apache.tapestry5.services.AssetFactory)6 IOException (java.io.IOException)5 AssetSource (org.apache.tapestry5.services.AssetSource)5 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)5 Context (org.apache.tapestry5.http.services.Context)4 BeginRender (org.apache.tapestry5.annotations.BeginRender)3 Location (org.apache.tapestry5.commons.Location)3 AbstractResource (org.apache.tapestry5.ioc.internal.util.AbstractResource)3 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)3 ClasspathAssetAliasManager (org.apache.tapestry5.services.ClasspathAssetAliasManager)3 StreamableResource (org.apache.tapestry5.services.assets.StreamableResource)3