%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.page.document.psml.NodeSetImpl |
|
|
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.jetspeed.page.document.psml; |
|
18 | ||
19 | import java.util.Comparator; |
|
20 | import java.util.HashMap; |
|
21 | import java.util.Iterator; |
|
22 | import java.util.Map; |
|
23 | import java.util.TreeMap; |
|
24 | import java.util.regex.Pattern; |
|
25 | ||
26 | import org.apache.jetspeed.page.document.Node; |
|
27 | import org.apache.jetspeed.page.document.NodeSet; |
|
28 | ||
29 | /** |
|
30 | * <p> |
|
31 | * PageSetImpl |
|
32 | * </p> |
|
33 | * <p> |
|
34 | * |
|
35 | * </p> |
|
36 | * |
|
37 | * @author <a href="mailto:weaver@apache.org">Scott T. Weaver </a> |
|
38 | * @version $Id: NodeSetImpl.java 516448 2007-03-09 16:25:47Z ate $ |
|
39 | * |
|
40 | */ |
|
41 | public class NodeSetImpl implements NodeSet |
|
42 | { |
|
43 | private Map nodes; |
|
44 | private Map subsets; |
|
45 | private String resolveToPath; |
|
46 | private Comparator comparator; |
|
47 | 0 | protected static final Map patternCache = new HashMap(); |
48 | ||
49 | public NodeSetImpl( String resolveToPath ) |
|
50 | 0 | { |
51 | 0 | this.resolveToPath = resolveToPath; |
52 | 0 | nodes = new TreeMap(); |
53 | 0 | subsets = new HashMap(); |
54 | 0 | } |
55 | ||
56 | /** |
|
57 | * |
|
58 | * @param resolveToPath |
|
59 | * @param comparator |
|
60 | */ |
|
61 | public NodeSetImpl( String resolveToPath, Comparator comparator ) |
|
62 | 0 | { |
63 | 0 | this.resolveToPath = resolveToPath; |
64 | 0 | nodes = new TreeMap(comparator); |
65 | 0 | this.comparator = comparator; |
66 | 0 | subsets = new HashMap(); |
67 | 0 | } |
68 | ||
69 | /** |
|
70 | * |
|
71 | * <p> |
|
72 | * get |
|
73 | * </p> |
|
74 | * |
|
75 | * @see org.apache.jetspeed.page.document.NodeSet#get(java.lang.String) |
|
76 | * @param name |
|
77 | * @return |
|
78 | */ |
|
79 | public Node get( String name ) |
|
80 | { |
|
81 | ||
82 | 0 | if (nodes.containsKey(name)) |
83 | { |
|
84 | 0 | return (Node) nodes.get(name); |
85 | } |
|
86 | 0 | else if (resolveToPath != null) |
87 | { |
|
88 | 0 | if (resolveToPath.endsWith(Node.PATH_SEPARATOR)) |
89 | { |
|
90 | 0 | return (Node) nodes.get(resolveToPath + name); |
91 | } |
|
92 | else |
|
93 | { |
|
94 | 0 | return (Node) nodes.get(resolveToPath + Node.PATH_SEPARATOR + name); |
95 | } |
|
96 | } |
|
97 | ||
98 | 0 | return null; |
99 | } |
|
100 | ||
101 | /** |
|
102 | * |
|
103 | * <p> |
|
104 | * add |
|
105 | * </p> |
|
106 | * |
|
107 | * @see org.apache.jetspeed.page.document.NodeSet#add(org.apache.jetspeed.page.document.Node) |
|
108 | * @param document |
|
109 | */ |
|
110 | public void add( Node node ) |
|
111 | { |
|
112 | 0 | String path = node.getPath(); |
113 | 0 | nodes.put(path, node); |
114 | 0 | if (subsets.containsKey(node.getType())) |
115 | { |
|
116 | 0 | ((NodeSet) subsets.get(node.getType())).add(node); |
117 | } |
|
118 | 0 | } |
119 | ||
120 | /** |
|
121 | * <p> |
|
122 | * size |
|
123 | * </p> |
|
124 | * |
|
125 | * @see org.apache.jetspeed.page.document.NodeSet#size() |
|
126 | * @return |
|
127 | */ |
|
128 | public int size() |
|
129 | { |
|
130 | 0 | return nodes.size(); |
131 | } |
|
132 | ||
133 | /** |
|
134 | * |
|
135 | * <p> |
|
136 | * iterator |
|
137 | * </p> |
|
138 | * |
|
139 | * @see org.apache.jetspeed.page.document.NodeSet#iterator() |
|
140 | * @return |
|
141 | */ |
|
142 | public Iterator iterator() |
|
143 | { |
|
144 | 0 | return nodes.values().iterator(); |
145 | } |
|
146 | ||
147 | /** |
|
148 | * <p> |
|
149 | * subset |
|
150 | * </p> |
|
151 | * |
|
152 | * @see org.apache.jetspeed.page.document.NodeSet#subset(java.lang.String) |
|
153 | * @param type |
|
154 | * @return |
|
155 | */ |
|
156 | public NodeSet subset( String type ) |
|
157 | { |
|
158 | 0 | NodeSet subset = (NodeSet) subsets.get(type); |
159 | 0 | if (subset == null) |
160 | { |
|
161 | 0 | subset = new NodeSetImpl(resolveToPath, comparator); |
162 | 0 | subsets.put(type, subset); |
163 | ||
164 | 0 | Iterator nodeItr = nodes.values().iterator(); |
165 | 0 | while (nodeItr.hasNext()) |
166 | { |
|
167 | 0 | Node node = (Node) nodeItr.next(); |
168 | 0 | if (node.getType().equals(type)) |
169 | { |
|
170 | 0 | subset.add(node); |
171 | } |
|
172 | 0 | } |
173 | } |
|
174 | ||
175 | 0 | return subset; |
176 | } |
|
177 | ||
178 | /** |
|
179 | * <p> |
|
180 | * exclusiveSubset |
|
181 | * </p> |
|
182 | * |
|
183 | * @see org.apache.jetspeed.page.document.NodeSet#exclusiveSubset(java.lang.String) |
|
184 | * @param regex |
|
185 | * @return |
|
186 | */ |
|
187 | public NodeSet exclusiveSubset( String regex ) |
|
188 | { |
|
189 | 0 | Iterator allNodes = nodes.entrySet().iterator(); |
190 | 0 | NodeSetImpl subset = new NodeSetImpl(resolveToPath, comparator); |
191 | 0 | final Pattern pattern = getPattern(regex); |
192 | 0 | while (allNodes.hasNext()) |
193 | { |
|
194 | 0 | Map.Entry entry = (Map.Entry) allNodes.next(); |
195 | 0 | Node node = (Node) entry.getValue(); |
196 | 0 | String key = (String) entry.getKey(); |
197 | 0 | if (!matches(pattern, key) && !matches(pattern, node.getName())) |
198 | { |
|
199 | 0 | subset.add(node); |
200 | } |
|
201 | 0 | } |
202 | ||
203 | 0 | return subset; |
204 | } |
|
205 | ||
206 | /** |
|
207 | * <p> |
|
208 | * inclusiveSubset |
|
209 | * </p> |
|
210 | * |
|
211 | * @see org.apache.jetspeed.page.document.NodeSet#inclusiveSubset(java.lang.String) |
|
212 | * @param regex |
|
213 | * @return |
|
214 | */ |
|
215 | public NodeSet inclusiveSubset( String regex ) |
|
216 | { |
|
217 | 0 | Iterator allNodes = nodes.entrySet().iterator(); |
218 | 0 | NodeSetImpl subset = new NodeSetImpl(resolveToPath, comparator); |
219 | 0 | final Pattern pattern = getPattern(regex); |
220 | 0 | while (allNodes.hasNext()) |
221 | { |
|
222 | 0 | Map.Entry entry = (Map.Entry) allNodes.next(); |
223 | 0 | String key = (String) entry.getKey(); |
224 | 0 | Node node = (Node) entry.getValue(); |
225 | 0 | if (matches(pattern, key) || matches(pattern, node.getName())) |
226 | { |
|
227 | 0 | subset.add(node); |
228 | } |
|
229 | 0 | } |
230 | ||
231 | 0 | return subset; |
232 | } |
|
233 | ||
234 | /** |
|
235 | * |
|
236 | * <p> |
|
237 | * getComparator |
|
238 | * </p> |
|
239 | * |
|
240 | * @return comparator used to order nodes |
|
241 | */ |
|
242 | public Comparator getComparator() |
|
243 | { |
|
244 | 0 | return comparator; |
245 | } |
|
246 | ||
247 | /** |
|
248 | * |
|
249 | * <p> |
|
250 | * matches |
|
251 | * </p> |
|
252 | * |
|
253 | * @param pattern |
|
254 | * @param value |
|
255 | * @return |
|
256 | */ |
|
257 | protected final boolean matches(Pattern pattern, String value) |
|
258 | { |
|
259 | 0 | return pattern.matcher(value).matches(); |
260 | } |
|
261 | ||
262 | /** |
|
263 | * |
|
264 | * <p> |
|
265 | * getPattern |
|
266 | * </p> |
|
267 | * |
|
268 | * @param regex |
|
269 | * @return |
|
270 | */ |
|
271 | protected final Pattern getPattern(String regex) |
|
272 | { |
|
273 | 0 | if(patternCache.containsKey(regex)) |
274 | { |
|
275 | 0 | return (Pattern)patternCache.get(regex); |
276 | } |
|
277 | else |
|
278 | { |
|
279 | 0 | Pattern pattern = Pattern.compile(regex); |
280 | 0 | patternCache.put(regex, pattern); |
281 | 0 | return pattern; |
282 | } |
|
283 | } |
|
284 | ||
285 | /** |
|
286 | * <p> |
|
287 | * contains |
|
288 | * </p> |
|
289 | * |
|
290 | * @see org.apache.jetspeed.page.document.NodeSet#contains() |
|
291 | * @return |
|
292 | */ |
|
293 | public boolean contains( Node node ) |
|
294 | { |
|
295 | 0 | return nodes.values().contains(node); |
296 | } |
|
297 | ||
298 | /** |
|
299 | * <p> |
|
300 | * isEmpty |
|
301 | * </p> |
|
302 | * |
|
303 | * @see org.apache.jetspeed.page.document.NodeSet#isEmpty() |
|
304 | * @return |
|
305 | */ |
|
306 | public boolean isEmpty() |
|
307 | { |
|
308 | 0 | return nodes.isEmpty(); |
309 | } |
|
310 | ||
311 | /** |
|
312 | * <p> |
|
313 | * remove |
|
314 | * </p> |
|
315 | * |
|
316 | * @param node to remove |
|
317 | * @return removed node |
|
318 | */ |
|
319 | public Node remove(Node node) |
|
320 | { |
|
321 | 0 | String path = node.getPath(); |
322 | 0 | if (nodes.get(path) == node) |
323 | { |
|
324 | 0 | nodes.remove(path); |
325 | 0 | if (subsets.containsKey(node.getType())) |
326 | { |
|
327 | 0 | ((NodeSetImpl) subsets.get(node.getType())).remove(node); |
328 | } |
|
329 | } |
|
330 | 0 | return null; |
331 | } |
|
332 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |