All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xbib.oai.client.identify.IdentifyResponse Maven / Gradle / Ivy

There is a newer version: 2.3.1
Show newest version
package org.xbib.oai.client.identify;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xbib.helianthus.common.http.AggregatedHttpMessage;
import org.xbib.oai.client.ClientOAIResponse;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.io.Writer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 */
public class IdentifyResponse implements ClientOAIResponse {

    private String repositoryName;

    private URL baseURL;

    private String protocolVersion;

    private List adminEmails = new ArrayList<>();

    private Date earliestDatestamp;

    private String deletedRecord;

    private String granularity;

    private String compression;

    @Override
    public void receivedResponse(AggregatedHttpMessage message, Writer writer) throws IOException {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(message.content().toStringUtf8()));
            Document doc = db.parse(is);
            setGranularity(getString("granularity", doc.getDocumentElement()));
        } catch (ParserConfigurationException | SAXException e) {
            throw new IOException(e);
        }
    }

    public void setRepositoryName(String repositoryName) {
        this.repositoryName = repositoryName;
    }
    
    public String getRepositoryName() {
        return repositoryName;
    }
    
    public void setBaseURL(URL url) {
        this.baseURL = url;
    }
    
    public URL getBaseURL() {
        return baseURL;
    }
    
    public void setProtocolVersion(String protocolVersion) {
        this.protocolVersion = protocolVersion;
    }
    
    public String getProtocolVersion() {
        return protocolVersion;
    }
    
    public void addAdminEmail(String email) {
        adminEmails.add(email);
    }
    
    public List getAdminEmails() {
        return adminEmails;
    }
    
    public void setEarliestDatestamp(Date earliestDatestamp) {
        this.earliestDatestamp = earliestDatestamp;
    }
    
    public Date getEarliestDatestamp() {
        return earliestDatestamp;
    }
    
    public void setDeletedRecord(String deletedRecord) {
        this.deletedRecord = deletedRecord;
    }
    
    public String getDeleteRecord() {
        return deletedRecord;
    }
    
    public void setGranularity(String granularity) {
        this.granularity = granularity;
    }
    
    public String getGranularity() {
        return granularity;
    }
    
    public void setCompression(String compression) {
        this.compression = compression;
    }
    
    public String getCompression() {
        return compression;
    }

    private String getString(String tagName, Element element) {
        NodeList list = element.getElementsByTagName(tagName);
        if (list != null && list.getLength() > 0) {
            NodeList subList = list.item(0).getChildNodes();
            if (subList != null && subList.getLength() > 0) {
                return subList.item(0).getNodeValue();
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy