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);
}
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())));
}
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());
}
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;
}
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);
}
}
}
}
Aggregations