Search in sources :

Example 1 with Mapper

use of org.apache.tapestry5.func.Mapper 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 Mapper

use of org.apache.tapestry5.func.Mapper 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 Mapper

use of org.apache.tapestry5.func.Mapper 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 Mapper

use of org.apache.tapestry5.func.Mapper in project tapestry-5 by apache.

the class PageCatalog method onRecomputeTotals.

public void onRecomputeTotals() {
    totals = new PageCatalogTotals();
    Flow<Page> pages = F.flow(getPages());
    totals.loadedPages = pages.count();
    totals.definedPages = getPageNames().size();
    totals.uniquePageNames = pages.map(new Mapper<Page, String>() {

        public String map(Page element) {
            return element.getName();
        }
    }).toSet().size();
    totals.components = pages.reduce(new Reducer<Integer, Page>() {

        public Integer reduce(Integer accumulator, Page element) {
            return accumulator + element.getStats().componentCount;
        }
    }, 0);
    Set<String> selectorIds = pages.map(new Mapper<Page, String>() {

        public String map(Page element) {
            return element.getSelector().toShortString();
        }
    }).toSet();
    totals.selectors = InternalUtils.joinSorted(selectorIds);
}
Also used : PageCatalogTotals(org.apache.tapestry5.internal.PageCatalogTotals) Page(org.apache.tapestry5.internal.structure.Page)

Example 5 with Mapper

use of org.apache.tapestry5.func.Mapper in project tapestry-5 by apache.

the class ObjectComponentEventResultProcessor method processResultValue.

public void processResultValue(Object value) throws IOException {
    List<String> names = F.flow(configuredClasses).map(new Mapper<Class, String>() {

        public String map(Class input) {
            return PlasticUtils.toTypeName(input);
        }
    }).toList();
    String message = String.format("A component event handler method returned the value %s. Return type %s can not be handled.", value, PlasticUtils.toTypeName(value.getClass()));
    throw new UnknownValueException(message, new AvailableValues("Configured return types", names));
}
Also used : Mapper(org.apache.tapestry5.func.Mapper) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues)

Aggregations

Mapper (org.apache.tapestry5.func.Mapper)3 Matcher (java.util.regex.Matcher)2 DynamicDelegate (org.apache.tapestry5.services.dynamic.DynamicDelegate)2 QName (javax.xml.namespace.QName)1 Binding (org.apache.tapestry5.Binding)1 Location (org.apache.tapestry5.commons.Location)1 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)1 AvailableValues (org.apache.tapestry5.commons.util.AvailableValues)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1 PageCatalogTotals (org.apache.tapestry5.internal.PageCatalogTotals)1 Page (org.apache.tapestry5.internal.structure.Page)1