Coverage Report - com.ccg.macros.at.All
 
Classes in this File Line Coverage Branch Coverage Complexity
All
61% 
71% 
2.8
 
 1  
 /*----------------------------------------------------------------
 2  
  * $Id: All.java,v 1.1.1.1 2005/06/08 02:39:52 pkb Exp $
 3  
  * 
 4  
  * (c)2001 - Paul Blankenbaker */
 5  
 //----------------------------------------------------------------
 6  
 
 7  
 package com.ccg.macros.at;
 8  
 
 9  
 import com.ccg.util.*;
 10  
 import com.ccg.io.*;
 11  
 import com.ccg.macros.*;
 12  
 
 13  
 import java.io.*;
 14  
 
 15  
 
 16  
 //----------------------------------------------------------------
 17  
 /** A simple command line utility that can be used to test all {@link
 18  
  * Base Base} classes in the package.
 19  
  * 
 20  
  * <p>This simple test utility creates a {@link AtMacros AtMacros}
 21  
  * object, registers and installs ALL ({@link Core Core}, {@link Html
 22  
  * Html}, etc) of the {@link Base Base} derived macro handlers defined
 23  
  * in the package. And then processes the standard input out to the
 24  
  * standard output.</p>
 25  
  *
 26  
  * <p>The end result is a fairly powerful macro processing language (I
 27  
  * use it all the time to generate/tweak HTML code).</p>
 28  
  *
 29  
  * <p>The following command line options are recognized:</p>
 30  
  *
 31  
  * <p>You can use many of the standard command line options which the
 32  
  * {@link CommandLineUtility CommandLineUtility} class recognizes. In
 33  
  * addtion, the following command line options are recognized:
 34  
  *
 35  
  * <dl>
 36  
  *
 37  
  * <dt><b>-Dname=val</b></dt><dd>This allows one to define macros on
 38  
  * the command line. You may include zero or more definitions. For
 39  
  * example "-Dfirst=Paul -Dlast=Blankenbaker" on the command line is
 40  
  * equivalent to
 41  
  * '@define("first","Paul")@define("last","Blankenbaker")' in a source
 42  
  * file. This allows one to pass/define values outside of the source
 43  
  * files.</dd>
 44  
  *
 45  
  * <dt><b>-nodefaults [true|false]</b></dt><dd>Specify this option to
 46  
  * turn off the loading of the user's default macro definitions. By
 47  
  * default, this application first tries to load the file
 48  
  * "$HOME/.etc/com/ccg/macros/default.At" (discarding all output) to
 49  
  * allow one to load a set of custom definitions (if this file isn't
 50  
  * found, it isn't loaded). Use the "-nodefaults" option to disable
 51  
  * this feature.</dd>
 52  
  *
 53  
  * </dl>
 54  
  * 
 55  
  * <p>To get a rough idea of ALL of the available macros, try running
 56  
  * it in the following manner:
 57  
  *
 58  
  * <pre>
 59  
  * echo "@htmlDumpDefinitions()" | java com.ccg.macros.at.All > defs.html
 60  
  * </pre>
 61  
  * 
 62  
  * @version $Revision: 1.1.1.1 $
 63  
  * 
 64  
  * @since 1.0
 65  
  * 
 66  
  * @author $Author: pkb $
 67  
  * 
 68  
  * @see AtMacros
 69  
  * @see Base */
 70  
 //----------------------------------------------------------------
 71  
 
 72  
 public class All extends CommandLineUtility {
 73  
 
 74  
   //----------------------------------------------------------------
 75  
   /** Initializes object with a set of command line arguments.
 76  
    * 
 77  
    * <p>This constructor initializes the object with the set of
 78  
    * command line arguments to use when its {@link #run}. However,
 79  
    * this is about all that is done (we don't actually verify the
 80  
    * arguments at this point).</p>
 81  
    * 
 82  
    * @since        1.0 */
 83  
   //----------------------------------------------------------------
 84  
 
 85  
   public All(final String[] args) {
 86  6
     super(args);
 87  
 
 88  6
     _RawArgs = args;
 89  6
   }
 90  
 
 91  
 
 92  
   //----------------------------------------------------------------
 93  
   /** Runs the macro processor.
 94  
    * 
 95  
    * <p>This method checks the command line arguments, and then runs
 96  
    * the source file through the macro processor writing the results
 97  
    * to the standard output device.</p>
 98  
    * 
 99  
    * @since        1.0 */
 100  
   //----------------------------------------------------------------
 101  
 
 102  
   public void run() {
 103  
 
 104  2
     OutputWriter o = null;
 105  
 
 106  
     try {
 107  2
       AtMacros am = new AtMacros();
 108  2
       All.installAll(am);
 109  
 
 110  
     //----------------------------------------------------------------  
 111  
     // See if any -Dmacro=val values on command line
 112  
     //----------------------------------------------------------------
 113  
 
 114  2
       String[] args = _RawArgs;
 115  
 
 116  2
       for (int i = 0; i < args.length; i++) {
 117  0
         String a = args[i];
 118  0
         if ((a.length() > 2) && a.startsWith("-D")) {
 119  0
           int epos = a.indexOf('=');
 120  0
           if (epos < 0) {
 121  0
             am.addMacro(a.substring(2),"");
 122  0
           }
 123  
           else {
 124  0
             String mname = a.substring(2,epos);
 125  0
             String mval = a.substring(epos+1);
 126  0
             am.addMacro(mname,mval);
 127  
           }
 128  
         }
 129  
       }
 130  
 
 131  
       //----------------------------------------------------------------  
 132  
       // Load default at macros (if user has any)
 133  
       //----------------------------------------------------------------
 134  
 
 135  2
       File ifile = null;
 136  2
       if (!getBoolean("nodefaults",Boolean.FALSE).booleanValue()) try {
 137  4
         ConfigSource cs = new ConfigSource(At.class);
 138  2
         ifile = cs.getFile();
 139  
 
 140  2
         if (ifile.exists()) {
 141  2
           am.interpret(new InputReader(ifile),OutputNull.getInstance());
 142  
         }
 143  0
       } catch (Exception e) {
 144  0
         Log.error("problem processing \""+ifile+"\"",e);
 145  2
       }
 146  
 
 147  
 
 148  
       //----------------------------------------------------------------  
 149  
       // Now, interpret input file
 150  
       //----------------------------------------------------------------
 151  
 
 152  2
       o = new OutputWriter(getOutputStream());
 153  2
       am.interpret(new InputReader(getInputStream()),o);
 154  
 
 155  0
     } catch (Exception e) {
 156  0
       e.printStackTrace();
 157  
     } finally {
 158  2
       if (o != null) try { o.close(); } catch (Throwable ignore) { }
 159  0
     }
 160  
 
 161  2
   }
 162  
 
 163  
 
 164  
   //----------------------------------------------------------------
 165  
   /** Global static method to install ALL of the macro handler objects
 166  
    * available in this package.
 167  
    * 
 168  
    * <p>This method installs ALL of the {@link Base Base} derived
 169  
    * macro handlers which one can find in this class. This is an easy
 170  
    * way to make sure that ALL of the macros defined in the {@link
 171  
    * com.ccg.macros.at} package are loaded and available to your
 172  
    * interpreter.
 173  
    *
 174  
    * @param        am
 175  
    * 
 176  
    *         The {@link AtMacros AtMacros} object to register all of the
 177  
    *         macros with. Must not be null.
 178  
    * 
 179  
    * @since        1.0 */
 180  
   //----------------------------------------------------------------
 181  
 
 182  
   public static void installAll(AtMacros am) {
 183  4
     (new Core()).install(am);
 184  4
     (new Html()).install(am);
 185  4
     (new Script()).install(am);
 186  4
     (new Strings()).install(am);
 187  4
   }
 188  
 
 189  
 
 190  
 
 191  
   //----------------------------------------------------------------
 192  
   /** Create a new {@link AtMacros AtMacros} object and {@link
 193  
    * #installAll install all} standard macros.
 194  
    * 
 195  
    * <p>This is a convenience method. It constructs and initializes a
 196  
    * {@link AtMacros AtMacros} object for you in one shot.
 197  
    * 
 198  
    * @return
 199  
    * 
 200  
    *         A newly created {@link AtMacros AtMacros} object with ALL of
 201  
    *         the standard macros installed.
 202  
    * 
 203  
    * @since        1.0
 204  
    * 
 205  
    * @see AtMacros */
 206  
   //----------------------------------------------------------------
 207  
 
 208  
   public static AtMacros createAtMacros() {
 209  2
     AtMacros macros = new AtMacros();
 210  2
     installAll(macros);
 211  2
     return macros;
 212  
   }
 213  
 
 214  
 
 215  
   //----------------------------------------------------------------
 216  
   /** Main entry point into the application.
 217  
    * 
 218  
    * @param        args
 219  
    * 
 220  
    *         Array of command line arguments. */
 221  
   //----------------------------------------------------------------
 222  
 
 223  
   static public void main(String args[]) {
 224  0
     All all = new All(args);
 225  0
     all.run();
 226  0
   }
 227  
 
 228  
 
 229  
   //----------------------------------------------------------------  
 230  
   // private data
 231  
   //----------------------------------------------------------------
 232  
 
 233  
   private String[] _RawArgs;
 234  
 
 235  
 }