/* * [Macro.java] * * Summary: Abstract class that all Macro code generators must extend. * * Copyright: (c) 2002-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 2002-07-01 initial version * 1.1 2011-11-25 convert from interface to abstract class. */ package com.mindprod.htmlmacros.macro; import com.mindprod.htmlmacros.support.Tools; import java.io.File; import static com.mindprod.htmlmacros.macro.Global.configuration; /** * Abstract class that all Macro code generators must extend. *
* To pass in global information, you could use the java -D system parameters, which could pass data or the name of a * Customiser class. You could also use a call to the macro early in the file with the global information with a special * keyword parameter. The expandNoRef method may call any methods it wants in any class. Macros should ideally not * generate * any leading or trailing whitespace (e.g. space \n). Doing so will not be catastrophic, but it will cause false * changes to be reported, actually failures to fully compress, which are later corrected by the compactor utility. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2011-11-25 convert from interface to abstract class. * @since 2002-07-01 */ @SuppressWarnings( { "InterfaceNamingConvention" } ) public abstract class Macro { /** * are the macro masters and the expansions is separate files? */ private static final boolean separateMasters = configuration.getCompacting() == '*'; /** * unique value for */ private static int unique; /** * file where we write the expanded macros, old style back on fileBeingProcessed. * New style is separate file one level up in mother directory of * file containing the macros. * With an Include, always points to the the document where all the text will end up. */ protected File fileBeingDistributed; /** * file where the macros are. With an Include, always points to the original master document. */ protected File fileBeingProcessed; /** * get an int guaranteed unique on the page * * @return return unique number; */ public static int getUniqueOnPage() { return ++unique; } /** * start a new page. reset unique generator */ public static void resetUniqueOnPage() { unique = 0; } /** * Get macro expansion. Must call either setFileBeingProcessed or fileBeingDistributed firs. * * @param parms parsed parameters for the macro, often alternating field name and value. * Parameters have lead and * trail spaces removed, but otherwise are intact. If you want & changed to & use * com.mindprod.entities.DeEntifyStrings.deEntifyHTML( parms[i], ' ' ); If you want & changed to * & use EntifyStrings.entifyHTML ( parms[i] ) * @param quiet true if want optional output suppressed. * @param verbose true if want extra output. * * @return expanded text, using embedded \n for line breaks. Do not include the or * s. Optionally (and usually) includes an lead and trail \n. */ public abstract String expandMacro( final String[] parms, final boolean quiet, final boolean verbose ); /** * set the target for the macro expansions. The fileBeingProcessed will be deduced. * * @param fileBeingDistributed target file where expanded macro goes. */ public void setFileBeingDistributedInferFileBeingProcessed( final File fileBeingDistributed ) { if ( separateMasters ) { if ( Tools.isFileAMaster( fileBeingDistributed ) ) { // swap roles. this.fileBeingProcessed = Tools.correspondingMaster( fileBeingDistributed ); this.fileBeingDistributed = fileBeingProcessed; } else { this.fileBeingProcessed = Tools.correspondingMaster( fileBeingDistributed ); this.fileBeingDistributed = fileBeingDistributed; } } else { // old style, write expanded back on original. this.fileBeingProcessed = fileBeingDistributed; this.fileBeingDistributed = fileBeingDistributed; } } /** * set the source for macros to expand. The fileBeingDistributed will be deduced. * * @param fileBeingProcessed source file containing macros to be expanded. * @param fileBeingDistributed target file where expanded macros go. */ public void setFileBeingProcessedAndFileBeingDistributed( final File fileBeingProcessed, final File fileBeingDistributed ) { if ( separateMasters ) { if ( Tools.isFileAMaster( fileBeingProcessed ) ) { this.fileBeingProcessed = fileBeingProcessed; this.fileBeingDistributed = Tools.correspondingDistributable( fileBeingProcessed ); } else { // swap roles. this.fileBeingProcessed = Tools.correspondingMaster( fileBeingDistributed ); this.fileBeingDistributed = fileBeingProcessed; } } else { // old style, write expanded back on original. this.fileBeingProcessed = fileBeingProcessed; this.fileBeingDistributed = fileBeingProcessed; } } }