View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: KeyCursor.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  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        // Set up key xpath
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        } // end of try-catch
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                 } // end of if ()
76                 if ( id == null) {
77                    throw new XindiceException("Could not get id from resource");
78                 } // end of if ()
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              } // end of try-catch
86  
87           } else {
88              throw new XindiceException("The object returned from source cursor was not an XML XML:DB resource");
89           } // end of else
90           
91        } // end of while ()
92        
93     }
94     
95  }// KeyCursor