View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: BaseCursor.java,v 1.1.1.1 2004/05/19 14:10:47 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.xindice.cursor;
21  
22  import java.util.AbstractList;
23  import java.util.ArrayList;
24  
25  import org.apache.log4j.Logger;
26  
27  import org.xmldb.api.base.ResourceSet;
28  
29  import org.backsource.xindice.XindiceException;
30  /***
31   * A base class for cursor working against other cursors.
32   *
33   * <p>This is a template class, in that subclasses must follow a certain pattern to use this as its parent. This is a greedy cursor, that expects its subclasses to swallow its source in first invokation. A subclass is expected to do its work in the swallow method. When the mothod returns it must have created an ArrayList with its result put into it.</p>
34  <pre>
35    public void swallow() throws XindiceException {
36      store = new ArrayList();
37      //Use source.iterator() to get source content
38      // transform
39      // put into store
40    }
41  </pre>
42   *
43   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
44   * @version $Revision: 1.1.1.1 $
45   */
46  
47  public abstract class BaseCursor extends AbstractList implements Cursor{
48     private static Logger log = Logger.getLogger( BaseCursor.class);
49     protected Cursor cursor;
50     protected ArrayList store;
51     
52     /***
53      * Construct a source cursor around the ResourceSet.
54      */
55     public BaseCursor (ResourceSet set){
56        cursor = new ResourceSetCursor(set);
57     }      
58  
59     /***
60      * Use given cursor as source.
61      */
62     public BaseCursor (Cursor cursor){
63        this.cursor = cursor;
64     }
65     
66     public void setSource(Cursor cursor) throws XindiceException {
67        this.cursor = cursor;
68     }
69     
70     public Object get(int i) throws java.lang.IndexOutOfBoundsException,IllegalStateException  {
71        if ( store == null) {
72           try {                     
73              swallow();
74           } catch (XindiceException e) {
75              log.error("Could not set up cursor:" + e,e);
76              throw new IllegalStateException("Cursor could not be set up: " +e);
77           } // end of try-catch
78  
79        } // end of if ()
80  
81        if ( store == null) {
82           throw new java.lang.IndexOutOfBoundsException("Storage is null");
83        } // end of if ()
84        
85        return store.get(i);
86     }
87     
88     public int size() throws IllegalStateException {
89        if ( store == null) {
90           try {                     
91              swallow();
92           } catch (XindiceException e) {
93              log.error("Could not set up cursor:" + e,e);
94              throw new IllegalStateException("Cursor could not be set up: " +e);
95           } //
96        } // end of if ()
97        
98        return store != null? store.size() : 0;      
99     }
100    
101 
102    protected abstract void swallow() throws XindiceException;
103 }// BaseCursor