You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2023/08/22 12:18:25 UTC
[myfaces] branch 2.3.x updated: MYFACES-4624
This is an automated email from the ASF dual-hosted git repository.
tandraschko pushed a commit to branch 2.3.x
in repository https://gitbox.apache.org/repos/asf/myfaces.git
The following commit(s) were added to refs/heads/2.3.x by this push:
new dc863651c MYFACES-4624
dc863651c is described below
commit dc863651c91a8ded00a73694179e5fa596c3c02a
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Tue Aug 22 14:18:08 2023 +0200
MYFACES-4624
---
.../search/SearchExpressionHandlerImpl.java | 1725 ++++++++++----------
.../component/search/SearchExpressionImplTest.java | 835 +++++-----
.../apache/myfaces/component/search/search1.xhtml | 3 +
3 files changed, 1292 insertions(+), 1271 deletions(-)
diff --git a/impl/src/main/java/org/apache/myfaces/component/search/SearchExpressionHandlerImpl.java b/impl/src/main/java/org/apache/myfaces/component/search/SearchExpressionHandlerImpl.java
index a0205a591..c5dbddbf9 100644
--- a/impl/src/main/java/org/apache/myfaces/component/search/SearchExpressionHandlerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/component/search/SearchExpressionHandlerImpl.java
@@ -1,860 +1,865 @@
-/*
- * 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.component.search;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import javax.faces.FacesException;
-import javax.faces.component.ContextCallback;
-import javax.faces.component.NamingContainer;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIViewRoot;
-import javax.faces.component.search.ComponentNotFoundException;
-import javax.faces.component.search.SearchExpressionContext;
-import javax.faces.component.search.SearchExpressionHandler;
-import javax.faces.component.search.SearchExpressionHint;
-import javax.faces.component.search.SearchKeywordContext;
-import javax.faces.context.FacesContext;
-import org.apache.myfaces.shared.renderkit.html.util.SharedStringBuilder;
-
-/**
- *
- */
-public class SearchExpressionHandlerImpl extends SearchExpressionHandler
-{
- private static final String SB_SPLIT = SearchExpressionHandlerImpl.class.getName() + "#split";
-
- protected void addHint(SearchExpressionContext searchExpressionContext, SearchExpressionHint hint)
- {
- // already available
- if (!searchExpressionContext.getExpressionHints().contains(hint))
- {
- searchExpressionContext.getExpressionHints().add(hint);
- }
- }
-
- protected boolean isHintSet(SearchExpressionContext searchExpressionContext, SearchExpressionHint hint)
- {
- if (searchExpressionContext.getExpressionHints() == null)
- {
- return false;
- }
-
- return searchExpressionContext.getExpressionHints().contains(hint);
- }
-
- @Override
- public String resolveClientId(SearchExpressionContext searchExpressionContext, String expression)
- {
- if (expression == null)
- {
- expression = "";
- }
- else
- {
- expression = expression.trim();
- }
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- addHint(searchExpressionContext, SearchExpressionHint.RESOLVE_SINGLE_COMPONENT);
-
- if (handler.isPassthroughExpression(searchExpressionContext, expression))
- {
- return expression;
- }
- else
- {
- CollectClientIdCallback callback = new CollectClientIdCallback();
-
- if (!expression.isEmpty())
- {
- handler.invokeOnComponent(
- searchExpressionContext, searchExpressionContext.getSource(), expression, callback);
- }
-
- if (!callback.isClientIdFound())
- {
- if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
- {
- //Ignore
- }
- else
- {
- throw new ComponentNotFoundException("Cannot find component for expression \""
- + expression + "\" referenced from \""
- + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
- }
- }
- return callback.getClientId();
- }
- }
-
- private static class CollectClientIdCallback implements ContextCallback
- {
- private String clientId = null;
-
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- if (clientId == null)
- {
- clientId = target.getClientId(context);
- }
- }
-
- private String getClientId()
- {
- return clientId;
- }
-
- private boolean isClientIdFound()
- {
- return clientId != null;
- }
- }
-
- @Override
- public List<String> resolveClientIds(SearchExpressionContext searchExpressionContext, String expressions)
- {
- if (expressions == null)
- {
- expressions = "";
- }
- else
- {
- expressions = expressions.trim();
- }
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- CollectClientIdsCallback callback = new CollectClientIdsCallback();
-
- if (!expressions.isEmpty())
- {
- for (String expression : handler.splitExpressions(facesContext, expressions))
- {
- if (handler.isPassthroughExpression(searchExpressionContext, expression))
- {
- // It will be resolved in the client, just add the expression.
- callback.addClientId(expression);
- }
- else
- {
- handler.invokeOnComponent(
- searchExpressionContext, searchExpressionContext.getSource(), expression, callback);
- }
- }
- }
-
- if (!callback.isClientIdFound())
- {
- if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
- {
- //Ignore
- }
- else
- {
- throw new ComponentNotFoundException("Cannot find component for expression \""
- + expressions + "\" referenced from \""
- + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
- }
- }
- return callback.getClientIds();
- }
-
- private static class CollectClientIdsCallback implements ContextCallback
- {
- private List<String> clientIds = null;
-
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- if (clientIds == null)
- {
- clientIds = new ArrayList<String>();
- }
- clientIds.add(target.getClientId(context));
- }
-
- private List<String> getClientIds()
- {
- return clientIds == null ? Collections.emptyList() : clientIds;
- }
-
- private boolean isClientIdFound()
- {
- return clientIds != null;
- }
-
- private void addClientId(String clientId)
- {
- if (clientIds == null)
- {
- clientIds = new ArrayList<String>();
- }
- clientIds.add(clientId);
- }
- }
-
- @Override
- public void resolveComponent(SearchExpressionContext searchExpressionContext, String expression,
- ContextCallback callback)
- {
- if (expression == null)
- {
- expression = "";
- }
- else
- {
- expression = expression.trim();
- }
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- SingleInvocationCallback checkCallback = new SingleInvocationCallback(callback);
-
- addHint(searchExpressionContext, SearchExpressionHint.RESOLVE_SINGLE_COMPONENT);
-
- if (!expression.isEmpty())
- {
- handler.invokeOnComponent(searchExpressionContext, searchExpressionContext.getSource(),
- expression, checkCallback);
- }
-
- if (!checkCallback.isInvoked())
- {
- if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
- {
- //Ignore
- }
- else
- {
- throw new ComponentNotFoundException("Cannot find component for expression \""
- + expression + "\" referenced from \""
- + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
- }
- }
- }
-
- private static class SingleInvocationCallback implements ContextCallback
- {
- private boolean invoked;
-
- private final ContextCallback innerCallback;
-
- public SingleInvocationCallback(ContextCallback innerCallback)
- {
- this.innerCallback = innerCallback;
- this.invoked = false;
- }
-
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- if (!isInvoked())
- {
- try
- {
- innerCallback.invokeContextCallback(context, target);
- }
- finally
- {
- invoked = true;
- }
- }
- }
-
- public boolean isInvoked()
- {
- return invoked;
- }
- }
-
- @Override
- public void resolveComponents(SearchExpressionContext searchExpressionContext, String expressions,
- ContextCallback callback)
- {
- if (expressions == null)
- {
- expressions = "";
- }
- else
- {
- expressions = expressions.trim();
- }
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- MultipleInvocationCallback checkCallback = new MultipleInvocationCallback(callback);
-
- if (!expressions.isEmpty())
- {
- for (String expression : handler.splitExpressions(facesContext, expressions))
- {
- handler.invokeOnComponent(searchExpressionContext, searchExpressionContext.getSource(),
- expression, checkCallback);
- }
- }
-
- if (!checkCallback.isInvoked())
- {
- if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
- {
- //Ignore
- }
- else
- {
- throw new ComponentNotFoundException("Cannot find component for expression \""
- + expressions + "\" referenced from \""
- + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
- }
- }
- }
-
- private static class MultipleInvocationCallback implements ContextCallback
- {
- private boolean invoked;
-
- private final ContextCallback innerCallback;
-
- public MultipleInvocationCallback(ContextCallback innerCallback)
- {
- this.innerCallback = innerCallback;
- this.invoked = false;
- }
-
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- try
- {
- innerCallback.invokeContextCallback(context, target);
- }
- finally
- {
- invoked = true;
- }
- }
-
- public boolean isInvoked()
- {
- return invoked;
- }
- }
-
- @Override
- public void invokeOnComponent(final SearchExpressionContext searchExpressionContext,
- UIComponent previous, String topExpression, ContextCallback topCallback)
- {
- if (topExpression == null)
- {
- topExpression = "";
- }
- else
- {
- topExpression = topExpression.trim();
- }
-
- // Command pattern to apply the keyword or command to the base and then invoke the callback
- FacesContext facesContext = searchExpressionContext.getFacesContext();
-
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- //Step 1: find base
- // Case ':' (root)
- char separatorChar = facesContext.getNamingContainerSeparatorChar();
- if (topExpression.charAt(0) == separatorChar)
- {
- UIComponent findBase = SearchComponentUtils.getRootComponent(previous);
- handler.invokeOnComponent(searchExpressionContext, findBase, topExpression.substring(1), topCallback);
- return;
- }
-
- //Step 2: Once you have a base where you can start, apply an expression
- if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
- {
- // A keyword means apply a command over the current source using an expression and the result must be
- // feedback into the algorithm.
-
- String command = extractKeyword(topExpression, 1, separatorChar);
- final String remaining =
- command.length()+1 < topExpression.length() ?
- topExpression.substring(1+command.length()+1) : null;
-
- final ContextCallback parentCallback = topCallback;
-
- // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
- // @root, @this , all commands change the source to be applied the action
- if (remaining != null)
- {
- if (facesContext.getApplication().getSearchKeywordResolver().isLeaf(searchExpressionContext, command))
- {
- throw new FacesException("Expression cannot have keywords or ids at the right side: "+command);
- }
- this.applyKeyword(searchExpressionContext, previous, command, remaining, new ContextCallback()
- {
- @Override
- public void invokeContextCallback(FacesContext facesContext, UIComponent target)
- {
- handler.invokeOnComponent(
- searchExpressionContext, target, remaining, parentCallback);
- }
- });
- }
- else
- {
- // Command completed, apply parent callback
- this.applyKeyword(searchExpressionContext, previous, command, null, parentCallback);
- }
-
- }
- else
- {
-
- //Split expression into tokens and apply loop
- String nextExpression = null;
- String expression;
- if (topExpression.indexOf(":@") > 0)
- {
- int idx = topExpression.indexOf(":@");
- nextExpression = topExpression.substring(idx+1);
- expression = topExpression.substring(0, idx);
- }
- else
- {
- expression = topExpression;
- }
-
- // Use findComponent(...) passing the expression provided
- UIComponent target = previous.findComponent(expression);
- if (target == null)
- {
- // If no component is found ...
- // First try to find the base component.
-
- // Extract the base id from the expression string
- int idx = expression.indexOf(separatorChar);
- String base = idx > 0 ? expression.substring(0, idx) : expression;
-
- // From the context component clientId, check if the base is part of the clientId
- String contextClientId = previous.getClientId(facesContext);
- int startCommon = contextClientId.lastIndexOf(base+facesContext.getNamingContainerSeparatorChar());
- if (startCommon >= 0
- && (startCommon == 0 || contextClientId.charAt(startCommon-1) == separatorChar )
- && (startCommon+base.length() <= contextClientId.length()-1 ||
- contextClientId.charAt(startCommon+base.length()+1) == separatorChar ))
- {
- // If there is a match, try to find a the first parent component whose id is equals to
- // the base id
- UIComponent parent = previous;
- while (parent != null )
- {
- if (base.equals(parent.getId()) && parent instanceof NamingContainer)
- {
- break;
- }
- else
- {
- parent = parent.getParent();
- }
- }
-
- // if a base component is found ...
- if (parent != null)
- {
- target = parent.findComponent(expression);
- if (target == null && !searchExpressionContext.getExpressionHints().contains(
- SearchExpressionHint.SKIP_VIRTUAL_COMPONENTS))
- {
- contextClientId = parent.getClientId(facesContext);
- // If no component is found,
- String targetClientId = contextClientId.substring(0, startCommon+base.length()) +
- expression.substring(base.length());
-
- if (nextExpression != null)
- {
- final String childExpression = nextExpression;
-
- parent.invokeOnComponent(facesContext, targetClientId, new ContextCallback()
- {
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- handler.invokeOnComponent(
- searchExpressionContext, target, childExpression, topCallback);
- }
- });
- }
- else
- {
- parent.invokeOnComponent(facesContext, targetClientId, topCallback);
- }
- return;
- }
- }
- }
- }
-
- if (target != null)
- {
- if (nextExpression != null)
- {
- handler.invokeOnComponent(searchExpressionContext, target, nextExpression, topCallback);
- }
- else
- {
- topCallback.invokeContextCallback(facesContext, target);
- }
-
- return;
- }
-
- // At this point if the algorithm hasn't returned and the topExpression does not have any separator char
- // we need to do the search backward using findComponent.
- if (target == null
- && searchExpressionContext.getSource() == previous
- && (topExpression.indexOf(separatorChar) == -1) )
- {
- UIComponent baseNC = previous.getNamingContainer();
- if (baseNC != null && baseNC.getParent() != null)
- {
- UIComponent parentNC = getParentNamingContainerUIViewRoot(baseNC.getParent());
- while (target == null && parentNC != null)
- {
- UIComponent parent = parentNC.getParent();
- target = parentNC.findComponent(expression);
- if (parent != null)
- {
- parentNC = getParentNamingContainerUIViewRoot(parent);
- }
- else
- {
- parentNC = null;
- }
- }
- if (target != null)
- {
- topCallback.invokeContextCallback(facesContext, target);
- return;
- }
- }
- }
-
- topCallback.invokeContextCallback(facesContext, previous);
- }
-
- }
-
- private static UIComponent getParentNamingContainerUIViewRoot(UIComponent component)
- {
- do
- {
- if (component instanceof NamingContainer || component instanceof UIViewRoot)
- {
- return component;
- }
-
- component = component.getParent();
- } while (component != null);
- return null;
- }
-
- // take the command and resolve it using the chain of responsibility pattern.
- protected void applyKeyword(SearchExpressionContext searchExpressionContext, UIComponent last,
- String command, String remainingExpression, ContextCallback topCallback)
- {
- SearchKeywordContext searchContext =
- new SearchKeywordContext(searchExpressionContext, topCallback, remainingExpression);
-
- searchExpressionContext.getFacesContext().getApplication()
- .getSearchKeywordResolver().resolve(searchContext, last, command);
- }
-
- @Override
- public boolean isPassthroughExpression(SearchExpressionContext searchExpressionContext, String topExpression)
- {
- if (topExpression == null || topExpression.trim().isEmpty())
- {
- return false;
- }
-
- topExpression = topExpression.trim();
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
-
- //Step 1: find base
- // Case ':' (root)
- char separatorChar = facesContext.getNamingContainerSeparatorChar();
- if (topExpression.charAt(0) == separatorChar)
- {
- // only keywords are passthrough expressions.
- return false;
- }
-
- //Step 2: Once you have a base where you can start, apply an expression
- if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
- {
- // A keyword means apply a command over the current source using an expression and the result must be
- // feedback into the algorithm.
-
- String command = extractKeyword(topExpression, 1, separatorChar);
- final String remaining =
- command.length()+1 < topExpression.length() ?
- topExpression.substring(1+command.length()+1) : null;
-
- final SearchExpressionHandler currentInstance =
- facesContext.getApplication().getSearchExpressionHandler();
-
- // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
- // @root, @this , all commands change the source to be applied the action
- boolean passthrough = facesContext.getApplication().getSearchKeywordResolver().isPassthrough(
- searchExpressionContext, command);
-
- if (passthrough)
- {
- return remaining != null ?
- currentInstance.isPassthroughExpression(searchExpressionContext, remaining) : true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- // Only keywords are valid to be passthrough. If it contains a chain of ids, this can only be resolved
- // server side, because the tree structure and related clientId logic is only available server side.
- return false;
- }
- }
-
- @Override
- public boolean isValidExpression(SearchExpressionContext searchExpressionContext, String topExpression)
- {
- if (topExpression == null || topExpression.trim().isEmpty())
- {
- return true;
- }
-
- topExpression = topExpression.trim();
-
- FacesContext facesContext = searchExpressionContext.getFacesContext();
- // Command pattern to apply the keyword or command to the base and then invoke the callback
- boolean isValid = true;
- //Step 1: find base
- // Case ':' (root)
- char separatorChar = facesContext.getNamingContainerSeparatorChar();
- if (topExpression.charAt(0) == separatorChar)
- {
- return facesContext.getApplication().getSearchExpressionHandler().isValidExpression(
- searchExpressionContext, topExpression.substring(1));
- }
-
- //Step 2: Once you have a base where you can start, apply an expression
- if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
- {
- // A keyword means apply a command over the current source using an expression and the result must be
- // feedback into the algorithm.
-
- String command = extractKeyword(topExpression, 1, separatorChar);
- final String remaining =
- command.length()+1 < topExpression.length() ?
- topExpression.substring(1+command.length()+1) : null;
-
- final SearchExpressionHandler currentInstance =
- facesContext.getApplication().getSearchExpressionHandler();
-
- // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
- // @root, @this , all commands change the source to be applied the action
- isValid = facesContext.getApplication().getSearchKeywordResolver().isResolverForKeyword(
- searchExpressionContext, command);
- if (remaining != null)
- {
- if (facesContext.getApplication().getSearchKeywordResolver().isLeaf(
- searchExpressionContext, command))
- {
- isValid = false;
- }
- return !isValid ? false : currentInstance.isValidExpression(searchExpressionContext, remaining);
- }
- }
- else
- {
- //Split expression into tokens and apply loop
- String nextExpression = null;
- String expression = null;
- if (topExpression.indexOf(":@") > 0)
- {
- int idx = topExpression.indexOf(":@");
- nextExpression = topExpression.substring(idx+1);
- expression = topExpression.substring(0,idx);
- }
- else
- {
- expression = topExpression;
- }
-
- //Check expression
- for (int i = 0; i < expression.length(); i++)
- {
- char c = expression.charAt(i);
- if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == separatorChar)
- {
- //continue
- }
- else
- {
- isValid = false;
- }
- }
-
- if (nextExpression != null)
- {
- return !isValid ? false : facesContext.getApplication().getSearchExpressionHandler()
- .isValidExpression(searchExpressionContext, nextExpression);
- }
- }
- return isValid;
- }
-
- private static String extractKeyword(String expression, int startIndex, char separatorChar)
- {
- int parenthesesCounter = -1;
- int count = -1;
- for (int i = startIndex; i < expression.length(); i++)
- {
- char c = expression.charAt(i);
- if (c == '(')
- {
- if (parenthesesCounter == -1)
- {
- parenthesesCounter = 0;
- }
- parenthesesCounter++;
- }
- if (c == ')')
- {
- parenthesesCounter--;
- }
- if (parenthesesCounter == 0)
- {
- //Close first parentheses
- count = i+1;
- break;
- }
- if (parenthesesCounter == -1)
- {
- if (c == separatorChar)
- {
- count = i;
- break;
- }
- }
- }
- if (count == -1)
- {
- return expression.substring(startIndex);
- }
- else
- {
- return expression.substring(startIndex, count);
- }
- }
-
- @Override
- public String[] splitExpressions(FacesContext context, String expressions)
- {
- // split expressions by blank or comma (and ignore blank and commas inside brackets)
- String[] splittedExpressions = split(context, expressions, EXPRESSION_SEPARATOR_CHARS);
- return splittedExpressions;
- }
-
- private static String[] split(FacesContext context, String value, char... separators)
- {
- if (value == null)
- {
- return null;
- }
-
- List<String> tokens = new ArrayList<String>();
- StringBuilder buffer = SharedStringBuilder.get(context, SB_SPLIT);
-
- int parenthesesCounter = 0;
-
- char[] charArray = value.toCharArray();
-
- for (char c : charArray)
- {
- if (c == '(')
- {
- parenthesesCounter++;
- }
-
- if (c == ')')
- {
- parenthesesCounter--;
- }
-
- if (parenthesesCounter == 0)
- {
- boolean isSeparator = false;
- for (char separator : separators)
- {
- if (c == separator)
- {
- isSeparator = true;
- }
- }
-
- if (isSeparator)
- {
- // lets add token inside buffer to our tokens
- String bufferString = buffer.toString().trim();
- if (bufferString.length() > 0)
- {
- tokens.add(bufferString);
- }
- // now we need to clear buffer
- buffer.delete(0, buffer.length());
- }
- else
- {
- buffer.append(c);
- }
- }
- else
- {
- buffer.append(c);
- }
- }
-
- // lets not forget about part after the separator
- tokens.add(buffer.toString());
-
- return tokens.toArray(new String[tokens.size()]);
- }
-
-}
\ No newline at end of file
+/*
+ * 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.component.search;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import javax.faces.FacesException;
+import javax.faces.component.ContextCallback;
+import javax.faces.component.NamingContainer;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
+import javax.faces.component.search.ComponentNotFoundException;
+import javax.faces.component.search.SearchExpressionContext;
+import javax.faces.component.search.SearchExpressionHandler;
+import javax.faces.component.search.SearchExpressionHint;
+import javax.faces.component.search.SearchKeywordContext;
+import javax.faces.context.FacesContext;
+import org.apache.myfaces.shared.renderkit.html.util.SharedStringBuilder;
+
+/**
+ *
+ */
+public class SearchExpressionHandlerImpl extends SearchExpressionHandler
+{
+ private static final String SB_SPLIT = SearchExpressionHandlerImpl.class.getName() + "#split";
+
+ protected void addHint(SearchExpressionContext searchExpressionContext, SearchExpressionHint hint)
+ {
+ // already available
+ if (!searchExpressionContext.getExpressionHints().contains(hint))
+ {
+ searchExpressionContext.getExpressionHints().add(hint);
+ }
+ }
+
+ protected boolean isHintSet(SearchExpressionContext searchExpressionContext, SearchExpressionHint hint)
+ {
+ if (searchExpressionContext.getExpressionHints() == null)
+ {
+ return false;
+ }
+
+ return searchExpressionContext.getExpressionHints().contains(hint);
+ }
+
+ @Override
+ public String resolveClientId(SearchExpressionContext searchExpressionContext, String expression)
+ {
+ if (expression == null)
+ {
+ expression = "";
+ }
+ else
+ {
+ expression = expression.trim();
+ }
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ addHint(searchExpressionContext, SearchExpressionHint.RESOLVE_SINGLE_COMPONENT);
+
+ if (handler.isPassthroughExpression(searchExpressionContext, expression))
+ {
+ return expression;
+ }
+ else
+ {
+ CollectClientIdCallback callback = new CollectClientIdCallback();
+
+ if (!expression.isEmpty())
+ {
+ handler.invokeOnComponent(
+ searchExpressionContext, searchExpressionContext.getSource(), expression, callback);
+ }
+
+ if (!callback.isClientIdFound())
+ {
+ if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
+ {
+ //Ignore
+ }
+ else
+ {
+ throw new ComponentNotFoundException("Cannot find component for expression \""
+ + expression + "\" referenced from \""
+ + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
+ }
+ }
+ return callback.getClientId();
+ }
+ }
+
+ private static class CollectClientIdCallback implements ContextCallback
+ {
+ private String clientId = null;
+
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ if (clientId == null)
+ {
+ clientId = target.getClientId(context);
+ }
+ }
+
+ private String getClientId()
+ {
+ return clientId;
+ }
+
+ private boolean isClientIdFound()
+ {
+ return clientId != null;
+ }
+ }
+
+ @Override
+ public List<String> resolveClientIds(SearchExpressionContext searchExpressionContext, String expressions)
+ {
+ if (expressions == null)
+ {
+ expressions = "";
+ }
+ else
+ {
+ expressions = expressions.trim();
+ }
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ CollectClientIdsCallback callback = new CollectClientIdsCallback();
+
+ if (!expressions.isEmpty())
+ {
+ for (String expression : handler.splitExpressions(facesContext, expressions))
+ {
+ if (handler.isPassthroughExpression(searchExpressionContext, expression))
+ {
+ // It will be resolved in the client, just add the expression.
+ callback.addClientId(expression);
+ }
+ else
+ {
+ handler.invokeOnComponent(
+ searchExpressionContext, searchExpressionContext.getSource(), expression, callback);
+ }
+ }
+ }
+
+ if (!callback.isClientIdFound())
+ {
+ if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
+ {
+ //Ignore
+ }
+ else
+ {
+ throw new ComponentNotFoundException("Cannot find component for expression \""
+ + expressions + "\" referenced from \""
+ + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
+ }
+ }
+ return callback.getClientIds();
+ }
+
+ private static class CollectClientIdsCallback implements ContextCallback
+ {
+ private List<String> clientIds = null;
+
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ if (clientIds == null)
+ {
+ clientIds = new ArrayList<String>();
+ }
+ clientIds.add(target.getClientId(context));
+ }
+
+ private List<String> getClientIds()
+ {
+ return clientIds == null ? Collections.emptyList() : clientIds;
+ }
+
+ private boolean isClientIdFound()
+ {
+ return clientIds != null;
+ }
+
+ private void addClientId(String clientId)
+ {
+ if (clientIds == null)
+ {
+ clientIds = new ArrayList<String>();
+ }
+ clientIds.add(clientId);
+ }
+ }
+
+ @Override
+ public void resolveComponent(SearchExpressionContext searchExpressionContext, String expression,
+ ContextCallback callback)
+ {
+ if (expression == null)
+ {
+ expression = "";
+ }
+ else
+ {
+ expression = expression.trim();
+ }
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ SingleInvocationCallback checkCallback = new SingleInvocationCallback(callback);
+
+ addHint(searchExpressionContext, SearchExpressionHint.RESOLVE_SINGLE_COMPONENT);
+
+ if (!expression.isEmpty())
+ {
+ handler.invokeOnComponent(searchExpressionContext, searchExpressionContext.getSource(),
+ expression, checkCallback);
+ }
+
+ if (!checkCallback.isInvoked())
+ {
+ if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
+ {
+ //Ignore
+ }
+ else
+ {
+ throw new ComponentNotFoundException("Cannot find component for expression \""
+ + expression + "\" referenced from \""
+ + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
+ }
+ }
+ }
+
+ private static class SingleInvocationCallback implements ContextCallback
+ {
+ private boolean invoked;
+
+ private final ContextCallback innerCallback;
+
+ public SingleInvocationCallback(ContextCallback innerCallback)
+ {
+ this.innerCallback = innerCallback;
+ this.invoked = false;
+ }
+
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ if (!isInvoked())
+ {
+ try
+ {
+ innerCallback.invokeContextCallback(context, target);
+ }
+ finally
+ {
+ invoked = true;
+ }
+ }
+ }
+
+ public boolean isInvoked()
+ {
+ return invoked;
+ }
+ }
+
+ @Override
+ public void resolveComponents(SearchExpressionContext searchExpressionContext, String expressions,
+ ContextCallback callback)
+ {
+ if (expressions == null)
+ {
+ expressions = "";
+ }
+ else
+ {
+ expressions = expressions.trim();
+ }
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ MultipleInvocationCallback checkCallback = new MultipleInvocationCallback(callback);
+
+ if (!expressions.isEmpty())
+ {
+ for (String expression : handler.splitExpressions(facesContext, expressions))
+ {
+ handler.invokeOnComponent(searchExpressionContext, searchExpressionContext.getSource(),
+ expression, checkCallback);
+ }
+ }
+
+ if (!checkCallback.isInvoked())
+ {
+ if (isHintSet(searchExpressionContext, SearchExpressionHint.IGNORE_NO_RESULT))
+ {
+ //Ignore
+ }
+ else
+ {
+ throw new ComponentNotFoundException("Cannot find component for expression \""
+ + expressions + "\" referenced from \""
+ + searchExpressionContext.getSource().getClientId(facesContext) + "\".");
+ }
+ }
+ }
+
+ private static class MultipleInvocationCallback implements ContextCallback
+ {
+ private boolean invoked;
+
+ private final ContextCallback innerCallback;
+
+ public MultipleInvocationCallback(ContextCallback innerCallback)
+ {
+ this.innerCallback = innerCallback;
+ this.invoked = false;
+ }
+
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ try
+ {
+ innerCallback.invokeContextCallback(context, target);
+ }
+ finally
+ {
+ invoked = true;
+ }
+ }
+
+ public boolean isInvoked()
+ {
+ return invoked;
+ }
+ }
+
+ @Override
+ public void invokeOnComponent(final SearchExpressionContext searchExpressionContext,
+ UIComponent previous, String topExpression, ContextCallback topCallback)
+ {
+ if (topExpression == null)
+ {
+ topExpression = "";
+ }
+ else
+ {
+ topExpression = topExpression.trim();
+ }
+
+ // Command pattern to apply the keyword or command to the base and then invoke the callback
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ //Step 1: find base
+ // Case ':' (root)
+ char separatorChar = facesContext.getNamingContainerSeparatorChar();
+ if (topExpression.charAt(0) == separatorChar)
+ {
+ UIComponent findBase = SearchComponentUtils.getRootComponent(previous);
+ handler.invokeOnComponent(searchExpressionContext, findBase, topExpression.substring(1), topCallback);
+ return;
+ }
+
+ //Step 2: Once you have a base where you can start, apply an expression
+ if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
+ {
+ // A keyword means apply a command over the current source using an expression and the result must be
+ // feedback into the algorithm.
+
+ String command = extractKeyword(topExpression, 1, separatorChar);
+ final String remaining =
+ command.length()+1 < topExpression.length() ?
+ topExpression.substring(1+command.length()+1) : null;
+
+ final ContextCallback parentCallback = topCallback;
+
+ // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
+ // @root, @this , all commands change the source to be applied the action
+ if (remaining != null)
+ {
+ if (facesContext.getApplication().getSearchKeywordResolver().isLeaf(searchExpressionContext, command))
+ {
+ throw new FacesException("Expression cannot have keywords or ids at the right side: "+command);
+ }
+ this.applyKeyword(searchExpressionContext, previous, command, remaining, new ContextCallback()
+ {
+ @Override
+ public void invokeContextCallback(FacesContext facesContext, UIComponent target)
+ {
+ handler.invokeOnComponent(
+ searchExpressionContext, target, remaining, parentCallback);
+ }
+ });
+ }
+ else
+ {
+ // Command completed, apply parent callback
+ this.applyKeyword(searchExpressionContext, previous, command, null, parentCallback);
+ }
+
+ }
+ else
+ {
+
+ //Split expression into tokens and apply loop
+ String nextExpression = null;
+ String expression;
+ if (topExpression.indexOf(":@") > 0)
+ {
+ int idx = topExpression.indexOf(":@");
+ nextExpression = topExpression.substring(idx+1);
+ expression = topExpression.substring(0, idx);
+ }
+ else
+ {
+ expression = topExpression;
+ }
+
+ // Use findComponent(...) passing the expression provided
+ UIComponent target = previous.findComponent(expression);
+ if (target == null)
+ {
+ // If no component is found ...
+ // First try to find the base component.
+
+ // Extract the base id from the expression string
+ int idx = expression.indexOf(separatorChar);
+ String base = idx > 0 ? expression.substring(0, idx) : expression;
+
+ // From the context component clientId, check if the base is part of the clientId
+ String contextClientId = previous.getClientId(facesContext);
+ int startCommon = contextClientId.lastIndexOf(base+facesContext.getNamingContainerSeparatorChar());
+ if (startCommon >= 0
+ && (startCommon == 0 || contextClientId.charAt(startCommon-1) == separatorChar )
+ && (startCommon+base.length() <= contextClientId.length()-1 ||
+ contextClientId.charAt(startCommon+base.length()+1) == separatorChar ))
+ {
+ // If there is a match, try to find a the first parent component whose id is equals to
+ // the base id
+ UIComponent parent = previous;
+ while (parent != null )
+ {
+ if (base.equals(parent.getId()) && parent instanceof NamingContainer)
+ {
+ break;
+ }
+ else
+ {
+ parent = parent.getParent();
+ }
+ }
+
+ // if a base component is found ...
+ if (parent != null)
+ {
+ target = parent.findComponent(expression);
+ if (target == null && !searchExpressionContext.getExpressionHints().contains(
+ SearchExpressionHint.SKIP_VIRTUAL_COMPONENTS))
+ {
+ contextClientId = parent.getClientId(facesContext);
+ // If no component is found,
+ String targetClientId = contextClientId.substring(0, startCommon+base.length()) +
+ expression.substring(base.length());
+
+ if (nextExpression != null)
+ {
+ final String childExpression = nextExpression;
+
+ parent.invokeOnComponent(facesContext, targetClientId, new ContextCallback()
+ {
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ handler.invokeOnComponent(
+ searchExpressionContext, target, childExpression, topCallback);
+ }
+ });
+ }
+ else
+ {
+ parent.invokeOnComponent(facesContext, targetClientId, topCallback);
+ }
+ return;
+ }
+ }
+ }
+ }
+
+ if (target != null)
+ {
+ if (nextExpression != null)
+ {
+ handler.invokeOnComponent(searchExpressionContext, target, nextExpression, topCallback);
+ }
+ else
+ {
+ topCallback.invokeContextCallback(facesContext, target);
+ }
+
+ return;
+ }
+
+ // At this point if the algorithm hasn't returned and the topExpression does not have any separator char
+ // we need to do the search backward using findComponent.
+ if (target == null
+ && searchExpressionContext.getSource() == previous
+ && (topExpression.indexOf(separatorChar) == -1) )
+ {
+ UIComponent baseNC = previous.getNamingContainer();
+ if (baseNC != null && baseNC.getParent() != null)
+ {
+ UIComponent parentNC = getParentNamingContainerUIViewRoot(baseNC.getParent());
+ while (target == null && parentNC != null)
+ {
+ UIComponent parent = parentNC.getParent();
+ target = parentNC.findComponent(expression);
+ if (parent != null)
+ {
+ parentNC = getParentNamingContainerUIViewRoot(parent);
+ }
+ else
+ {
+ parentNC = null;
+ }
+ }
+ if (target != null)
+ {
+ topCallback.invokeContextCallback(facesContext, target);
+ return;
+ }
+ }
+ }
+
+ if (target == null && nextExpression == null)
+ {
+ return;
+ }
+
+ topCallback.invokeContextCallback(facesContext, previous);
+ }
+
+ }
+
+ private static UIComponent getParentNamingContainerUIViewRoot(UIComponent component)
+ {
+ do
+ {
+ if (component instanceof NamingContainer || component instanceof UIViewRoot)
+ {
+ return component;
+ }
+
+ component = component.getParent();
+ } while (component != null);
+ return null;
+ }
+
+ // take the command and resolve it using the chain of responsibility pattern.
+ protected void applyKeyword(SearchExpressionContext searchExpressionContext, UIComponent last,
+ String command, String remainingExpression, ContextCallback topCallback)
+ {
+ SearchKeywordContext searchContext =
+ new SearchKeywordContext(searchExpressionContext, topCallback, remainingExpression);
+
+ searchExpressionContext.getFacesContext().getApplication()
+ .getSearchKeywordResolver().resolve(searchContext, last, command);
+ }
+
+ @Override
+ public boolean isPassthroughExpression(SearchExpressionContext searchExpressionContext, String topExpression)
+ {
+ if (topExpression == null || topExpression.trim().isEmpty())
+ {
+ return false;
+ }
+
+ topExpression = topExpression.trim();
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+
+ //Step 1: find base
+ // Case ':' (root)
+ char separatorChar = facesContext.getNamingContainerSeparatorChar();
+ if (topExpression.charAt(0) == separatorChar)
+ {
+ // only keywords are passthrough expressions.
+ return false;
+ }
+
+ //Step 2: Once you have a base where you can start, apply an expression
+ if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
+ {
+ // A keyword means apply a command over the current source using an expression and the result must be
+ // feedback into the algorithm.
+
+ String command = extractKeyword(topExpression, 1, separatorChar);
+ final String remaining =
+ command.length()+1 < topExpression.length() ?
+ topExpression.substring(1+command.length()+1) : null;
+
+ final SearchExpressionHandler currentInstance =
+ facesContext.getApplication().getSearchExpressionHandler();
+
+ // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
+ // @root, @this , all commands change the source to be applied the action
+ boolean passthrough = facesContext.getApplication().getSearchKeywordResolver().isPassthrough(
+ searchExpressionContext, command);
+
+ if (passthrough)
+ {
+ return remaining != null ?
+ currentInstance.isPassthroughExpression(searchExpressionContext, remaining) : true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ else
+ {
+ // Only keywords are valid to be passthrough. If it contains a chain of ids, this can only be resolved
+ // server side, because the tree structure and related clientId logic is only available server side.
+ return false;
+ }
+ }
+
+ @Override
+ public boolean isValidExpression(SearchExpressionContext searchExpressionContext, String topExpression)
+ {
+ if (topExpression == null || topExpression.trim().isEmpty())
+ {
+ return true;
+ }
+
+ topExpression = topExpression.trim();
+
+ FacesContext facesContext = searchExpressionContext.getFacesContext();
+ // Command pattern to apply the keyword or command to the base and then invoke the callback
+ boolean isValid = true;
+ //Step 1: find base
+ // Case ':' (root)
+ char separatorChar = facesContext.getNamingContainerSeparatorChar();
+ if (topExpression.charAt(0) == separatorChar)
+ {
+ return facesContext.getApplication().getSearchExpressionHandler().isValidExpression(
+ searchExpressionContext, topExpression.substring(1));
+ }
+
+ //Step 2: Once you have a base where you can start, apply an expression
+ if (topExpression.charAt(0) == KEYWORD_PREFIX.charAt(0))
+ {
+ // A keyword means apply a command over the current source using an expression and the result must be
+ // feedback into the algorithm.
+
+ String command = extractKeyword(topExpression, 1, separatorChar);
+ final String remaining =
+ command.length()+1 < topExpression.length() ?
+ topExpression.substring(1+command.length()+1) : null;
+
+ final SearchExpressionHandler currentInstance =
+ facesContext.getApplication().getSearchExpressionHandler();
+
+ // If the keyword is @child, @composite, @form, @namingcontainer, @next, @none, @parent, @previous,
+ // @root, @this , all commands change the source to be applied the action
+ isValid = facesContext.getApplication().getSearchKeywordResolver().isResolverForKeyword(
+ searchExpressionContext, command);
+ if (remaining != null)
+ {
+ if (facesContext.getApplication().getSearchKeywordResolver().isLeaf(
+ searchExpressionContext, command))
+ {
+ isValid = false;
+ }
+ return !isValid ? false : currentInstance.isValidExpression(searchExpressionContext, remaining);
+ }
+ }
+ else
+ {
+ //Split expression into tokens and apply loop
+ String nextExpression = null;
+ String expression = null;
+ if (topExpression.indexOf(":@") > 0)
+ {
+ int idx = topExpression.indexOf(":@");
+ nextExpression = topExpression.substring(idx+1);
+ expression = topExpression.substring(0,idx);
+ }
+ else
+ {
+ expression = topExpression;
+ }
+
+ //Check expression
+ for (int i = 0; i < expression.length(); i++)
+ {
+ char c = expression.charAt(i);
+ if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == separatorChar)
+ {
+ //continue
+ }
+ else
+ {
+ isValid = false;
+ }
+ }
+
+ if (nextExpression != null)
+ {
+ return !isValid ? false : facesContext.getApplication().getSearchExpressionHandler()
+ .isValidExpression(searchExpressionContext, nextExpression);
+ }
+ }
+ return isValid;
+ }
+
+ private static String extractKeyword(String expression, int startIndex, char separatorChar)
+ {
+ int parenthesesCounter = -1;
+ int count = -1;
+ for (int i = startIndex; i < expression.length(); i++)
+ {
+ char c = expression.charAt(i);
+ if (c == '(')
+ {
+ if (parenthesesCounter == -1)
+ {
+ parenthesesCounter = 0;
+ }
+ parenthesesCounter++;
+ }
+ if (c == ')')
+ {
+ parenthesesCounter--;
+ }
+ if (parenthesesCounter == 0)
+ {
+ //Close first parentheses
+ count = i+1;
+ break;
+ }
+ if (parenthesesCounter == -1)
+ {
+ if (c == separatorChar)
+ {
+ count = i;
+ break;
+ }
+ }
+ }
+ if (count == -1)
+ {
+ return expression.substring(startIndex);
+ }
+ else
+ {
+ return expression.substring(startIndex, count);
+ }
+ }
+
+ @Override
+ public String[] splitExpressions(FacesContext context, String expressions)
+ {
+ // split expressions by blank or comma (and ignore blank and commas inside brackets)
+ String[] splittedExpressions = split(context, expressions, EXPRESSION_SEPARATOR_CHARS);
+ return splittedExpressions;
+ }
+
+ private static String[] split(FacesContext context, String value, char... separators)
+ {
+ if (value == null)
+ {
+ return null;
+ }
+
+ List<String> tokens = new ArrayList<String>();
+ StringBuilder buffer = SharedStringBuilder.get(context, SB_SPLIT);
+
+ int parenthesesCounter = 0;
+
+ char[] charArray = value.toCharArray();
+
+ for (char c : charArray)
+ {
+ if (c == '(')
+ {
+ parenthesesCounter++;
+ }
+
+ if (c == ')')
+ {
+ parenthesesCounter--;
+ }
+
+ if (parenthesesCounter == 0)
+ {
+ boolean isSeparator = false;
+ for (char separator : separators)
+ {
+ if (c == separator)
+ {
+ isSeparator = true;
+ }
+ }
+
+ if (isSeparator)
+ {
+ // lets add token inside buffer to our tokens
+ String bufferString = buffer.toString().trim();
+ if (bufferString.length() > 0)
+ {
+ tokens.add(bufferString);
+ }
+ // now we need to clear buffer
+ buffer.delete(0, buffer.length());
+ }
+ else
+ {
+ buffer.append(c);
+ }
+ }
+ else
+ {
+ buffer.append(c);
+ }
+ }
+
+ // lets not forget about part after the separator
+ tokens.add(buffer.toString());
+
+ return tokens.toArray(new String[tokens.size()]);
+ }
+
+}
diff --git a/impl/src/test/java/org/apache/myfaces/component/search/SearchExpressionImplTest.java b/impl/src/test/java/org/apache/myfaces/component/search/SearchExpressionImplTest.java
index 423a835ca..abdcc6193 100644
--- a/impl/src/test/java/org/apache/myfaces/component/search/SearchExpressionImplTest.java
+++ b/impl/src/test/java/org/apache/myfaces/component/search/SearchExpressionImplTest.java
@@ -1,411 +1,424 @@
-/*
- * 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.component.search;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import javax.faces.application.StateManager;
-import javax.faces.component.ContextCallback;
-import javax.faces.component.UIComponent;
-import javax.faces.component.UIOutput;
-import javax.faces.component.search.ComponentNotFoundException;
-import javax.faces.component.search.SearchExpressionContext;
-import javax.faces.component.search.SearchExpressionHandler;
-import javax.faces.component.search.SearchExpressionHint;
-import javax.faces.context.FacesContext;
-import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
-import org.junit.Assert;
-import org.junit.Test;
-
-/**
- *
- */
-public class SearchExpressionImplTest extends AbstractMyFacesRequestTestCase
-{
- @Override
- protected boolean isScanAnnotations()
- {
- return true;
- }
-
- @Override
- protected void setUpWebConfigParams() throws Exception
- {
- super.setUpWebConfigParams();
- servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.component.search");
- servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
- servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
- }
-
- @Test
- public void testSearchExpression1() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, out);
- Collection<String> list = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "mainForm:showName");
-
- boolean found = false;
- for (String clientId : list)
- {
- System.out.println(clientId);
- found = true;
- }
- Assert.assertTrue(found);
-
- String componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@this").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@this:@parent:showName").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "mainForm:table:0:baseText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:0:baseText");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@parent:showName:@parent:showName").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@form:showName").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, " @namingcontainer:showName ").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- UIOutput name = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:name");
- searchContext = SearchExpressionContext.createSearchExpressionContext(facesContext, name);
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@previous").get(0);
- Assert.assertEquals(componentId, "mainForm:labelName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@next").get(0);
- Assert.assertEquals(componentId, "mainForm:msgName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "@parent:@id(msgName)").get(0);
- Assert.assertEquals(componentId, "mainForm:msgName");
-
- componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
- searchContext, "topLevelOutputText").get(0);
- Assert.assertEquals(componentId, "topLevelOutputText");
-
- facesContext.getViewRoot().invokeOnComponent(facesContext, "mainForm:table:3:nested:1:nestedButton",
- new ContextCallback()
- {
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(context, target);
- String componentId = handler.resolveClientIds(searchContext, "mainForm:showName").get(0);
- Assert.assertEquals(componentId, "mainForm:showName");
-
- componentId = handler.resolveClientIds(searchContext, "nested:nestedText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:3:nested:1:nestedText");
-
- componentId = handler.resolveClientIds(searchContext, "table:baseText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:3:baseText");
-
- componentId = handler.resolveClientIds(searchContext, "table:0:baseText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:0:baseText");
-
- componentId = handler.resolveClientIds(searchContext, "nested:0:nestedText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:3:nested:0:nestedText");
-
- componentId = handler.resolveClientIds(searchContext, "table:nested").get(0);
- Assert.assertEquals(componentId, "mainForm:table:3:nested");
-
- componentId = handler.resolveClientIds(searchContext, "table:1:nested:0:nestedText").get(0);
- Assert.assertEquals(componentId, "mainForm:table:1:nested:0:nestedText");
-
- //System.out.println(componentId);
- }
- });
-
- facesContext.getViewRoot().invokeOnComponent(facesContext, "mainForm:table:3:baseText",
- new ContextCallback()
- {
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(context, target);
- Assert.assertEquals(handler.resolveClientId(searchContext, "@next"), "mainForm:table:3:nestedText");
- }
- });
-
- processRemainingPhases();
- }
-
- @Test
- public void testSearchExpressionIdKeyword() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
-
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- Assert.assertEquals("panelGridId", handler.resolveClientId(searchContext, "mainForm:@parent"));
-
- processRemainingPhases();
- }
-
- @Test
- public void testResolveComponent() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
-
- CollectComponentsCallback callback = new CollectComponentsCallback();
- facesContext.getApplication().getSearchExpressionHandler().resolveComponent(
- searchContext, "@id(name)", callback);
-
- Assert.assertEquals(1, callback.getComponents().size());
-
- processRemainingPhases();
- }
-
- @Test
- public void testResolveComponents() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
-
- CollectComponentsCallback callback = new CollectComponentsCallback();
- facesContext.getApplication().getSearchExpressionHandler().resolveComponents(
- searchContext, " @id(name) @none ", callback);
-
- Assert.assertEquals(2, callback.getComponents().size());
-
- processRemainingPhases();
- }
-
- class CollectComponentsCallback implements ContextCallback
- {
- private ArrayList<UIComponent> components = new ArrayList<>();
-
- @Override
- public void invokeContextCallback(FacesContext context, UIComponent target)
- {
- components.add(target);
- }
-
- public ArrayList<UIComponent> getComponents()
- {
- return components;
- }
- }
-
- @Test
- public void testResolveClientId1() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
- Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
-
- expressionHints.add(SearchExpressionHint.IGNORE_NO_RESULT);
- SearchExpressionContext searchContextWithIgnoreNoResult =
- SearchExpressionContext.createSearchExpressionContext(facesContext, out, expressionHints, null);
-
- String clientId = handler.resolveClientId(searchContextWithIgnoreNoResult, "@none");
- Assert.assertNull(clientId);
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, out);
-
- try
- {
- clientId = handler.resolveClientId(searchContext, "@none");
- Assert.fail();
- }
- catch (ComponentNotFoundException e)
- {
- //No op
- }
- }
-
- @Test
- public void testResolveClientIds1() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
- Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
- expressionHints.add(SearchExpressionHint.IGNORE_NO_RESULT);
- SearchExpressionContext searchContextWithIgnoreNoResult =
- SearchExpressionContext.createSearchExpressionContext(facesContext, out, expressionHints, null);
-
- List<String> clientId = handler.resolveClientIds(searchContextWithIgnoreNoResult, "@none");
- Assert.assertTrue(clientId.isEmpty());
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, out);
-
- try
- {
- clientId = handler.resolveClientIds(searchContext, "@none");
- Assert.fail();
- }
- catch (ComponentNotFoundException e)
- {
- //No op
- }
- }
-
- @Test
- public void testIsValid() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, null);
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:showName"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested:1:nestedText"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:baseText"));
- Assert.assertTrue(handler.isValidExpression(searchContext, " mainForm:table:0:baseText"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested:0:nestedText"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:1:nested:0:nestedText"));
-
- Assert.assertTrue(handler.isValidExpression(searchContext, " "));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@this"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@this:@parent:showName"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@parent:showName:@parent:showName "));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@form:showName"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@namingcontainer:showName"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@previous"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@next"));
- Assert.assertTrue(handler.isValidExpression(searchContext, "@parent:@id(msgName)"));
-
- Assert.assertFalse(handler.isValidExpression(searchContext, "@whoNows"));
- Assert.assertFalse(handler.isValidExpression(searchContext, "@parent:@whoNows"));
- Assert.assertFalse(handler.isValidExpression(searchContext, "mainForm:@whoNows"));
- Assert.assertFalse(handler.isValidExpression(searchContext, "!whoNows"));
-
- Assert.assertFalse(handler.isValidExpression(searchContext, "@none:@parent"));
- Assert.assertFalse(handler.isValidExpression(searchContext, "@all:@parent"));
- }
-
- @Test
- public void testIsPassthroughExpression() throws Exception
- {
- startViewRequest("/search1.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
- SearchExpressionContext searchContext =
- SearchExpressionContext.createSearchExpressionContext(facesContext, null);
- SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
-
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested:1:nestedText"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:baseText"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:0:baseText"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested:0:nestedText"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:1:nested:0:nestedText"));
-
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@this"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@this:@parent:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:showName:@parent:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@form"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@form:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@namingcontainer:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@previous"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@next"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:@id(msgName)"));
-
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@whoNows"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:@whoNows"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:@whoNows"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContext, "!whoNows"));
-
- Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
- expressionHints.add(SearchExpressionHint.RESOLVE_CLIENT_SIDE);
- SearchExpressionContext searchContextWithAjaxResolve =
- SearchExpressionContext.createSearchExpressionContext(facesContext, null, expressionHints, null);
-
- Assert.assertTrue(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form:showName"));
- Assert.assertFalse(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form:@child(0)"));
- }
-
- /*
- @Test
- public void testCompositeComponentExpression() throws Exception
- {
- startViewRequest("/testCompositeActionSource.xhtml");
- processLifecycleExecute();
- executeBeforeRender();
- executeBuildViewCycle();
-
-
- }*/
-}
+/*
+ * 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.component.search;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import javax.faces.application.StateManager;
+import javax.faces.component.ContextCallback;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIOutput;
+import javax.faces.component.search.ComponentNotFoundException;
+import javax.faces.component.search.SearchExpressionContext;
+import javax.faces.component.search.SearchExpressionHandler;
+import javax.faces.component.search.SearchExpressionHint;
+import javax.faces.context.FacesContext;
+import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class SearchExpressionImplTest extends AbstractMyFacesRequestTestCase
+{
+ @Override
+ protected boolean isScanAnnotations()
+ {
+ return true;
+ }
+
+ @Override
+ protected void setUpWebConfigParams() throws Exception
+ {
+ super.setUpWebConfigParams();
+ servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.component.search");
+ servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
+ servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
+ }
+
+ @Test
+ public void testSearchExpression1() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, out);
+ Collection<String> list = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "mainForm:showName");
+
+ boolean found = false;
+ for (String clientId : list)
+ {
+ System.out.println(clientId);
+ found = true;
+ }
+ Assert.assertTrue(found);
+
+ String componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@this").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@this:@parent:showName").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@form:inputOutsideForm").get(0);
+ Assert.assertEquals("mainForm:inputOutsideForm", componentId);
+
+ try
+ {
+ facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(searchContext, ":inputOutsideForm");
+ Assert.fail();
+ }
+ catch (ComponentNotFoundException e)
+ {
+ }
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "mainForm:table:0:baseText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:0:baseText");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@parent:showName:@parent:showName").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@form:showName").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, " @namingcontainer:showName ").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ UIOutput name = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:name");
+ searchContext = SearchExpressionContext.createSearchExpressionContext(facesContext, name);
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@previous").get(0);
+ Assert.assertEquals(componentId, "mainForm:labelName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@next").get(0);
+ Assert.assertEquals(componentId, "mainForm:msgName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "@parent:@id(msgName)").get(0);
+ Assert.assertEquals(componentId, "mainForm:msgName");
+
+ componentId = facesContext.getApplication().getSearchExpressionHandler().resolveClientIds(
+ searchContext, "topLevelOutputText").get(0);
+ Assert.assertEquals(componentId, "topLevelOutputText");
+
+ facesContext.getViewRoot().invokeOnComponent(facesContext, "mainForm:table:3:nested:1:nestedButton",
+ new ContextCallback()
+ {
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(context, target);
+ String componentId = handler.resolveClientIds(searchContext, "mainForm:showName").get(0);
+ Assert.assertEquals(componentId, "mainForm:showName");
+
+ componentId = handler.resolveClientIds(searchContext, "nested:nestedText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:3:nested:1:nestedText");
+
+ componentId = handler.resolveClientIds(searchContext, "table:baseText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:3:baseText");
+
+ componentId = handler.resolveClientIds(searchContext, "table:0:baseText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:0:baseText");
+
+ componentId = handler.resolveClientIds(searchContext, "nested:0:nestedText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:3:nested:0:nestedText");
+
+ componentId = handler.resolveClientIds(searchContext, "table:nested").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:3:nested");
+
+ componentId = handler.resolveClientIds(searchContext, "table:1:nested:0:nestedText").get(0);
+ Assert.assertEquals(componentId, "mainForm:table:1:nested:0:nestedText");
+
+ //System.out.println(componentId);
+ }
+ });
+
+ facesContext.getViewRoot().invokeOnComponent(facesContext, "mainForm:table:3:baseText",
+ new ContextCallback()
+ {
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(context, target);
+ Assert.assertEquals(handler.resolveClientId(searchContext, "@next"), "mainForm:table:3:nestedText");
+ }
+ });
+
+ processRemainingPhases();
+ }
+
+ @Test
+ public void testSearchExpressionIdKeyword() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
+
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ Assert.assertEquals("panelGridId", handler.resolveClientId(searchContext, "mainForm:@parent"));
+
+ processRemainingPhases();
+ }
+
+ @Test
+ public void testResolveComponent() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
+
+ CollectComponentsCallback callback = new CollectComponentsCallback();
+ facesContext.getApplication().getSearchExpressionHandler().resolveComponent(
+ searchContext, "@id(name)", callback);
+
+ Assert.assertEquals(1, callback.getComponents().size());
+
+ processRemainingPhases();
+ }
+
+ @Test
+ public void testResolveComponents() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, facesContext.getViewRoot());
+
+ CollectComponentsCallback callback = new CollectComponentsCallback();
+ facesContext.getApplication().getSearchExpressionHandler().resolveComponents(
+ searchContext, " @id(name) @none ", callback);
+
+ Assert.assertEquals(2, callback.getComponents().size());
+
+ processRemainingPhases();
+ }
+
+ class CollectComponentsCallback implements ContextCallback
+ {
+ private ArrayList<UIComponent> components = new ArrayList<>();
+
+ @Override
+ public void invokeContextCallback(FacesContext context, UIComponent target)
+ {
+ components.add(target);
+ }
+
+ public ArrayList<UIComponent> getComponents()
+ {
+ return components;
+ }
+ }
+
+ @Test
+ public void testResolveClientId1() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
+ Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
+
+ expressionHints.add(SearchExpressionHint.IGNORE_NO_RESULT);
+ SearchExpressionContext searchContextWithIgnoreNoResult =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, out, expressionHints, null);
+
+ String clientId = handler.resolveClientId(searchContextWithIgnoreNoResult, "@none");
+ Assert.assertNull(clientId);
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, out);
+
+ try
+ {
+ clientId = handler.resolveClientId(searchContext, "@none");
+ Assert.fail();
+ }
+ catch (ComponentNotFoundException e)
+ {
+ //No op
+ }
+ }
+
+ @Test
+ public void testResolveClientIds1() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ UIOutput out = (UIOutput) facesContext.getViewRoot().findComponent("mainForm:showName");
+ Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
+ expressionHints.add(SearchExpressionHint.IGNORE_NO_RESULT);
+ SearchExpressionContext searchContextWithIgnoreNoResult =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, out, expressionHints, null);
+
+ List<String> clientId = handler.resolveClientIds(searchContextWithIgnoreNoResult, "@none");
+ Assert.assertTrue(clientId.isEmpty());
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, out);
+
+ try
+ {
+ clientId = handler.resolveClientIds(searchContext, "@none");
+ Assert.fail();
+ }
+ catch (ComponentNotFoundException e)
+ {
+ //No op
+ }
+ }
+
+ @Test
+ public void testIsValid() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, null);
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:showName"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested:1:nestedText"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:baseText"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, " mainForm:table:0:baseText"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested:0:nestedText"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:3:nested"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "mainForm:table:1:nested:0:nestedText"));
+
+ Assert.assertTrue(handler.isValidExpression(searchContext, " "));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@this"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@this:@parent:showName"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@parent:showName:@parent:showName "));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@form:showName"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@namingcontainer:showName"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@previous"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@next"));
+ Assert.assertTrue(handler.isValidExpression(searchContext, "@parent:@id(msgName)"));
+
+ Assert.assertFalse(handler.isValidExpression(searchContext, "@whoNows"));
+ Assert.assertFalse(handler.isValidExpression(searchContext, "@parent:@whoNows"));
+ Assert.assertFalse(handler.isValidExpression(searchContext, "mainForm:@whoNows"));
+ Assert.assertFalse(handler.isValidExpression(searchContext, "!whoNows"));
+
+ Assert.assertFalse(handler.isValidExpression(searchContext, "@none:@parent"));
+ Assert.assertFalse(handler.isValidExpression(searchContext, "@all:@parent"));
+ }
+
+ @Test
+ public void testIsPassthroughExpression() throws Exception
+ {
+ startViewRequest("/search1.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+ SearchExpressionContext searchContext =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, null);
+ SearchExpressionHandler handler = facesContext.getApplication().getSearchExpressionHandler();
+
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested:1:nestedText"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:baseText"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:0:baseText"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested:0:nestedText"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:3:nested"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:table:1:nested:0:nestedText"));
+
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@this"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@this:@parent:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:showName:@parent:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@form"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@form:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@namingcontainer:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@previous"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@next"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:@id(msgName)"));
+
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@whoNows"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "@parent:@whoNows"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "mainForm:@whoNows"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContext, "!whoNows"));
+
+ Set<SearchExpressionHint> expressionHints = new HashSet<SearchExpressionHint>();
+ expressionHints.add(SearchExpressionHint.RESOLVE_CLIENT_SIDE);
+ SearchExpressionContext searchContextWithAjaxResolve =
+ SearchExpressionContext.createSearchExpressionContext(facesContext, null, expressionHints, null);
+
+ Assert.assertTrue(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form:showName"));
+ Assert.assertFalse(handler.isPassthroughExpression(searchContextWithAjaxResolve, "@form:@child(0)"));
+ }
+
+ /*
+ @Test
+ public void testCompositeComponentExpression() throws Exception
+ {
+ startViewRequest("/testCompositeActionSource.xhtml");
+ processLifecycleExecute();
+ executeBeforeRender();
+ executeBuildViewCycle();
+
+
+ }*/
+}
diff --git a/impl/src/test/resources/org/apache/myfaces/component/search/search1.xhtml b/impl/src/test/resources/org/apache/myfaces/component/search/search1.xhtml
index 429213a4c..0c015f64b 100644
--- a/impl/src/test/resources/org/apache/myfaces/component/search/search1.xhtml
+++ b/impl/src/test/resources/org/apache/myfaces/component/search/search1.xhtml
@@ -29,8 +29,11 @@
<h:outputText id="topLevelOutputText" value="test" />
<h:panelGrid id="panelGridId" columns="1">
+
<h:form id="mainForm">
+ <h:inputText id="inputOutsideForm"></h:inputText>
+
<h:outputLabel id="labelName" for="name" value="Name: "/>
<div>
<h:inputText id="name" value="NAME"/>