Search in sources :

Example 1 with AttributeToken

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

the class SaxTemplateParser method possibleTapestryComponent.

/**
 * @param elementName
 * @param identifiedType
 *         the type of the element, usually null, but may be the
 *         component type derived from element
 */
private void possibleTapestryComponent(TemplateParserState state, String elementName, String identifiedType) {
    String id = null;
    String type = identifiedType;
    String mixins = null;
    int count = tokenStream.getAttributeCount();
    Location location = getLocation();
    List<TemplateToken> attributeTokens = CollectionFactory.newList();
    for (int i = 0; i < count; i++) {
        QName qname = tokenStream.getAttributeName(i);
        if (isXMLSpaceAttribute(qname))
            continue;
        // The name will be blank for an xmlns: attribute
        String localName = qname.getLocalPart();
        if (InternalUtils.isBlank(localName))
            continue;
        String uri = qname.getNamespaceURI();
        String value = tokenStream.getAttributeValue(i);
        Version version = NAMESPACE_URI_TO_VERSION.get(uri);
        if (version != null) {
            if (T_5_4.sameOrEarlier(version)) {
                strictMixinParameters = true;
            }
            if (localName.equalsIgnoreCase(ID_ATTRIBUTE_NAME)) {
                id = nullForBlank(value);
                validateId(id, "Component id '%s' is not valid; component ids must be valid Java identifiers: start with a letter, and consist of letters, numbers and underscores.");
                continue;
            }
            if (type == null && localName.equalsIgnoreCase(TYPE_ATTRIBUTE_NAME)) {
                type = nullForBlank(value);
                continue;
            }
            if (localName.equalsIgnoreCase(MIXINS_ATTRIBUTE_NAME)) {
                mixins = nullForBlank(value);
                continue;
            }
        // Anything else is the name of a Tapestry component parameter
        // that is simply
        // not part of the template's doctype for the element being
        // instrumented.
        }
        attributeTokens.add(new AttributeToken(uri, localName, value, location));
    }
    boolean isComponent = (id != null || type != null);
    if (mixins != null && !isComponent)
        throw new TapestryException(String.format("You may not specify mixins for element <%s> because it does not represent a component (which requires either an id attribute or a type attribute).", elementName), location, null);
    if (isComponent) {
        tokenAccumulator.add(new StartComponentToken(elementName, id, type, mixins, location));
    } else {
        tokenAccumulator.add(new StartElementToken(tokenStream.getNamespaceURI(), elementName, location));
    }
    addDefineNamespaceTokens();
    tokenAccumulator.addAll(attributeTokens);
    if (id != null)
        componentIds.put(id, location);
    processBody(state.insideComponent(isComponent));
}
Also used : Version(org.apache.tapestry5.internal.services.SaxTemplateParser.Version) QName(javax.xml.namespace.QName) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) Location(org.apache.tapestry5.commons.Location)

Example 2 with AttributeToken

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

the class PageElementFactoryImplTest method unclosed_attribute_expression.

@Test
public void unclosed_attribute_expression() {
    TypeCoercer typeCoercer = mockTypeCoercer();
    BindingSource bindingSource = mockBindingSource();
    ComponentResources resources = mockComponentResources();
    Location location = mockLocation();
    AttributeToken token = new AttributeToken(null, "fred", "${flintstone", location);
    replay();
    PageElementFactory factory = new PageElementFactoryImpl(typeCoercer, bindingSource);
    try {
        factory.newAttributeElement(resources, token);
        unreachable();
    } catch (TapestryException ex) {
        assertEquals(ex.getMessage(), "Attribute expression \'${flintstone\' is missing a closing brace.");
        assertSame(ex.getLocation(), location);
    }
    verify();
}
Also used : BindingSource(org.apache.tapestry5.services.BindingSource) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) AttributeToken(org.apache.tapestry5.internal.parser.AttributeToken) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 3 with AttributeToken

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

the class TemplateParserImplTest method component_with_parameters.

@Test
public void component_with_parameters() {
    List<TemplateToken> tokens = tokens("componentWithParameters.tml");
    assertEquals(tokens.size(), 9);
    TemplateToken templateToken = get(tokens, 2);
    Location l = templateToken.getLocation();
    AttributeToken t1 = get(tokens, 3);
    // TODO: Not sure what order the attributes appear in. Order in the XML? Sorted
    // alphabetically? Random 'cause they're hashed?
    assertEquals(t1.name, "cherry");
    assertEquals(t1.value, "bomb");
    assertSame(t1.getLocation(), l);
    AttributeToken t2 = get(tokens, 4);
    assertEquals(t2.name, "align");
    assertEquals(t2.value, "right");
    assertSame(t2.getLocation(), l);
    TextToken t3 = get(tokens, 5);
    assertEquals(t3.text.trim(), "fred's body");
    get(tokens, 6);
}
Also used : Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 4 with AttributeToken

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

use of org.apache.tapestry5.internal.parser.AttributeToken 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)

Aggregations

Test (org.testng.annotations.Test)5 Location (org.apache.tapestry5.commons.Location)4 Resource (org.apache.tapestry5.commons.Resource)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 AttributeToken (org.apache.tapestry5.internal.parser.AttributeToken)2 AbstractResource (org.apache.tapestry5.ioc.internal.util.AbstractResource)2 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)2 BindingSource (org.apache.tapestry5.services.BindingSource)2 QName (javax.xml.namespace.QName)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 Version (org.apache.tapestry5.internal.services.SaxTemplateParser.Version)1 RenderCommand (org.apache.tapestry5.runtime.RenderCommand)1 RenderQueue (org.apache.tapestry5.runtime.RenderQueue)1