1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38
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
95
96
97
98 @Override
99 public TagAttribute[] getAll()
100 {
101 return attributes;
102 }
103
104
105
106
107
108
109
110
111 @Override
112 public TagAttribute get(String localName)
113 {
114 return get("", localName);
115 }
116
117
118
119
120
121
122
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
138
139
140
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
151
152
153
154 @Override
155 public String[] getNamespaces()
156 {
157 return namespaces;
158 }
159
160
161
162
163
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 }