001package com.hfg.citation; 002 003import com.hfg.javascript.JsArray; 004import com.hfg.javascript.JsObjMap; 005import com.hfg.util.StringUtil; 006import com.hfg.util.collection.CollectionUtil; 007 008//------------------------------------------------------------------------------ 009/** 010 Citation Style Language (CSL). 011 <div> 012 @author J. Alex Taylor, hairyfatguy.com 013 </div> 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class CSL 037{ 038 039 private static final String ABSTRACT = "abstract"; 040 private static final String AUTHOR = "author"; 041 private static final String CONTAINER_TITLE = "container-title"; 042 private static final String DATE_PARTS = "date-parts"; 043 private static final String DOI = "DOI"; 044 private static final String FAMILY = "family"; 045 private static final String GIVEN = "given"; 046 private static final String ISBN = "ISBN"; 047 private static final String ISSN = "ISSN"; 048 private static final String ISSUE = "issue"; 049 private static final String ISSUED = "issued"; 050 private static final String MEDIUM = "medium"; 051 private static final String PAGE = "page"; 052 private static final String PUBLISHER = "publisher"; 053 private static final String PUBLISHER_PLACE = "publisher-place"; 054 private static final String SOURCE = "source"; 055 private static final String TITLE = "title"; 056 private static final String TYPE = "type"; 057 private static final String URL = "URL"; 058 private static final String VERSION = "version"; 059 private static final String VOLUME = "volume"; 060 061 private enum Type { 062 article("article"), 063 article_journal("article-journal"), 064 article_magazine("article-magazine"), 065 article_newspaper("article-newspaper"), 066 bill("bill"), 067 book("book"), 068 broadcast("broadcast"), 069 chapter("chapter"), 070 dataset("dataset"), 071 entry("entry"), 072 entry_dictionary("entry-dictionary"), 073 entry_encyclopedia("entry-encyclopedia"), 074 figure("figure"), 075 graphic("graphic"), 076 interview("interview"), 077 legal_case("legal_case"), 078 legislation("legislation"), 079 manuscript("manuscript"), 080 map("map"), 081 motion_picture("motion_picture"), 082 musical_score("musical_score"), 083 pamphlet("pamphlet"), 084 paper_conference("paper-conference"), 085 patent("patent"), 086 personal_communication("personal_communication"), 087 post("post"), 088 post_weblog("post-weblog"), 089 report("report"), 090 review("review"), 091 review_book("review-book"), 092 song("song"), 093 speech("speech"), 094 thesis("thesis"), 095 treaty("treaty"), 096 webpage("webpage"), 097 // These are official types but they are needed 098 software("software"), 099 general("general"); 100 101 private String mValue; 102 103 //------------------------------------------------------------------------ 104 private Type(String inValue) 105 { 106 mValue = inValue; 107 } 108 109 //------------------------------------------------------------------------ 110 public String value() 111 { 112 return mValue; 113 } 114 } 115 116 //########################################################################### 117 // PUBLIC METHODS 118 //########################################################################### 119 120 //--------------------------------------------------------------------------- 121 /** 122 Converts a citation into CSL json format. 123 See <a href='https://github.com/citation-style-language/schema/blob/master/csl-data.json'> 124 https://github.com/citation-style-language/schema/blob/master/csl-data.json</a> 125 @param inCitation the source citation 126 @return a json map containing the citation data in CSL format 127 */ 128 public static JsObjMap generateJSON(Citation inCitation) 129 { 130 JsObjMap js = new JsObjMap(); 131 132 js.put(TYPE, mapToType(inCitation.getType())); 133 134 135 if (StringUtil.isSet(inCitation.getAbstract())) 136 { 137 js.put(ABSTRACT, inCitation.getAbstract()); 138 } 139 140 if (CollectionUtil.hasValues(inCitation.getAuthors())) 141 { 142 JsArray authorArray = new JsArray(); 143 for (Author author : inCitation.getAuthors()) 144 { 145 JsObjMap authorFields = new JsObjMap(); 146 147 if (StringUtil.isSet(author.getLastName())) 148 { 149 authorFields.put(FAMILY, author.getLastName()); 150 } 151 152 if (StringUtil.isSet(author.getLastName())) 153 { 154 authorFields.put(GIVEN, author.getFirstName() + (author.getMiddleName() != null ? " " + author.getMiddleName() : "")); 155 } 156 157 // TODO: Support more complex name strucutres 158 159 authorArray.add(authorFields); 160 } 161 js.put(AUTHOR, authorArray); 162 } 163 164 165 if (inCitation.getJournal() != null) 166 { 167 String journalString = inCitation.getJournal().getAbbrev(); 168 if (null == journalString) 169 { 170 journalString = inCitation.getJournal().getTitle(); 171 } 172 173 js.put(CONTAINER_TITLE, journalString); 174 } 175 176 if (StringUtil.isSet(inCitation.getDOI())) 177 { 178 js.put(DOI, inCitation.getDOI()); 179 } 180 181 if (StringUtil.isSet(inCitation.getIssue())) 182 { 183 js.put(ISSUE, inCitation.getIssue()); 184 } 185 186 if (StringUtil.isSet(inCitation.getPages())) 187 { 188 js.put(PAGE, inCitation.getPages()); 189 } 190 191 if (inCitation.getYear() != null) 192 { 193 // "issued" : { "date-parts" : [[ "2013", "5", "27" ] ] }, 194 JsArray innerDateParts = new JsArray(); 195 innerDateParts.add(inCitation.getYear().toString()); 196 197 JsArray dateParts = new JsArray(); 198 dateParts.add(innerDateParts); 199 200 JsObjMap issuedMap = new JsObjMap(); 201 issuedMap.put(DATE_PARTS, innerDateParts); 202 js.put(ISSUED, issuedMap); 203 } 204 205 206 if (StringUtil.isSet(inCitation.getISBN())) 207 { 208 js.put(ISBN, inCitation.getISBN()); 209 } 210 211 if (StringUtil.isSet(inCitation.getISSN())) 212 { 213 js.put(ISSN, inCitation.getISSN()); 214 } 215 216 if (inCitation.getMediaType() != null) 217 { 218 js.put(MEDIUM, inCitation.getMediaType()); 219 } 220 221 if (StringUtil.isSet(inCitation.getPublisher())) 222 { 223 js.put(PUBLISHER, inCitation.getPublisher()); 224 } 225 226 if (StringUtil.isSet(inCitation.getPlaceOfPublication())) 227 { 228 js.put(PUBLISHER_PLACE, inCitation.getPlaceOfPublication()); 229 } 230 231 if (StringUtil.isSet(inCitation.getSource())) 232 { 233 js.put(SOURCE, inCitation.getSource()); 234 } 235 236 if (StringUtil.isSet(inCitation.getTitle())) 237 { 238 js.put(TITLE, inCitation.getTitle()); 239 } 240 241 if (StringUtil.isSet(inCitation.getURL())) 242 { 243 js.put(URL, inCitation.getURL()); 244 } 245 246 if (StringUtil.isSet(inCitation.getVersion())) 247 { 248 js.put(VERSION, inCitation.getVersion()); 249 } 250 251 if (StringUtil.isSet(inCitation.getVolume())) 252 { 253 js.put(VOLUME, inCitation.getVolume()); 254 } 255 256 return js; 257 } 258 259 //--------------------------------------------------------------------------- 260 // TODO: Finish this mapping 261 private static Type mapToType(CitationType inValue) 262 { 263 Type type; 264 switch (inValue) 265 { 266 case book: 267 type = Type.book; 268 break; 269 case chapter: 270 type = Type.chapter; 271 break; 272 case general: 273 type = Type.general; 274 break; 275 case journal: 276 type = Type.article_journal; 277 break; 278 case software: 279 type = Type.software; 280 break; 281 default: 282 type = Type.general; 283 break; 284 } 285 286 return type; 287 } 288}