Search in sources :

Example 1 with DynamicDelegate

use of org.apache.tapestry5.services.dynamic.DynamicDelegate 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 2 with DynamicDelegate

use of org.apache.tapestry5.services.dynamic.DynamicDelegate in project tapestry-5 by apache.

the class DynamicTemplateSaxParser method createExpansionExtractor.

private static Mapper<DynamicDelegate, String> createExpansionExtractor(final String expression, final Location location, final BindingSource bindingSource) {
    return new Mapper<DynamicDelegate, String>() {

        public String map(DynamicDelegate delegate) {
            try {
                Binding binding = bindingSource.newBinding("dynamic template binding", delegate.getComponentResources().getContainerResources(), delegate.getComponentResources(), BindingConstants.PROP, expression, location);
                Object boundValue = binding.get();
                return boundValue == null ? null : boundValue.toString();
            } catch (Throwable t) {
                throw new TapestryException(ExceptionUtils.toMessage(t), location, t);
            }
        }
    };
}
Also used : Binding(org.apache.tapestry5.Binding) Mapper(org.apache.tapestry5.func.Mapper) DynamicDelegate(org.apache.tapestry5.services.dynamic.DynamicDelegate) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 3 with DynamicDelegate

use of org.apache.tapestry5.services.dynamic.DynamicDelegate in project tapestry-5 by apache.

the class DynamicTemplateSaxParser method createCompositeExtractorFromText.

private Mapper<DynamicDelegate, String> createCompositeExtractorFromText(String text, Location location) {
    Matcher matcher = EXPANSION_PATTERN.matcher(text);
    List<Mapper<DynamicDelegate, String>> extractors = CollectionFactory.newList();
    int startx = 0;
    while (matcher.find()) {
        int matchStart = matcher.start();
        if (matchStart != startx) {
            String prefix = text.substring(startx, matchStart);
            extractors.add(createTextExtractor(prefix));
        }
        // Group 1 includes the real text of the expansion, with whitespace
        // around the
        // expression (but inside the curly braces) excluded.
        String expression = matcher.group(1);
        extractors.add(createExpansionExtractor(expression, location, bindingSource));
        startx = matcher.end();
    }
    if (startx < text.length())
        extractors.add(createTextExtractor(text.substring(startx, text.length())));
    if (extractors.size() == 1)
        return extractors.get(0);
    return creatCompositeExtractor(extractors);
}
Also used : Mapper(org.apache.tapestry5.func.Mapper) Matcher(java.util.regex.Matcher)

Example 4 with DynamicDelegate

use of org.apache.tapestry5.services.dynamic.DynamicDelegate in project tapestry-5 by apache.

the class DynamicTemplateSaxParser method createBlockElement.

private static DynamicTemplateElement createBlockElement(final String blockId, final Location location) {
    return new DynamicTemplateElement() {

        public void render(MarkupWriter writer, RenderQueue queue, DynamicDelegate delegate) {
            try {
                Block block = delegate.getBlock(blockId);
                queue.push((RenderCommand) block);
            } catch (Exception ex) {
                throw new TapestryException(String.format("Exception rendering block '%s' as part of dynamic template: %s", blockId, ExceptionUtils.toMessage(ex)), location, ex);
            }
        }
    };
}
Also used : RenderQueue(org.apache.tapestry5.runtime.RenderQueue) Block(org.apache.tapestry5.Block) DynamicDelegate(org.apache.tapestry5.services.dynamic.DynamicDelegate) MarkupWriter(org.apache.tapestry5.MarkupWriter) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Aggregations

DynamicDelegate (org.apache.tapestry5.services.dynamic.DynamicDelegate)3 Matcher (java.util.regex.Matcher)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 Mapper (org.apache.tapestry5.func.Mapper)2 QName (javax.xml.namespace.QName)1 Binding (org.apache.tapestry5.Binding)1 Block (org.apache.tapestry5.Block)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 Location (org.apache.tapestry5.commons.Location)1 RenderQueue (org.apache.tapestry5.runtime.RenderQueue)1