Moved common functionality of the different list responses into a base class (ListBase<T>) that uses generics. Also made it so that AtomSerializer creates proper urn:uuid:s for it's atom id:s

git-svn-id: https://oai4j-client.svn.sourceforge.net/svnroot/oai4j-client/trunk@6 201c9376-5148-0410-b534-98494c770f31
This commit is contained in:
bobcat_zed 2008-03-16 19:15:05 +00:00
parent dde8114c22
commit 0c4362c4fe
5 changed files with 30 additions and 143 deletions

View File

@ -17,6 +17,7 @@
package se.kb.oai.ore.impl;
import java.util.List;
import java.util.UUID;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
@ -138,6 +139,6 @@ public class AtomSerializer extends SerializerBase {
}
protected String createAtomId() {
return "urn:uid:" + (int) (Math.random() * 1000);
return "urn:uuid:" + UUID.randomUUID().toString();
}
}

View File

@ -17,21 +17,19 @@
package se.kb.oai.pmh;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
/**
* Class that represents the response from a <code>ListIdentifiers</code> request.
*
* You can use it to get a list of <code>Headers</code> that holds identifiers.
*
* @author Oskar Grenholm, National Library of Sweden
*/
public class IdentifiersList extends ResponseBase {
public class IdentifiersList extends ListBase<Header> {
private static final String HEADER_XPATH = "oai:ListIdentifiers/oai:header";
private List<Header> headers;
/**
* Creates an <code>IdentifiersList</code> from the returned response.
@ -42,42 +40,9 @@ public class IdentifiersList extends ResponseBase {
public IdentifiersList(Document document) throws ErrorResponseException {
super(document);
this.headers = new LinkedList<Header>();
super.list = new LinkedList<Header>();
for(Node node : xpath.selectNodes(HEADER_XPATH)) {
headers.add(new Header(node));
list.add(new Header(node));
}
}
/**
* Get the size of the list.
*
* @return the size
*/
public int size() {
return headers.size();
}
/**
* Get the identifiers as a list of <code>Headers</code>.
*
* @return a list of identifiers
*/
public List<Header> asList() {
return headers;
}
/**
* Get the <code>ResumptionToken</code>, if any, for this response.
*
* @return the <code>ResumptionToken</code>, or <code>null</code>
* if none available
*/
public ResumptionToken getResumptionToken() {
if (super.resumptionToken == null
|| super.resumptionToken.getId() == null
|| super.resumptionToken.getId().length() == 0)
return null;
return super.resumptionToken;
}
}

View File

@ -17,23 +17,20 @@
package se.kb.oai.pmh;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
/**
* This class represents the response from a <code>ListMetadataFormats</code> request
* to the OAI-PMH server.
* to the OAI-PMH server. You can use it to get a list of <code>MetadataFormats</code>.
*
* @author Oskar Grenholm, National Library of Sweden
*/
public class MetadataFormatsList extends ResponseBase {
public class MetadataFormatsList extends ListBase<MetadataFormat> {
private static final String METADATAFORMAT_XPATH = "oai:ListMetadataFormats/oai:metadataFormat";
private List<MetadataFormat> metadataFormats;
/**
* Creates a <code>MetadataFormatsList</code> from the returned response.
*
@ -43,27 +40,20 @@ public class MetadataFormatsList extends ResponseBase {
public MetadataFormatsList(Document document) throws ErrorResponseException {
super(document);
this.metadataFormats = new LinkedList<MetadataFormat>();
super.list = new LinkedList<MetadataFormat>();
for(Node metadataFormat : xpath.selectNodes(METADATAFORMAT_XPATH)) {
metadataFormats.add(new MetadataFormat(metadataFormat));
list.add(new MetadataFormat(metadataFormat));
}
}
/**
* Get the size of the list.
*
* @return the size
* The response from a <code>ListMetadataFormats</code> request will never
* have a resumption token, so this method will throw a <code>NoSuchMethodError</code>
* to prevent it from being used in that way.
*/
public int size() {
return metadataFormats.size();
@Override
public ResumptionToken getResumptionToken() {
throw new NoSuchMethodError("The response from the verb 'ListMetadataFormats' " +
"can't have a ResumptionToken!");
}
/**
* Get the metadata formats as a list of <code>MetadataFormats</code>.
*
* @return a list of metadata formats
*/
public List<MetadataFormat> asList() {
return metadataFormats;
}
}

View File

@ -17,22 +17,21 @@
package se.kb.oai.pmh;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
/**
* Class that represents the response from a <code>ListRecords</code> request.
* You can use it to get the list of <code>Records</code> that made up the
* response.
*
* @author Oskar Grenholm, National Library of Sweden
*/
public class RecordsList extends ResponseBase {
public class RecordsList extends ListBase<Record> {
private static final String RECORD_XPATH = "oai:ListRecords/oai:record";
private List<Record> records;
/**
* Creates a <code>RecordsList</code> from the returned response.
*
@ -42,42 +41,9 @@ public class RecordsList extends ResponseBase {
public RecordsList(Document document) throws ErrorResponseException {
super(document);
this.records = new LinkedList<Record>();
super.list = new LinkedList<Record>();
for (Node record : xpath.selectNodes(RECORD_XPATH)) {
records.add(new Record(document, record));
}
}
/**
* Get the size of the list.
*
* @return the size
*/
public int size() {
return records.size();
}
/**
* Get the records as a list of <code>Records</code>.
*
* @return a list of records
*/
public List<Record> asList() {
return records;
}
/**
* Get the <code>ResumptionToken</code>, if any, for this response.
*
* @return the <code>ResumptionToken</code>, or <code>null</code>
* if none available
*/
public ResumptionToken getResumptionToken() {
if (super.resumptionToken == null
|| super.resumptionToken.getId() == null
|| super.resumptionToken.getId().length() == 0)
return null;
return super.resumptionToken;
}
list.add(new Record(document, record));
}
}
}

View File

@ -17,22 +17,20 @@
package se.kb.oai.pmh;
import java.util.LinkedList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
/**
* Class that represents the response from a <code>ListSets</code> request.
* You can use it to get a list of <code>Sets</code>.
*
* @author Oskar Grenholm, National Library of Sweden
*/
public class SetsList extends ResponseBase {
public class SetsList extends ListBase<Set> {
private static final String SET_XPATH = "oai:ListSets/oai:set";
private List<Set> sets;
/**
* Creates an <code>SetsList</code> from the returned response.
*
@ -42,42 +40,9 @@ public class SetsList extends ResponseBase {
public SetsList(Document document) throws ErrorResponseException {
super(document);
this.sets = new LinkedList<Set>();
super.list = new LinkedList<Set>();
for (Node set : xpath.selectNodes(SET_XPATH)) {
sets.add(new Set(set));
list.add(new Set(set));
}
}
/**
* Get the size of the list.
*
* @return the size
*/
public int size() {
return sets.size();
}
/**
* Get the sets as a list of <code>Sets</code>.
*
* @return a list of sets
*/
public List<Set> asList() {
return sets;
}
/**
* Get the <code>ResumptionToken</code>, if any, for this response.
*
* @return the <code>ResumptionToken</code>, or <code>null</code>
* if none available
*/
public ResumptionToken getResumptionToken() {
if (super.resumptionToken == null
|| super.resumptionToken.getId() == null
|| super.resumptionToken.getId().length() == 0)
return null;
return super.resumptionToken;
}
}