View Javadoc

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      {
49          this.nodes = nodes;
50      }
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          Iterator nodesIter = nodes.iterator();
62          while (nodesIter.hasNext())
63          {
64              Node node = (Node) nodesIter.next();
65              if (node.getName().equals(name) || node.getPath().equals(name))
66              {
67                  return node;
68              }
69          }
70          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          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          List subsetNodes = null;
94          Iterator nodesIter = nodes.iterator();
95          while (nodesIter.hasNext())
96          {
97              Node node = (Node) nodesIter.next();
98              if (node.getType().equals(type))
99              {
100                 if (subsetNodes == null)
101                 {
102                     subsetNodes = new ArrayList(nodes.size());
103                 }
104                 subsetNodes.add(node);
105             }
106         }
107 
108         // wrap matching nodes in new NodeSet
109         if (subsetNodes != null)
110             return new NodeSetImpl(subsetNodes);
111         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         List subsetNodes = null;
126         Pattern pattern = Pattern.compile(regex);
127         Iterator nodesIter = nodes.iterator();
128         while (nodesIter.hasNext())
129         {
130             Node node = (Node) nodesIter.next();
131             if (pattern.matcher(node.getName()).matches() || pattern.matcher(node.getPath()).matches())
132             {
133                 if (subsetNodes == null)
134                 {
135                     subsetNodes = new ArrayList(nodes.size());
136                 }
137                 subsetNodes.add(node);
138             }
139         }
140 
141         // wrap matching nodes in new NodeSet
142         if (subsetNodes != null)
143             return new NodeSetImpl(subsetNodes);
144         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         List subsetNodes = null;
159         Pattern pattern = Pattern.compile(regex);
160         Iterator nodesIter = nodes.iterator();
161         while (nodesIter.hasNext())
162         {
163             Node node = (Node) nodesIter.next();
164             if (!pattern.matcher(node.getName()).matches() && !pattern.matcher(node.getPath()).matches())
165             {
166                 if (subsetNodes == null)
167                 {
168                     subsetNodes = new ArrayList(nodes.size());
169                 }
170                 subsetNodes.add(node);
171             }
172         }
173 
174         // wrap matching nodes in new NodeSet
175         if (subsetNodes != null)
176             return new NodeSetImpl(subsetNodes);
177         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         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         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         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         throw new RuntimeException("NodeSet list is immutable from proxy.");
221     }
222 }