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;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import javax.faces.view.facelets.TagAttribute;
28  import javax.faces.view.facelets.TagAttributes;
29  
30  /**
31   * A set of TagAttributes, usually representing all attributes on a Tag.
32   * 
33   * TODO: PROFILE - Explore the possibility of using HashMap instead of sorted arrays. 
34   *       The footprint should be higher, but the instanciation and access speed should be faster 
35   *       Instanciation: from O(n log n) to O(1)
36   *       Access: from O(log n) to O(1)
37   * 
38   * @see org.apache.myfaces.view.facelets.tag.Tag
39   * @see org.apache.myfaces.view.facelets.tag.TagAttributeImpl
40   * @author Jacob Hookom
41   * @version $Id: TagAttributesImpl.java 1187700 2011-10-22 12:19:37Z bommel $
42   */
43  public final class TagAttributesImpl extends TagAttributes
44  {
45      private final static TagAttribute[] EMPTY = new TagAttribute[0];
46  
47      private final TagAttribute[] _attributes;
48  
49      private final String[] _namespaces;
50  
51      private final List<TagAttribute[]> _nsattrs;
52  
53      /**
54       * 
55       */
56      public TagAttributesImpl(TagAttribute[] attrs)
57      {
58          _attributes = attrs;
59  
60          // grab namespaces
61          Set<String> set = new HashSet<String>();
62          for (TagAttribute attribute : _attributes)
63          {
64              set.add(attribute.getNamespace());
65          }
66          
67          _namespaces = set.toArray(new String[set.size()]);
68          Arrays.sort(_namespaces);
69  
70          // assign attrs
71          int size = _namespaces.length;
72          List<List<TagAttribute>> temp = new ArrayList<List<TagAttribute>>(size);
73          for (int i = 0; i < size; i++)
74          {
75              temp.add(new ArrayList<TagAttribute>());
76          }
77          
78          for (TagAttribute attribute : _attributes)
79          {
80              temp.get(Arrays.binarySearch(_namespaces, attribute.getNamespace())).add(attribute);
81          }
82          
83          _nsattrs = new ArrayList<TagAttribute[]>(size);
84          for (List<TagAttribute> l : temp)
85          {
86              _nsattrs.add(l.toArray(new TagAttribute[l.size()]));
87          }
88      }
89  
90      /**
91       * Return an array of all TagAttributes in this set
92       * 
93       * @return a non-null array of TagAttributes
94       */
95      public TagAttribute[] getAll()
96      {
97          return _attributes;
98      }
99  
100     /**
101      * Using no namespace, find the TagAttribute
102      * 
103      * @see #get(String, String)
104      * @param localName
105      *            tag attribute name
106      * @return the TagAttribute found, otherwise null
107      */
108     public TagAttribute get(String localName)
109     {
110         return get("", localName);
111     }
112 
113     /**
114      * Find a TagAttribute that matches the passed namespace and local name.
115      * 
116      * @param ns
117      *            namespace of the desired attribute
118      * @param localName
119      *            local name of the attribute
120      * @return a TagAttribute found, otherwise null
121      */
122     public TagAttribute get(String ns, String localName)
123     {
124         if (ns != null && localName != null)
125         {
126             int idx = Arrays.binarySearch(_namespaces, ns);
127             if (idx >= 0)
128             {
129                 for (TagAttribute attribute : _nsattrs.get(idx))
130                 {
131                     if (localName.equals(attribute.getLocalName()))
132                     {
133                         return attribute;
134                     }
135                 }
136             }
137         }
138         
139         return null;
140     }
141 
142     /**
143      * Get all TagAttributes for the passed namespace
144      * 
145      * @param namespace
146      *            namespace to search
147      * @return a non-null array of TagAttributes
148      */
149     public TagAttribute[] getAll(String namespace)
150     {
151         int idx = 0;
152         if (namespace == null)
153         {
154             idx = Arrays.binarySearch(_namespaces, "");
155         }
156         else
157         {
158             idx = Arrays.binarySearch(_namespaces, namespace);
159         }
160         
161         if (idx >= 0)
162         {
163             return _nsattrs.get(idx);
164         }
165         
166         return EMPTY;
167     }
168 
169     /**
170      * A list of Namespaces found in this set
171      * 
172      * @return a list of Namespaces found in this set
173      */
174     public String[] getNamespaces()
175     {
176         return _namespaces;
177     }
178 
179     /*
180      * (non-Javadoc)
181      * 
182      * @see java.lang.Object#toString()
183      */
184     @Override
185     public String toString()
186     {
187         StringBuffer sb = new StringBuffer();
188         for (TagAttribute attribute : _attributes)
189         {
190             sb.append(attribute);
191             sb.append(' ');
192         }
193         
194         if (sb.length() > 1)
195         {
196             sb.setLength(sb.length() - 1);
197         }
198         
199         return sb.toString();
200     }
201 }