Search in sources :

Example 1 with Attribute

use of org.apache.tapestry5.rest.jackson.test.rest.entities.Attribute 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 Attribute

use of org.apache.tapestry5.rest.jackson.test.rest.entities.Attribute in project tapestry-5 by apache.

the class PageElementFactoryImpl method parseAttributeExpansionExpression.

private StringProvider parseAttributeExpansionExpression(String expression, ComponentResources resources, final Location location) {
    final List<StringProvider> providers = newList();
    int startx = 0;
    while (true) {
        int expansionx = expression.indexOf(InternalConstants.EXPANSION_START, startx);
        if (expansionx < 0) {
            if (startx < expression.length())
                providers.add(new LiteralStringProvider(expression.substring(startx)));
            break;
        }
        if (startx != expansionx)
            providers.add(new LiteralStringProvider(expression.substring(startx, expansionx)));
        int endx = expression.indexOf("}", expansionx);
        if (endx < 0)
            throw new TapestryException(String.format("Attribute expression '%s' is missing a closing brace.", expression), location, null);
        String expansion = expression.substring(expansionx + 2, endx);
        final Binding binding = bindingSource.newBinding("attribute expansion", resources, resources, BindingConstants.PROP, expansion, location);
        final StringProvider provider = new StringProvider() {

            public String provideString() {
                try {
                    Object raw = binding.get();
                    return typeCoercer.coerce(raw, String.class);
                } catch (Exception ex) {
                    throw new TapestryException(ex.getMessage(), location, ex);
                }
            }
        };
        providers.add(provider);
        // Restart the search after '}'
        startx = endx + 1;
    }
    if (providers.size() == 1)
        return providers.get(0);
    return new StringProvider() {

        public String provideString() {
            StringBuilder builder = new StringBuilder();
            for (StringProvider provider : providers) builder.append(provider.provideString());
            return builder.toString();
        }
    };
}
Also used : Binding(org.apache.tapestry5.Binding) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 3 with Attribute

use of org.apache.tapestry5.rest.jackson.test.rest.entities.Attribute in project tapestry-5 by apache.

the class DynamicTemplateSaxParser method element.

private DynamicTemplateElement element() {
    String elementURI = tokenStream.getNamespaceURI();
    String elementName = tokenStream.getLocalName();
    String blockId = null;
    int count = tokenStream.getAttributeCount();
    List<DynamicTemplateAttribute> attributes = CollectionFactory.newList();
    Location location = getLocation();
    for (int i = 0; i < count; i++) {
        QName qname = tokenStream.getAttributeName(i);
        // 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);
        if (localName.equals("id")) {
            Matcher matcher = PARAM_ID_PATTERN.matcher(value);
            if (matcher.matches()) {
                blockId = matcher.group(1);
                continue;
            }
        }
        Mapper<DynamicDelegate, String> attributeValueExtractor = createCompositeExtractorFromText(value, location);
        attributes.add(new DynamicTemplateAttribute(uri, localName, attributeValueExtractor));
    }
    if (blockId != null)
        return block(blockId);
    List<DynamicTemplateElement> body = CollectionFactory.newList();
    boolean atEnd = false;
    while (!atEnd) {
        switch(tokenStream.next()) {
            case START_ELEMENT:
                // Recurse into this new element
                body.add(element());
                break;
            case END_ELEMENT:
                body.add(END);
                atEnd = true;
                break;
            default:
                addTextContent(body);
        }
    }
    return createElementWriterElement(elementURI, elementName, attributes, body);
}
Also used : Matcher(java.util.regex.Matcher) QName(javax.xml.namespace.QName) DynamicDelegate(org.apache.tapestry5.services.dynamic.DynamicDelegate) Location(org.apache.tapestry5.commons.Location)

Example 4 with Attribute

use of org.apache.tapestry5.rest.jackson.test.rest.entities.Attribute in project tapestry-5 by apache.

the class PageLoaderImpl method startComponent.

