/* * [CiteMacro.java] * * Summary: Implements Cite Macro to create references to a book, dvd or electronics item. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-09-01 initial version, simplification of BookMacro */ package com.mindprod.stores; import com.mindprod.htmlmacros.macro.Global; import com.mindprod.htmlmacros.macro.Macro; import com.mindprod.htmlmacros.support.Tools; import com.mindprod.isbn.ISBNValidate; import java.io.File; /** * Implements Cite Macro to create references to a book, dvd or electronics item. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-09-01 initial version, simplification of Book * @since 2009-09-01 */ @SuppressWarnings( { "ConstantConditions" } ) public class CiteMacro extends Macro { /** * how to use the macro, works with ISBN and DVD UPC */ private static final String USAGE = "\nCite macro needs {isbn/asin/upc} {title}"; /** * Generate HTML for one citations. Assumes all data is clean. Generates link to only one bookstore. * * @param product ISBN13 without dashes or spaces. * @param title Title of the book. * * @return String representing all the HTML needed for one book referral. */ private String expand( final String product, final String title, final String dir ) { final String citedName = dir + "/" + product + ".html"; final File citedFile = new File( Global.configuration.getLocalWebrootWithSlashes(), citedName ); if ( citedFile.exists() ) { // link to cited file. class will be electronic dvd or book // we don't need a class on the " + Tools.completeLink( citedName, title, dir, fileBeingDistributed ) + ""; } else { throw new IllegalArgumentException( "Warning: no book/asin/dvd for Cite macro " + product + " " + title ); } } /** * Generate macro expansion for Book macro * * @param parms parsed params for the macro. * @param quiet true if want output suppressed. * @param verbose @return expanded text */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( parms.length != 2 ) { throw new IllegalArgumentException( USAGE ); } final String product = parms[ 0 ]; final String title = parms[ 1 ]; final String dir; switch ( product.length() ) { case 10: dir = "electronic"; break; case 12: dir = "dvd"; break; case 13: ISBNValidate.ensureISBN13Valid( product ); dir = "book"; break; default: throw new IllegalArgumentException( "[product] should be 10, 12 or 13 chars without dashes" ); } // generate the HTML to refer this book. return expand( product, title, dir ); } }