Search in sources :

Example 1 with ComponentTemplate

use of org.apache.tapestry5.internal.parser.ComponentTemplate 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 2 with ComponentTemplate

use of org.apache.tapestry5.internal.parser.ComponentTemplate in project tapestry-5 by apache.

the class TemplateParserImplTest method just_HTML.

@Test
public void just_HTML() {
    Resource resource = getResource("justHTML.tml");
    ComponentTemplate template = getParser().parseTemplate(resource);
    assertSame(template.getResource(), resource);
    assertFalse(template.usesStrictMixinParameters());
    List<TemplateToken> tokens = template.getTokens();
    // They add up quick ...
    assertEquals(tokens.size(), 20);
    StartElementToken t0 = get(tokens, 0);
    // Spot check a few things ...
    assertEquals(t0.name, "html");
    assertEquals(t0.namespaceURI, "");
    checkLine(t0, 1);
    TextToken t1 = get(tokens, 1);
    // Concerned this may not work cross platform.
    assertEquals(t1.text, "\n    ");
    StartElementToken t2 = get(tokens, 2);
    assertEquals(t2.name, "head");
    checkLine(t2, 2);
    TextToken t5 = get(tokens, 5);
    assertEquals(t5.text, "title");
    checkLine(t5, 3);
    get(tokens, 6);
    StartElementToken t12 = get(tokens, 12);
    assertEquals(t12.name, "p");
    AttributeToken t13 = get(tokens, 13);
    assertEquals(t13.name, "class");
    assertEquals(t13.value, "important");
    assertEquals(t13.namespaceURI, "");
    TextToken t14 = get(tokens, 14);
    // Simplify the text, converting consecutive whitespace to just a single space.
    assertEquals(t14.text.replaceAll("\\s+", " ").trim(), "Tapestry rocks! Line 2");
    // Line number is the *start* line of the whole text block.
    checkLine(t14, 6);
}
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 3 with ComponentTemplate

use of org.apache.tapestry5.internal.parser.ComponentTemplate in project tapestry-5 by apache.

the class ComponentTemplateSourceImplTest method child_component_inherits_parent_template.

@Test
public void child_component_inherits_parent_template() {
    TemplateParser parser = mockTemplateParser();
    ComponentTemplate template = mockComponentTemplate();
    ComponentModel model = mockComponentModel();
    ComponentModel parentModel = mockComponentModel();
    Resource resource = mockResource();
    ComponentResourceLocator locator = mockLocator(model, english, null);
    train_getComponentClassName(model, "foo.Bar");
    train_getParentModel(model, parentModel);
    expect(locator.locateTemplate(parentModel, english)).andReturn(resource).once();
    expect(resource.exists()).andReturn(true);
    expect(resource.toURL()).andReturn(null);
    train_parseTemplate(parser, resource, template);
    replay();
    ComponentTemplateSource source = new ComponentTemplateSourceImpl(true, parser, locator, converter, componentRequestSelectorAnalyzer, threadLocale);
    assertSame(source.getTemplate(model, english), template);
    verify();
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) Resource(org.apache.tapestry5.commons.Resource) ClasspathResource(org.apache.tapestry5.ioc.internal.util.ClasspathResource) ComponentResourceLocator(org.apache.tapestry5.services.pageload.ComponentResourceLocator) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate) Test(org.testng.annotations.Test)

Example 4 with ComponentTemplate

use of org.apache.tapestry5.internal.parser.ComponentTemplate in project tapestry-5 by apache.

the class PageLoaderImpl method buildOverrideSearch.

private List<ComponentTemplate> buildOverrideSearch(ComponentAssembler assembler, ComponentTemplate template) {
    List<ComponentTemplate> result = CollectionFactory.newList();
    result.add(template);
    ComponentModel model = assembler.getModel();
    ComponentTemplate lastTemplate = template;
    while (lastTemplate.isExtension()) {
        ComponentModel parentModel = model.getParentModel();
        if (parentModel == null) {
            throw new RuntimeException(String.format("Component %s uses an extension template, but does not have a parent component.", model.getComponentClassName()));
        }
        ComponentTemplate parentTemplate = templateSource.getTemplate(parentModel, assembler.getSelector());
        result.add(parentTemplate);
        lastTemplate = parentTemplate;
        model = parentModel;
    }
    return result;
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel)

Example 5 with ComponentTemplate

use of org.apache.tapestry5.internal.parser.ComponentTemplate in project tapestry-5 by apache.

the class ComponentTemplateSourceImpl method getTemplate.

public ComponentTemplate getTemplate(ComponentModel componentModel, ComponentResourceSelector selector) {
    String componentName = componentModel.getComponentClassName();
    MultiKey key = new MultiKey(componentName, selector);
    // First cache is key to resource.
    Resource resource = templateResources.get(key);
    if (resource == null) {
        resource = locateTemplateResource(componentModel, selector);
        templateResources.put(key, resource);
    }
    // If we haven't yet parsed the template into the cache, do so now.
    ComponentTemplate result = templates.get(resource);
    if (result == null) {
        result = parseTemplate(resource);
        templates.put(resource, result);
    }
    return result;
}
Also used : MultiKey(org.apache.tapestry5.commons.util.MultiKey) Resource(org.apache.tapestry5.commons.Resource) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate)

Aggregations

Resource (org.apache.tapestry5.commons.Resource)8 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)7 Test (org.testng.annotations.Test)7 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)6 ComponentModel (org.apache.tapestry5.model.ComponentModel)6 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)5 AbstractResource (org.apache.tapestry5.ioc.internal.util.AbstractResource)2 File (java.io.File)1 URLClassLoader (java.net.URLClassLoader)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 InvalidationListener (org.apache.tapestry5.commons.services.InvalidationListener)1 MultiKey (org.apache.tapestry5.commons.util.MultiKey)1 EmbeddedComponentModel (org.apache.tapestry5.model.EmbeddedComponentModel)1