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.HashMap;
24  import java.util.HashSet;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.faces.view.facelets.TagAttribute;
30  import javax.faces.view.facelets.TagAttributes;
31  
32  /**
33   * A set of TagAttributes, usually representing all attributes on a Tag.
34   *
35   * See org.apache.myfaces.view.facelets.tag.Tag
36   * See org.apache.myfaces.view.facelets.tag.TagAttributeImpl
37   * @author Jacob Hookom
38   * @version $Id$
39   */
40  public final class TagAttributesImpl extends TagAttributes
41  {
42      private final static TagAttribute[] EMPTY = new TagAttribute[0];
43  
44      private final TagAttribute[] attributes;
45      private final String[] namespaces;
46      private final HashMap<String, TagAttribute[]> namespaceAttributes;
47      private final HashMap<String, Map<String, TagAttribute>> namespaceLocalNameAttributes;
48  
49      public TagAttributesImpl(TagAttribute[] attributes)
50      {
51          this.attributes = attributes;
52          this.namespaceAttributes = new HashMap<>(1);
53          this.namespaceLocalNameAttributes = new HashMap<>(1);
54          
55          Set<String> namespacesSet = new HashSet<>();
56          HashMap<String, List<TagAttribute>> namespaceAttributesAsList = new HashMap<>();
57          
58          for (TagAttribute attribute : attributes)
59          {
60              namespacesSet.add(attribute.getNamespace());
61  
62              
63              List<TagAttribute> tagAttributes = namespaceAttributesAsList.get(attribute.getNamespace());
64              if (tagAttributes == null)
65              {
66                  tagAttributes = new ArrayList<>(attributes.length);
67                  namespaceAttributesAsList.put(attribute.getNamespace(), tagAttributes);
68              }
69              tagAttributes.add(attribute);
70              
71              
72              Map<String, TagAttribute> localeNameAttributes = namespaceLocalNameAttributes.get(attribute.getNamespace());
73              if (localeNameAttributes == null)
74              {
75                  localeNameAttributes = new HashMap<>(attributes.length);
76                  namespaceLocalNameAttributes.put(attribute.getNamespace(), localeNameAttributes);
77              }
78              localeNameAttributes.put(attribute.getLocalName(), attribute);
79          }
80  
81  
82          this.namespaces = namespacesSet.toArray(new String[namespacesSet.size()]);
83          Arrays.sort(this.namespaces);
84          
85          for (Map.Entry<String, List<TagAttribute>> entry : namespaceAttributesAsList.entrySet())
86          {
87              String key = entry.getKey();
88              List<TagAttribute> value = entry.getValue();
89              this.namespaceAttributes.put(key, value.toArray(new TagAttribute[value.size()]));
90          }
91      }
92  
93      /**
94       * Return an array of all TagAttributes in this set
95       * 
96       * @return a non-null array of TagAttributes
97       */
98      @Override
99      public TagAttribute[] getAll()
100     {
101         return attributes;
102     }
103 
104     /**
105      * Using no namespace, find the TagAttribute
106      * 
107      * See #get(String, String)
108      * @param localName tag attribute name
109      * @return the TagAttribute found, otherwise null
110      */
111     @Override
112     public TagAttribute get(String localName)
113     {
114         return get("", localName);
115     }
116 
117     /**
118      * Find a TagAttribute that matches the passed namespace and local name.
119      * 
120      * @param ns namespace of the desired attribute
121      * @param localName local name of the attribute
122      * @return a TagAttribute found, otherwise null
123      */
124     @Override
125     public TagAttribute get(String ns, String localName)
126     {
127         Map<String, TagAttribute> nsAttributes = namespaceLocalNameAttributes.get(ns);
128         if (nsAttributes != null)
129         {
130             return nsAttributes.get(localName);
131         }
132 
133         return null;
134     }
135 
136     /**
137      * Get all TagAttributes for the passed namespace
138      * 
139      * @param namespace namespace to search
140      * @return a non-null array of TagAttributes
141      */
142     @Override
143     public TagAttribute[] getAll(String namespace)
144     {
145         TagAttribute[] retVal = namespaceAttributes.get(namespace);
146         return retVal == null ? EMPTY : retVal;
147     }
148 
149     /**
150      * A list of Namespaces found in this set
151      * 
152      * @return a list of Namespaces found in this set
153      */
154     @Override
155     public String[] getNamespaces()
156     {
157         return namespaces;
158     }
159 
160     /*
161      * (non-Javadoc)
162      * 
163      * See java.lang.Object#toString()
164      */
165     @Override
166     public String toString()
167     {
168         StringBuilder sb = new StringBuilder();
169         for (TagAttribute attribute : attributes)
170         {
171             sb.append(attribute);
172             sb.append(' ');
173         }
174         
175         if (sb.length() > 1)
176         {
177             sb.setLength(sb.length() - 1);
178         }
179         
180         return sb.toString();
181     }
182 }