%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.page.document.proxy.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.proxy; |
|
18 | ||
19 | import java.util.ArrayList; |
|
20 | import java.util.Iterator; |
|
21 | import java.util.List; |
|
22 | import java.util.regex.Pattern; |
|
23 | ||
24 | import org.apache.jetspeed.page.document.Node; |
|
25 | import org.apache.jetspeed.page.document.NodeSet; |
|
26 | ||
27 | /** |
|
28 | * This class implements generic NodeSet ordered lists |
|
29 | * used with proxied instances of PSML Folders to create a |
|
30 | * logical view of site content. |
|
31 | * |
|
32 | * @author <a href="mailto:rwatler@apache.org">Randy Watler</a> |
|
33 | * @version $Id: NodeSetImpl.java 552972 2007-07-03 20:42:07Z taylor $ |
|
34 | */ |
|
35 | public class NodeSetImpl implements NodeSet |
|
36 | { |
|
37 | /** |
|
38 | * nodes - list of proxy nodes |
|
39 | */ |
|
40 | private List nodes; |
|
41 | ||
42 | /** |
|
43 | * NodeSetImpl - construct immutable proxy Node NodeSet list |
|
44 | * |
|
45 | * @param nodes list of proxy Nodes |
|
46 | */ |
|
47 | public NodeSetImpl(List nodes) |
|
48 | 0 | { |
49 | 0 | this.nodes = nodes; |
50 | 0 | } |
51 | ||
52 | /** |
|
53 | * get - return proxy Node by name or path |
|
54 | * |
|
55 | * @param name node name |
|
56 | * @return Node proxy |
|
57 | */ |
|
58 | public Node get(String name) |
|
59 | { |
|
60 | // search nodes for matching name or path |
|
61 | 0 | Iterator nodesIter = nodes.iterator(); |
62 | 0 | while (nodesIter.hasNext()) |
63 | { |
|
64 | 0 | Node node = (Node) nodesIter.next(); |
65 | 0 | if (node.getName().equals(name) || node.getPath().equals(name)) |
66 | { |
|
67 | 0 | return node; |
68 | } |
|
69 | 0 | } |
70 | 0 | return null; |
71 | } |
|
72 | ||
73 | /** |
|
74 | * iterator - return iterator over ordered list |
|
75 | * |
|
76 | * @return proxy NodeSet list iterator |
|
77 | */ |
|
78 | public Iterator iterator() |
|
79 | { |
|
80 | 0 | return nodes.listIterator(); |
81 | } |
|
82 | ||
83 | /** |
|
84 | * subset - construct new NodeSet containing Node proxies |
|
85 | * of the specified type |
|
86 | * |
|
87 | * @param type node type |
|
88 | * @return proxy NodeSet list |
|
89 | */ |
|
90 | public NodeSet subset(String type) |
|
91 | { |
|
92 | // search for matching nodes |
|
93 | 0 | List subsetNodes = null; |
94 | 0 | Iterator nodesIter = nodes.iterator(); |
95 | 0 | while (nodesIter.hasNext()) |
96 | { |
|
97 | 0 | Node node = (Node) nodesIter.next(); |
98 | 0 | if (node.getType().equals(type)) |
99 | { |
|
100 | 0 | if (subsetNodes == null) |
101 | { |
|
102 | 0 | subsetNodes = new ArrayList(nodes.size()); |
103 | } |
|
104 | 0 | subsetNodes.add(node); |
105 | } |
|
106 | 0 | } |
107 | ||
108 | // wrap matching nodes in new NodeSet |
|
109 | 0 | if (subsetNodes != null) |
110 | 0 | return new NodeSetImpl(subsetNodes); |
111 | 0 | return null; |
112 | } |
|
113 | ||
114 | /** |
|
115 | * inclusiveSubset - construct new NodeSet containing Node |
|
116 | * proxies whose name or path matches |
|
117 | * the specified regex pattern |
|
118 | * |
|
119 | * @param regex proxy Node name/path match pattern |
|
120 | * @return proxy NodeSet list |
|
121 | */ |
|
122 | public NodeSet inclusiveSubset(String regex) |
|
123 | { |
|
124 | // search for matching nodes |
|
125 | 0 | List subsetNodes = null; |
126 | 0 | Pattern pattern = Pattern.compile(regex); |
127 | 0 | Iterator nodesIter = nodes.iterator(); |
128 | 0 | while (nodesIter.hasNext()) |
129 | { |
|
130 | 0 | Node node = (Node) nodesIter.next(); |
131 | 0 | if (pattern.matcher(node.getName()).matches() || pattern.matcher(node.getPath()).matches()) |
132 | { |
|
133 | 0 | if (subsetNodes == null) |
134 | { |
|
135 | 0 | subsetNodes = new ArrayList(nodes.size()); |
136 | } |
|
137 | 0 | subsetNodes.add(node); |
138 | } |
|
139 | 0 | } |
140 | ||
141 | // wrap matching nodes in new NodeSet |
|
142 | 0 | if (subsetNodes != null) |
143 | 0 | return new NodeSetImpl(subsetNodes); |
144 | 0 | return null; |
145 | } |
|
146 | ||
147 | /** |
|
148 | * exclusiveSubset - construct new NodeSet containing Node |
|
149 | * proxies whose name or path does not match |
|
150 | * the specified regex pattern |
|
151 | * |
|
152 | * @param regex proxy Node name/path match pattern |
|
153 | * @return proxy NodeSet list |
|
154 | */ |
|
155 | public NodeSet exclusiveSubset(String regex) |
|
156 | { |
|
157 | // search for matching nodes |
|
158 | 0 | List subsetNodes = null; |
159 | 0 | Pattern pattern = Pattern.compile(regex); |
160 | 0 | Iterator nodesIter = nodes.iterator(); |
161 | 0 | while (nodesIter.hasNext()) |
162 | { |
|
163 | 0 | Node node = (Node) nodesIter.next(); |
164 | 0 | if (!pattern.matcher(node.getName()).matches() && !pattern.matcher(node.getPath()).matches()) |
165 | { |
|
166 | 0 | if (subsetNodes == null) |
167 | { |
|
168 | 0 | subsetNodes = new ArrayList(nodes.size()); |
169 | } |
|
170 | 0 | subsetNodes.add(node); |
171 | } |
|
172 | 0 | } |
173 | ||
174 | // wrap matching nodes in new NodeSet |
|
175 | 0 | if (subsetNodes != null) |
176 | 0 | return new NodeSetImpl(subsetNodes); |
177 | 0 | return null; |
178 | } |
|
179 | ||
180 | /** |
|
181 | * size - return size of NodeSet list |
|
182 | * |
|
183 | * @return size of list |
|
184 | */ |
|
185 | public int size() |
|
186 | { |
|
187 | 0 | return nodes.size(); |
188 | } |
|
189 | ||
190 | /** |
|
191 | * contains - test named Node proxy for existance in NodeSet list |
|
192 | * |
|
193 | * @param node proxy Node |
|
194 | * @return Node proxy |
|
195 | */ |
|
196 | public boolean contains(Node node) |
|
197 | { |
|
198 | 0 | return nodes.contains(node); |
199 | } |
|
200 | ||
201 | /** |
|
202 | * isEmpty - returns flag indicationg whether NodeSet list is |
|
203 | * empty or not |
|
204 | * |
|
205 | * @return empty flag |
|
206 | */ |
|
207 | public boolean isEmpty() |
|
208 | { |
|
209 | 0 | return nodes.isEmpty(); |
210 | } |
|
211 | ||
212 | /** |
|
213 | * add - adds specified proxyNode to the ordered NodeSet list |
|
214 | * |
|
215 | * @param node proxy Node |
|
216 | */ |
|
217 | public void add(Node node) |
|
218 | { |
|
219 | // not implementd for immutable proxy lists |
|
220 | 0 | throw new RuntimeException("NodeSet list is immutable from proxy."); |
221 | } |
|
222 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |