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.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
32
33
34
35
36
37
38
39
40
41
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
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
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
93
94
95
96 public TagAttribute[] getAll()
97 {
98 return _attributes;
99 }
100
101
102
103
104
105
106
107
108
109 public TagAttribute get(String localName)
110 {
111 return get("", localName);
112 }
113
114
115
116
117
118
119
120
121
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
145
146
147
148
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
172
173
174
175 public String[] getNamespaces()
176 {
177 return _namespaces;
178 }
179
180
181
182
183
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 }