/* * [Formula.java] * * Summary: Display chemical formula, usually of a gas. * * Copyright: (c) 2005-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-14 original version * 1.1 2010-11-12 rename from Gas to Chemical to Formula, handle double digits */ package com.mindprod.htmlmacros.macro; import com.mindprod.common18.ST; import com.mindprod.fastcat.FastCat; import java.util.regex.Pattern; import static java.lang.System.*; /** * Display chemical formula, usually of a gas. *

* The main purpose of this is to keep formulas on one line, and accurate. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2010-11-12 rename from Gas to Chemical to Formula, handle double digits * @since 2009-09-14 */ @SuppressWarnings( { "UnusedDeclaration" } ) public final class Formula extends Macro { /** * how to use the macro */ private static final String USAGE = "\nFormula macro needs formula e.g. CO2"; private static final Pattern crack = Pattern.compile( " " ); /** * guts of Age macro expansion * * @param formula formula of the gas to expandNoRef * * @return HTML expansion of the formula */ private String expand( String formula ) { String[] components = crack.split( formula ); for ( String component : components ) { if ( ST.isLegal( component, "0123456789" ) ) { continue; } else { switch ( component ) { case "⇒": case "→": case "+": case "-": case "C12H22O11": // sucrose case "C12": // carbon 12 case "C13": // carbon 13 case "C14": // carbon 14 case "CH3CH2OH": // ethanol case "C3H7OH": // isopropanol case "C6H12O6": // glucose/fructos case "C8H18": // octane case "CH4": // methane case "ClO2": // chlorine dioxide case "CO2": // carbon dioxide case "D2": // deuterium case "D2O": // heavy water case "H+": // hydrogen ion case "H2": // hydrogen case "H2O": // water case "H2O2": // hydrogen peroxide case "H2S": // hydrogen sulphide case "HCN": // hydrogen cyanide case "N2": // nitrogen case "N2O": // nitrous oxide case "NF3": // nitrogen fluoride case "NH4": // ammonia case "NaCl": // salt case "NaOH": // sodium hydroxide case "O2": // oxygen case "O3": // ozone case "OH-": // hydroxyl ion case "SF6": // sulphur fluoride break; default: err.println( "Warning: Formula macro: unusual gas/chemical formula : " + formula + " at " + component + "\n" ); break; } } } final FastCat sb = new FastCat( formula.length() * 3 ); boolean inMolecule = false; formula = formula.replace( "⇒", "\u21d2" ); formula = formula.replace( "→", "\u2192" ); sb.append( "" ); for ( int i = 0; i < formula.length(); i++ ) { // we do NOT use final char c = formula.charAt( i ); if ( c == '+' ) { if ( inMolecule ) { sb.append( "⁺" ); // superscript + } else { sb.append( '+' ); } } else if ( c == '-' ) { if ( inMolecule ) { sb.append( "⁻" );// superscript - } else { sb.append( '-' ); } } else if ( '0' <= c && c <= '9' ) { if ( inMolecule ) { sb.append( "Ȉ" ); sb.append( c ); // subscript digit sb.append( ';' ); } else { sb.append( c ); } } else if ( c == ' ' ) { inMolecule = false; sb.append( c ); } else if ( c == '\u21d2' ) { inMolecule = false; sb.append( "⇒" ); } else if ( c == '\u2192' ) { inMolecule = false; sb.append( "→" ); } else { inMolecule = true; sb.append( c ); } } sb.append( "" ); return sb.toString(); } /** * Generate Formula formula * * @param parms formula without spaces or superscripts * @param quiet true if want output suppressed. * @param verbose @return expanded macro text */ public String expandMacro( String[] parms, final boolean quiet, final boolean verbose ) { if ( !quiet ) { out.print( "C" ); } if ( parms.length != 1 ) { throw new IllegalArgumentException( USAGE ); } return expand( parms[ 0 ] ); } }