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  
20  package org.apache.myfaces.component.search;
21  
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.search.SearchExpressionContext;
24  import javax.faces.component.search.SearchKeywordContext;
25  import javax.faces.component.search.SearchKeywordResolver;
26  
27  /**
28   *
29   */
30  public class CompositeSearchKeywordResolver extends SearchKeywordResolver
31  {
32      private int size;
33  
34      private SearchKeywordResolver[] resolvers;
35  
36      public CompositeSearchKeywordResolver()
37      {
38          this.size = 0;
39          this.resolvers = new SearchKeywordResolver[2];
40      }
41  
42      public void add(SearchKeywordResolver elResolver)
43      {
44          if (elResolver == null)
45          {
46              throw new NullPointerException();
47          }
48  
49          if (this.size >= this.resolvers.length)
50          {
51              SearchKeywordResolver[] nr = new SearchKeywordResolver[this.size * 2];
52              System.arraycopy(this.resolvers, 0, nr, 0, this.size);
53              this.resolvers = nr;
54          }
55          this.resolvers[this.size++] = elResolver;
56      }
57  
58      @Override
59      public void resolve(SearchKeywordContext context, UIComponent current, String keyword)
60      {
61          context.setKeywordResolved(false);
62          int sz = this.size;
63          for (int i = 0; i < sz; i++)
64          {
65              if (this.resolvers[i].isResolverForKeyword(context.getSearchExpressionContext(), keyword))
66              {
67                  this.resolvers[i].resolve(context, current, keyword);
68                  if (context.isKeywordResolved())
69                  {
70                      return;
71                  }
72              }
73          }
74      }
75  
76      @Override
77      public boolean isResolverForKeyword(SearchExpressionContext searchExpressionContext, String keyword)
78      {
79          int sz = this.size;
80          for (int i = 0; i < sz; i++)
81          {
82              if (this.resolvers[i].isResolverForKeyword(searchExpressionContext, keyword))
83              {
84                  return true;
85              }
86          }
87          return false;
88      }
89  
90      @Override
91      public boolean isPassthrough(SearchExpressionContext searchExpressionContext, String keyword)
92      {
93          int sz = this.size;
94          for (int i = 0; i < sz; i++)
95          {
96              if (this.resolvers[i].isResolverForKeyword(searchExpressionContext, keyword))
97              {
98                  return this.resolvers[i].isPassthrough(searchExpressionContext, keyword);
99              }
100         }
101         return false;
102     }
103 
104     @Override
105     public boolean isLeaf(SearchExpressionContext searchExpressionContext, String keyword)
106     {
107         int sz = this.size;
108         for (int i = 0; i < sz; i++)
109         {
110             if (this.resolvers[i].isResolverForKeyword(searchExpressionContext, keyword))
111             {
112                 return this.resolvers[i].isLeaf(searchExpressionContext, keyword);
113             }
114         }
115         return false;
116     }
117 }