Search in sources :

Example 91 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class StepCountUtil method isCachedContentRequest.

/**
 * Check if the request is for cached content.
 *
 * @param request the request being processed
 * @return true if content is cached, otherwise false
 */
public static boolean isCachedContentRequest(final Request request) {
    // Get target id on request
    String targetId = request.getParameter(Environment.TARGET_ID);
    if (targetId == null) {
        return false;
    }
    // Get target
    ComponentWithContext targetWithContext = WebUtilities.getComponentById(targetId, true);
    if (targetWithContext == null) {
        return false;
    }
    // Check for caching key
    WComponent target = targetWithContext.getComponent();
    UIContextHolder.pushContext(targetWithContext.getContext());
    try {
        // TODO Look at implementing CacheableTarget interface
        String key = null;
        if (target instanceof WContent) {
            key = ((WContent) target).getCacheKey();
        } else if (target instanceof WImage) {
            key = ((WImage) target).getCacheKey();
        } else if (target instanceof WVideo) {
            key = ((WVideo) target).getCacheKey();
        } else if (target instanceof WAudio) {
            key = ((WAudio) target).getCacheKey();
        }
        return !Util.empty(key);
    } finally {
        UIContextHolder.popContext();
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WContent(com.github.bordertech.wcomponents.WContent) WVideo(com.github.bordertech.wcomponents.WVideo) WImage(com.github.bordertech.wcomponents.WImage) ComponentWithContext(com.github.bordertech.wcomponents.ComponentWithContext) WAudio(com.github.bordertech.wcomponents.WAudio)

Example 92 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class TreeUtil method doTraverse.

/**
 * Internal implementation of tree traversal method.
 *
 * @param node the node to traverse.
 * @param visibleOnly if true, only visit visible components.
 * @param visitor the visitor to notify as the tree is traversed.
 * @return how the traversal should continue.
 */
private static VisitorResult doTraverse(final WComponent node, final boolean visibleOnly, final WComponentTreeVisitor visitor) {
    if (visibleOnly) {
        // Certain components have their visibility altered to implement custom processing.
        if (node instanceof WInvisibleContainer) {
            WComponent parent = node.getParent();
            // If inside a CardManager, skip the InvisibleContainer and process the visible card.
            if (parent instanceof WCardManager) {
                WComponent visible = ((WCardManager) node.getParent()).getVisible();
                if (visible == null) {
                    return VisitorResult.ABORT_BRANCH;
                }
                return doTraverse(visible, visibleOnly, visitor);
            } else if (parent instanceof WWindow) {
                // Abort branch if WWindow is not in ACTIVE state
                if (((WWindow) parent).getState() != WWindow.ACTIVE_STATE) {
                    return VisitorResult.ABORT_BRANCH;
                }
            }
        } else if (node instanceof WRepeatRoot) {
        // Let be processed.
        } else if (!node.isVisible()) {
            // For most components, we just need to see if they're marked as visible
            return VisitorResult.ABORT_BRANCH;
        }
    }
    VisitorResult result = visitor.visit(node);
    switch(result) {
        case ABORT_BRANCH:
            // Continue processing, but not down this branch
            return VisitorResult.CONTINUE;
        case CONTINUE:
            // Process repeater rows
            if (node instanceof WRepeater) {
                // Get parent repeater
                WRepeater repeater = (WRepeater) node;
                // Get row contexts
                List<UIContext> rowContextList = repeater.getRowContexts();
                WRepeatRoot repeatRoot = (WRepeatRoot) repeater.getRepeatedComponent().getParent();
                for (UIContext rowContext : rowContextList) {
                    UIContextHolder.pushContext(rowContext);
                    try {
                        result = doTraverse(repeatRoot, visibleOnly, visitor);
                    } finally {
                        UIContextHolder.popContext();
                    }
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            } else if (node instanceof Container) {
                Container container = (Container) node;
                for (int i = 0; i < container.getChildCount(); i++) {
                    result = doTraverse(container.getChildAt(i), visibleOnly, visitor);
                    if (VisitorResult.ABORT.equals(result)) {
                        return VisitorResult.ABORT;
                    }
                }
            }
            return VisitorResult.CONTINUE;
        default:
            // Abort entire traversal
            return VisitorResult.ABORT;
    }
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) VisitorResult(com.github.bordertech.wcomponents.util.WComponentTreeVisitor.VisitorResult) Container(com.github.bordertech.wcomponents.Container) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeatRoot(com.github.bordertech.wcomponents.WRepeater.WRepeatRoot) UIContext(com.github.bordertech.wcomponents.UIContext) WCardManager(com.github.bordertech.wcomponents.WCardManager) WWindow(com.github.bordertech.wcomponents.WWindow) WInvisibleContainer(com.github.bordertech.wcomponents.WInvisibleContainer) WRepeater(com.github.bordertech.wcomponents.WRepeater)

Example 93 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class WMenuItemGroupRenderer_Test method testDoPaint.

@Test
public void testDoPaint() throws IOException, SAXException, XpathException {
    String groupName = "WMenuItemGroupRenderer_Test.testDoPaint.groupName";
    WMenuItemGroup menuGroup = new WMenuItemGroup(groupName);
    WComponent wrapped = wrapMenuGroup(menuGroup);
    setActiveContext(createUIContext());
    assertXpathExists("//ui:menugroup", wrapped);
    assertXpathEvaluatesTo(groupName, "normalize-space(//ui:menugroup/ui:decoratedlabel)", wrapped);
    assertXpathEvaluatesTo(menuGroup.getId(), "//ui:menugroup/@id", wrapped);
    assertXpathNotExists("//ui:menugroup/ui:submenu", wrapped);
    assertXpathNotExists("//ui:menugroup/ui:menuitem", wrapped);
    assertXpathNotExists("//ui:menugroup/ui:separator", wrapped);
    menuGroup.addSeparator();
    assertXpathExists("//ui:menugroup/ui:separator", wrapped);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WMenuItemGroup(com.github.bordertech.wcomponents.WMenuItemGroup) Test(org.junit.Test)

Example 94 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class WTabGroupRenderer_Test method testXssEscaping.

@Test
public void testXssEscaping() throws IOException, SAXException, XpathException {
    WTabGroup tabGroup = new WTabGroup(getMaliciousContent());
    tabGroup.addTab(new WText("dummy"), "dummy", TabMode.CLIENT);
    WComponent wrapped = wrapTabGroup(tabGroup);
    assertSafeContent(wrapped);
    tabGroup.setToolTip(getMaliciousAttribute("ui:tab"));
    assertSafeContent(wrapped);
    tabGroup.setAccessibleText(getMaliciousAttribute("ui:tab"));
    assertSafeContent(wrapped);
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WText(com.github.bordertech.wcomponents.WText) WTabGroup(com.github.bordertech.wcomponents.WTabGroup) Test(org.junit.Test)

Example 95 with WComponent

use of com.github.bordertech.wcomponents.WComponent in project wcomponents by BorderTech.

the class InterceptorComponent_Test method testBackingComponentAccessors.

@Test
public void testBackingComponentAccessors() {
    WComponent backing = new WLabel();
    InterceptorComponent interceptor = new InterceptorComponent(backing);
    Assert.assertSame("Incorrect backing component returned", backing, interceptor.getBackingComponent());
    interceptor = new InterceptorComponent();
    interceptor.setBackingComponent(backing);
    Assert.assertSame("Incorrect backing component returned", backing, interceptor.getBackingComponent());
}
Also used : WComponent(com.github.bordertech.wcomponents.WComponent) WLabel(com.github.bordertech.wcomponents.WLabel) Test(org.junit.Test)

Aggregations

WComponent (com.github.bordertech.wcomponents.WComponent)107 Test (org.junit.Test)35 XmlStringBuilder (com.github.bordertech.wcomponents.XmlStringBuilder)30 UIContext (com.github.bordertech.wcomponents.UIContext)20 SystemException (com.github.bordertech.wcomponents.util.SystemException)16 DefaultWComponent (com.github.bordertech.wcomponents.DefaultWComponent)14 ComponentWithContext (com.github.bordertech.wcomponents.ComponentWithContext)8 WApplication (com.github.bordertech.wcomponents.WApplication)8 WLabel (com.github.bordertech.wcomponents.WLabel)8 AbstractWComponent (com.github.bordertech.wcomponents.AbstractWComponent)7 IOException (java.io.IOException)6 WebXmlRenderContext (com.github.bordertech.wcomponents.servlet.WebXmlRenderContext)5 Diagnostic (com.github.bordertech.wcomponents.validation.Diagnostic)5 PrintWriter (java.io.PrintWriter)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 Size (com.github.bordertech.wcomponents.Size)4 WRepeater (com.github.bordertech.wcomponents.WRepeater)4 WText (com.github.bordertech.wcomponents.WText)4 WTextField (com.github.bordertech.wcomponents.WTextField)4