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 import java.util.Iterator;
22 import java.util.ArrayList;
23 import org.apache.log4j.Logger;
24
25 import org.w3c.dom.Node;
26 import org.w3c.dom.Attr;
27
28 import org.jaxen.JaxenException;
29 import org.jaxen.dom.DOMXPath;
30
31 import org.xmldb.api.base.Resource;
32 import org.xmldb.api.base.ResourceSet;
33 import org.xmldb.api.modules.XMLResource;
34 import org.xmldb.api.base.XMLDBException;
35
36 import org.backsource.xindice.XindiceException;
37 /***
38 * A cursor that only returns the keys of the resources, the object returned by ist source cursor must be Resources.
39 *
40 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
41 * @version $Revision: 1.1.1.1 $
42 */
43
44 public class KeyCursor extends BaseCursor {
45 private static Logger log = Logger.getLogger(KeyCursor.class);
46 public KeyCursor (ResourceSet set){
47 super(set);
48 }
49 public KeyCursor (Cursor cursor){
50 super(cursor);
51 }
52
53 public void swallow() throws XindiceException {
54
55 DOMXPath xp = null;
56 try {
57 xp = new DOMXPath("/*/@src:key");
58 xp.addNamespace("src","http://xml.apache.org/xindice/Query");
59 } catch (JaxenException e) {
60 throw new XindiceException("Could not construct DOM XPath:" + e,e);
61 }
62
63 Iterator it = cursor.iterator();
64 store = new ArrayList();
65 while ( it.hasNext()) {
66 Object o = it.next();
67 if ( o instanceof XMLResource) {
68 try {
69 String id = ((XMLResource)o).getId();
70 if ( id == null) {
71 Node doc = ((XMLResource)o).getContentAsDOM();
72 Attr at = (Attr)xp.selectSingleNode(doc);
73 log.debug("Selected attr " + at +" from doc " + ((XMLResource)o).getContent() + " with xpath " + xp.toString());
74 id = (at != null ? at.getNodeValue(): null);
75 }
76 if ( id == null) {
77 throw new XindiceException("Could not get id from resource");
78 }
79
80 store.add(id);
81 }catch ( JaxenException e) {
82 throw new XindiceException("Could select get id from resource: " +e,e);
83 } catch (XMLDBException e) {
84 throw new XindiceException("Could not get id from resource: " +e,e);
85 }
86
87 } else {
88 throw new XindiceException("The object returned from source cursor was not an XML XML:DB resource");
89 }
90
91 }
92
93 }
94
95 }