Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TagAttributesImpl |
|
| 3.142857142857143;3.143 |
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$ | |
42 | */ | |
43 | public final class TagAttributesImpl extends TagAttributes | |
44 | { | |
45 | 0 | 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 | 0 | { |
58 | 0 | _attributes = attrs; |
59 | ||
60 | // grab namespaces | |
61 | 0 | Set<String> set = new HashSet<String>(); |
62 | 0 | for (TagAttribute attribute : _attributes) |
63 | { | |
64 | 0 | set.add(attribute.getNamespace()); |
65 | } | |
66 | ||
67 | 0 | _namespaces = set.toArray(new String[set.size()]); |
68 | 0 | Arrays.sort(_namespaces); |
69 | ||
70 | // assign attrs | |
71 | 0 | int size = _namespaces.length; |
72 | 0 | List<List<TagAttribute>> temp = new ArrayList<List<TagAttribute>>(size); |
73 | 0 | for (int i = 0; i < size; i++) |
74 | { | |
75 | 0 | temp.add(new ArrayList<TagAttribute>()); |
76 | } | |
77 | ||
78 | 0 | for (TagAttribute attribute : _attributes) |
79 | { | |
80 | 0 | temp.get(Arrays.binarySearch(_namespaces, attribute.getNamespace())).add(attribute); |
81 | } | |
82 | ||
83 | 0 | _nsattrs = new ArrayList<TagAttribute[]>(size); |
84 | 0 | for (int i = 0; i < size; i++) |
85 | { | |
86 | 0 | List<TagAttribute> l = temp.get(i); |
87 | 0 | _nsattrs.add(l.toArray(new TagAttribute[l.size()])); |
88 | } | |
89 | 0 | } |
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 | 0 | 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 | 0 | 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 | 0 | if (ns != null && localName != null) |
126 | { | |
127 | 0 | int idx = Arrays.binarySearch(_namespaces, ns); |
128 | 0 | if (idx >= 0) |
129 | { | |
130 | 0 | for (TagAttribute attribute : _nsattrs.get(idx)) |
131 | { | |
132 | 0 | if (localName.equals(attribute.getLocalName())) |
133 | { | |
134 | 0 | return attribute; |
135 | } | |
136 | } | |
137 | } | |
138 | } | |
139 | ||
140 | 0 | 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 | 0 | int idx = 0; |
153 | 0 | if (namespace == null) |
154 | { | |
155 | 0 | idx = Arrays.binarySearch(_namespaces, ""); |
156 | } | |
157 | else | |
158 | { | |
159 | 0 | idx = Arrays.binarySearch(_namespaces, namespace); |
160 | } | |
161 | ||
162 | 0 | if (idx >= 0) |
163 | { | |
164 | 0 | return _nsattrs.get(idx); |
165 | } | |
166 | ||
167 | 0 | 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 | 0 | return _namespaces; |
178 | } | |
179 | ||
180 | /* | |
181 | * (non-Javadoc) | |
182 | * | |
183 | * @see java.lang.Object#toString() | |
184 | */ | |
185 | @Override | |
186 | public String toString() | |
187 | { | |
188 | 0 | StringBuffer sb = new StringBuffer(); |
189 | 0 | for (TagAttribute attribute : _attributes) |
190 | { | |
191 | 0 | sb.append(attribute); |
192 | 0 | sb.append(' '); |
193 | } | |
194 | ||
195 | 0 | if (sb.length() > 1) |
196 | { | |
197 | 0 | sb.setLength(sb.length() - 1); |
198 | } | |
199 | ||
200 | 0 | return sb.toString(); |
201 | } | |
202 | } |