Search in sources :

Example 1 with ResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.ResourceStreamLocator in project wicket by apache.

the class ResourceStreamLocatorTest method createAndTestResource.

/**
 * @param sourcePath
 * @param style
 * @param variation
 * @param locale
 * @param extension
 */
public void createAndTestResource(IResourceFinder sourcePath, String style, String variation, Locale locale, String extension) {
    IResourceStreamLocator locator = new ResourceStreamLocator(sourcePath);
    IResourceStream resource = locator.locate(this.getClass(), this.getClass().getName().replace('.', '/'), style, variation, locale, "txt", false);
    compareFilename(resource, extension);
}
Also used : IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)

Example 2 with ResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.ResourceStreamLocator in project wicket by apache.

the class CachingResourceStreamLocatorTest method strictMatchingDoesntInvalidateNonStrictMatching.

/**
 * Tests strict before non-strict matching with a specific locale.
 */
@Test
public void strictMatchingDoesntInvalidateNonStrictMatching() {
    IResourceStreamLocator resourceStreamLocator = new ResourceStreamLocator(new ClassPathResourceFinder(""));
    CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    String style = null;
    String variation = null;
    Locale locale = new Locale("nl", "NL");
    String extension = null;
    String filename = "org/apache/wicket/ajax/res/js/wicket-ajax-jquery-debug.js";
    // a strict lookup of a localized resource should not find the non-localized resource
    IResourceStream strictLocate = cachingLocator.locate(AbstractDefaultAjaxBehavior.class, filename, style, variation, locale, extension, true);
    assertThat(strictLocate, is(nullValue()));
    // but a non-strict lookup with a locale should fall back to the non-localized resource
    IResourceStream nonStrictLocate = cachingLocator.locate(AbstractDefaultAjaxBehavior.class, filename, style, variation, locale, extension, false);
    assertThat(nonStrictLocate, is(not(nullValue())));
}
Also used : Locale(java.util.Locale) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStream(org.apache.wicket.util.resource.IResourceStream) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) ClassPathResourceFinder(org.apache.wicket.core.util.resource.ClassPathResourceFinder) Test(org.junit.Test)

Example 3 with ResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.ResourceStreamLocator in project wicket by apache.

the class MarkupParserTest method fileDocument.

/**
 * @throws ParseException
 * @throws ResourceStreamNotFoundException
 * @throws IOException
 */
@Test
public void fileDocument() throws ParseException, ResourceStreamNotFoundException, IOException {
    IResourceStreamLocator locator = new ResourceStreamLocator();
    MarkupResourceStream resource = newMarkupResourceStream(locator, getClass(), "1", null, null, "html");
    MarkupParser parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    IMarkupFragment tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "2", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "3", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "4", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    // File from jar (URL resource)
    resource = newMarkupResourceStream(locator, PageExpiredErrorPage.class, null, null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "5", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "6", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "7", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "8", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
    // Assert.assertEquals(docText, tokens.get(0).toString());
    resource = newMarkupResourceStream(locator, getClass(), "9", null, null, "html");
    parser = new MarkupParser(resource);
    parser.setWicketNamespace("wcn");
    tokens = parser.parse();
    log.info("tok(0)=" + tokens.get(0));
// Assert.assertEquals(docText, tokens.get(0).toString());
}
Also used : PageExpiredErrorPage(org.apache.wicket.markup.html.pages.PageExpiredErrorPage) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) Test(org.junit.Test)

Example 4 with ResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.ResourceStreamLocator in project wicket by apache.

the class ResourceSettings method getResourceStreamLocator.

public IResourceStreamLocator getResourceStreamLocator() {
    if (resourceStreamLocator == null) {
        // Create compound resource locator using source path from
        // application settings
        resourceStreamLocator = new ResourceStreamLocator(getResourceFinders());
        resourceStreamLocator = new CachingResourceStreamLocator(resourceStreamLocator);
    }
    return resourceStreamLocator;
}
Also used : CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) CachingResourceStreamLocator(org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator)

Example 5 with ResourceStreamLocator

use of org.apache.wicket.core.util.resource.locator.ResourceStreamLocator in project wicket by apache.

the class PackageTextTemplate method load.

/**
 * Loads the template if it is not loaded yet
 */
private void load() {
    if (buffer.length() == 0) {
        String path = Packages.absolutePath(scope, fileName);
        Application app = Application.get();
        // first try default class loading locator to find the resource
        IResourceStream stream = app.getResourceSettings().getResourceStreamLocator().locate(scope, path, getStyle(), getVariation(), getLocale(), null, false);
        if (stream == null) {
            // if the default locator didn't find the resource then fallback
            stream = new ResourceStreamLocator().locate(scope, path, getStyle(), getVariation(), getLocale(), null, false);
        }
        if (stream == null) {
            throw new IllegalArgumentException("resource " + fileName + " not found for scope " + scope + " (path = " + path + ")");
        }
        setLastModified(stream.lastModifiedTime());
        try {
            if (encoding != null) {
                buffer.append(Streams.readString(stream.getInputStream(), encoding));
            } else {
                buffer.append(Streams.readString(stream.getInputStream()));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ResourceStreamNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) ResourceStreamLocator(org.apache.wicket.core.util.resource.locator.ResourceStreamLocator) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) Application(org.apache.wicket.Application)

Aggregations

ResourceStreamLocator (org.apache.wicket.core.util.resource.locator.ResourceStreamLocator)8 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)7 Test (org.junit.Test)5 ClassPathResourceFinder (org.apache.wicket.core.util.resource.ClassPathResourceFinder)4 CachingResourceStreamLocator (org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator)4 IResourceStream (org.apache.wicket.util.resource.IResourceStream)4 Locale (java.util.Locale)3 IOException (java.io.IOException)1 Application (org.apache.wicket.Application)1 PageExpiredErrorPage (org.apache.wicket.markup.html.pages.PageExpiredErrorPage)1 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)1