Program: 24065
Project: 24065
Buggy source:
/** 
 * @param trimDifferential
 * @throws DefinitionException , FHIRException 
 * @throws Exception
 */
private void processPaths(StructureDefinitionSnapshotComponent result,StructureDefinitionSnapshotComponent base,StructureDefinitionDifferentialComponent differential,int baseCursor,int diffCursor,int baseLimit,int diffLimit,String url,String profileName,String contextPath,boolean trimDifferential,String contextName,String resultPathBase,boolean slicingDone) throws DefinitionException, FHIRException {
  while (baseCursor <= baseLimit) {
    ElementDefinition currentBase=base.getElement().get(baseCursor);
    String cpath=fixedPath(contextPath,currentBase.getPath());
    List<ElementDefinition> diffMatches=getDiffMatches(differential,cpath,diffCursor,diffLimit,profileName);
    if (!currentBase.hasSlicing()) {
      if (diffMatches.isEmpty()) {
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        markDerived(outcome);
        if (resultPathBase == null)         resultPathBase=outcome.getPath();
 else         if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        baseCursor++;
      }
 else       if (diffMatches.size() == 1 && (!diffMatches.get(0).hasSlicing() || slicingDone)) {
        ElementDefinition template=null;
        if (diffMatches.get(0).hasType() && diffMatches.get(0).getType().size() == 1 && diffMatches.get(0).getType().get(0).hasProfile() && !diffMatches.get(0).getType().get(0).getCode().equals("String_Node_Str")) {
          String p=diffMatches.get(0).getType().get(0).getProfile().get(0).asStringValue();
          StructureDefinition sd=context.fetchResource(StructureDefinition.class,p);
          if (sd != null) {
            if (!sd.hasSnapshot()) {
              StructureDefinition sdb=context.fetchResource(StructureDefinition.class,sd.getBase());
              if (sdb == null)               throw new DefinitionException("String_Node_Str" + sd.getBase());
              generateSnapshot(sdb,sd,sd.getUrl(),sd.getName());
            }
            template=sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath());
            if (!diffMatches.get(0).getType().get(0).getCode().equals("String_Node_Str")) {
              template.setMin(currentBase.getMin());
              template.setMax(currentBase.getMax());
            }
          }
        }
        if (template == null)         template=currentBase.copy();
        ElementDefinition outcome=updateURLs(url,template);
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (diffMatches.get(0).hasName())         outcome.setName(diffMatches.get(0).getName());
        outcome.setSlicing(null);
        updateFromDefinition(outcome,diffMatches.get(0),profileName,trimDifferential,url);
        if (outcome.getPath().endsWith("String_Node_Str") && outcome.getType().size() == 1 && !outcome.getType().get(0).getCode().equals("String_Node_Str"))         outcome.setPath(outcome.getPath().substring(0,outcome.getPath().length() - 3) + Utilities.capitalize(outcome.getType().get(0).getCode()));
        if (resultPathBase == null)         resultPathBase=outcome.getPath();
 else         if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        baseCursor++;
        diffCursor=differential.getElement().indexOf(diffMatches.get(0)) + 1;
        if (differential.getElement().size() > diffCursor && outcome.getPath().contains("String_Node_Str") && isDataType(outcome.getType())) {
          if (pathStartsWith(differential.getElement().get(diffCursor).getPath(),diffMatches.get(0).getPath() + "String_Node_Str")) {
            if (outcome.getType().size() > 1)             throw new DefinitionException(diffMatches.get(0).getPath() + "String_Node_Str" + differential.getElement().get(diffCursor).getPath()+ "String_Node_Str"+ typeCode(outcome.getType())+ "String_Node_Str"+ profileName);
            StructureDefinition dt=getProfileForDataType(outcome.getType().get(0));
            if (dt == null)             throw new DefinitionException(diffMatches.get(0).getPath() + "String_Node_Str" + differential.getElement().get(diffCursor).getPath()+ "String_Node_Str"+ typeCode(outcome.getType())+ "String_Node_Str"+ profileName+ "String_Node_Str");
            contextName=dt.getUrl();
            int start=diffCursor;
            while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(),diffMatches.get(0).getPath() + "String_Node_Str"))             diffCursor++;
            processPaths(result,dt.getSnapshot(),differential,1,start - 1,dt.getSnapshot().getElement().size() - 1,diffCursor - 1,url,profileName + pathTail(diffMatches,0),diffMatches.get(0).getPath(),trimDifferential,contextName,resultPathBase,false);
          }
        }
      }
 else {
        if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))         throw new DefinitionException("String_Node_Str" + currentBase.getPath() + "String_Node_Str"+ currentBase.getName()+ "String_Node_Str"+ contextName);
        if (!diffMatches.get(0).hasSlicing() && !isExtension(currentBase))         throw new DefinitionException("String_Node_Str" + currentBase.getPath());
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (!diffMatches.get(0).hasSlicing())         outcome.setSlicing(makeExtensionSlicing());
 else         outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
        if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        int start=0;
        if (!diffMatches.get(0).hasName()) {
          updateFromDefinition(outcome,diffMatches.get(0),profileName,trimDifferential,url);
          if (!outcome.hasType()) {
            throw new DefinitionException("String_Node_Str");
          }
          start=1;
        }
 else         checkExtensionDoco(outcome);
        int nbl=findEndOfElement(base,baseCursor);
        int ndc=diffCursor;
        int ndl=diffCursor;
        for (int i=start; i < diffMatches.size(); i++) {
          ndc=differential.getElement().indexOf(diffMatches.get(i));
          ndl=findEndOfElement(differential,ndc);
          processPaths(result,base,differential,baseCursor,ndc,nbl,ndl,url,profileName + pathTail(diffMatches,i),contextPath,trimDifferential,contextName,resultPathBase,true);
        }
        baseCursor=nbl + 1;
        diffCursor=ndl + 1;
      }
    }
 else {
      String path=currentBase.getPath();
      ElementDefinition original=currentBase;
      if (diffMatches.isEmpty()) {
        while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
          ElementDefinition outcome=updateURLs(url,base.getElement().get(baseCursor).copy());
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          result.getElement().add(outcome);
          baseCursor++;
        }
      }
 else {
        boolean closed=currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
        int diffpos=0;
        boolean isExtension=cpath.endsWith("String_Node_Str") || cpath.endsWith("String_Node_Str");
        if (diffMatches.get(0).hasSlicing()) {
          if (!isExtension)           diffpos++;
          ElementDefinitionSlicingComponent dSlice=diffMatches.get(0).getSlicing();
          ElementDefinitionSlicingComponent bSlice=currentBase.getSlicing();
          if (!orderMatches(dSlice.getOrderedElement(),bSlice.getOrderedElement()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
          if (!discriiminatorMatches(dSlice.getDiscriminator(),bSlice.getDiscriminator()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
          if (!ruleMatches(dSlice.getRules(),bSlice.getRules()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
        }
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (diffMatches.get(0).hasSlicing() && !isExtension) {
          updateFromSlicing(outcome.getSlicing(),diffMatches.get(0).getSlicing());
          updateFromDefinition(outcome,diffMatches.get(0),profileName,closed,url);
        }
        if (diffMatches.get(0).hasSlicing() && !diffMatches.get(0).hasName())         diffpos++;
        result.getElement().add(outcome);
        List<ElementDefinition> baseMatches=getSiblings(base.getElement(),currentBase);
        for (        ElementDefinition baseItem : baseMatches) {
          baseCursor=base.getElement().indexOf(baseItem);
          outcome=updateURLs(url,baseItem.copy());
          updateFromBase(outcome,currentBase);
          outcome.setPath(fixedPath(contextPath,outcome.getPath()));
          outcome.setSlicing(null);
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getName().equals(outcome.getName())) {
            int nbl=findEndOfElement(base,baseCursor);
            int ndc=differential.getElement().indexOf(diffMatches.get(diffpos));
            int ndl=findEndOfElement(differential,ndc);
            processPaths(result,base,differential,baseCursor,ndc,nbl,ndl,url,profileName + pathTail(diffMatches,diffpos),contextPath,closed,contextName,resultPathBase,true);
            baseCursor=nbl + 1;
            diffCursor=ndl + 1;
            diffpos++;
          }
 else {
            result.getElement().add(outcome);
            baseCursor++;
            while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
              outcome=updateURLs(url,currentBase.copy());
              outcome.setPath(fixedPath(contextPath,outcome.getPath()));
              if (!outcome.getPath().startsWith(resultPathBase))               throw new DefinitionException("String_Node_Str");
              result.getElement().add(outcome);
              baseCursor++;
            }
          }
        }
        if (closed && diffpos < diffMatches.size())         throw new DefinitionException("String_Node_Str" + profileName + "String_Node_Str"+ path+ "String_Node_Str"+ cpath+ "String_Node_Str");
        while (diffpos < diffMatches.size()) {
          ElementDefinition diffItem=diffMatches.get(diffpos);
          for (          ElementDefinition baseItem : baseMatches)           if (baseItem.getName().equals(diffItem.getName()))           throw new DefinitionException("String_Node_Str");
          outcome=updateURLs(url,original.copy());
          outcome.setPath(fixedPath(contextPath,outcome.getPath()));
          updateFromBase(outcome,currentBase);
          outcome.setSlicing(null);
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          result.getElement().add(outcome);
          updateFromDefinition(outcome,diffItem,profileName,trimDifferential,url);
          diffpos++;
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * @param trimDifferential
 * @throws DefinitionException , FHIRException 
 * @throws Exception
 */
private void processPaths(StructureDefinitionSnapshotComponent result,StructureDefinitionSnapshotComponent base,StructureDefinitionDifferentialComponent differential,int baseCursor,int diffCursor,int baseLimit,int diffLimit,String url,String profileName,String contextPath,boolean trimDifferential,String contextName,String resultPathBase,boolean slicingDone) throws DefinitionException, FHIRException {
  while (baseCursor <= baseLimit) {
    ElementDefinition currentBase=base.getElement().get(baseCursor);
    String cpath=fixedPath(contextPath,currentBase.getPath());
    List<ElementDefinition> diffMatches=getDiffMatches(differential,cpath,diffCursor,diffLimit,profileName);
    if (!currentBase.hasSlicing()) {
      if (diffMatches.isEmpty()) {
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        markDerived(outcome);
        if (resultPathBase == null)         resultPathBase=outcome.getPath();
 else         if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        baseCursor++;
      }
 else       if (diffMatches.size() == 1 && (!diffMatches.get(0).hasSlicing() || slicingDone)) {
        ElementDefinition template=null;
        if (diffMatches.get(0).hasType() && diffMatches.get(0).getType().size() == 1 && diffMatches.get(0).getType().get(0).hasProfile() && !diffMatches.get(0).getType().get(0).getCode().equals("String_Node_Str")) {
          String p=diffMatches.get(0).getType().get(0).getProfile().get(0).asStringValue();
          StructureDefinition sd=context.fetchResource(StructureDefinition.class,p);
          if (sd != null) {
            if (!sd.hasSnapshot()) {
              StructureDefinition sdb=context.fetchResource(StructureDefinition.class,sd.getBase());
              if (sdb == null)               throw new DefinitionException("String_Node_Str" + sd.getBase());
              generateSnapshot(sdb,sd,sd.getUrl(),sd.getName());
            }
            template=sd.getSnapshot().getElement().get(0).copy().setPath(currentBase.getPath());
            if (!diffMatches.get(0).getType().get(0).getCode().equals("String_Node_Str")) {
              template.setMin(currentBase.getMin());
              template.setMax(currentBase.getMax());
            }
          }
        }
        if (template == null)         template=currentBase.copy();
 else         template=overWriteWithCurrent(template,currentBase);
        ElementDefinition outcome=updateURLs(url,template);
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (diffMatches.get(0).hasName())         outcome.setName(diffMatches.get(0).getName());
        outcome.setSlicing(null);
        updateFromDefinition(outcome,diffMatches.get(0),profileName,trimDifferential,url);
        if (outcome.getPath().endsWith("String_Node_Str") && outcome.getType().size() == 1 && !outcome.getType().get(0).getCode().equals("String_Node_Str"))         outcome.setPath(outcome.getPath().substring(0,outcome.getPath().length() - 3) + Utilities.capitalize(outcome.getType().get(0).getCode()));
        if (resultPathBase == null)         resultPathBase=outcome.getPath();
 else         if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        baseCursor++;
        diffCursor=differential.getElement().indexOf(diffMatches.get(0)) + 1;
        if (differential.getElement().size() > diffCursor && outcome.getPath().contains("String_Node_Str") && isDataType(outcome.getType())) {
          if (pathStartsWith(differential.getElement().get(diffCursor).getPath(),diffMatches.get(0).getPath() + "String_Node_Str")) {
            if (outcome.getType().size() > 1)             throw new DefinitionException(diffMatches.get(0).getPath() + "String_Node_Str" + differential.getElement().get(diffCursor).getPath()+ "String_Node_Str"+ typeCode(outcome.getType())+ "String_Node_Str"+ profileName);
            StructureDefinition dt=getProfileForDataType(outcome.getType().get(0));
            if (dt == null)             throw new DefinitionException(diffMatches.get(0).getPath() + "String_Node_Str" + differential.getElement().get(diffCursor).getPath()+ "String_Node_Str"+ typeCode(outcome.getType())+ "String_Node_Str"+ profileName+ "String_Node_Str");
            contextName=dt.getUrl();
            int start=diffCursor;
            while (differential.getElement().size() > diffCursor && pathStartsWith(differential.getElement().get(diffCursor).getPath(),diffMatches.get(0).getPath() + "String_Node_Str"))             diffCursor++;
            processPaths(result,dt.getSnapshot(),differential,1,start - 1,dt.getSnapshot().getElement().size() - 1,diffCursor - 1,url,profileName + pathTail(diffMatches,0),diffMatches.get(0).getPath(),trimDifferential,contextName,resultPathBase,false);
          }
        }
      }
 else {
        if (!unbounded(currentBase) && !isSlicedToOneOnly(diffMatches.get(0)))         throw new DefinitionException("String_Node_Str" + currentBase.getPath() + "String_Node_Str"+ currentBase.getName()+ "String_Node_Str"+ contextName);
        if (!diffMatches.get(0).hasSlicing() && !isExtension(currentBase))         throw new DefinitionException("String_Node_Str" + currentBase.getPath());
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (!diffMatches.get(0).hasSlicing())         outcome.setSlicing(makeExtensionSlicing());
 else         outcome.setSlicing(diffMatches.get(0).getSlicing().copy());
        if (!outcome.getPath().startsWith(resultPathBase))         throw new DefinitionException("String_Node_Str");
        result.getElement().add(outcome);
        int start=0;
        if (!diffMatches.get(0).hasName()) {
          updateFromDefinition(outcome,diffMatches.get(0),profileName,trimDifferential,url);
          if (!outcome.hasType()) {
            throw new DefinitionException("String_Node_Str");
          }
          start=1;
        }
 else         checkExtensionDoco(outcome);
        int nbl=findEndOfElement(base,baseCursor);
        int ndc=diffCursor;
        int ndl=diffCursor;
        for (int i=start; i < diffMatches.size(); i++) {
          ndc=differential.getElement().indexOf(diffMatches.get(i));
          ndl=findEndOfElement(differential,ndc);
          processPaths(result,base,differential,baseCursor,ndc,nbl,ndl,url,profileName + pathTail(diffMatches,i),contextPath,trimDifferential,contextName,resultPathBase,true);
        }
        baseCursor=nbl + 1;
        diffCursor=ndl + 1;
      }
    }
 else {
      String path=currentBase.getPath();
      ElementDefinition original=currentBase;
      if (diffMatches.isEmpty()) {
        while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path)) {
          ElementDefinition outcome=updateURLs(url,base.getElement().get(baseCursor).copy());
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          result.getElement().add(outcome);
          baseCursor++;
        }
      }
 else {
        boolean closed=currentBase.getSlicing().getRules() == SlicingRules.CLOSED;
        int diffpos=0;
        boolean isExtension=cpath.endsWith("String_Node_Str") || cpath.endsWith("String_Node_Str");
        if (diffMatches.get(0).hasSlicing()) {
          if (!isExtension)           diffpos++;
          ElementDefinitionSlicingComponent dSlice=diffMatches.get(0).getSlicing();
          ElementDefinitionSlicingComponent bSlice=currentBase.getSlicing();
          if (!orderMatches(dSlice.getOrderedElement(),bSlice.getOrderedElement()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
          if (!discriiminatorMatches(dSlice.getDiscriminator(),bSlice.getDiscriminator()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
          if (!ruleMatches(dSlice.getRules(),bSlice.getRules()))           throw new DefinitionException("String_Node_Str" + summariseSlicing(dSlice) + "String_Node_Str"+ summariseSlicing(bSlice)+ "String_Node_Str"+ path+ "String_Node_Str"+ contextName+ "String_Node_Str");
        }
        ElementDefinition outcome=updateURLs(url,currentBase.copy());
        outcome.setPath(fixedPath(contextPath,outcome.getPath()));
        updateFromBase(outcome,currentBase);
        if (diffMatches.get(0).hasSlicing() && !isExtension) {
          updateFromSlicing(outcome.getSlicing(),diffMatches.get(0).getSlicing());
          updateFromDefinition(outcome,diffMatches.get(0),profileName,closed,url);
        }
        if (diffMatches.get(0).hasSlicing() && !diffMatches.get(0).hasName())         diffpos++;
        result.getElement().add(outcome);
        List<ElementDefinition> baseMatches=getSiblings(base.getElement(),currentBase);
        for (        ElementDefinition baseItem : baseMatches) {
          baseCursor=base.getElement().indexOf(baseItem);
          outcome=updateURLs(url,baseItem.copy());
          updateFromBase(outcome,currentBase);
          outcome.setPath(fixedPath(contextPath,outcome.getPath()));
          outcome.setSlicing(null);
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          if (diffpos < diffMatches.size() && diffMatches.get(diffpos).getName().equals(outcome.getName())) {
            int nbl=findEndOfElement(base,baseCursor);
            int ndc=differential.getElement().indexOf(diffMatches.get(diffpos));
            int ndl=findEndOfElement(differential,ndc);
            processPaths(result,base,differential,baseCursor,ndc,nbl,ndl,url,profileName + pathTail(diffMatches,diffpos),contextPath,closed,contextName,resultPathBase,true);
            baseCursor=nbl + 1;
            diffCursor=ndl + 1;
            diffpos++;
          }
 else {
            result.getElement().add(outcome);
            baseCursor++;
            while (baseCursor < base.getElement().size() && base.getElement().get(baseCursor).getPath().startsWith(path) && !base.getElement().get(baseCursor).getPath().equals(path)) {
              outcome=updateURLs(url,currentBase.copy());
              outcome.setPath(fixedPath(contextPath,outcome.getPath()));
              if (!outcome.getPath().startsWith(resultPathBase))               throw new DefinitionException("String_Node_Str");
              result.getElement().add(outcome);
              baseCursor++;
            }
          }
        }
        if (closed && diffpos < diffMatches.size())         throw new DefinitionException("String_Node_Str" + profileName + "String_Node_Str"+ path+ "String_Node_Str"+ cpath+ "String_Node_Str");
        while (diffpos < diffMatches.size()) {
          ElementDefinition diffItem=diffMatches.get(diffpos);
          for (          ElementDefinition baseItem : baseMatches)           if (baseItem.getName().equals(diffItem.getName()))           throw new DefinitionException("String_Node_Str");
          outcome=updateURLs(url,original.copy());
          outcome.setPath(fixedPath(contextPath,outcome.getPath()));
          updateFromBase(outcome,currentBase);
          outcome.setSlicing(null);
          if (!outcome.getPath().startsWith(resultPathBase))           throw new DefinitionException("String_Node_Str");
          result.getElement().add(outcome);
          updateFromDefinition(outcome,diffItem,profileName,trimDifferential,url);
          diffpos++;
        }
      }
    }
  }
}