/* * [KJV.java] * * Summary: Generate a reference to an KJV: reference to King James Bible. * * Copyright: (c) 2007-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.8 2009-02-06 include go package in ZIP bundle. * For this to work, you must have the King James Bible online in the kjv directory * in the same file tree structure as it is on Mindprod.com with the same name tags. */ package com.mindprod.htmlmacros.macro; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.BibleBook; import com.mindprod.htmlmacros.support.Tools; import java.util.regex.Pattern; import static java.lang.System.*; /** * Generate a reference to an KJV: reference to King James Bible. * * @author Roedy Green, Canadian Mind Products * @version 1.8 2009-02-06 include go package in ZIP bundle. * For this to work, you must have the King James Bible online in the kjv directory * in the same file tree structure as it is on Mindprod.com with the same name tags. * @see com.mindprod.htmlmacros.support.ConfigurationForMindprod#KJV_DIRS_ALPHABETICALLY * @since 2007-03-12 */ public final class KJV extends Macro { /** how to use the macro */ /** * how to use the macro */ private static final String USAGE = "\nKJV macro needs (! = display or ~ = suppress book)" + " book fromChapter:fromVerse " + "[toChapter:toVerse]"; /** * Pattern to split chapter:verse. */ private static final Pattern SPLIT_ON_COLON = Pattern.compile( ":" ); /** * guts to Generate reference to King James Bible. * * @param bookNeeded true if need to display book * @param book enum for book of bible. * @param fromChapter as an int. * @param fromVerse as an int. * @param toChapter as an int, 0 if none. * @param toVerse as an int. * * @return expanded href to that piece of the KJV. * @noinspection WeakerAccess */ public String expand( boolean bookNeeded, BibleBook book, int fromChapter, int fromVerse, int toChapter, int toVerse ) { // generate URL final FastCat sb = new FastCat( 20 ); sb.append( "" ); // generate the human readable form if ( bookNeeded ) { sb.append( book.getFullName() ); } if ( bookNeeded && fromChapter > 0 ) { sb.append( " " ); } if ( fromChapter > 0 ) { sb.append( fromChapter ); } if ( fromVerse > 0 ) { sb.append( ":" ); sb.append( fromVerse ); } if ( toChapter > 0 ) { if ( toChapter != fromChapter ) { sb.append( "-" ); sb.append( toChapter ); if ( toVerse > 0 ) { sb.append( ":" ); sb.append( toVerse ); } } else { if ( toVerse > 0 && toVerse != fromVerse ) { sb.append( "-" ); sb.append( toVerse ); } } } sb.append( "" ); return sb.toString(); } /** * typical use: <!-- macro KJV ! 1_James 2:3 2:5 --> *

* ! fromChapter:fromVerse toChapter:toVerse -- display book too. *

* ~ fromChapter:fromVerse toChapter:toVerse -- suppress book display. * * @param parms book from to as Strings. * @param quiet true if want output suppressed. * @param verbose @return expanded macro HTML */ public final String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "K" ); } if ( !( 3 <= parms.length && parms.length <= 4 ) ) { throw new IllegalArgumentException( USAGE ); } char code = parms[ 0 ].charAt( 0 ); final boolean bookNeeded; switch ( code ) { case '!': bookNeeded = true; break; case '~': bookNeeded = false; break; default: throw new IllegalArgumentException( USAGE ); } BibleBook book; try { book = BibleBook.valueOfAlias( parms[ 1 ] ); } catch ( IllegalArgumentException e ) { throw new IllegalArgumentException( "KJV macro needs a bookname in standard form: e.g. 1_James." ); } String[] from = SPLIT_ON_COLON.split( parms[ 2 ] ); if ( from.length != 2 ) { throw new IllegalArgumentException( USAGE ); } int fromChapter = Integer.parseInt( from[ 0 ] ); int fromVerse = Integer.parseInt( from[ 1 ] ); // handle toChapter:toVerse int toChapter = 0; int toVerse = 0; if ( parms.length == 4 ) { String[] to = SPLIT_ON_COLON.split( parms[ 3 ] ); if ( from.length != 2 ) { throw new IllegalArgumentException( USAGE ); } toChapter = Integer.parseInt( to[ 0 ] ); toVerse = Integer.parseInt( to[ 1 ] ); } if ( ( toChapter > 0 ) && !( fromChapter * 1000 + fromVerse <= toChapter * 1000 + toVerse ) ) { throw new IllegalArgumentException( USAGE ); } if ( ( !bookNeeded ) && fromChapter == 0 ) { throw new IllegalArgumentException( "KJV macro needs something to show. Try ! instead of ~." ); } return expand( bookNeeded, book, fromChapter, fromVerse, toChapter, toVerse ); } }