Program: 74126
Project: 74126
Buggy source:
/** 
 * Read in a template makefile, substitute variables and write the resulting makefile. <p>If a <code>.mk.in</code> file with the name of the sanitized model name, then that file is used as a template.  For example, if the model name is <code>Foo</code> and the file <code>Foo.mk.in</code> exists, then the file <code>Foo.mk.in</code> is used as a makefile template. <p>If no <code>.mk.in</code> file is found, then the makefile template can be found by looking up a resource name makefile.in in the package named by the <i>generatorPackage</i> parameter.  Thus, if the <i>generatorPackage</i> has the value "ptolemy.codegen.c", then we look for the resouce "ptolemy.codegen.c.makefile.in", which is usually found as <code>$PTII/ptolemy/codegen/c/makefile.in</code>. <p>The makefile is written to a directory named by the <i>codeDirectory</i> parameter, with a file name that is a sanitized version of the model name, and a ".mk" extension. Thus, for a model named "Foo", we might generate a makefile in "$HOME/codegen/Foo.mk". <p>Under Java under Windows, your <code>$HOME</code> variable is set to the value of the <code>user.home</code>System property, which is usually something like <code>C:\Documents and Settings\<i>yourlogin</i></code>, thus for user <code>mrptolemy</code> the makefile would be <code>C:\Documents and Settings\mrptolemy\codegen\Foo.mk</code>. <p>The following variables are substituted <dl> <dt><code>@modelName@</code> <dd>The sanitized model name, created by invoking  {@link ptolemy.util.StringUtilities#sanitizeName(String)}on the model name. <dt><code>@PTCGIncludes@</code> <dd>The elements of the set of include command arguments that were added by calling   {@link #addInclude(String)}, where each element is separated by a space. <dt><code>@PTCGLibraries@</code> <dd>The elements of the set of library command arguments that were added by calling   {@link #addLibrary(String)}, where each element is separated by a space. </dl>
 * @exception IllegalActionException  If there is a problem readinga parameter, if there is a problem creating the codeDirectory directory or if there is a problem writing the code to a file.
 */
