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.trinidad.change;
20  
21  import java.util.List;
22  
23  import javax.faces.component.UIComponent;
24  
25  import org.w3c.dom.Element;
26  import org.w3c.dom.NamedNodeMap;
27  import org.w3c.dom.Node;
28  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
29  
30  
31  /**
32   * Utility functions for use by Changes.
33   * @version $Name:  $ ($Revision: adfrt/faces/adf-faces-api/src/main/java/oracle/adf/view/faces/change/ChangeUtils.java#0 $) $Date: 10-nov-2005.19:09:58 $
34   */
35  class ChangeUtils 
36  {
37    private ChangeUtils()
38    {
39    }
40  
41    /**
42     * Given a parent component and the identifier for the child, looks up among
43     *  the children for a child with the specified identifier and returns.
44     * Returns null if there were to be no such child
45     * @param parent the parent UIComponent
46     * @param childId the 'id' identifier value of child to be searched in the parent's 
47     *        children.
48     */
49    @SuppressWarnings("unchecked")
50    public static UIComponent getChildForId(UIComponent parent, String childId)
51    {
52      return getChildForId(parent, childId, "id");
53    }
54    
55    /**
56     * Given a parent component and the identifier value for the child, looks up among
57     * the children for a child with the specified identifier and returns.
58     * Returns null if there were to be no such child
59     * @param parent the parent UIComponent
60     * @param childId the identifier value of child to be searched in the parent's 
61     *        children.
62     * @param identifier the identifier type 
63     */
64    @SuppressWarnings("unchecked")
65    public static UIComponent getChildForId(
66      UIComponent parent, 
67      String childId,
68      String identifier)
69    {
70      if (parent == null)
71        return null;
72  
73      int numChildren = parent.getChildCount();
74      if (numChildren == 0)
75        return null;
76  
77      List<UIComponent> children = parent.getChildren();
78      
79      for (int i=0; i<numChildren; i++)
80      {
81        UIComponent child = children.get(i);
82        Object attrVal = child.getAttributes().get(identifier);
83        
84        if ( childId.equals(attrVal) )
85          return child;
86      }
87      return null;
88    }
89    
90    /**
91     * Given a parent component and the identifier for the child, looks up among
92     * the children for a child with the specified identifier and returns the index
93     * of the child
94     * Returns -1 if there were to be no such child
95     * @param parent
96     * @param childId the identifier of child to be searched in the parent's 
97     * children
98     */
99    @SuppressWarnings("unchecked")
100   public static int getChildIndexForId(UIComponent parent, String childId)
101   {
102     if (parent == null)
103       throw new NullPointerException(_LOG.getMessage(
104         "PARENT_CANNOT_BE_NULL"));
105 
106     int numChildren = parent.getChildCount();
107     if (numChildren == 0)
108       return -1;
109 
110     List<UIComponent> children = parent.getChildren();      
111     UIComponent child;    
112     for (int i=0; i<numChildren; i++)
113     {
114       child = children.get(i);
115       if ( childId.equals(child.getId()) )
116         return i;
117     }
118     return -1;
119   }
120   
121   /**
122    * Given a node representing a component, returns the named facet's Element.
123    * @param componentNode The node to search for a facet contained in it.
124    * @param facetName The name of the facet to search for.
125    * @return
126    */
127   static Element __getFacetElement(
128     Node componentNode,
129     String facetName)
130   {
131     assert componentNode != null;
132     assert (facetName != null) && (facetName.length() > 0);
133     
134     Node currChild = componentNode.getFirstChild();
135     
136     while (currChild != null)
137     {
138       // check for local name match
139       if ("facet".equals(currChild.getLocalName()))
140       {
141         // check for namespace match
142         if (__JSF_CORE_NAMESPACE.equals(currChild.getNamespaceURI()))
143         {
144           NamedNodeMap attributes = currChild.getAttributes();
145 
146           if (facetName.equals(attributes.getNamedItem("name").getNodeValue()))
147           {
148             return (Element)currChild;
149           }
150         }
151       }
152 
153       currChild = currChild.getNextSibling();
154     }
155     
156     return null;
157   }
158 
159   /**
160    * Removes all of the children from the parent Node.
161    * @param parentNode 
162    */
163   static void __removeAllChildren(Node parentNode)
164   {
165     Node nukeChild = parentNode.getFirstChild();
166     
167     while (nukeChild != null)
168     {
169       parentNode.removeChild(nukeChild);
170       nukeChild = parentNode.getFirstChild();
171     }
172   }
173 
174   static final String __JSF_CORE_NAMESPACE = "http://java.sun.com/jsf/core";
175   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(
176     ChangeUtils.class);
177 }