com.hfg.citation.CSL Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.citation;
import com.hfg.javascript.JsArray;
import com.hfg.javascript.JsObjMap;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
//------------------------------------------------------------------------------
/**
Citation Style Language (CSL).
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class CSL
{
private static final String ABSTRACT = "abstract";
private static final String AUTHOR = "author";
private static final String CONTAINER_TITLE = "container-title";
private static final String DATE_PARTS = "date-parts";
private static final String DOI = "DOI";
private static final String FAMILY = "family";
private static final String GIVEN = "given";
private static final String ISBN = "ISBN";
private static final String ISSN = "ISSN";
private static final String ISSUE = "issue";
private static final String ISSUED = "issued";
private static final String MEDIUM = "medium";
private static final String PAGE = "page";
private static final String PUBLISHER = "publisher";
private static final String PUBLISHER_PLACE = "publisher-place";
private static final String SOURCE = "source";
private static final String TITLE = "title";
private static final String TYPE = "type";
private static final String URL = "URL";
private static final String VERSION = "version";
private static final String VOLUME = "volume";
private enum Type {
article("article"),
article_journal("article-journal"),
article_magazine("article-magazine"),
article_newspaper("article-newspaper"),
bill("bill"),
book("book"),
broadcast("broadcast"),
chapter("chapter"),
dataset("dataset"),
entry("entry"),
entry_dictionary("entry-dictionary"),
entry_encyclopedia("entry-encyclopedia"),
figure("figure"),
graphic("graphic"),
interview("interview"),
legal_case("legal_case"),
legislation("legislation"),
manuscript("manuscript"),
map("map"),
motion_picture("motion_picture"),
musical_score("musical_score"),
pamphlet("pamphlet"),
paper_conference("paper-conference"),
patent("patent"),
personal_communication("personal_communication"),
post("post"),
post_weblog("post-weblog"),
report("report"),
review("review"),
review_book("review-book"),
song("song"),
speech("speech"),
thesis("thesis"),
treaty("treaty"),
webpage("webpage"),
// These are official types but they are needed
software("software"),
general("general");
private String mValue;
//------------------------------------------------------------------------
private Type(String inValue)
{
mValue = inValue;
}
//------------------------------------------------------------------------
public String value()
{
return mValue;
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
/**
Converts a citation into CSL json format.
See
https://github.com/citation-style-language/schema/blob/master/csl-data.json
@param inCitation the source citation
@return a json map containing the citation data in CSL format
*/
public static JsObjMap generateJSON(Citation inCitation)
{
JsObjMap js = new JsObjMap();
js.put(TYPE, mapToType(inCitation.getType()));
if (StringUtil.isSet(inCitation.getAbstract()))
{
js.put(ABSTRACT, inCitation.getAbstract());
}
if (CollectionUtil.hasValues(inCitation.getAuthors()))
{
JsArray authorArray = new JsArray();
for (Author author : inCitation.getAuthors())
{
JsObjMap authorFields = new JsObjMap();
if (StringUtil.isSet(author.getLastName()))
{
authorFields.put(FAMILY, author.getLastName());
}
if (StringUtil.isSet(author.getLastName()))
{
authorFields.put(GIVEN, author.getFirstName() + (author.getMiddleName() != null ? " " + author.getMiddleName() : ""));
}
// TODO: Support more complex name strucutres
authorArray.add(authorFields);
}
js.put(AUTHOR, authorArray);
}
if (inCitation.getJournal() != null)
{
String journalString = inCitation.getJournal().getAbbrev();
if (null == journalString)
{
journalString = inCitation.getJournal().getTitle();
}
js.put(CONTAINER_TITLE, journalString);
}
if (StringUtil.isSet(inCitation.getDOI()))
{
js.put(DOI, inCitation.getDOI());
}
if (StringUtil.isSet(inCitation.getIssue()))
{
js.put(ISSUE, inCitation.getIssue());
}
if (StringUtil.isSet(inCitation.getPages()))
{
js.put(PAGE, inCitation.getPages());
}
if (inCitation.getYear() != null)
{
// "issued" : { "date-parts" : [[ "2013", "5", "27" ] ] },
JsArray innerDateParts = new JsArray();
innerDateParts.add(inCitation.getYear().toString());
JsArray dateParts = new JsArray();
dateParts.add(innerDateParts);
JsObjMap issuedMap = new JsObjMap();
issuedMap.put(DATE_PARTS, innerDateParts);
js.put(ISSUED, issuedMap);
}
if (StringUtil.isSet(inCitation.getISBN()))
{
js.put(ISBN, inCitation.getISBN());
}
if (StringUtil.isSet(inCitation.getISSN()))
{
js.put(ISSN, inCitation.getISSN());
}
if (inCitation.getMediaType() != null)
{
js.put(MEDIUM, inCitation.getMediaType());
}
if (StringUtil.isSet(inCitation.getPublisher()))
{
js.put(PUBLISHER, inCitation.getPublisher());
}
if (StringUtil.isSet(inCitation.getPlaceOfPublication()))
{
js.put(PUBLISHER_PLACE, inCitation.getPlaceOfPublication());
}
if (StringUtil.isSet(inCitation.getSource()))
{
js.put(SOURCE, inCitation.getSource());
}
if (StringUtil.isSet(inCitation.getTitle()))
{
js.put(TITLE, inCitation.getTitle());
}
if (StringUtil.isSet(inCitation.getURL()))
{
js.put(URL, inCitation.getURL());
}
if (StringUtil.isSet(inCitation.getVersion()))
{
js.put(VERSION, inCitation.getVersion());
}
if (StringUtil.isSet(inCitation.getVolume()))
{
js.put(VOLUME, inCitation.getVolume());
}
return js;
}
//---------------------------------------------------------------------------
// TODO: Finish this mapping
private static Type mapToType(CitationType inValue)
{
Type type;
switch (inValue)
{
case book:
type = Type.book;
break;
case chapter:
type = Type.chapter;
break;
case general:
type = Type.general;
break;
case journal:
type = Type.article_journal;
break;
case software:
type = Type.software;
break;
default:
type = Type.general;
break;
}
return type;
}
}