Search in sources :

Example 1 with Component

use of org.apache.tapestry5.annotations.Component in project tapestry-5 by apache.

the class ResponseRendererImpl method findContentType.

public ContentType findContentType(Object component) {
    Component c = (Component) component;
    String pageName = c.getComponentResources().getPageName();
    Page page = pageCache.get(pageName);
    return pageContentAnalyzer.findContentType(page);
}
Also used : Page(org.apache.tapestry5.internal.structure.Page) Component(org.apache.tapestry5.runtime.Component)

Example 2 with Component

use of org.apache.tapestry5.annotations.Component in project tapestry-5 by apache.

the class SaxTemplateParser method element.

/**
 * Processes an element through to its matching end tag.
 *
 * An element can be:
 *
 * a Tapestry component via <t:type>
 *
 * a Tapestry component via t:type="type" and/or t:id="id"
 *
 * a Tapestry component via a library namespace
 *
 * A parameter element via <t:parameter>
 *
 * A parameter element via <p:name>
 *
 * A <t:remove> element (in the 5.1 schema)
 *
 * A <t:content> element (in the 5.1 schema)
 *
 * A <t:block> element
 *
 * The body <t:body>
 *
 * An ordinary element
 */
void element(TemplateParserState initialState) {
    TemplateParserState state = setupForElement(initialState);
    String uri = tokenStream.getNamespaceURI();
    String name = tokenStream.getLocalName();
    Version version = NAMESPACE_URI_TO_VERSION.get(uri);
    if (T_5_1.sameOrEarlier(version)) {
        if (name.equalsIgnoreCase("remove")) {
            removeContent();
            return;
        }
        if (name.equalsIgnoreCase("content")) {
            limitContent(state);
            return;
        }
        if (name.equalsIgnoreCase("extension-point")) {
            extensionPoint(state);
            return;
        }
        if (name.equalsIgnoreCase("replace")) {
            throw new RuntimeException("The <replace> element may only appear directly within an extend element.");
        }
        if (MUST_BE_ROOT.contains(name))
            mustBeRoot(name);
    }
    if (version != null) {
        if (name.equalsIgnoreCase("body")) {
            body();
            return;
        }
        if (name.equalsIgnoreCase("container")) {
            mustBeRoot(name);
        }
        if (name.equalsIgnoreCase("block")) {
            block(state);
            return;
        }
        if (name.equalsIgnoreCase("parameter")) {
            if (T_5_3.sameOrEarlier(version)) {
                throw new RuntimeException(String.format("The <parameter> element has been deprecated in Tapestry 5.3 in favour of '%s' namespace.", TAPESTRY_PARAMETERS_URI));
            }
            classicParameter(state);
            return;
        }
        possibleTapestryComponent(state, null, tokenStream.getLocalName().replace('.', '/'));
        return;
    }
    if (uri != null && uri.startsWith(LIB_NAMESPACE_URI_PREFIX)) {
        libraryNamespaceComponent(state);
        return;
    }
    if (TAPESTRY_PARAMETERS_URI.equals(uri)) {
        parameterElement(state);
        return;
    }
    // Just an ordinary element ... unless it has t:id or t:type
    possibleTapestryComponent(state, tokenStream.getLocalName(), null);
}
Also used : Version(org.apache.tapestry5.internal.services.SaxTemplateParser.Version)

Example 3 with Component

use of org.apache.tapestry5.annotations.Component 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 4 with Component

use of org.apache.tapestry5.annotations.Component in project tapestry-5 by apache.

the class ComponentDefaultProviderImplTest method no_matching_property_for_default.

@Test
public void no_matching_property_for_default() {
    String parameterName = "myparam";
    String id = "mycomponentid";
    ComponentResources resources = mockComponentResources();
    Component container = mockComponent();
    PropertyAccess access = mockPropertyAccess();
    ClassPropertyAdapter classPropertyAdapter = mockClassPropertyAdapter();
    BindingSource bindingSource = mockBindingSource();
    train_getId(resources, id);
    train_getContainer(resources, container);
    train_getAdapter(access, container, classPropertyAdapter);
    train_getPropertyAdapter(classPropertyAdapter, id, null);
    replay();
    ComponentDefaultProvider source = new ComponentDefaultProviderImpl(access, bindingSource, null, null, null);
    assertNull(source.defaultBinding(parameterName, resources));
    verify();
}
Also used : BindingSource(org.apache.tapestry5.services.BindingSource) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) Component(org.apache.tapestry5.runtime.Component) ComponentResources(org.apache.tapestry5.ComponentResources) PropertyAccess(org.apache.tapestry5.commons.services.PropertyAccess) ComponentDefaultProvider(org.apache.tapestry5.services.ComponentDefaultProvider) Test(org.testng.annotations.Test)

Example 5 with Component

use of org.apache.tapestry5.annotations.Component in project tapestry-5 by apache.

the class MutableComponentModelImplTest method add_duplicate_parameters_to_embedded.

@Test
public void add_duplicate_parameters_to_embedded() {
    Resource r = mockResource();
    Logger logger = mockLogger();
    replay();
    MutableComponentModel model = new MutableComponentModelImpl(CLASS_NAME, logger, r, null, false, null);
    MutableEmbeddedComponentModel fred = model.addEmbeddedComponent("fred", "Fred", COMPONENT_CLASS_NAME, false, null);
    fred.addParameter("city", "bedrock");
    try {
        fred.addParameter("city", "slateville");
        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "A value for parameter 'city' of embedded component fred (of component class org.example.components.Foo) has already been provided.");
    }
    verify();
}
Also used : Resource(org.apache.tapestry5.commons.Resource) MutableComponentModel(org.apache.tapestry5.model.MutableComponentModel) Logger(org.slf4j.Logger) MutableEmbeddedComponentModel(org.apache.tapestry5.model.MutableEmbeddedComponentModel) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)68 Component (org.apache.tapestry5.runtime.Component)43 ComponentResources (org.apache.tapestry5.ComponentResources)41 MarkupWriter (org.apache.tapestry5.MarkupWriter)18 ComponentModel (org.apache.tapestry5.model.ComponentModel)18 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)17 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)16 Binding (org.apache.tapestry5.Binding)14 Location (org.apache.tapestry5.commons.Location)14 Instantiator (org.apache.tapestry5.internal.services.Instantiator)12 Page (org.apache.tapestry5.internal.structure.Page)12 Messages (org.apache.tapestry5.commons.Messages)8 Resource (org.apache.tapestry5.commons.Resource)8 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)7 MutableComponentModel (org.apache.tapestry5.model.MutableComponentModel)7 BindingFactory (org.apache.tapestry5.services.BindingFactory)7 Logger (org.slf4j.Logger)7 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)6 BindingSource (org.apache.tapestry5.services.BindingSource)6 PropertyOverrides (org.apache.tapestry5.PropertyOverrides)5