private EmbeddedComponentAssembler startComponent(AssemblerContext context) {
    StartComponentToken token = context.next(StartComponentToken.class);
    ComponentAssembler assembler = context.assembler;
    String elementName = token.getElementName();
    // Initial guess: the type from the token (but this may be null in many cases).
    String embeddedType = token.getComponentType();
    // This may be null for an anonymous component.
    String embeddedId = token.getId();
    String embeddedComponentClassName = null;
    final EmbeddedComponentModel embeddedModel = embeddedId == null ? null : assembler.getModel().getEmbeddedComponentModel(embeddedId);
    if (embeddedId == null)
        embeddedId = assembler.generateEmbeddedId(embeddedType);
    if (embeddedModel != null) {
        String modelType = embeddedModel.getComponentType();
        if (InternalUtils.isNonBlank(modelType) && embeddedType != null) {
            throw new TapestryException(String.format("Embedded component '%s' provides a type attribute in the template ('%s') " + "as well as in the component class ('%s'). You should not provide a type attribute " + "in the template when defining an embedded component within the component class.", embeddedId, embeddedType, modelType), token, null);
        }
        embeddedType = modelType;
        embeddedComponentClassName = embeddedModel.getComponentClassName();
    }
    String componentClassName = embeddedComponentClassName;
    if (InternalUtils.isNonBlank(embeddedType)) {
        try {
            componentClassName = componentClassResolver.resolveComponentTypeToClassName(embeddedType);
        } catch (RuntimeException ex) {
            throw new TapestryException(ex.getMessage(), token, ex);
        }
    }
    // OK, now we can record an action to get it instantiated.
    EmbeddedComponentAssembler embeddedAssembler = assembler.createEmbeddedAssembler(embeddedId, componentClassName, embeddedModel, token.getMixins(), token.getLocation());
    addActionForEmbeddedComponent(context, embeddedAssembler, embeddedId, elementName, componentClassName);
    addParameterBindingActions(context, embeddedAssembler, embeddedModel);
    if (embeddedModel != null && embeddedModel.getInheritInformalParameters()) {
        // Another two-step: The first "captures" the container and embedded component. The second
        // occurs at the end of the page setup.
        assembler.add(new PageAssemblyAction() {

            public void execute(PageAssembly pageAssembly) {
                final ComponentPageElement container = pageAssembly.activeElement.peek();
                final ComponentPageElement embedded = pageAssembly.createdElement.peek();
                pageAssembly.deferred.add(new PageAssemblyAction() {

                    public void execute(PageAssembly pageAssembly) {
                        copyInformalParameters(container, embedded);
                    }
                });
            }
        });
    }
    return embeddedAssembler;
}
Also used : EmbeddedComponentModel(org.apache.tapestry5.model.EmbeddedComponentModel) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 5 with Attribute

use of org.apache.tapestry5.rest.jackson.test.rest.entities.Attribute 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)

Aggregations

TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)5 Location (org.apache.tapestry5.commons.Location)4 Element (org.apache.tapestry5.dom.Element)4 MarkupWriter (org.apache.tapestry5.MarkupWriter)3 RenderCommand (org.apache.tapestry5.runtime.RenderCommand)3 RenderQueue (org.apache.tapestry5.runtime.RenderQueue)3 Test (org.testng.annotations.Test)3 QName (javax.xml.namespace.QName)2 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 Link (org.apache.tapestry5.http.Link)2 AttributeToken (org.apache.tapestry5.internal.parser.AttributeToken)2 BindingSource (org.apache.tapestry5.services.BindingSource)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Matcher (java.util.regex.Matcher)1 Validator (javax.validation.Validator)1 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)1 Binding (org.apache.tapestry5.Binding)1 ComponentResources (org.apache.tapestry5.ComponentResources)1 FieldValidator (org.apache.tapestry5.FieldValidator)1 HeartbeatDeferred (org.apache.tapestry5.annotations.HeartbeatDeferred)1