001    package org.apache.myfaces.tobago.compat;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import javax.faces.component.ContextCallback;
021    import javax.faces.component.NamingContainer;
022    import javax.faces.component.UIComponent;
023    import javax.faces.context.FacesContext;
024    
025    public class FacesInvokeOnComponent12 {
026    
027      public static boolean invokeOnComponent(
028          FacesContext context, UIComponent component, String clientId, ContextCallback callback) {
029        String thisClientId = component.getClientId(context);
030    
031        if (clientId.equals(thisClientId)) {
032          callback.invokeContextCallback(context, component);
033          return true;
034        } else if (component instanceof NamingContainer) {
035          // This component is a naming container. If the client id shows it's inside this naming container,
036          // then process further.
037          // Otherwise we know the client id we're looking for is not in this naming container,
038          // so for improved performance short circuit and return false.
039          if (clientId.startsWith(thisClientId)
040              && (clientId.charAt(thisClientId.length()) == NamingContainer.SEPARATOR_CHAR)) {
041            if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
042              return true;
043            }
044          }
045        } else {
046          if (invokeOnComponentFacetsAndChildren(context, component, clientId, callback)) {
047            return true;
048          }
049        }
050    
051        return false;
052      }
053    
054      private static boolean invokeOnComponentFacetsAndChildren(
055          FacesContext context, UIComponent component, String clientId, ContextCallback callback) {
056        for (java.util.Iterator<UIComponent> it = component.getFacetsAndChildren(); it.hasNext();) {
057          UIComponent child = it.next();
058          if (child.invokeOnComponent(context, clientId, callback)) {
059            return true;
060          }
061        }
062        return false;
063      }
064    }