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