Program: 96666
Project: 96666
Buggy source:
/** 
 * Parse through template to find xml defining artifact sets and replace it with the result of publishing those artifacts. Only used by Publish SRS
 * @param artifacts = null if the template defines the artifacts to be used in the publishing
 * @param folder = null when not using an extension template
 * @param outlineNumber if null will find based on first artifact
 */
public InputStream applyTemplate(List<Artifact> artifacts,String templateContent,String templateOptions,String templateStyles,IContainer folder,String outlineNumber,String outlineType,PresentationType presentationType) throws OseeCoreException {
  excludeFolders=renderer.getBooleanOption("String_Node_Str");
  ArtifactId view=(ArtifactId)renderer.getOption(IRenderer.VIEW_ID);
  artifactsToExclude=getNonApplicableArtifacts(artifacts,view == null ? ArtifactId.SENTINEL : view);
  if (!artifacts.isEmpty()) {
    ApplicabilityEndpoint applEndpoint=ServiceUtil.getOseeClient().getApplicabilityEndpoint(artifacts.get(0).getBranch());
    applicabilityTokens=new HashMap<>();
    Collection<ApplicabilityToken> appTokens=applEndpoint.getApplicabilityTokenMap();
    for (    ApplicabilityToken token : appTokens) {
      applicabilityTokens.put(token,token);
    }
  }
  WordMLProducer wordMl=null;
  CharBackedInputStream charBak=null;
  try {
    charBak=new CharBackedInputStream();
    wordMl=new WordMLProducer(charBak);
    templateContent=templateContent.replaceAll(PGNUMTYPE_START_1,"String_Node_Str");
    if (!templateStyles.isEmpty()) {
      templateContent=templateContent.replaceAll(STYLES,templateStyles);
    }
    this.outlineNumber=outlineNumber == null ? peekAtFirstArtifactToGetParagraphNumber(templateContent,null,artifacts) : outlineNumber;
    templateContent=wordMl.setHeadingNumbers(this.outlineNumber,templateContent,outlineType);
    Matcher matcher=headElementsPattern.matcher(templateContent);
    int lastEndIndex=0;
    while (matcher.find()) {
      wordMl.addWordMl(templateContent.substring(lastEndIndex,matcher.start()));
      lastEndIndex=matcher.end();
      JSONObject jsonObject=new JSONObject(templateOptions);
      elementType=jsonObject.getString("String_Node_Str");
      if (elementType.equals(ARTIFACT)) {
        parseOutliningOptions(templateOptions);
        if (artifacts == null) {
          artifacts=renderer.getArtifactsOption(artifactName);
        }
        if (presentationType == PresentationType.SPECIALIZED_EDIT && artifacts.size() == 1) {
          outlining=false;
        }
        processArtifactSet(templateOptions,artifacts,wordMl,outlineType,presentationType,(ArtifactId)renderer.getOption(IRenderer.VIEW_ID));
      }
 else       if (elementType.equals(NESTED_TEMPLATE)) {
        parseNestedTemplateOptions(templateOptions,folder,wordMl,presentationType);
      }
 else {
        throw new OseeArgumentException("String_Node_Str","String_Node_Str");
      }
    }
    String endOfTemplate=templateContent.substring(lastEndIndex);
    wordMl.addWordMl(updateFooter(endOfTemplate));
    displayNonTemplateArtifacts(nonTemplateArtifacts,"String_Node_Str");
  }
 catch (  CharacterCodingException ex) {
    OseeCoreException.wrapAndThrow(ex);
  }
catch (  JSONException ex) {
    OseeCoreException.wrapAndThrow(ex);
  }
  return charBak;
}

Fixed version (ground truth):
/** 
 * Parse through template to find xml defining artifact sets and replace it with the result of publishing those artifacts. Only used by Publish SRS
 * @param artifacts = null if the template defines the artifacts to be used in the publishing
 * @param folder = null when not using an extension template
 * @param outlineNumber if null will find based on first artifact
 */
public InputStream applyTemplate(List<Artifact> artifacts,String templateContent,String templateOptions,String templateStyles,IContainer folder,String outlineNumber,String outlineType,PresentationType presentationType) throws OseeCoreException {
  excludeFolders=renderer.getBooleanOption("String_Node_Str");
  ArtifactId view=(ArtifactId)renderer.getOption(IRenderer.VIEW_ID);
  artifactsToExclude=getNonApplicableArtifacts(artifacts,view == null ? ArtifactId.SENTINEL : view);
  if (!artifacts.isEmpty()) {
    BranchId branchId=artifacts.get(0).getBranch();
    Branch branch=BranchManager.getBranch(branchId);
    if (branch.getBranchType().equals(BranchType.MERGE)) {
      branch=branch.getParentBranch();
    }
    ApplicabilityEndpoint applEndpoint=ServiceUtil.getOseeClient().getApplicabilityEndpoint(branch.getParentBranch());
    applicabilityTokens=new HashMap<>();
    Collection<ApplicabilityToken> appTokens=applEndpoint.getApplicabilityTokenMap();
    for (    ApplicabilityToken token : appTokens) {
      applicabilityTokens.put(token,token);
    }
  }
  WordMLProducer wordMl=null;
  CharBackedInputStream charBak=null;
  try {
    charBak=new CharBackedInputStream();
    wordMl=new WordMLProducer(charBak);
    templateContent=templateContent.replaceAll(PGNUMTYPE_START_1,"String_Node_Str");
    if (!templateStyles.isEmpty()) {
      templateContent=templateContent.replaceAll(STYLES,templateStyles);
    }
    this.outlineNumber=outlineNumber == null ? peekAtFirstArtifactToGetParagraphNumber(templateContent,null,artifacts) : outlineNumber;
    templateContent=wordMl.setHeadingNumbers(this.outlineNumber,templateContent,outlineType);
    Matcher matcher=headElementsPattern.matcher(templateContent);
    int lastEndIndex=0;
    while (matcher.find()) {
      wordMl.addWordMl(templateContent.substring(lastEndIndex,matcher.start()));
      lastEndIndex=matcher.end();
      JSONObject jsonObject=new JSONObject(templateOptions);
      elementType=jsonObject.getString("String_Node_Str");
      if (elementType.equals(ARTIFACT)) {
        parseOutliningOptions(templateOptions);
        if (artifacts == null) {
          artifacts=renderer.getArtifactsOption(artifactName);
        }
        if (presentationType == PresentationType.SPECIALIZED_EDIT && artifacts.size() == 1) {
          outlining=false;
        }
        processArtifactSet(templateOptions,artifacts,wordMl,outlineType,presentationType,(ArtifactId)renderer.getOption(IRenderer.VIEW_ID));
      }
 else       if (elementType.equals(NESTED_TEMPLATE)) {
        parseNestedTemplateOptions(templateOptions,folder,wordMl,presentationType);
      }
 else {
        throw new OseeArgumentException("String_Node_Str","String_Node_Str");
      }
    }
    String endOfTemplate=templateContent.substring(lastEndIndex);
    wordMl.addWordMl(updateFooter(endOfTemplate));
    displayNonTemplateArtifacts(nonTemplateArtifacts,"String_Node_Str");
  }
 catch (  CharacterCodingException ex) {
    OseeCoreException.wrapAndThrow(ex);
  }
catch (  JSONException ex) {
    OseeCoreException.wrapAndThrow(ex);
  }
  return charBak;
}