package com.mindprod.htmlmacros;
import java.io.IOException;

/**
 * Expands HTML boilerplate refresher macros
 * that simply include from another file e.g.
 * <!-- macro Include dogs.txt -->
 * <!-- generated by macro Include -->
 * <img src="images/animals/dog.jpg" height="410" width="511" alt="dog">
 * <!-- /generated by macro Include -->
 * It is not this classes's duty to create the generated and /generated
 * comment tags, just the material between them.
 */
public class Include implements Macro
   {

   /**
   * Generate macro expansion for Include macro
   *
   * @param parms parsed params for the macro,
   * usually alternating field name and value,
   * in this case just "filename".
   * @return expanded text
   */
   public String expand ( String[] parms )
      {
      if ( parms.length != 1 )
         {
         throw new IllegalArgumentException ( "macro syntax error: try <!-- macro Include file.txt -->" );
         }
      String fromFile = parms[0];
      try
         {
         return HunkIO.readEntireFile ( fromFile ) ;
         }
      catch ( IOException e )
         {
         return "error: unable to find file " + fromFile + " to include.";
         }
      }
   // end expand
   }
// end Include