Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package org.shareok.data.plosdata; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Iterator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.shareok.data.plosdata.PlosUtil.JournalType; import org.springframework.beans.BeansException; import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * note: dc.identifier.uri will be generated after the articles are imported * @author Tao Zhao */ public class PlosData { // private static final String language = "en_US"; // private static final String publisher = "PLOS ONE"; // private static final String peerreview = "Yes"; // private static final String rights = "Attribution 3.0 United States"; // private static final String rightsUri = "http://creativecommons.org/licenses/by/3.0/us/"; // private static final boolean rightsRequestable = false; private String peerReviewNotes; private String language; private String publisher; private String peerReview; private String rights; private String rightsUri; private boolean rightsRequestable; private String doi; private String uri; private String type; private String title; private String citation; private String relationUri; private String abstractText; private String acknowledgements; private String authorContributions; private String isPartOfSeries; private Date dateAvailable; private Date dateIssued; private String[] authors; private String[] subjects; private JournalType journalType; /** * * @return journal type */ public JournalType getJournalType() { return journalType; } /** * * @return dc.description.peerreviewnotes */ public String getPeerReviewNotes() { return peerReviewNotes; } /** * * @return dc.language.iso; */ public String getLanguage() { return language; } /** * * @return dc.publisher */ public String getPublisher() { return publisher; } /** * * @return dc.description.peerreview */ public String getPeerReview() { return peerReview; } /** * * @return dc.rights */ public String getRights() { return rights; } /** * * @return dc.relation.uri */ public String getRightsUri() { return rightsUri; } /** * * @return dc.relation.uri */ public String getRelationUri() { return relationUri; } /** * * @return dc.rights.requestable */ public boolean isRightsRequestable() { return rightsRequestable; } /** * * @return dc.identifier.doi */ public String getDoi() { return doi; } /** * * @return dc.identifier.uri * http://dx.doi.org/10.1371/journal.pone.0088732 * or * http://hdl.handle.net/1969.1/152287, this should be generated by DSpace */ public String getUri() { return uri; } /** * * @return dc.type */ public String getType() { return type; } /** * * @return dc.title */ public String getTitle() { return title; } /** * * @return dc.description.abstract */ public String getAbstractText() { return abstractText; } /** * * @return dc.description */ public String getAcknowledgements() { return acknowledgements; } /** * * @return dc.relation.ispartofseries */ public String getIsPartOfSeries() { return isPartOfSeries; } /** * * @return dc.date * * NOT implemented in the code */ public Date getDateAvailable() { return dateAvailable; } /** * * @return dc.date */ public Date getDateIssued() { return dateIssued; } /** * * @return dc.contributor.author */ public String[] getAuthors() { return authors; } /** * * @return dc.subject */ public String[] getSubjects() { return subjects; } /** * * @return dc.identifier.citation */ public String getCitation() { return citation; } /** * * @return dc.description */ public String getAuthorContributions() { return authorContributions; } /** * * @param journalType */ public void setJournalType(JournalType journalType) { this.journalType = journalType; } /** * * @param doi */ public void setDoi(String doi) { this.doi = doi; } /** * * @param uri */ public void setUri(String uri) { this.uri = uri; } /** * * @param relationUri */ public void setRelationUri(String relationUri) { this.relationUri = relationUri; } /** * * @param type */ public void setType(String type) { this.type = type; } /** * * @param title */ public void setTitle(String title) { this.title = title; } /** * * @param abstractText */ public void setAbstractText(String abstractText) { this.abstractText = abstractText; } /** * * @param acknowledgements */ public void setAcknowledgements(String acknowledgements) { this.acknowledgements = acknowledgements; } /** * * @param ispartofseries */ public void setIsPartOfSeries(String isPartOfSeries) { this.isPartOfSeries = isPartOfSeries; } /** * * @param dateAvailable */ public void setDateAvailable(Date dateAvailable) { this.dateAvailable = dateAvailable; } /** * * @param dateIssued */ public void setDateIssued(Date dateIssued) { this.dateIssued = dateIssued; } /** * * @param authors */ public void setAuthors(String[] authors) { this.authors = authors; } /** * * @param subjects */ public void setSubjects(String[] subjects) { this.subjects = subjects; } /** * * @param citation */ public void setCitation(String citation) { this.citation = citation; } public void setPeerReviewNotes(String peerReviewNotes) { this.peerReviewNotes = peerReviewNotes; } public void setLanguage(String language) { this.language = language; } public void setPublisher(String publisher) { this.publisher = publisher; } public void setPeerReview(String peerReview) { this.peerReview = peerReview; } public void setRights(String rights) { this.rights = rights; } public void setRightsUri(String rightsUri) { this.rightsUri = rightsUri; } public void setRightsRequestable(boolean rightsRequestable) { this.rightsRequestable = rightsRequestable; } public void setAuthorContributions(String authorContributions) { this.authorContributions = authorContributions; } public void getDataFromExcel(String fileName) { } /** * * @param type * type is one of the strings: pone, pbio, pgen, pmed, pcbi, ppat, pntd */ public void setPlosJournalType(String type) throws Exception { if (type.equals("pone")) journalType = JournalType.PLOSONE; else if (type.equals("pbio")) journalType = JournalType.PLOSBIO; else if (type.equals("pgen")) journalType = JournalType.PLOSGEN; else if (type.equals("pmed")) journalType = JournalType.PLOSMED; else if (type.equals("pcbi")) journalType = JournalType.PLOSCBI; else if (type.equals("ppat")) journalType = JournalType.PLOSPAT; else if (type.equals("pntd")) journalType = JournalType.PLOSNTD; else throw new Exception("Journal type is not defined!"); } /** * Generate the metadata xml file * @param fileName */ public void exportXmlByDoiData(String fileName) { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("dublin_core"); doc.appendChild(rootElement); // Add the type node: Element element = doc.createElement("dcvalue"); element.appendChild(doc.createTextNode("Research Article")); rootElement.appendChild(element); Attr attr = doc.createAttribute("element"); attr.setValue("type"); element.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); element.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); element.setAttributeNode(attr); // Add the abstract node: String abs = getAbstractText(); if (null != abs) { Element elementAbs = doc.createElement("dcvalue"); elementAbs.appendChild(doc.createTextNode(abs)); rootElement.appendChild(elementAbs); attr = doc.createAttribute("element"); attr.setValue("description"); elementAbs.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementAbs.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("abstract"); elementAbs.setAttributeNode(attr); } // Add the language node: String lang = getLanguage(); if (null != lang) { Element elementLang = doc.createElement("dcvalue"); elementLang.appendChild(doc.createTextNode(lang)); rootElement.appendChild(elementLang); attr = doc.createAttribute("element"); attr.setValue("language"); elementLang.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementLang.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("iso"); elementLang.setAttributeNode(attr); } // Add the title node: String tit = getTitle(); if (null != tit) { Element elementTitle = doc.createElement("dcvalue"); elementTitle.appendChild(doc.createTextNode(tit)); rootElement.appendChild(elementTitle); attr = doc.createAttribute("element"); attr.setValue("title"); elementTitle.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementTitle.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementTitle.setAttributeNode(attr); } // Add the available date node: // Element elementAvailable = doc.createElement("dcvalue"); // elementAvailable.appendChild(doc.createTextNode(getDateAvailable().toString())); // rootElement.appendChild(elementAvailable); // // attr = doc.createAttribute("element"); // attr.setValue("date"); // elementAvailable.setAttributeNode(attr); // // attr = doc.createAttribute("qualifier"); // attr.setValue("available"); // elementAvailable.setAttributeNode(attr); // Add the issued date node: Date issueDate = getDateIssued(); if (null != issueDate) { SimpleDateFormat format_issuedDate = new SimpleDateFormat("yyyy-MM-dd"); Element elementIssued = doc.createElement("dcvalue"); elementIssued.appendChild(doc.createTextNode(format_issuedDate.format(issueDate))); rootElement.appendChild(elementIssued); attr = doc.createAttribute("element"); attr.setValue("date"); elementIssued.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("issued"); elementIssued.setAttributeNode(attr); } // Add the author nodes: String[] authorSet = getAuthors(); if (null != authorSet && authorSet.length > 0) { for (String author : authorSet) { Element elementAuthor = doc.createElement("dcvalue"); elementAuthor.appendChild(doc.createTextNode(author)); rootElement.appendChild(elementAuthor); attr = doc.createAttribute("element"); attr.setValue("contributor"); elementAuthor.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("author"); elementAuthor.setAttributeNode(attr); } } // Add the acknowledgements node: String ack = getAcknowledgements(); if (null != ack) { Element elementAck = doc.createElement("dcvalue"); elementAck.appendChild(doc.createTextNode(ack)); rootElement.appendChild(elementAck); attr = doc.createAttribute("element"); attr.setValue("description"); elementAck.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementAck.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementAck.setAttributeNode(attr); } // Add the author contributions node: String contrib = getAuthorContributions(); if (null != contrib) { Element elementContribution = doc.createElement("dcvalue"); elementContribution.appendChild(doc.createTextNode(contrib)); rootElement.appendChild(elementContribution); attr = doc.createAttribute("element"); attr.setValue("description"); elementContribution.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementContribution.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementContribution.setAttributeNode(attr); } // Add the publisher node: String puber = getPublisher(); if (null != puber) { Element elementPublisher = doc.createElement("dcvalue"); elementPublisher.appendChild(doc.createTextNode(puber)); rootElement.appendChild(elementPublisher); attr = doc.createAttribute("element"); attr.setValue("publisher"); elementPublisher.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementPublisher.setAttributeNode(attr); } // Add the citation node: String cit = getCitation(); if (null != cit) { Element elementCitation = doc.createElement("dcvalue"); elementCitation.appendChild(doc.createTextNode(cit)); rootElement.appendChild(elementCitation); attr = doc.createAttribute("element"); attr.setValue("identifier"); elementCitation.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementCitation.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("citation"); elementCitation.setAttributeNode(attr); } // Add the rights node: String rit = getRights(); if (null != rit) { Element elementRights = doc.createElement("dcvalue"); elementRights.appendChild(doc.createTextNode(rit)); rootElement.appendChild(elementRights); attr = doc.createAttribute("element"); attr.setValue("rights"); elementRights.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementRights.setAttributeNode(attr); } // Add the rights URI node: String ritUri = getRightsUri(); if (null != ritUri) { Element elementRightsUri = doc.createElement("dcvalue"); elementRightsUri.appendChild(doc.createTextNode(ritUri)); rootElement.appendChild(elementRightsUri); attr = doc.createAttribute("element"); attr.setValue("rights"); elementRightsUri.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("uri"); elementRightsUri.setAttributeNode(attr); } // Add the rights requestable node: Element elementRightsRequestable = doc.createElement("dcvalue"); elementRightsRequestable.appendChild(doc.createTextNode(Boolean.toString(isRightsRequestable()))); rootElement.appendChild(elementRightsRequestable); attr = doc.createAttribute("element"); attr.setValue("rights"); elementRightsRequestable.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementRightsRequestable.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("requestable"); elementRightsRequestable.setAttributeNode(attr); // Add the is part of node: String partOf = getIsPartOfSeries(); if (null != partOf) { Element elementIsPartOf = doc.createElement("dcvalue"); elementIsPartOf.appendChild(doc.createTextNode(partOf)); rootElement.appendChild(elementIsPartOf); attr = doc.createAttribute("element"); attr.setValue("relation"); elementIsPartOf.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("ispartofseries"); elementIsPartOf.setAttributeNode(attr); } // Add the relation uri node: String reUri = getRelationUri(); if (null != reUri) { Element elementRelationUri = doc.createElement("dcvalue"); elementRelationUri.appendChild(doc.createTextNode(reUri)); rootElement.appendChild(elementRelationUri); attr = doc.createAttribute("element"); attr.setValue("relation"); elementRelationUri.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("uri"); elementRelationUri.setAttributeNode(attr); } // Add the subject nodes: String[] subjectSet = getSubjects(); if (null != subjectSet && subjectSet.length > 0) { for (String subject : subjectSet) { Element elementSubject = doc.createElement("dcvalue"); elementSubject.appendChild(doc.createTextNode(subject)); rootElement.appendChild(elementSubject); attr = doc.createAttribute("element"); attr.setValue("subject"); elementSubject.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementSubject.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("none"); elementSubject.setAttributeNode(attr); } } // Add the peerReview node: String review = getPeerReview(); if (null != review) { Element elementPeerReview = doc.createElement("dcvalue"); elementPeerReview.appendChild(doc.createTextNode(review)); rootElement.appendChild(elementPeerReview); attr = doc.createAttribute("element"); attr.setValue("description"); elementPeerReview.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementPeerReview.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("peerreview"); elementPeerReview.setAttributeNode(attr); } // Add the peer review notes node: String peer = getPeerReviewNotes(); if (null != peer) { Element elementPeerReviewNotes = doc.createElement("dcvalue"); elementPeerReviewNotes.appendChild(doc.createTextNode(peer)); rootElement.appendChild(elementPeerReviewNotes); attr = doc.createAttribute("element"); attr.setValue("description"); elementPeerReviewNotes.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementPeerReviewNotes.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("peerreviewnotes"); elementPeerReviewNotes.setAttributeNode(attr); } // Add the doi node: String doi = getDoi(); if (null != doi) { Element elementDoi = doc.createElement("dcvalue"); elementDoi.appendChild(doc.createTextNode(doi)); rootElement.appendChild(elementDoi); attr = doc.createAttribute("element"); attr.setValue("identifier"); elementDoi.setAttributeNode(attr); attr = doc.createAttribute("language"); attr.setValue("en_US"); elementDoi.setAttributeNode(attr); attr = doc.createAttribute("qualifier"); attr.setValue("doi"); elementDoi.setAttributeNode(attr); } // Generate the xml file: TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(fileName)); transformer.transform(source, result); } catch (ParserConfigurationException | TransformerException pce) { pce.printStackTrace(); System.exit(0); } catch (DOMException | BeansException e) { e.printStackTrace(); System.exit(0); } } }