View Javadoc

1   /*
2    * Copyright (C) 1998-2001 Semiotek Inc.  All Rights Reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted under the terms of either of the following
6    * Open Source licenses:
7    *
8    * The GNU General Public License, version 2, or any later version, as
9    * published by the Free Software Foundation
10   * (http://www.fsf.org/copyleft/gpl.html);
11   *
12   *  or
13   *
14   * The Semiotek Public License (http://webmacro.org/LICENSE.)
15   *
16   * This software is provided "as is", with NO WARRANTY, not even the
17   * implied warranties of fitness to purpose, or merchantability. You
18   * assume all risks and liabilities associated with its use.
19   *
20   * See www.webmacro.org for more information on the WebMacro project.
21   */
22  
23  
24  package org.webmacro.engine;
25  
26  
27  /***
28   * MacroBuilder.java
29   *
30   * Represents the invocation of a (C-style) macro, which gets expanded
31   * during the building of a template.  Not to be confused with Macro
32   * as used by WebMacro, which is something else.
33   * @author Brian Goetz
34   */
35  
36  public class MacroBuilder implements Builder
37  {
38  
39      private String name;
40      private ListBuilder argsBuilder;
41      private int lineNo, colNo;
42      private String[] nullArgs = new String[0];
43  
44      public MacroBuilder (String name,
45                           ListBuilder argsBuilder,
46                           int lineNo, int colNo)
47      {
48          this.name = name;
49          this.argsBuilder = argsBuilder;
50          this.lineNo = lineNo;
51          this.colNo = colNo;
52      }
53  
54      /***
55       * Expand the macro.  Gets the macro definition from the build context.
56       */
57      public Object build (BuildContext bc) throws BuildException
58      {
59          MacroDefinition md = bc.getMacro(name);
60          if (md == null)
61          {
62              boolean relax = bc.getBroker().getBooleanSetting("RelaxedDirectiveBuilding");
63              if (relax)
64                  return "#" + name + " ";
65              else
66                  throw new BuildException("#" + name + ": no such Macro or Directive");
67          }
68          Object[] args = argsBuilder.buildAsArray(bc);
69          return md.expand(args, bc);
70      }
71  }
72