Search in sources :

Example 1 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException 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 TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException 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 TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class PropBindingFactoryTest method method_call_as_terminal.

@Test
public void method_call_as_terminal() {
    TargetBean bean = new TargetBean();
    ComponentResources resources = newComponentResources(bean);
    Location l = mockLocation();
    replay();
    Binding binding = factory.newBinding("test binding", resources, null, "stringHolderMethod().stringValue()", l);
    assertSame(binding.getBindingType(), String.class);
    bean.getStringHolder().setValue("first");
    assertEquals(binding.get(), "first");
    try {
        binding.set("read-only");
        unreachable();
    } catch (TapestryException ex) {
        assertEquals(ex.getMessage(), "Expression 'stringHolderMethod().stringValue()' for class org.apache.tapestry5.internal.bindings.TargetBean is read-only.");
        assertSame(ex.getLocation(), l);
    }
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 4 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class PropBindingFactoryTest method read_only_property.

@Test
public void read_only_property() {
    TargetBean bean = new TargetBean();
    ComponentResources resources = newComponentResources(bean);
    Location l = mockLocation();
    replay();
    Binding binding = factory.newBinding("test binding", resources, null, "readOnly", l);
    assertEquals(binding.get(), "ReadOnly");
    try {
        binding.set("fail");
        unreachable();
    } catch (TapestryException ex) {
        assertEquals(ex.getMessage(), "Expression 'readOnly' for class org.apache.tapestry5.internal.bindings.TargetBean is read-only.");
        assertEquals(ex.getLocation(), l);
    }
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) ComponentResources(org.apache.tapestry5.ComponentResources) Location(org.apache.tapestry5.commons.Location) Test(org.testng.annotations.Test)

Example 5 with TapestryException

use of org.apache.tapestry5.commons.internal.util.TapestryException in project tapestry-5 by apache.

the class BindingSourceImpl method newBinding.

public Binding newBinding(String description, ComponentResources container, ComponentResources component, String defaultPrefix, String expression, Location location) {
    assert InternalUtils.isNonBlank(description);
    assert container != null;
    assert InternalUtils.isNonBlank(defaultPrefix);
    assert component != null;
    // TAP5-845: The expression may be the empty string. This is ok, if it's compatible with
    // the default prefix (the empty string is not a valid property expression, but is valid
    // as a literal string, perhaps as an informal parameter).
    // Location might be null
    String subexpression = expression;
    int colonx = expression.indexOf(':');
    BindingFactory factory = null;
    if (colonx > 0) {
        String prefix = expression.substring(0, colonx);
        factory = factories.get(prefix);
        if (factory != null)
            subexpression = expression.substring(colonx + 1);
    }
    if (factory == null)
        factory = factories.get(defaultPrefix);
    try {
        return factory.newBinding(interner.intern(description), container, component, subexpression, location);
    } catch (Exception ex) {
        throw new TapestryException(String.format("Could not convert '%s' into a component parameter binding: %s", expression, ExceptionUtils.toMessage(ex)), location, ex);
    }
}
Also used : TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException) BindingFactory(org.apache.tapestry5.services.BindingFactory)

Aggregations

TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)37 Location (org.apache.tapestry5.commons.Location)12 Test (org.testng.annotations.Test)12 ComponentResources (org.apache.tapestry5.ComponentResources)11 Binding (org.apache.tapestry5.Binding)7 BindingFactory (org.apache.tapestry5.services.BindingFactory)4 IOException (java.io.IOException)3 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)3 Component (org.apache.tapestry5.runtime.Component)3 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)2 TrackableComponentEventCallback (org.apache.tapestry5.TrackableComponentEventCallback)2 PropertyModel (org.apache.tapestry5.beanmodel.PropertyModel)2 Messages (org.apache.tapestry5.commons.Messages)2 Link (org.apache.tapestry5.http.Link)2 Instantiator (org.apache.tapestry5.internal.services.Instantiator)2 ComponentPageElement (org.apache.tapestry5.internal.structure.ComponentPageElement)2 Page (org.apache.tapestry5.internal.structure.Page)2 OperationException (org.apache.tapestry5.ioc.internal.OperationException)2 JSONObject (org.apache.tapestry5.json.JSONObject)2 BindingSource (org.apache.tapestry5.services.BindingSource)2