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

com.hfg.citation.MLA Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.citation;


import com.hfg.css.CSS;
import com.hfg.datetime.ThreadSafeDateFormat;
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;

//------------------------------------------------------------------------------
/**
 Modern Language Association (MLA) citation style.
 
Example:
 Author(s). "Title of Article." Title of Journal, Volume, Issue, Year, pages.

 Taylor, J. Alex, and Richard S. Johnson. “Sequence Database Searches Via de Novo Peptide Sequencing by Tandem
   Mass Spectrometry.” Rapid Communications in Mass Spectrometry, vol. 11, no. 9, 1997, pp. 1067–1075.,
   doi:10.1002/(sici)1097-0231(19970615)11:9<1067::aid-rcm953>3.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 MLA implements CitationFormat { private static ThreadSafeDateFormat sDateFormat = new ThreadSafeDateFormat("d MMM. YYYY"); //########################################################################## // 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 //########################################################################### //--------------------------------------------------------------------------- // Last, First M., and First M. Last. private String generateAuthorListForBibliography(Citation inCitation) { StringBuilderPlus authorList = null; if (CollectionUtil.hasValues(inCitation.getAuthors())) { authorList = new StringBuilderPlus().setDelimiter(", "); for (int i = 1; i <= inCitation.getAuthors().size(); i++) { Author author = inCitation.getAuthors().get(i - 1); if (1 == i) { // First Author authorList.append(author.getLastName()); authorList.delimitedAppend(author.getFirstName()); if (1 == author.getFirstName().length()) { authorList.append("."); } Character middleInitial = author.getMiddleInitial(); if (middleInitial != null) { authorList.append(" " + middleInitial + "."); } } else if (inCitation.getAuthors().size() == i) { // Last author authorList.delimitedAppend("and "); authorList.append(author.getFirstName()); if (1 == author.getFirstName().length()) { authorList.append("."); } Character middleInitial = author.getMiddleInitial(); if (middleInitial != null) { authorList.append(" " + middleInitial + "."); } authorList.append(" " + author.getLastName()); } else { authorList.delimitedAppend(author.getFirstName()); if (1 == author.getFirstName().length()) { authorList.append("."); } Character middleInitial = author.getMiddleInitial(); if (middleInitial != null) { authorList.append(" " + middleInitial + "."); } authorList.append(" " + author.getLastName()); } } if (! authorList.endsWith(".")) { authorList.append("."); } } 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 = generateAuthorListForBibliography(inCitation); if (StringUtil.isSet(authorList)) { tag.addContent(authorList); } 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); } if (StringUtil.isSet(inCitation.getVolume())) { tag.addContent(" "); tag.addContent(inCitation.getVolume()); tag.addContent("."); } if (StringUtil.isSet(inCitation.getIssue())) { tag.addContent(inCitation.getIssue()); } if (inCitation.getYear() != null) { tag.addContent(" (" + inCitation.getYear() + ")"); } if (StringUtil.isSet(inCitation.getPages())) { tag.addContent(": " + inCitation.getPages() + "."); } if (! StringUtil.isSet(inCitation.getWebsiteTitle())) { tag.addContent(" Print."); } else { tag.addContent(inCitation.getWebsiteTitle()); tag.addContent(". Web. "); if (inCitation.getAccessDate() != null) { tag.addContent(sDateFormat.format(inCitation.getAccessDate())); tag.addContent("."); } } return tag; } //--------------------------------------------------------------------------- private WmlParagraph generateJournalCitationAsDocx(Citation inCitation, WmlXMLTag inParentTag) { WmlParagraph p = new WmlParagraph(inParentTag.getParentDoc()); inParentTag.addSubtag(p); String authorList = generateAuthorListForBibliography(inCitation); if (StringUtil.isSet(authorList)) { p.addTextRun(authorList); } 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(); } if (StringUtil.isSet(inCitation.getVolume())) { p.addTextRun(" "); p.addTextRun(inCitation.getVolume()); p.addTextRun("."); } if (StringUtil.isSet(inCitation.getIssue())) { p.addTextRun(inCitation.getIssue()); } if (inCitation.getYear() != null) { p.addTextRun(" (" + inCitation.getYear() + ")"); } if (StringUtil.isSet(inCitation.getPages())) { p.addTextRun(": " + inCitation.getPages() + "."); } if (! StringUtil.isSet(inCitation.getWebsiteTitle())) { p.addTextRun(" Print."); } else { p.addTextRun(inCitation.getWebsiteTitle()); p.addTextRun(". Web. "); if (inCitation.getAccessDate() != null) { p.addTextRun(sDateFormat.format(inCitation.getAccessDate())); 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 = generateAuthorListForBibliography(inCitation); if (StringUtil.isSet(authorList)) { tag.addContent(authorList); } if (StringUtil.isSet(inCitation.getTitle())) { tag.addSpan(inCitation.getTitle()).addStyle(CSS.ITALIC); tag.addContent("."); if (StringUtil.isSet(inCitation.getWebsiteTitle()) || StringUtil.isSet(inCitation.getURL())) { tag.addContent(" Computer software."); } if (StringUtil.isSet(inCitation.getVersion())) { tag.addContent(" Vers. " + inCitation.getVersion() + "."); } } if (StringUtil.isSet(inCitation.getPlaceOfPublication())) { tag.addContent(" " + inCitation.getPlaceOfPublication() + ":"); } if (StringUtil.isSet(inCitation.getPublisher())) { tag.addContent(" " + inCitation.getPublisher()); } if (inCitation.getYear() != null) { tag.addContent(", " + inCitation.getYear() + "."); } else { tag.addContent("."); } if (! StringUtil.isSet(inCitation.getWebsiteTitle()) && ! StringUtil.isSet(inCitation.getURL())) { tag.addContent(" Computer software."); } else { tag.addContent(inCitation.getWebsiteTitle()); tag.addContent(". Web. "); if (inCitation.getAccessDate() != null) { tag.addContent(sDateFormat.format(inCitation.getAccessDate())); tag.addContent("."); } } return tag; } //--------------------------------------------------------------------------- private WmlParagraph generateSoftwareCitationAsDocx(Citation inCitation, WmlXMLTag inParentTag) { WmlParagraph p = new WmlParagraph(inParentTag.getParentDoc()); inParentTag.addSubtag(p); String authorList = generateAuthorListForBibliography(inCitation); if (StringUtil.isSet(authorList)) { p.addTextRun(authorList); } if (StringUtil.isSet(inCitation.getTitle())) { p.addTextRun(inCitation.getTitle()).getProperties().setItalics(); p.addTextRun("."); if (StringUtil.isSet(inCitation.getWebsiteTitle()) || StringUtil.isSet(inCitation.getURL())) { p.addTextRun(" Computer software."); } if (StringUtil.isSet(inCitation.getVersion())) { p.addTextRun(" Vers. " + inCitation.getVersion() + "."); } } if (StringUtil.isSet(inCitation.getPlaceOfPublication())) { p.addTextRun(" " + inCitation.getPlaceOfPublication() + ":"); } if (StringUtil.isSet(inCitation.getPublisher())) { p.addTextRun(" " + inCitation.getPublisher()); } if (inCitation.getYear() != null) { p.addTextRun(", " + inCitation.getYear() + "."); } else { p.addTextRun("."); } if (! StringUtil.isSet(inCitation.getWebsiteTitle()) && ! StringUtil.isSet(inCitation.getURL())) { p.addTextRun(" Computer software."); } else { p.addTextRun(inCitation.getWebsiteTitle()); p.addTextRun(". Web. "); if (inCitation.getAccessDate() != null) { p.addTextRun(sDateFormat.format(inCitation.getAccessDate())); p.addTextRun("."); } } return p; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy