View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.om.registry.base;
18  
19  import java.util.Iterator;
20  import java.util.Vector;
21  
22  import java.util.NoSuchElementException;
23  import java.lang.UnsupportedOperationException;
24  import java.lang.IllegalStateException;
25  
26  import org.apache.jetspeed.services.Registry;
27  
28  
29  /***
30   * PortletIterator - seamless iterator over nested vectors of portlet collections
31   * 
32   * 
33   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
34   * @version $Id: PortletIterator.java,v 1.3 2004/02/23 03:08:26 jford Exp $
35   */
36  public class PortletIterator implements Iterator
37  {
38      protected BasePortletEntry entry;
39      protected String method;
40      protected Vector vector ;
41      protected int index = 0;    
42  
43      public PortletIterator(BasePortletEntry entry, String method)
44      {
45          this.entry = entry;
46          this.method = method;
47          this.vector = getVector();
48      }
49  
50      public boolean hasNext()
51      {
52          int size = vector.size();
53  
54          if (size == 0)
55              return false;
56  
57          if (index >= size)
58          {
59              entry = getParentEntry(entry);
60              if (entry  == null)
61                  return false;
62              vector = getVector();
63  
64              if (vector == null)
65              {
66                  return false;
67              }
68              index = 0;
69              if (vector.size() == 0)
70                  return false;
71          }
72          return true;
73      }
74  
75      public void remove() throws IllegalStateException, UnsupportedOperationException
76      {
77          throw new UnsupportedOperationException("The remove() method is not supported");
78      }
79  
80      protected BasePortletEntry getParentEntry(BasePortletEntry entry)
81      {
82          String parentName = entry.getParent();
83          if (parentName == null || parentName.equals(""))
84              return null;
85  
86          BasePortletEntry parent = null;
87          parent = (BasePortletEntry)Registry.getEntry( Registry.PORTLET, entry.getParent() );        
88          return parent;
89      }
90  
91      public Object next() throws NoSuchElementException
92      {
93          Object o = vector.elementAt(index);
94          index++;
95          return o;
96      }
97  
98      protected Vector getVector()
99      {
100         try
101         {
102             this.vector = (Vector)this.entry.getClass().getMethod(this.method, null).invoke(this.entry, null);
103         }
104         catch (Exception e)
105         {
106             this.vector = null;
107         }
108 
109         return this.vector;
110     }
111 }
112