diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPage.java b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPage.java index 4260327e36..4a56438770 100644 --- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPage.java +++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/component/AbstractUIPage.java @@ -28,11 +28,15 @@ import jakarta.faces.component.UIViewRoot; import jakarta.faces.component.behavior.ClientBehaviorContext; import jakarta.faces.component.behavior.ClientBehaviorHolder; +import jakarta.faces.component.visit.VisitCallback; +import jakarta.faces.component.visit.VisitContext; +import jakarta.faces.component.visit.VisitResult; import jakarta.faces.context.FacesContext; import java.lang.invoke.MethodHandles; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Iterator; +import java.util.Set; /** * {@link org.apache.myfaces.tobago.internal.taglib.component.PageTagDeclaration} @@ -90,28 +94,15 @@ public void markSubmittedForm(final FacesContext facesContext) { if (LOG.isDebugEnabled()) { LOG.debug("sourceId = '" + sourceId + "'"); } - command = viewRoot.findComponent(sourceId); + FindCommandVisitor visitor = new FindCommandVisitor(); + viewRoot.visitTree(VisitContext + .createVisitContext(facesContext, Set.of(sourceId), ComponentUtils.SET_SKIP_UNRENDERED), + visitor); + command = visitor.getCommand(); } else { LOG.warn("No sourceId found!"); } - if (command == null && sourceId != null) { - // If currentActionId component was inside a sheet the id contains the - // rowIndex and is therefore not found here. - // We do not need the row here because we want just to find the - // related form, so removing the rowIndex will help here. - sourceId = cutIteratorFromId(sourceId); - try { - command = viewRoot.findComponent(sourceId); - } catch (final Exception e) { - // ignore - if (LOG.isTraceEnabled()) { - LOG.trace("sourceId='{}'", sourceId); - LOG.trace("Exception in findComponent", e); - } - } - } - if (LOG.isTraceEnabled()) { LOG.trace(sourceId); LOG.trace("command:{}", command); @@ -138,41 +129,22 @@ public void markSubmittedForm(final FacesContext facesContext) { } } - // TODO: Remove this method if proven this never happens anymore - // TODO: This workaround is stil needed for Mojarra - // TODO: Otherwise actions in tree/sheet will not be detected - protected String cutIteratorFromId(final String sourceId) { - - final char[] chars = sourceId.toCharArray(); - final int n = chars.length; - final char colon = getFacesContext().getNamingContainerSeparatorChar(); - final StringBuilder builder = new StringBuilder(n); - char lastInBuilder = ' '; // any non-colon - for (char c : chars) { - if (c == colon) { // colon - if (lastInBuilder != colon) { - builder.append(c); - lastInBuilder = c; - } - } else if ('0' <= c && c <= '9') { // number - - } else { // any other - builder.append(c); - lastInBuilder = c; - } + public abstract String getLabel(); + + public abstract String getFocusId(); + + private static class FindCommandVisitor implements VisitCallback { + private UIComponent command; + + @Override + public VisitResult visit(VisitContext context, UIComponent target) { + command = target; + return VisitResult.COMPLETE; } - if (builder.length() == n) { - return sourceId; - } else if (lastInBuilder == colon) { - builder.deleteCharAt(builder.length() - 1); - return builder.toString(); - } else { - return builder.toString(); + public UIComponent getCommand() { + return command; } } - public abstract String getLabel(); - - public abstract String getFocusId(); } diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/component/AbstractUIPageUnitTest.java b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/component/AbstractUIPageUnitTest.java deleted file mode 100644 index 5adc5af62e..0000000000 --- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/component/AbstractUIPageUnitTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.myfaces.tobago.internal.component; - -import org.apache.myfaces.tobago.component.RendererTypes; -import org.apache.myfaces.tobago.component.Tags; -import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase; -import org.apache.myfaces.tobago.util.ComponentUtils; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class AbstractUIPageUnitTest extends AbstractTobagoTestBase { - - @Test - public void testCutIteratorFromId() { - - final AbstractUIPage page = (AbstractUIPage) ComponentUtils.createComponent( - facesContext, Tags.page.componentType(), RendererTypes.Page, null); - - Assertions.assertEquals("abc", page.cutIteratorFromId("abc")); - - Assertions.assertEquals("a:b:c", page.cutIteratorFromId("a:b:c")); - - Assertions.assertEquals("a:c", page.cutIteratorFromId("a:5:c")); - - Assertions.assertEquals("a:c", page.cutIteratorFromId("a:55555555555555:c")); - - Assertions.assertEquals("a:c", page.cutIteratorFromId("a:555:555:555:55555:c")); - - Assertions.assertEquals("", page.cutIteratorFromId("")); - - Assertions.assertEquals("", page.cutIteratorFromId("5")); - - Assertions.assertEquals("sheet", page.cutIteratorFromId("sheet:5")); - } -}