View Javadoc

1   package net.sf.jpkgmk.prototype;
2   
3   import java.io.File;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.util.List;
7   
8   import net.sf.jpkgmk.PackageException;
9   import net.sf.jpkgmk.pkgmap.PkgMap;
10  import net.sf.jpkgmk.pkgmap.PkgMapEntry;
11  import net.sf.jpkgmk.util.StringUtil;
12  import net.sf.jpkgmk.util.VariableMap;
13  import net.sf.jpkgmk.util.VariableResolver;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  
18  /**
19   * Include command to include another prototype file.
20   * 
21   * <p>
22   * For details refer to <a href="http://docs.sun.com/app/docs/doc/817-0406/6mg76ste0?a=view">
23   * http://docs.sun.com/app/docs/doc/817-0406/6mg76ste0?a=view</a> 
24   * in section &quot;<i>Nesting prototype Files</i>&quot;.
25   * </p>
26   * 
27   * @author gommma (gommma AT users.sourceforge.net)
28   * @author Last changed by: $Author: gommma $
29   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
30   * @since 1.0
31   */
32  public class PrototypeEntryCommandInclude extends PrototypeEntryCommand
33  {
34      private Log log = LogFactory.getLog(PrototypeEntryCommandInclude.class);
35      private String includePath;
36      private Prototype prototype;
37  
38      public PrototypeEntryCommandInclude(String includePath) {
39          super(CommandType.INCLUDE);
40          this.includePath = includePath;
41      }
42  
43      @Override
44      protected String getCommandLinePart() {
45          StringBuffer sb = new StringBuffer();
46          if(!StringUtil.isNullOrEmpty(includePath)) {
47              sb.append(includePath);
48          }
49          return sb.toString();
50      }
51  
52      public List<PkgMapEntry> createPkgMapEntry(File basedir, VariableResolver variableResolver) {
53          try {
54              Prototype prototype = getPrototype(variableResolver);
55              // TODO Read specs - Merge the variableResolver of the new prototype with the given one of the current prototype???
56  
57              // Create a temporary pkgmap java object for being able to return the pkgMapEntries
58              PkgMap pkgMap = prototype.createPkgMap(basedir);
59              return pkgMap.getPkgMapEntries();
60          } catch (IOException e) {
61              throw new PackageException("Exception while building nested prototype.", e);
62          }
63      }
64  
65      public void create(File basedir, VariableResolver variableResolver) throws IOException {
66          log.debug("<Entering> create(File basedir)");
67          Prototype prototype = getPrototype(variableResolver);
68          prototype.createFiles(basedir);
69      }
70  
71      /**
72       * @param variableResolver
73       * @return The prototype object which is lazily created if it was not created before.
74       * @throws IOException
75       */
76      Prototype getPrototype(VariableResolver variableResolver) throws IOException {
77          if(this.prototype == null) {
78              File prototypeFile = getPrototypeFile(variableResolver);
79              PrototypeParser protoParser = new PrototypeParser();
80              this.prototype = protoParser.parse(prototypeFile);
81              // Inherit all variables from parent prototype to child prototype. From the specs of "param=value" (=variables):
82              // "Places the indicated parameter in the current environment. Spans to subsequent included prototype files."
83              this.prototype.setVariableMap((VariableMap)variableResolver);
84          }
85          return this.prototype;
86      }
87  
88      File getPrototypeFile(VariableResolver variableResolver) throws FileNotFoundException {
89          File prototypeFile = super.resolveFile(this.includePath, variableResolver);
90          return prototypeFile;
91      }
92  }