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 javax.faces.view;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIImportConstants;
28  import javax.faces.component.UIViewAction;
29  import javax.faces.component.UIViewParameter;
30  import javax.faces.component.UIViewRoot;
31  import javax.faces.context.FacesContext;
32  
33  /**
34   * @since 2.0
35   */
36  public abstract class ViewMetadata
37  {
38      public abstract UIViewRoot createMetadataView(FacesContext context);
39      
40      public abstract String getViewId();
41      
42      public static Collection<UIViewParameter> getViewParameters(UIViewRoot root)
43      {
44          LinkedList<UIViewParameter> result = null;
45          UIComponent metadataFacet = root.getFacet (UIViewRoot.METADATA_FACET_NAME);
46          Iterator<UIComponent> children;
47          
48          if (metadataFacet == null)
49          {
50               // No metadata, so return an empty collection.
51               
52               return Collections.emptyList();
53          }
54          
55          // Iterate over all the children, keep only the view parameters.
56          
57          if (metadataFacet.getChildCount() > 0)
58          {
59              children = metadataFacet.getChildren().iterator();
60              
61              while (children.hasNext())
62              {
63                   UIComponent component = children.next();
64                   
65                   if (result == null)
66                   {
67                       result = new LinkedList<UIViewParameter>();
68                   }
69                   
70                   if (component instanceof UIViewParameter)
71                   {
72                        result.add ((UIViewParameter) component);
73                   }
74              }
75          }
76          
77          // TODO: does this need to be immutable?  Spec does not indicate either
78          // way.
79          if (result == null)
80          {
81              return Collections.emptyList();
82          }
83          else
84          {
85              return Collections.unmodifiableCollection (result);
86          }
87      }
88      
89      /**
90       * @since 2.2
91       * @param root
92       * @return 
93       */
94      public static Collection<UIViewAction> getViewActions(UIViewRoot root)
95      {
96          LinkedList<UIViewAction> result = null;
97          UIComponent metadataFacet = root.getFacet (UIViewRoot.METADATA_FACET_NAME);
98          Iterator<UIComponent> children;
99          
100         if (metadataFacet == null)
101         {
102              // No metadata, so return an empty collection.
103              
104              return Collections.emptyList();
105         }
106         
107         // Iterate over all the children, keep only the view parameters.
108         
109         if (metadataFacet.getChildCount() > 0)
110         {
111             children = metadataFacet.getChildren().iterator();
112             
113             while (children.hasNext())
114             {
115                  UIComponent component = children.next();
116                  
117                  if (result == null)
118                  {
119                      result = new LinkedList<UIViewAction>();
120                  }
121                  
122                  if (component instanceof UIViewAction)
123                  {
124                       result.add ((UIViewAction) component);
125                  }
126             }
127         }
128         
129         // TODO: does this need to be immutable?  Spec does not indicate either
130         // way.
131         if (result == null)
132         {
133             return Collections.emptyList();
134         }
135         else
136         {
137             return Collections.unmodifiableCollection (result);
138         }
139     }
140     
141     /**
142      * @since 2.2
143      * @param root
144      * @return 
145      */
146     public static boolean hasMetadata(UIViewRoot root)
147     {
148         UIComponent metadataFacet = root.getFacet(UIViewRoot.METADATA_FACET_NAME);
149         return metadataFacet != null ? metadataFacet.getChildCount() > 0 : false;
150     }
151     
152     /**
153      * @since 2.3
154      * @param root
155      * @return 
156      */
157     public static Collection<UIImportConstants> getImportConstants(UIViewRoot root)
158     {
159         LinkedList<UIImportConstants> result = null;
160         UIComponent metadataFacet = root.getFacet (UIViewRoot.METADATA_FACET_NAME);
161         Iterator<UIComponent> children;
162         
163         if (metadataFacet == null)
164         {
165              // No metadata, so return an empty collection.
166              
167              return Collections.emptyList();
168         }
169         
170         // Iterate over all the children, keep only the view parameters.
171         
172         if (metadataFacet.getChildCount() > 0)
173         {
174             children = metadataFacet.getChildren().iterator();
175             
176             while (children.hasNext())
177             {
178                  UIComponent component = children.next();
179                  
180                  if (result == null)
181                  {
182                      result = new LinkedList<UIImportConstants>();
183                  }
184                  
185                  if (component instanceof UIImportConstants)
186                  {
187                       result.add ((UIImportConstants) component);
188                  }
189             }
190         }
191         
192         // TODO: does this need to be immutable?  Spec does not indicate either
193         // way.
194         if (result == null)
195         {
196             return Collections.emptyList();
197         }
198         else
199         {
200             return Collections.unmodifiableCollection (result);
201         }
202     }
203 
204 }