com.hfg.citation.APA 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 java.net.MalformedURLException;
import java.net.URL;
import com.hfg.css.CSS;
import com.hfg.exception.ProgrammingException;
import com.hfg.html.HTMLTag;
import com.hfg.html.Span;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlParagraph;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXMLTag;
//------------------------------------------------------------------------------
/**
APA (American Psychological Association) citation style.
Example:
Taylor, J. A., & Johnson, R. S. (1997). Sequence database searches via de novo peptide sequencing
by tandem mass spectrometry. Rapid Communications in Mass Spectrometry, 11(9), 1067-1075.
doi:10.1002/(sici)1097-0231(19970615)11:93.0.co;2-l
@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 APA implements CitationFormat
{
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//---------------------------------------------------------------------------
@Override
public String generateAsString(Citation inCitation)
{
String formattedCitation;
switch (inCitation.getType())
{
case journal:
formattedCitation = generateJournalCitationAsString(inCitation);
break;
case software:
formattedCitation = generateSoftwareCitationAsString(inCitation);
break;
default:
throw new ProgrammingException("No support for " + inCitation.getType() + " citations yet!");
}
return formattedCitation;
}
//---------------------------------------------------------------------------
@Override
public HTMLTag generateAsHTML(Citation inCitation)
{
HTMLTag tag;
switch (inCitation.getType())
{
case journal:
tag = generateJournalCitationAsHTML(inCitation);
break;
case software:
tag = generateSoftwareCitationAsHTML(inCitation);
break;
default:
throw new ProgrammingException("No support for " + inCitation.getType() + " citations yet!");
}
return tag;
}
//---------------------------------------------------------------------------
@Override
public WmlParagraph generateAsDocx(Citation inCitation, WmlXMLTag inParentTag)
{
WmlParagraph p;
switch (inCitation.getType())
{
case journal:
p = generateJournalCitationAsDocx(inCitation, inParentTag);
break;
case software:
p = generateSoftwareCitationAsDocx(inCitation, inParentTag);
break;
default:
throw new ProgrammingException("No support for " + inCitation.getType() + " citations yet!");
}
return p;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//---------------------------------------------------------------------------
private String generateAuthorList(Citation inCitation)
{
StringBuilderPlus authorList = null;
if (CollectionUtil.hasValues(inCitation.getAuthors()))
{
authorList = new StringBuilderPlus().setDelimiter(", ");
if (2 == inCitation.getAuthors().size())
{
authorList.setDelimiter(", & ");
}
for (Author author : inCitation.getAuthors())
{
authorList.delimitedAppend(author.getLastName());
authorList.append(", ");
authorList.append(author.getFirstInitial() + ".");
Character middleInitial = author.getMiddleInitial();
if (middleInitial != null)
{
authorList.append(" " + middleInitial + ".");
}
}
}
return authorList != null ? authorList.toString() : null;
}
//---------------------------------------------------------------------------
private String generateJournalCitationAsString(Citation inCitation)
{
return StringUtil.stripHTMLTags(generateJournalCitationAsHTML(inCitation).toHTML());
}
//---------------------------------------------------------------------------
private HTMLTag generateJournalCitationAsHTML(Citation inCitation)
{
Span tag = new Span();
String authorList = generateAuthorList(inCitation);
if (StringUtil.isSet(authorList))
{
tag.addContent(authorList);
}
if (inCitation.getYear() != null)
{
tag.addContent(" (" + inCitation.getYear() + ").");
}
if (StringUtil.isSet(inCitation.getTitle()))
{
tag.addContent(" " + inCitation.getTitle() + ".");
}
if (inCitation.getJournal() != null)
{
String journalString = inCitation.getJournal().getAbbrev();
if (null == journalString)
{
journalString = inCitation.getJournal().getTitle();
}
tag.addContent(" ");
tag.addSpan(journalString).addStyle(CSS.ITALIC);
tag.addContent(", ");
}
if (StringUtil.isSet(inCitation.getVolume()))
{
tag.addContent(inCitation.getVolume());
}
if (StringUtil.isSet(inCitation.getIssue()))
{
tag.addContent("(" + inCitation.getIssue() + "),");
}
if (StringUtil.isSet(inCitation.getPages()))
{
tag.addContent(" " + inCitation.getPages() + ".");
}
if (StringUtil.isSet(inCitation.getDOI()))
{
tag.addContent(" ");
if (!inCitation.getDOI().startsWith("doi"))
{
tag.addContent("doi:");
}
tag.addContent(inCitation.getDOI());
}
if (StringUtil.isSet(inCitation.getURL()))
{
tag.addContent(" Retrieved from ");
tag.addLink(inCitation.getURL(), inCitation.getURL());
tag.addContent(".");
}
return tag;
}
//---------------------------------------------------------------------------
private WmlParagraph generateJournalCitationAsDocx(Citation inCitation, WmlXMLTag inParentTag)
{
WmlParagraph p = new WmlParagraph(inParentTag.getParentDoc());
inParentTag.addSubtag(p);
String authorList = generateAuthorList(inCitation);
if (StringUtil.isSet(authorList))
{
p.addTextRun(authorList);
}
if (inCitation.getYear() != null)
{
p.addTextRun(" (" + inCitation.getYear() + ").");
}
if (StringUtil.isSet(inCitation.getTitle()))
{
p.addTextRun(" " + inCitation.getTitle() + ".");
}
if (inCitation.getJournal() != null)
{
String journalString = inCitation.getJournal().getAbbrev();
if (null == journalString)
{
journalString = inCitation.getJournal().getTitle();
}
p.addTextRun(" ");
p.addTextRun(journalString).getProperties().setItalics();
p.addTextRun(", ");
}
if (StringUtil.isSet(inCitation.getVolume()))
{
p.addTextRun(inCitation.getVolume());
}
if (StringUtil.isSet(inCitation.getIssue()))
{
p.addTextRun("(" + inCitation.getIssue() + "),");
}
if (StringUtil.isSet(inCitation.getPages()))
{
p.addTextRun(" " + inCitation.getPages() + ".");
}
if (StringUtil.isSet(inCitation.getDOI()))
{
p.addTextRun(" ");
if (!inCitation.getDOI().startsWith("doi"))
{
p.addTextRun("doi:");
}
try
{
String doiURL = inCitation.getDOI();
if (! doiURL.startsWith("http"))
{
doiURL = "http://doi.org/" + doiURL;
}
p.addHyperlink(new URL(doiURL), inCitation.getDOI());
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
if (StringUtil.isSet(inCitation.getURL()))
{
p.addTextRun(" Retrieved from ");
try
{
p.addHyperlink(new URL(inCitation.getURL()), inCitation.getURL());
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
p.addTextRun(".");
}
return p;
}
//---------------------------------------------------------------------------
private String generateSoftwareCitationAsString(Citation inCitation)
{
return StringUtil.stripHTMLTags(generateSoftwareCitationAsHTML(inCitation).toHTML());
}
//---------------------------------------------------------------------------
private HTMLTag generateSoftwareCitationAsHTML(Citation inCitation)
{
Span tag = new Span();
String authorList = generateAuthorList(inCitation);
if (StringUtil.isSet(authorList))
{
tag.addContent(authorList);
if (inCitation.getYear() != null)
{
tag.addContent(" (" + inCitation.getYear() + ").");
}
if (StringUtil.isSet(inCitation.getTitle()))
{
tag.addContent(" " + inCitation.getTitle() + " [Computer software].");
}
}
else
{
if (StringUtil.isSet(inCitation.getTitle()))
{
tag.addContent(inCitation.getTitle() + " [Computer software].");
}
if (inCitation.getYear() != null)
{
tag.addContent(" (" + inCitation.getYear() + ").");
}
}
if (StringUtil.isSet(inCitation.getPlaceOfPublication()))
{
tag.addContent(" " + inCitation.getPlaceOfPublication());
}
if (StringUtil.isSet(inCitation.getPublisher()))
{
tag.addContent(" " + inCitation.getPublisher());
}
if (!tag.getContent().endsWith("."))
{
tag.addContent(".");
}
return tag;
}
//---------------------------------------------------------------------------
private WmlParagraph generateSoftwareCitationAsDocx(Citation inCitation, WmlXMLTag inParentTag)
{
WmlParagraph p = new WmlParagraph(inParentTag.getParentDoc());
inParentTag.addSubtag(p);
String authorList = generateAuthorList(inCitation);
if (StringUtil.isSet(authorList))
{
p.addTextRun(authorList);
if (inCitation.getYear() != null)
{
p.addTextRun(" (" + inCitation.getYear() + ").");
}
if (StringUtil.isSet(inCitation.getTitle()))
{
p.addTextRun(" " + inCitation.getTitle() + " [Computer software].");
}
}
else
{
if (StringUtil.isSet(inCitation.getTitle()))
{
p.addTextRun(inCitation.getTitle() + " [Computer software].");
}
if (inCitation.getYear() != null)
{
p.addTextRun(" (" + inCitation.getYear() + ").");
}
}
if (StringUtil.isSet(inCitation.getPlaceOfPublication()))
{
p.addTextRun(" " + inCitation.getPlaceOfPublication()
+ (inCitation.getPlaceOfPublication().endsWith(".") ? "" : "."));
}
if (StringUtil.isSet(inCitation.getPublisher()))
{
p.addTextRun(" " + inCitation.getPublisher()
+ (inCitation.getPublisher().endsWith(".") ? "" : "."));
}
return p;
}
}