1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }