001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.oaipmh.responses;
025:
026: import java.io.IOException;
027: import java.util.Iterator;
028: import org.fao.oaipmh.OaiPmh;
029: import org.fao.oaipmh.exceptions.OaiPmhException;
030: import org.fao.oaipmh.requests.ListRequest;
031: import org.jdom.Element;
032: import org.jdom.JDOMException;
033: import org.xml.sax.SAXException;
034:
035: //=============================================================================
036:
037: public abstract class ListResponse extends AbstractResponse {
038: //---------------------------------------------------------------------------
039: //---
040: //--- Constructor
041: //---
042: //---------------------------------------------------------------------------
043:
044: public ListResponse() {
045: }
046:
047: //---------------------------------------------------------------------------
048:
049: public ListResponse(ListRequest lr, Element response) {
050: super (response);
051:
052: listReq = lr;
053: build(response);
054: }
055:
056: //---------------------------------------------------------------------------
057: //---
058: //--- API methods
059: //---
060: //---------------------------------------------------------------------------
061:
062: public boolean hasNext() {
063: if (iterator.hasNext())
064: return true;
065:
066: if (token != null && !token.isTokenEmpty())
067: return true;
068:
069: return false;
070: }
071:
072: //---------------------------------------------------------------------------
073:
074: public Object next() throws IOException, OaiPmhException,
075: JDOMException, SAXException, Exception {
076: if (iterator.hasNext())
077: return createObject((Element) iterator.next());
078:
079: if (token == null || token.isTokenEmpty())
080: throw new RuntimeException("Iterator exausted");
081:
082: build(listReq.resume(token));
083:
084: //--- just to avoid problems...
085: iterator.hasNext();
086:
087: return createObject((Element) iterator.next());
088: }
089:
090: //---------------------------------------------------------------------------
091:
092: public ResumptionToken getResumptionToken() {
093: return token;
094: }
095:
096: //---------------------------------------------------------------------------
097:
098: public void setResumptionToken(ResumptionToken token) {
099: this .token = token;
100: }
101:
102: //---------------------------------------------------------------------------
103: //---
104: //--- Protected methods
105: //---
106: //---------------------------------------------------------------------------
107:
108: protected abstract Object createObject(Element object);
109:
110: protected abstract String getListElementName();
111:
112: //---------------------------------------------------------------------------
113: //---
114: //--- Private methods
115: //---
116: //---------------------------------------------------------------------------
117:
118: private void build(Element response) {
119: Element operElem = response.getChild(listReq.getVerb(),
120: OaiPmh.Namespaces.OAI_PMH);
121: Element resToken = operElem.getChild("resumptionToken",
122: OaiPmh.Namespaces.OAI_PMH);
123:
124: token = (resToken == null) ? null : new ResumptionToken(
125: resToken);
126: iterator = operElem.getChildren(getListElementName(),
127: OaiPmh.Namespaces.OAI_PMH).iterator();
128: }
129:
130: //---------------------------------------------------------------------------
131: //---
132: //--- Variables
133: //---
134: //---------------------------------------------------------------------------
135:
136: private ListRequest listReq;
137: private ResumptionToken token;
138: private Iterator iterator;
139: }
140:
141: //=============================================================================
|