001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.page.document.proxy;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.regex.Pattern;
023:
024: import org.apache.jetspeed.page.document.Node;
025: import org.apache.jetspeed.page.document.NodeSet;
026:
027: /**
028: * This class implements generic NodeSet ordered lists
029: * used with proxied instances of PSML Folders to create a
030: * logical view of site content.
031: *
032: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
033: * @version $Id: NodeSetImpl.java 552972 2007-07-03 20:42:07Z taylor $
034: */
035: public class NodeSetImpl implements NodeSet {
036: /**
037: * nodes - list of proxy nodes
038: */
039: private List nodes;
040:
041: /**
042: * NodeSetImpl - construct immutable proxy Node NodeSet list
043: *
044: * @param nodes list of proxy Nodes
045: */
046: public NodeSetImpl(List nodes) {
047: this .nodes = nodes;
048: }
049:
050: /**
051: * get - return proxy Node by name or path
052: *
053: * @param name node name
054: * @return Node proxy
055: */
056: public Node get(String name) {
057: // search nodes for matching name or path
058: Iterator nodesIter = nodes.iterator();
059: while (nodesIter.hasNext()) {
060: Node node = (Node) nodesIter.next();
061: if (node.getName().equals(name)
062: || node.getPath().equals(name)) {
063: return node;
064: }
065: }
066: return null;
067: }
068:
069: /**
070: * iterator - return iterator over ordered list
071: *
072: * @return proxy NodeSet list iterator
073: */
074: public Iterator iterator() {
075: return nodes.listIterator();
076: }
077:
078: /**
079: * subset - construct new NodeSet containing Node proxies
080: * of the specified type
081: *
082: * @param type node type
083: * @return proxy NodeSet list
084: */
085: public NodeSet subset(String type) {
086: // search for matching nodes
087: List subsetNodes = null;
088: Iterator nodesIter = nodes.iterator();
089: while (nodesIter.hasNext()) {
090: Node node = (Node) nodesIter.next();
091: if (node.getType().equals(type)) {
092: if (subsetNodes == null) {
093: subsetNodes = new ArrayList(nodes.size());
094: }
095: subsetNodes.add(node);
096: }
097: }
098:
099: // wrap matching nodes in new NodeSet
100: if (subsetNodes != null)
101: return new NodeSetImpl(subsetNodes);
102: return null;
103: }
104:
105: /**
106: * inclusiveSubset - construct new NodeSet containing Node
107: * proxies whose name or path matches
108: * the specified regex pattern
109: *
110: * @param regex proxy Node name/path match pattern
111: * @return proxy NodeSet list
112: */
113: public NodeSet inclusiveSubset(String regex) {
114: // search for matching nodes
115: List subsetNodes = null;
116: Pattern pattern = Pattern.compile(regex);
117: Iterator nodesIter = nodes.iterator();
118: while (nodesIter.hasNext()) {
119: Node node = (Node) nodesIter.next();
120: if (pattern.matcher(node.getName()).matches()
121: || pattern.matcher(node.getPath()).matches()) {
122: if (subsetNodes == null) {
123: subsetNodes = new ArrayList(nodes.size());
124: }
125: subsetNodes.add(node);
126: }
127: }
128:
129: // wrap matching nodes in new NodeSet
130: if (subsetNodes != null)
131: return new NodeSetImpl(subsetNodes);
132: return null;
133: }
134:
135: /**
136: * exclusiveSubset - construct new NodeSet containing Node
137: * proxies whose name or path does not match
138: * the specified regex pattern
139: *
140: * @param regex proxy Node name/path match pattern
141: * @return proxy NodeSet list
142: */
143: public NodeSet exclusiveSubset(String regex) {
144: // search for matching nodes
145: List subsetNodes = null;
146: Pattern pattern = Pattern.compile(regex);
147: Iterator nodesIter = nodes.iterator();
148: while (nodesIter.hasNext()) {
149: Node node = (Node) nodesIter.next();
150: if (!pattern.matcher(node.getName()).matches()
151: && !pattern.matcher(node.getPath()).matches()) {
152: if (subsetNodes == null) {
153: subsetNodes = new ArrayList(nodes.size());
154: }
155: subsetNodes.add(node);
156: }
157: }
158:
159: // wrap matching nodes in new NodeSet
160: if (subsetNodes != null)
161: return new NodeSetImpl(subsetNodes);
162: return null;
163: }
164:
165: /**
166: * size - return size of NodeSet list
167: *
168: * @return size of list
169: */
170: public int size() {
171: return nodes.size();
172: }
173:
174: /**
175: * contains - test named Node proxy for existance in NodeSet list
176: *
177: * @param node proxy Node
178: * @return Node proxy
179: */
180: public boolean contains(Node node) {
181: return nodes.contains(node);
182: }
183:
184: /**
185: * isEmpty - returns flag indicationg whether NodeSet list is
186: * empty or not
187: *
188: * @return empty flag
189: */
190: public boolean isEmpty() {
191: return nodes.isEmpty();
192: }
193:
194: /**
195: * add - adds specified proxyNode to the ordered NodeSet list
196: *
197: * @param node proxy Node
198: */
199: public void add(Node node) {
200: // not implementd for immutable proxy lists
201: throw new RuntimeException(
202: "NodeSet list is immutable from proxy.");
203: }
204: }
|