protected void _writeMakefile() throws IllegalActionException {
  if (!((BooleanToken)overwriteFiles.getToken()).booleanValue() && codeDirectory.asFile().exists()) {
    if (!MessageHandler.yesNoQuestion(codeDirectory.asFile() + "String_Node_Str")) {
      return;
    }
  }
  File codeDirectoryFile=codeDirectory.asFile();
  if (codeDirectoryFile.isFile()) {
    throw new IllegalActionException(this,"String_Node_Str" + codeDirectory.stringValue() + "String_Node_Str"+ "String_Node_Str");
  }
  if (!codeDirectoryFile.isDirectory() && !codeDirectoryFile.mkdirs()) {
    throw new IllegalActionException(this,"String_Node_Str" + codeDirectory.stringValue() + "String_Node_Str");
  }
  Map substituteMap;
  try {
    substituteMap=CodeGeneratorUtilities.newMap(this);
    substituteMap.put("String_Node_Str",_sanitizedModelName);
    substituteMap.put("String_Node_Str",_concatenateElements(_includes));
    substituteMap.put("String_Node_Str",_concatenateElements(_libraries));
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    if (((BooleanToken)generateCpp.getToken()).booleanValue()) {
      substituteMap.put("String_Node_Str","String_Node_Str");
    }
 else {
      substituteMap.put("String_Node_Str","String_Node_Str");
    }
    String osName=StringUtilities.getProperty("String_Node_Str");
    if (osName != null) {
      if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else {
        substituteMap.put("String_Node_Str","String_Node_Str" + osName + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
      }
    }
  }
 catch (  IllegalActionException ex) {
    throw new InternalErrorException(this,ex,"String_Node_Str" + _model);
  }
  List templateList=new LinkedList();
  URIAttribute uriAttribute=(URIAttribute)_model.getAttribute("String_Node_Str",URIAttribute.class);
  if (uriAttribute != null) {
    String uriString=uriAttribute.getURI().toString();
    templateList.add(uriString.substring(0,uriString.lastIndexOf("String_Node_Str") + 1) + _sanitizedModelName + "String_Node_Str");
  }
  String generatorDirectory=generatorPackageList.stringValue().replace('.','/');
  templateList.add("String_Node_Str" + generatorDirectory + "String_Node_Str");
  templateList.add("String_Node_Str" + generatorDirectory + (_isTopLevel() ? "String_Node_Str" : "String_Node_Str"));
  String makefileOutputName=codeDirectory.stringValue() + ((!codeDirectory.stringValue().endsWith("String_Node_Str") && !codeDirectory.stringValue().endsWith("String_Node_Str")) ? "String_Node_Str" : "String_Node_Str") + _sanitizedModelName+ "String_Node_Str";
  BufferedReader makefileTemplateReader=null;
  StringBuffer errorMessage=new StringBuffer();
  String makefileTemplateName=null;
  boolean success=false;
  try {
    Iterator templates=templateList.iterator();
    while (templates.hasNext()) {
      makefileTemplateName=(String)templates.next();
      try {
        makefileTemplateReader=CodeGeneratorUtilities.openAsFileOrURL(makefileTemplateName);
      }
 catch (      IOException ex) {
        errorMessage.append("String_Node_Str" + makefileTemplateName + "String_Node_Str");
      }
      if (makefileTemplateReader != null) {
        _executeCommands.stdout("String_Node_Str" + makefileTemplateName + "String_Node_Str"+ _eol+ "String_Node_Str"+ makefileOutputName+ "String_Node_Str");
        CodeGeneratorUtilities.substitute(makefileTemplateReader,substituteMap,makefileOutputName);
        success=true;
        break;
      }
    }
  }
 catch (  Exception ex) {
    throw new IllegalActionException(this,ex,"String_Node_Str" + makefileTemplateName + "String_Node_Str"+ makefileOutputName+ "String_Node_Str");
  }
 finally {
    if (makefileTemplateReader != null) {
      try {
        makefileTemplateReader.close();
      }
 catch (      IOException ex) {
        throw new IllegalActionException(this,ex,"String_Node_Str" + makefileTemplateName + "String_Node_Str");
      }
    }
  }
  if (!success) {
    throw new IllegalActionException(this,errorMessage.toString());
  }
}

Fixed version (ground truth):
/** 
 * Read in a template makefile, substitute variables and write the resulting makefile. <p>If a <code>.mk.in</code> file with the name of the sanitized model name, then that file is used as a template.  For example, if the model name is <code>Foo</code> and the file <code>Foo.mk.in</code> exists, then the file <code>Foo.mk.in</code> is used as a makefile template. <p>If no <code>.mk.in</code> file is found, then the makefile template can be found by looking up a resource name makefile.in in the package named by the <i>generatorPackage</i> parameter.  Thus, if the <i>generatorPackage</i> has the value "ptolemy.codegen.c", then we look for the resouce "ptolemy.codegen.c.makefile.in", which is usually found as <code>$PTII/ptolemy/codegen/c/makefile.in</code>. <p>The makefile is written to a directory named by the <i>codeDirectory</i> parameter, with a file name that is a sanitized version of the model name, and a ".mk" extension. Thus, for a model named "Foo", we might generate a makefile in "$HOME/codegen/Foo.mk". <p>Under Java under Windows, your <code>$HOME</code> variable is set to the value of the <code>user.home</code>System property, which is usually something like <code>C:\Documents and Settings\<i>yourlogin</i></code>, thus for user <code>mrptolemy</code> the makefile would be <code>C:\Documents and Settings\mrptolemy\codegen\Foo.mk</code>. <p>The following variables are substituted <dl> <dt><code>@modelName@</code> <dd>The sanitized model name, created by invoking  {@link ptolemy.util.StringUtilities#sanitizeName(String)}on the model name. <dt><code>@PTCGIncludes@</code> <dd>The elements of the set of include command arguments that were added by calling   {@link #addInclude(String)}, where each element is separated by a space. <dt><code>@PTCGLibraries@</code> <dd>The elements of the set of library command arguments that were added by calling   {@link #addLibrary(String)}, where each element is separated by a space. </dl>
 * @exception IllegalActionException  If there is a problem readinga parameter, if there is a problem creating the codeDirectory directory or if there is a problem writing the code to a file.
 */
protected void _writeMakefile() throws IllegalActionException {
  if (!((BooleanToken)overwriteFiles.getToken()).booleanValue() && codeDirectory.asFile().exists()) {
    if (!MessageHandler.yesNoQuestion(codeDirectory.asFile() + "String_Node_Str")) {
      return;
    }
  }
  File codeDirectoryFile=codeDirectory.asFile();
  if (codeDirectoryFile.isFile()) {
    throw new IllegalActionException(this,"String_Node_Str" + codeDirectory.stringValue() + "String_Node_Str"+ "String_Node_Str");
  }
  if (!codeDirectoryFile.isDirectory() && !codeDirectoryFile.mkdirs()) {
    throw new IllegalActionException(this,"String_Node_Str" + codeDirectory.stringValue() + "String_Node_Str");
  }
  Map substituteMap;
  try {
    substituteMap=CodeGeneratorUtilities.newMap(this);
    substituteMap.put("String_Node_Str",_sanitizedModelName);
    substituteMap.put("String_Node_Str",_concatenateElements(_includes));
    substituteMap.put("String_Node_Str",_concatenateElements(_libraries));
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    substituteMap.put("String_Node_Str","String_Node_Str");
    if (((BooleanToken)generateCpp.getToken()).booleanValue()) {
      substituteMap.put("String_Node_Str","String_Node_Str");
    }
 else {
      substituteMap.put("String_Node_Str","String_Node_Str");
    }
    String osName=StringUtilities.getProperty("String_Node_Str");
    if (osName != null) {
      if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else       if (osName.startsWith("String_Node_Str")) {
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
        substituteMap.put("String_Node_Str","String_Node_Str");
      }
 else {
        substituteMap.put("String_Node_Str","String_Node_Str" + osName + "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str"+ "String_Node_Str");
      }
    }
  }
 catch (  IllegalActionException ex) {
    throw new InternalErrorException(this,ex,"String_Node_Str" + _model);
  }
  List<String> templateList=new LinkedList<String>();
  URIAttribute uriAttribute=(URIAttribute)_model.getAttribute("String_Node_Str",URIAttribute.class);
  if (uriAttribute != null) {
    String uriString=uriAttribute.getURI().toString();
    templateList.add(uriString.substring(0,uriString.lastIndexOf("String_Node_Str") + 1) + _sanitizedModelName + "String_Node_Str");
  }
  String generatorDirectory=generatorPackageList.stringValue().replace('.','/');
  if (getContainer().getContainer() != null) {
    templateList.add("String_Node_Str" + generatorDirectory + (_isTopLevel() ? "String_Node_Str" : "String_Node_Str"));
  }
  templateList.add("String_Node_Str" + generatorDirectory + "String_Node_Str");
  templateList.add("String_Node_Str");
  String makefileOutputName=codeDirectory.stringValue() + ((!codeDirectory.stringValue().endsWith("String_Node_Str") && !codeDirectory.stringValue().endsWith("String_Node_Str")) ? "String_Node_Str" : "String_Node_Str") + _sanitizedModelName+ "String_Node_Str";
  BufferedReader makefileTemplateReader=null;
  StringBuffer errorMessage=new StringBuffer();
  String makefileTemplateName=null;
  boolean success=false;
  try {
    Iterator templates=templateList.iterator();
    while (templates.hasNext()) {
      makefileTemplateName=(String)templates.next();
      try {
        makefileTemplateReader=CodeGeneratorUtilities.openAsFileOrURL(makefileTemplateName);
      }
 catch (      IOException ex) {
        errorMessage.append("String_Node_Str" + makefileTemplateName + "String_Node_Str");
      }
      if (makefileTemplateReader != null) {
        _executeCommands.stdout("String_Node_Str" + makefileTemplateName + "String_Node_Str"+ _eol+ "String_Node_Str"+ makefileOutputName+ "String_Node_Str");
        CodeGeneratorUtilities.substitute(makefileTemplateReader,substituteMap,makefileOutputName);
        success=true;
        break;
      }
    }
  }
 catch (  Exception ex) {
    throw new IllegalActionException(this,ex,"String_Node_Str" + makefileTemplateName + "String_Node_Str"+ makefileOutputName+ "String_Node_Str");
  }
 finally {
    if (makefileTemplateReader != null) {
      try {
        makefileTemplateReader.close();
      }
 catch (      IOException ex) {
        throw new IllegalActionException(this,ex,"String_Node_Str" + makefileTemplateName + "String_Node_Str");
      }
    }
  }
  if (!success) {
    throw new IllegalActionException(this,errorMessage.toString());
  }
}