Search in sources :

Example 36 with ComponentModel

use of org.apache.tapestry5.model.ComponentModel in project tapestry-5 by apache.

the class ComponentTemplateSourceImpl method getTemplate.

public ComponentTemplate getTemplate(ComponentModel componentModel, ComponentResourceSelector selector) {
    String componentName = componentModel.getComponentClassName();
    MultiKey key = new MultiKey(componentName, selector);
    // First cache is key to resource.
    Resource resource = templateResources.get(key);
    if (resource == null) {
        resource = locateTemplateResource(componentModel, selector);
        templateResources.put(key, resource);
    }
    // If we haven't yet parsed the template into the cache, do so now.
    ComponentTemplate result = templates.get(resource);
    if (result == null) {
        result = parseTemplate(resource);
        templates.put(resource, result);
    }
    return result;
}
Also used : MultiKey(org.apache.tapestry5.commons.util.MultiKey) Resource(org.apache.tapestry5.commons.Resource) ComponentTemplate(org.apache.tapestry5.internal.parser.ComponentTemplate)

Example 37 with ComponentModel

use of org.apache.tapestry5.model.ComponentModel in project tapestry-5 by apache.

the class PageActivationContextCollectorImpl method collectPageActivationContext.

public Object[] collectPageActivationContext(String pageName) {
    Boolean hasHandler = cache.get(pageName);
    if (hasHandler == null) {
        ComponentModel model = modelSource.getPageModel(pageName);
        hasHandler = model.handlesEvent(EventConstants.PASSIVATE);
        cache.put(pageName, hasHandler);
    }
    if (!hasHandler)
        return EMPTY;
    // Get or create a page instance and trigger the event.
    Page page = requestPageCache.get(pageName);
    ComponentPageElement element = page.getRootElement();
    final Holder<Object[]> holder = Holder.create();
    ComponentEventCallback callback = new ComponentEventCallback() {

        public boolean handleResult(Object result) {
            holder.put(typeCoercer.coerce(result, Object[].class));
            return true;
        }
    };
    element.triggerEvent(EventConstants.PASSIVATE, null, callback);
    if (!holder.hasValue())
        return EMPTY;
    return holder.get();
}
Also used : ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentModel(org.apache.tapestry5.model.ComponentModel) Page(org.apache.tapestry5.internal.structure.Page) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback)

Example 38 with ComponentModel

use of org.apache.tapestry5.model.ComponentModel in project tapestry-5 by apache.

the class MarkupWriterFactoryImpl method hasHTML5Doctype.

private boolean hasHTML5Doctype(Page page) {
    ComponentModel componentModel = page.getRootComponent().getComponentResources().getComponentModel();
    ComponentResourceSelector selector = componentRequestSelectorAnalyzer.buildSelectorForRequest();
    List<TemplateToken> tokens = templateSource.getTemplate(componentModel, selector).getTokens();
    DTDToken dtd = null;
    for (TemplateToken token : tokens) {
        if (token.getTokenType() == TokenType.DTD) {
            dtd = (DTDToken) token;
            break;
        }
    }
    return dtd != null && dtd.name.equalsIgnoreCase("html") && dtd.publicId == null && dtd.systemId == null;
}
Also used : ComponentResourceSelector(org.apache.tapestry5.services.pageload.ComponentResourceSelector) ComponentModel(org.apache.tapestry5.model.ComponentModel) TemplateToken(org.apache.tapestry5.internal.parser.TemplateToken) DTDToken(org.apache.tapestry5.internal.parser.DTDToken)

Example 39 with ComponentModel

use of org.apache.tapestry5.model.ComponentModel in project tapestry-5 by apache.

the class LinkDecorationListener method trigger.

private void trigger(String pageName, String eventType, Link link, Object parameters) {
    String pageClassName = resolver.resolvePageNameToClassName(pageName);
    ComponentModel model = modelSource.getModel(pageClassName);
    if (model.handlesEvent(eventType)) {
        Component page = componentSource.getPage(pageName);
        page.getComponentResources().triggerEvent(eventType, new Object[] { link, parameters }, null);
    }
}
Also used : ComponentModel(org.apache.tapestry5.model.ComponentModel) Component(org.apache.tapestry5.runtime.Component)

Example 40 with ComponentModel

use of org.apache.tapestry5.model.ComponentModel in project tapestry-5 by apache.

the class ComponentEventImplTest method unable_to_coerce.

@Test
public void unable_to_coerce() {
    ComponentEventCallback handler = mockComponentEventHandler();
    EventContext context = mockEventContext();
    ComponentPageElementResources resources = mockComponentPageElementResources();
    Logger logger = mockLogger();
    ComponentModel model = mockComponentModel();
    train_isDebugEnabled(logger, true);
    logger.debug(eq(TapestryMarkers.EVENT_HANDLER_METHOD), isA(String.class));
    train_toClass(resources, Integer.class.getName(), Integer.class);
    train_getCount(context, 1);
    expect(context.get(Integer.class, 0)).andThrow(new NumberFormatException("Not so easy, is it?"));
    replay();
    ComponentEvent event = new ComponentEventImpl("eventType", "someId", context, handler, resources, false, model, logger);
    event.setMethodDescription("foo.Bar.baz()");
    try {
        event.coerceContext(0, "java.lang.Integer");
        unreachable();
    } catch (IllegalArgumentException ex) {
        // Different JVMs will report the conversion error slightly differently,
        // so we don't try to check that part of the error message.
        assertTrue(ex.getMessage().startsWith("Exception in method foo.Bar.baz(), parameter #1:"));
    }
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) ComponentModel(org.apache.tapestry5.model.ComponentModel) ComponentPageElementResources(org.apache.tapestry5.internal.structure.ComponentPageElementResources) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Logger(org.slf4j.Logger) ComponentEventCallback(org.apache.tapestry5.ComponentEventCallback) Test(org.testng.annotations.Test)

Aggregations

ComponentModel (org.apache.tapestry5.model.ComponentModel)64 Test (org.testng.annotations.Test)48 ComponentResources (org.apache.tapestry5.ComponentResources)16 ComponentEventCallback (org.apache.tapestry5.ComponentEventCallback)15 Logger (org.slf4j.Logger)14 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)12 Instantiator (org.apache.tapestry5.internal.services.Instantiator)11 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)11 Resource (org.apache.tapestry5.commons.Resource)10 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)10 Component (org.apache.tapestry5.runtime.Component)10 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)9 EventContext (org.apache.tapestry5.EventContext)8 ClasspathResource (org.apache.tapestry5.ioc.internal.util.ClasspathResource)7 EmbeddedComponentModel (org.apache.tapestry5.model.EmbeddedComponentModel)7 ComponentTemplate (org.apache.tapestry5.internal.parser.ComponentTemplate)6 ComponentPageElementResources (org.apache.tapestry5.internal.structure.ComponentPageElementResources)6 ComponentResourceLocator (org.apache.tapestry5.services.pageload.ComponentResourceLocator)6 Messages (org.apache.tapestry5.commons.Messages)5 PersistentFieldStrategy (org.apache.tapestry5.services.PersistentFieldStrategy)5