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.impl;
18  
19  import java.util.Comparator;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.TreeMap;
24  import java.util.regex.Pattern;
25  
26  import org.apache.commons.collections.map.LRUMap;
27  import org.apache.jetspeed.page.document.Node;
28  import org.apache.jetspeed.page.document.NodeSet;
29  
30  /***
31   * NodeSetImpl
32   *
33   * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
34   * @version $Id$
35   */
36  public class NodeSetImpl implements NodeSet
37  {
38      public static final NodeSetImpl EMPTY_NODE_SET = new NodeSetImpl();
39  
40      private static final Map patternCache = new LRUMap(128);
41  
42      private Map nodes;
43      private Comparator comparator;
44  
45      public NodeSetImpl(List nodes, Comparator comparator)
46      {
47          this.nodes = new TreeMap(comparator);        
48          Object[] nodeToCopy = nodes.toArray();
49          for (int ix = 0; ix < nodeToCopy.length; ix++)
50          {
51              Node node = (Node)nodeToCopy[ix];
52              if (!this.nodes.containsKey( node.getName()))
53              {
54                  this.nodes.put(node.getName(), node);
55              }
56          }         
57          this.comparator = comparator;
58      }
59  
60      public NodeSetImpl(List nodes)
61      {
62          this(nodes, null);
63      }
64  
65      public NodeSetImpl(Comparator comparator)
66      {
67          this.comparator = comparator;
68      }
69  
70      public NodeSetImpl(NodeSet nodeSet)
71      {
72          this((nodeSet instanceof NodeSetImpl) ? ((NodeSetImpl)nodeSet).comparator : (Comparator)null);
73      }
74  
75      public NodeSetImpl()
76      {
77      }
78  
79      /***
80       * getCachedPattern
81       *
82       * @param regex pattern
83       * @return cached pattern
84       */
85      private Pattern getCachedPattern(String regex)
86      {
87          synchronized (patternCache)
88          {
89              if (patternCache.containsKey(regex))
90              {
91                  return (Pattern)patternCache.get(regex);
92              }
93              else
94              {
95                  Pattern pattern = Pattern.compile(regex);
96                  patternCache.put(regex, pattern);
97                  return pattern;
98              }
99          }
100     }
101 
102     /* (non-Javadoc)
103      * @see org.apache.jetspeed.page.document.NodeSet#add(org.apache.jetspeed.page.document.Node)
104      */
105     public void add(Node node)
106     {
107         if (nodes == null)
108         {
109             nodes = new TreeMap(comparator);
110         }
111         if (!nodes.containsKey(node.getName()))
112         {
113             nodes.put(node.getName(), node);
114         }
115     }
116     
117     /* (non-Javadoc)
118      * @see org.apache.jetspeed.page.document.NodeSet#get(java.lang.String)
119      */
120     public Node get(String name)
121     {
122         if (nodes != null)
123         {
124             return (Node)nodes.get(name);
125         }
126         return null;
127     }
128     
129     /* (non-Javadoc)
130      * @see org.apache.jetspeed.page.document.NodeSet#iterator()
131      */
132     public Iterator iterator()
133     {
134         if (nodes == null)
135         {
136             nodes = new TreeMap(comparator);
137         }
138         return nodes.values().iterator();
139     }
140     
141     /* (non-Javadoc)
142      * @see org.apache.jetspeed.page.document.NodeSet#subset(java.lang.String)
143      */
144     public NodeSet subset(String type)
145     {
146         NodeSetImpl subset = new NodeSetImpl(comparator);
147         Iterator nodeItr = iterator();
148         while (nodeItr.hasNext())
149         {
150             Node node = (Node) nodeItr.next();
151             if (node.getType().equals(type))
152             {
153                 subset.add(node);
154             }
155         }
156         return subset;
157     }
158     
159     /* (non-Javadoc)
160      * @see org.apache.jetspeed.page.document.NodeSet#inclusiveSubset(java.lang.String)
161      */
162     public NodeSet inclusiveSubset(String regex)
163     {
164         Pattern pattern = getCachedPattern(regex);
165         NodeSetImpl subset = new NodeSetImpl(comparator);
166         Iterator nodeItr = iterator();
167         while (nodeItr.hasNext())
168         {
169             Node node = (Node) nodeItr.next();
170             if (pattern.matcher(node.getName()).matches())
171             {
172                 subset.add(node);
173             }
174         }
175         return subset;
176     }
177     
178     /* (non-Javadoc)
179      * @see org.apache.jetspeed.page.document.NodeSet#exclusiveSubset(java.lang.String)
180      */
181     public NodeSet exclusiveSubset(String regex)
182     {
183         Pattern pattern = getCachedPattern(regex);
184         NodeSetImpl subset = new NodeSetImpl(comparator);
185         Iterator nodeItr = iterator();
186         while (nodeItr.hasNext())
187         {
188             Node node = (Node) nodeItr.next();
189             if (!pattern.matcher(node.getName()).matches())
190             {
191                 subset.add(node);
192             }
193         }
194         return subset;
195     }
196     
197     /* (non-Javadoc)
198      * @see org.apache.jetspeed.page.document.NodeSet#size()
199      */
200     public int size()
201     {
202         if (nodes != null)
203         {
204             return nodes.size();
205         }
206         return 0;
207     }
208     
209     /* (non-Javadoc)
210      * @see org.apache.jetspeed.page.document.NodeSet#contains(org.apache.jetspeed.page.document.Node)
211      */
212     public boolean contains(Node node)
213     {
214         if (nodes != null)
215         {
216             return nodes.containsValue(node);
217         }
218         return false;
219     }
220     
221     /* (non-Javadoc)
222      * @see org.apache.jetspeed.page.document.NodeSet#isEmpty()
223      */
224     public boolean isEmpty()
225     {
226         if (nodes != null)
227         {
228             return nodes.isEmpty();
229         }
230         return true;
231     }
232 }