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.view.facelets.tag.composite;
20  
21  import java.beans.FeatureDescriptor;
22  import java.util.Arrays;
23  
24  import javax.faces.view.facelets.FaceletContext;
25  import javax.faces.view.facelets.Tag;
26  import javax.faces.view.facelets.TagAttribute;
27  
28  /**
29   * TagAttribute utils for <composite:xxx> TagHandlers.
30   * 
31   * @author Jakob Korherr (latest modification by $Author$)
32   * @version $Revision$ $Date$
33   */
34  public final class CompositeTagAttributeUtils
35  {
36      
37      // prevent this from being instantiated
38      private CompositeTagAttributeUtils()
39      {
40      }
41      
42      /**
43       * Adds all attributes from the given Tag which are NOT listed in 
44       * standardAttributesSorted as a ValueExpression to the given BeanDescriptor.
45       * NOTE that standardAttributesSorted has to be alphabetically sorted in
46       * order to use binary search.
47       * @param descriptor
48       * @param tag
49       * @param standardAttributesSorted
50       * @param ctx
51       */
52      public static void addUnspecifiedAttributes(FeatureDescriptor descriptor, 
53              Tag tag, String[] standardAttributesSorted, FaceletContext ctx)
54      {
55          for (TagAttribute attribute : tag.getAttributes().getAll())
56          {
57              final String name = attribute.getLocalName();
58              if (Arrays.binarySearch(standardAttributesSorted, name) < 0)
59              {
60                  // attribute not found in standard attributes
61                  // --> put it on the BeanDescriptor
62                  descriptor.setValue(name, attribute.getValueExpression(ctx, Object.class));
63              }
64          }
65      }
66      
67      /**
68       * Returns true if the given Tag contains attributes that are not
69       * specified in standardAttributesSorted.
70       * NOTE that standardAttributesSorted has to be alphabetically sorted in
71       * order to use binary search.
72       * @param tag
73       * @param standardAttributesSorted
74       * @return
75       */
76      public static boolean containsUnspecifiedAttributes(Tag tag, String[] standardAttributesSorted)
77      {
78          for (TagAttribute attribute : tag.getAttributes().getAll())
79          {
80              final String name = attribute.getLocalName();
81              if (Arrays.binarySearch(standardAttributesSorted, name) < 0)
82              {
83                  return true;
84              }
85          }
86          return false;
87      }
88      
89      /**
90       * Applies the "displayName", "shortDescription", "expert", "hidden",
91       * and "preferred" attributes to the BeanDescriptor.
92       * @param descriptor
93       * @param ctx
94       * @param displayName
95       * @param shortDescription
96       * @param expert
97       * @param hidden
98       * @param preferred
99       */
100     public static void addDevelopmentAttributes(FeatureDescriptor descriptor, 
101             FaceletContext ctx, TagAttribute displayName, TagAttribute shortDescription, 
102             TagAttribute expert, TagAttribute hidden, TagAttribute preferred)
103     {
104         if (displayName != null)
105         {
106             descriptor.setDisplayName(displayName.getValue(ctx));
107         }
108         if (shortDescription != null)
109         {
110             descriptor.setShortDescription(shortDescription.getValue(ctx));
111         }
112         if (expert != null)
113         {
114             descriptor.setExpert(expert.getBoolean(ctx));
115         }
116         if (hidden != null)
117         {
118             descriptor.setHidden(hidden.getBoolean(ctx));
119         }
120         if (preferred != null)
121         {
122             descriptor.setPreferred(preferred.getBoolean(ctx));
123         }
124     }
125     
126     /**
127      * Applies the "displayName", "shortDescription", "expert", "hidden",
128      * and "preferred" attributes to the BeanDescriptor if they are all literal values.
129      * Thus no FaceletContext is necessary.
130      * @param descriptor
131      * @param displayName
132      * @param shortDescription
133      * @param expert
134      * @param hidden
135      * @param preferred
136      */
137     public static void addDevelopmentAttributesLiteral(FeatureDescriptor descriptor, 
138             TagAttribute displayName, TagAttribute shortDescription, 
139             TagAttribute expert, TagAttribute hidden, TagAttribute preferred)
140     {
141         if (displayName != null)
142         {
143             descriptor.setDisplayName(displayName.getValue());
144         }
145         if (shortDescription != null)
146         {
147             descriptor.setShortDescription(shortDescription.getValue());
148         }
149         if (expert != null)
150         {
151             descriptor.setExpert(Boolean.valueOf(expert.getValue()));
152         }
153         if (hidden != null)
154         {
155             descriptor.setHidden(Boolean.valueOf(hidden.getValue()));
156         }
157         if (preferred != null)
158         {
159             descriptor.setPreferred(Boolean.valueOf(preferred.getValue()));
160         }
161     }
162     
163     /**
164      * Returns true if all specified attributes are either null or literal.
165      * @param attributes
166      */
167     public static boolean areAttributesLiteral(TagAttribute... attributes)
168     {
169         for (TagAttribute attribute : attributes)
170         {
171             if (attribute != null && !attribute.isLiteral())
172             {
173                 // the attribute exists and is not literal
174                 return false;
175             }
176         }
177         // all attributes are literal
178         return true;
179     }
180     
181 }