1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }
78
79 }
80
81 if ( store == null) {
82 throw new java.lang.IndexOutOfBoundsException("Storage is null");
83 }
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 }
97
98 return store != null? store.size() : 0;
99 }
100
101
102 protected abstract void swallow() throws XindiceException;
103 }