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

de.undercouch.citeproc.ris.RISConverter Maven / Gradle / Ivy

package de.undercouch.citeproc.ris;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jbibtex.ParseException;

import de.undercouch.citeproc.bibtex.DateParser;
import de.undercouch.citeproc.bibtex.NameParser;
import de.undercouch.citeproc.csl.CSLDate;
import de.undercouch.citeproc.csl.CSLItemData;
import de.undercouch.citeproc.csl.CSLItemDataBuilder;
import de.undercouch.citeproc.csl.CSLName;
import de.undercouch.citeproc.csl.CSLType;

/**
 * Converts RIS references to CSL citation items
 * 
 * @author Michel Kraemer
 */
public class RISConverter {
    /**
     * 

* Loads a RIS library from a stream. *

*

* This method does not close the given stream. The caller is responsible for * closing it. *

* * @param is * the input stream to read from * @return the RIS library * @throws IOException * if the library could not be read * @throws ParseException * if the library is invalid */ public RISLibrary loadLibrary(InputStream is) throws IOException, ParseException { Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8); RISParser parser = new RISParser(); return parser.parse(reader); } /** * Converts the given library to a map of CSL citation items * * @param lib * the library * @return a map consisting of citation keys and citation items */ public Map toItemData(RISLibrary lib) { Map result = new HashMap<>(); for (RISReference ref : lib.getReferences()) { CSLItemData item = toItemData(ref); result.put(item.getId(), toItemData(ref)); } return result; } /** * Converts an RIS reference to a citation item * * @param ref * the reference to convert * @return the citation item */ public CSLItemData toItemData(RISReference ref) { // map type CSLType type = toType(ref.getType()); CSLItemDataBuilder builder = new CSLItemDataBuilder().type(type); // map label if (ref.getId() != null) { builder.id(ref.getId()); } else if (ref.getLabel() != null) { builder.id(ref.getLabel()); } // map date of last access if (ref.getAccessDate() != null) { builder.accessed(DateParser.toDate(ref.getAccessDate())); } // map authors if (ref.getAuthors() != null) { builder.author(toAuthors(ref.getAuthors())); } // map editors if (ref.getEditors() != null) { builder.editor(toAuthors(ref.getEditors())); } // map container title if (ref.getJournal() != null) { builder.containerTitle(ref.getJournal()); builder.collectionTitle(ref.getJournal()); } else if (ref.getNameOfDatabase() != null) { builder.containerTitle(ref.getNameOfDatabase()); } else { builder.containerTitle(ref.getBookOrConference()); builder.collectionTitle(ref.getBookOrConference()); } // map date if (ref.getDate() != null) { CSLDate date = DateParser.toDate(ref.getDate()); builder.issued(date); builder.eventDate(date); } else { CSLDate date = DateParser.toDate(ref.getYear()); builder.issued(date); builder.eventDate(date); } // map URL { builder.URL(ref.getURL()); } // map notes if (ref.getResearchNotes() != null) { builder.note(ref.getResearchNotes()); } else { builder.note(StringUtils.join(ref.getNotes(), '\n')); } // map issue builder.issue(ref.getIssue()); builder.number(ref.getNumber()); // map location builder.eventPlace(ref.getPlace()); builder.publisherPlace(ref.getPlace()); // map other attributes builder.abstrct(ref.getAbstrct()); builder.callNumber(ref.getCallNumber()); builder.DOI(ref.getDOI()); builder.edition(ref.getEdition()); builder.ISBN(ref.getIsbnOrIssn()); builder.ISSN(ref.getIsbnOrIssn()); builder.keyword(StringUtils.join(ref.getKeywords(), ',')); builder.language(ref.getLanguage()); builder.numberOfVolumes(ref.getNumberOfVolumes()); builder.originalTitle(ref.getOriginalPublication()); if (ref.getStartPage() != null && ref.getEndPage() != null) { builder.page(ref.getStartPage() + "-" + ref.getEndPage()); } else if (ref.getStartPage() != null) { builder.page(ref.getStartPage()); } else if (ref.getEndPage() != null) { builder.page(ref.getEndPage()); } builder.publisher(ref.getPublisher()); builder.reviewedTitle(ref.getReviewedItem()); builder.section(ref.getSection()); builder.titleShort(ref.getShortTitle()); builder.title(ref.getTitle()); builder.volume(ref.getVolume()); // create citation item return builder.build(); } /** * Converts a RIS reference type to a CSL type * * @param type * the type to convert * @return the converted type (never null, falls back to * {@link CSLType#ARTICLE}) */ public CSLType toType(RISType type) { switch (type) { case ABST : return CSLType.ARTICLE; case ADVS : return CSLType.ARTICLE; case AGGR : return CSLType.DATASET; case ANCIENT : return CSLType.ARTICLE; case ART : return CSLType.ARTICLE; case BILL : return CSLType.BILL; case BLOG : return CSLType.WEBPAGE; case BOOK : return CSLType.BOOK; case CASE : return CSLType.LEGAL_CASE; case CHAP : return CSLType.CHAPTER; case CHART : return CSLType.ARTICLE; case CLSWK : return CSLType.ARTICLE; case COMP : return CSLType.ARTICLE; case CONF : return CSLType.PAPER_CONFERENCE; case CPAPER : return CSLType.PAPER_CONFERENCE; case CTLG : return CSLType.BOOK; case DATA : return CSLType.DATASET; case DBASE : return CSLType.DATASET; case DICT : return CSLType.ENTRY_DICTIONARY; case EBOOK : return CSLType.BOOK; case ECHAP : return CSLType.CHAPTER; case EDBOOK : return CSLType.BOOK; case EJOUR : return CSLType.ARTICLE_JOURNAL; case ELEC : return CSLType.ARTICLE; case ENCYC : return CSLType.ENTRY_ENCYCLOPEDIA; case EQUA : return CSLType.ARTICLE; case FIGURE : return CSLType.FIGURE; case GOVDOC : return CSLType.LEGISLATION; case GRANT : return CSLType.LEGISLATION; case HEAR : return CSLType.ARTICLE; case ICOMM : return CSLType.PERSONAL_COMMUNICATION; case INPR : return CSLType.PAPER_CONFERENCE; case JFULL : return CSLType.ARTICLE_JOURNAL; case JOUR : return CSLType.ARTICLE_JOURNAL; case LEGAL : return CSLType.LEGISLATION; case MANSCPT : return CSLType.MANUSCRIPT; case MAP : return CSLType.MAP; case MGZN : return CSLType.ARTICLE_MAGAZINE; case MPCT : return CSLType.MOTION_PICTURE; case MULTI : return CSLType.WEBPAGE; case MUSIC : return CSLType.SONG; case NEWS : return CSLType.ARTICLE_NEWSPAPER; case PAMP : return CSLType.PAMPHLET; case PAT : return CSLType.PATENT; case PCOMM : return CSLType.PERSONAL_COMMUNICATION; case RPRT : return CSLType.REPORT; case SER : return CSLType.ARTICLE; case SLIDE : return CSLType.ARTICLE; case SOUND : return CSLType.SONG; case STAND : return CSLType.ARTICLE; case STAT : return CSLType.LEGISLATION; case THES : return CSLType.THESIS; case UNPB : return CSLType.ARTICLE; case VIDEO : return CSLType.MOTION_PICTURE; default : return CSLType.ARTICLE; } } private static CSLName[] toAuthors(String[] authors) { List result = new ArrayList<>(); for (String a : authors) { CSLName[] names = NameParser.parse(a); result.addAll(Arrays.asList(names)); } return result.toArray(new CSLName[0]); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy