/* * [CurrCon.java] * * Summary: Generate a CurrCon currency conversion. * * 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.8 2009-02-06 include go package in ZIP bundle. */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.ST; import com.mindprod.currcon.Geom; import com.mindprod.fastcat.FastCat; import com.mindprod.htmlmacros.support.Tools; import static com.mindprod.currcon.Geom.*; import static com.mindprod.htmlmacros.macro.Global.configuration; import static java.lang.System.*; /** * Generate a CurrCon currency conversion. * * @author Roedy Green, Canadian Mind Products * @version 1.8 2009-02-06 include go package in ZIP bundle. * @see com.mindprod.htmlmacros.macro.CMPApplet * @since 2009 */ public final class CurrCon extends Macro { /** * suffix for a tag */ private static final String TAG_TAIL = configuration.getTagTail(); /** * how to use the macro */ private static final String USAGE = "\ntry "; /** * set true to add instance parms to help debug CurrCon */ private static final boolean GENERATE_INSTANCE = false; /** * generate code for people who have no java * * @param currency e.g. USD CAD * @param amount amount in that currency, e.g. 10.00 * * @return generated HTML */ private static String noJava( String currency, String amount ) { if ( amount.length() == 0 ) { return "CurrCon Applet needs Java 1.8 or later to display prices in your local currency."; } final FastCat sb = new FastCat( 4 ); // display for people without java. Values will not change. if ( currency.equals( "USD" ) || currency.equals( "CAD" ) ) { sb.append( "$" ); } else if ( currency.equals( "EUR" ) ) { sb.append( "€" ); } sb.append( amount ); sb.append( " " ); sb.append( currency ); return sb.toString(); } /** * @param show which field to show, e.g. c$AN * @param currency base currency country usually USD, CAD, EUR * @param amount amount as a string e.g. 10.00 * * @return expanded macro HTML */ private String expand( String show, String currency, String amount ) { /* calculate width needed */ // how many pixels wide the Applet needs to be. // Applets can't change their space allocation. int width = 0; boolean showSymbols = false; for ( int i = 0; i < show.length(); i++ ) { char c = show.charAt( i ); switch ( c ) { case '$': showSymbols = true; break; case 'A': width += COMPACT_AMOUNT_DISPLAY_WIDTH; if ( showSymbols ) { width += SYMBOL_DISPLAY_WIDTH; } break; case 'c': /* add a currency code selector */ width += CURR_CODE_SELECT_WIDTH; break; case 'C': /* * add a currency code display, you can't change, except via * a corresponding c somewhere */ width += CURR_CODE_DISPLAY_WIDTH; break; case 'N': /* add a currency name display */ width += CURR_NAME_DISPLAY_WIDTH; break; case 'P': width += PRECISE_AMOUNT_DISPLAY_WIDTH; if ( showSymbols ) { width += SYMBOL_DISPLAY_WIDTH; } break; case 'I': default: throw new IllegalArgumentException( "invalid show parameter " + show ); } // end switch } // end for if ( configuration.isUsingHTML5( fileBeingDistributed ) ) { return expandUsingHTML5( show, currency, amount, width ); } else { return expandUsingHTML4( show, currency, amount, width ); } } // end expandNoRef private String expandUsingHTML4( final String show, final String currency, final String amount, final int width ) { final FastCat sb = new FastCat( 25 ); sb.append( "" ); sb.append( "" ); return sb.toString(); } private String expandUsingHTML5( final String show, final String currency, final String amount, final int width ) { final FastCat sb = new FastCat( 31 ); sb.append( "\n" ); sb.append( "\n" ); sb.append( "\n" ); sb.append( "\n" ); sb.append( "\n" ); if ( GENERATE_INSTANCE ) { sb.append( "\n" ); } if ( currency.length() != 0 ) { sb.append( "\n" ); } if ( amount.length() != 0 ) { sb.append( "\n" ); } sb.append( noJava( currency, amount ) ); sb.append( "\n" ); return sb.toString(); } /** * Generate a CurrCon currency conversion. expands * * @param parms show currency amount level. * @param quiet true if want output suppressed. * @param verbose @return expanded macro HTML */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "$" ); } // all parms are optional sometimes. String show = ""; String currency = ""; String amount = ""; if ( parms.length > 0 ) { show = parms[ 0 ]; if ( !ST.isLegal( show, "$APCcN" ) ) { throw new IllegalArgumentException( "Illegal command code character in CurrCon show parameter: " + show ); } } if ( parms.length > 1 ) { currency = parms[ 1 ].toUpperCase(); } if ( parms.length > 2 ) { amount = parms[ 2 ]; if ( !ST.isLegal( amount, "0123456789.,-" ) ) { throw new IllegalArgumentException( "Illegal character in CurrCon Amount: " + amount ); } } if ( show.length() == 0 ) { throw new IllegalArgumentException( "CurrCon missing show parm: " + USAGE ); } if ( show.indexOf( 'A' ) >= 0 || show.indexOf( 'P' ) >= 0 ) { // amount and currency mandatory if ( amount.length() == 0 ) { throw new IllegalArgumentException( "CurrCon missing amount: " + USAGE ); } if ( currency.length() != 3 ) { if ( currency.equals( "US" ) ) { throw new IllegalArgumentException( "CurrCon currency should be USD not US: " + USAGE ); } if ( currency.equals( "CN" ) ) { throw new IllegalArgumentException( "CurrCon currency should be CAD not CN: " + USAGE ); } throw new IllegalArgumentException( "CurrCon missing USD CAD etc. currency: " + USAGE ); } else { if ( currency.equals( "CDN" ) ) { throw new IllegalArgumentException( "CurrCon currency should be CAD not CDN: " + USAGE ); } if ( currency.equals( "USA" ) ) { throw new IllegalArgumentException( "CurrCon currency should be USD not USA: " + USAGE ); } // currency length is 3. We don't knew until later if we will have an exchange rate for that currency. } } else { // amount and currency not needed // If provided, just ignore. } return expand( show, currency, amount ); } }