View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.component.search;
20  
21  import java.util.List;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  import javax.faces.FacesException;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.search.UntargetableComponent;
27  import javax.faces.component.search.SearchExpressionContext;
28  import javax.faces.component.search.SearchKeywordContext;
29  import javax.faces.component.search.SearchKeywordResolver;
30  
31  /**
32   *
33   */
34  public class ChildSearchKeywordResolver extends SearchKeywordResolver
35  {
36  
37      public static final String CHILD_KEYWORD = "child";
38  
39      private static final Pattern PATTERN = Pattern.compile("child\\((\\d+)\\)");
40  
41      @Override
42      public void resolve(SearchKeywordContext expressionContext, UIComponent current, String keyword)
43      {
44          Matcher matcher = PATTERN.matcher(keyword);
45  
46          if (matcher.matches())
47          {
48              int childNumber = Integer.parseInt(matcher.group(1));
49  
50              if (childNumber + 1 > current.getChildCount())
51              {
52                  throw new FacesException("Component with clientId \""
53                          + current.getClientId(expressionContext.getSearchExpressionContext().getFacesContext()) 
54                          + "\" has fewer children as \"" + 
55                            childNumber + "\". Expression: \"" + keyword + "\"");
56              }
57  
58              List<UIComponent> list = current.getChildren();
59              int count = 0;
60              for (int i = 0; i < current.getChildCount(); i++)
61              {
62                  if (! (list.get(i) instanceof UntargetableComponent))
63                  {
64                      count++;
65                  }
66                  if (count == childNumber + 1)
67                  {
68                      expressionContext.invokeContextCallback(current.getChildren().get(childNumber));
69                      break;
70                  }
71              }
72              if (count < childNumber)
73              {
74                  throw new FacesException("Component with clientId \""
75                          + current.getClientId(expressionContext.getSearchExpressionContext().getFacesContext()) 
76                          + "\" has fewer children as \"" + 
77                            childNumber + "\". Expression: \"" + keyword + "\"");
78              }
79          }
80          else
81          {
82              throw new FacesException(
83                      "Expression does not match following pattern @child(n). Expression: \"" + keyword + "\"");
84          }
85      }
86  
87      @Override
88      public boolean isResolverForKeyword(SearchExpressionContext searchExpressionContext, String command)
89      {
90          if (command.length() > 6 && command.substring(0, CHILD_KEYWORD.length()).equalsIgnoreCase(CHILD_KEYWORD))
91          {
92              Matcher matcher = PATTERN.matcher(command);
93  
94              if (matcher.matches())
95              {
96                  return true;
97              }
98              else
99              {
100                 return false;
101             }
102         }
103         return false;
104     }
105 
106     @Override
107     public boolean isPassthrough(SearchExpressionContext searchExpressionContext, String keyword)
108     {
109         return false;
110     }
111     
112     @Override
113     public boolean isLeaf(SearchExpressionContext searchExpressionContext, String keyword)
114     {
115         return false;
116     }
117 
118 }