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 1187701 2011-10-22 12:21:54Z 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 (int i = 0; i < size; i++)
85          {
86              List<TagAttribute> l = temp.get(i);
87              _nsattrs.add(l.toArray(new TagAttribute[l.size()]));
88          }
89      }
90  
91      /**
92       * Return an array of all TagAttributes in this set
93       * 
94       * @return a non-null array of TagAttributes
95       */
96      public TagAttribute[] getAll()
97      {
98          return _attributes;
99      }
100 
101     /**
102      * Using no namespace, find the TagAttribute
103      * 
104      * @see #get(String, String)
105      * @param localName
106      *            tag attribute name
107      * @return the TagAttribute found, otherwise null
108      */
109     public TagAttribute get(String localName)
110     {
111         return get("", localName);
112     }
113 
114     /**
115      * Find a TagAttribute that matches the passed namespace and local name.
116      * 
117      * @param ns
118      *            namespace of the desired attribute
119      * @param localName
120      *            local name of the attribute
121      * @return a TagAttribute found, otherwise null
122      */
123     public TagAttribute get(String ns, String localName)
124     {
125         if (ns != null && localName != null)
126         {
127             int idx = Arrays.binarySearch(_namespaces, ns);
128             if (idx >= 0)
129             {
130                 for (TagAttribute attribute : _nsattrs.get(idx))
131                 {
132                     if (localName.equals(attribute.getLocalName()))
133                     {
134                         return attribute;
135                     }
136                 }
137             }
138         }
139         
140         return null;
141     }
142 
143     /**
144      * Get all TagAttributes for the passed namespace
145      * 
146      * @param namespace
147      *            namespace to search
148      * @return a non-null array of TagAttributes
149      */
150     public TagAttribute[] getAll(String namespace)
151     {
152         int idx = 0;
153         if (namespace == null)
154         {
155             idx = Arrays.binarySearch(_namespaces, "");
156         }
157         else
158         {
159             idx = Arrays.binarySearch(_namespaces, namespace);
160         }
161         
162         if (idx >= 0)
163         {
164             return _nsattrs.get(idx);
165         }
166         
167         return EMPTY;
168     }
169 
170     /**
171      * A list of Namespaces found in this set
172      * 
173      * @return a list of Namespaces found in this set
174      */
175     public String[] getNamespaces()
176     {
177         return _namespaces;
178     }
179 
180     /*
181      * (non-Javadoc)
182      * 
183      * @see java.lang.Object#toString()
184      */
185     @Override
186     public String toString()
187     {
188         StringBuffer sb = new StringBuffer();
189         for (TagAttribute attribute : _attributes)
190         {
191             sb.append(attribute);
192             sb.append(' ');
193         }
194         
195         if (sb.length() > 1)
196         {
197             sb.setLength(sb.length() - 1);
198         }
199         
200         return sb.toString();
201     }
202 }