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);
}
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);
}
}
};
}
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);
}
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);
}
}
};
}
Aggregations