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.tobago.internal.util;
21  
22  import javax.faces.component.NamingContainer;
23  import javax.faces.component.UIComponent;
24  
25  // Will be normally called via ComponentUtils.
26  public class FindComponentUtils {
27  
28    public static UIComponent findComponent(UIComponent from, String relativeId) {
29      int idLength = relativeId.length();
30      if (idLength > 0 && relativeId.charAt(0) == '@') {
31        if (relativeId.equals("@this")) {
32          return from;
33        }
34      }
35  
36      // Figure out how many colons
37      int colonCount = 0;
38      while (colonCount < idLength) {
39        if (relativeId.charAt(colonCount) != NamingContainer.SEPARATOR_CHAR) {
40          break;
41        }
42        colonCount++;
43      }
44  
45      // colonCount == 0: fully relative
46      // colonCount == 1: absolute (still normal findComponent syntax)
47      // colonCount > 1: for each extra colon after 1, go up a naming container
48      // (to the view root, if naming containers run out)
49      if (colonCount > 1) {
50        relativeId = relativeId.substring(colonCount);
51        for (int j = 1; j < colonCount; j++) {
52          while (from.getParent() != null) {
53            from = from.getParent();
54            if (from instanceof NamingContainer) {
55              break;
56            }
57          }
58        }
59      }
60      return from.findComponent(relativeId);
61    }
62  }