Program: 148393
Project: 148393
Buggy source:
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    isConcept=false;
    TreePath tPath=null;
    if (((TreeSelection)selection).getPaths().length > 0)     tPath=((TreeSelection)selection).getPaths()[0];
    if (declNew != null) {
      decl=declNew;
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDModelGroup) {
      for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration)         decl=(XSDElementDeclaration)tPath.getSegment(i);
 else         if (tPath.getSegment(i) instanceof XSDParticle)         decl=(XSDElementDeclaration)((XSDParticle)tPath.getSegment(i)).getTerm();
      }
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration) {
      decl=(XSDElementDeclaration)selection.getFirstElement();
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDParticle) {
      decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getContent();
    }
 else {
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    List<XSDComplexTypeDefinition> types=Util.getComplexTypes(decl.getSchema());
    if (showDlg) {
      if (tPath != null)       for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration) {
          XSDTypeDefinition type=(((XSDElementDeclaration)tPath.getSegment(i)).getTypeDefinition());
          if (!type.equals(decl.getTypeDefinition()))           types.remove(type);
        }
        if (tPath.getSegment(i) instanceof XSDParticle) {
          XSDTypeDefinition type=((XSDElementDeclaration)(((XSDParticle)tPath.getSegment(i)).getTerm())).getTypeDefinition();
          if (!type.equals(decl.getTypeDefinition()))           types.remove(type);
        }
      }
      dialog=new ComplexTypeInputDialog(this,page.getSite().getShell(),schema,decl.getTypeDefinition(),types,isXSDModelGroup);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    if (!showDlg && !validateType()) {
      return Status.CANCEL_STATUS;
    }
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    boolean anonymous=(typeName == null) || ("String_Node_Str".equals(typeName));
    boolean alreadyExists=false;
    XSDComplexTypeDefinition complexType=null;
    XSDParticle subParticle=null;
    XSDParticle groupParticle=null;
    XSDElementDeclaration subElement=null;
    XSDElementDeclaration parent=null;
    Object pObject=Util.getParent(decl);
    if (pObject instanceof XSDElementDeclaration)     parent=(XSDElementDeclaration)pObject;
 else     if (pObject instanceof XSDComplexTypeDefinition)     complexType=(XSDComplexTypeDefinition)pObject;
    if (!anonymous) {
      EList list=schema.getTypeDefinitions();
      String ns="String_Node_Str";
      if (typeName.lastIndexOf("String_Node_Str") != -1) {
        ns=typeName.substring(typeName.lastIndexOf("String_Node_Str") + 3);
        typeName=typeName.substring(0,typeName.lastIndexOf("String_Node_Str"));
      }
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDTypeDefinition td=(XSDTypeDefinition)iter.next();
        if ((td.getName().equals(typeName) && (td instanceof XSDComplexTypeDefinition))) {
          alreadyExists=true;
          complexType=(XSDComplexTypeDefinition)td;
          break;
        }
      }
    }
 else {
      if (parent != null && decl.getTypeDefinition() instanceof XSDComplexTypeDefinition)       complexType=(XSDComplexTypeDefinition)decl.getTypeDefinition();
      if (complexType != null && complexType.getName() == null) {
        alreadyExists=true;
      }
      if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition)       alreadyExists=false;
    }
    if (complexType != null) {
      XSDParticleImpl partCnt=(XSDParticleImpl)complexType.getContentType();
      XSDModelGroupImpl mdlGrp=(XSDModelGroupImpl)partCnt.getTerm();
      if (isChoice)       mdlGrp.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll) {
        mdlGrp.setCompositor(XSDCompositor.ALL_LITERAL);
      }
 else {
        mdlGrp.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      }
      partCnt.unsetMaxOccurs();
      partCnt.unsetMinOccurs();
      XSDTypeDefinition superType=null;
      for (      XSDTypeDefinition type : types) {
        if (type.getName().equals(superTypeName)) {
          superType=type;
          break;
        }
      }
      complexType.setName(typeName);
      if (superType != null) {
        complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
        complexType.setBaseTypeDefinition(superType);
      }
      if (isAbstract)       complexType.setAbstract(isAbstract);
 else       complexType.unsetAbstract();
      if (parent != null)       parent.updateElement();
      if (complexType != null)       complexType.updateElement();
    }
    if (!alreadyExists) {
      subElement=factory.createXSDElementDeclaration();
      subElement.setName("String_Node_Str");
      subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
      subParticle=factory.createXSDParticle();
      subParticle.unsetMaxOccurs();
      subParticle.unsetMinOccurs();
      subParticle.setContent(subElement);
      subParticle.updateElement();
      XSDModelGroup group=factory.createXSDModelGroup();
      if (isChoice)       group.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll)       group.setCompositor(XSDCompositor.ALL_LITERAL);
 else       group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      group.getContents().add(0,subParticle);
      group.updateElement();
      complexType=factory.createXSDComplexTypeDefinition();
      if (!anonymous) {
        XSDTypeDefinition superType=null;
        for (        XSDTypeDefinition type : types) {
          if (type.getName().equals(superTypeName)) {
            superType=type;
            break;
          }
        }
        complexType.setName(typeName);
        if (superType != null) {
          complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
          complexType.setBaseTypeDefinition(superType);
        }
        if (isAbstract)         complexType.setAbstract(isAbstract);
 else         complexType.unsetAbstract();
        schema.getContents().add(complexType);
      }
      complexType.updateElement();
      groupParticle=factory.createXSDParticle();
      groupParticle.unsetMaxOccurs();
      groupParticle.unsetMinOccurs();
      groupParticle.setContent(group);
      groupParticle.updateElement();
      complexType.setContent(groupParticle);
      complexType.updateElement();
    }
    if (anonymous)     decl.setAnonymousTypeDefinition(complexType);
 else {
      decl.setTypeDefinition(complexType);
    }
    if (isConcept) {
      ArrayList keys=new ArrayList();
      EList list=decl.getIdentityConstraintDefinitions();
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDIdentityConstraintDefinition icd=(XSDIdentityConstraintDefinition)iter.next();
        if (icd.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL))         keys.add(icd);
      }
      decl.getIdentityConstraintDefinitions().removeAll(keys);
      XSDElementDeclaration firstDecl=null;
      if (complexType.getContent() instanceof XSDParticle) {
        if (((XSDParticle)complexType.getContent()).getTerm() instanceof XSDModelGroup) {
          XSDModelGroup group=(XSDModelGroup)((XSDParticle)complexType.getContent()).getTerm();
          EList gpl=group.getContents();
          for (Iterator iter=gpl.iterator(); iter.hasNext(); ) {
            XSDParticle part=(XSDParticle)iter.next();
            if (part.getTerm() instanceof XSDElementDeclaration) {
              firstDecl=(XSDElementDeclaration)part.getTerm();
              break;
            }
          }
        }
      }
      if (firstDecl != null) {
        XSDIdentityConstraintDefinition uniqueKey=factory.createXSDIdentityConstraintDefinition();
        uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
        uniqueKey.setName(decl.getName());
        XSDXPathDefinition selector=factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("String_Node_Str");
        uniqueKey.setSelector(selector);
        XSDXPathDefinition field=factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue(firstDecl.getAliasName());
        uniqueKey.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(uniqueKey);
      }
    }
    decl.updateElement();
    schema.update();
    page.refresh();
    declNew=null;
    page.markDirty();
  }
 catch (  Exception e) {
    log.error(e.getStackTrace());
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}

Fixed version (ground truth):
public IStatus doAction(){
  try {
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    isConcept=false;
    TreePath tPath=null;
    if (((TreeSelection)selection).getPaths().length > 0)     tPath=((TreeSelection)selection).getPaths()[0];
    if (declNew != null) {
      decl=declNew;
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDModelGroup) {
      for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration)         decl=(XSDElementDeclaration)tPath.getSegment(i);
 else         if (tPath.getSegment(i) instanceof XSDParticle)         decl=(XSDElementDeclaration)((XSDParticle)tPath.getSegment(i)).getTerm();
      }
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration) {
      decl=(XSDElementDeclaration)selection.getFirstElement();
      checkConcept();
    }
 else     if (selection.getFirstElement() instanceof XSDParticle) {
      decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getContent();
    }
 else {
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    List<XSDComplexTypeDefinition> types=Util.getComplexTypes(decl.getSchema());
    if (showDlg) {
      if (tPath != null)       for (int i=0; i < tPath.getSegmentCount(); i++) {
        if (tPath.getSegment(i) instanceof XSDElementDeclaration) {
          XSDTypeDefinition type=(((XSDElementDeclaration)tPath.getSegment(i)).getTypeDefinition());
          if (!type.equals(decl.getTypeDefinition()))           types.remove(type);
        }
        if (tPath.getSegment(i) instanceof XSDParticle) {
          XSDTypeDefinition type=((XSDElementDeclaration)(((XSDParticle)tPath.getSegment(i)).getTerm())).getTypeDefinition();
          if (!type.equals(decl.getTypeDefinition()))           types.remove(type);
        }
      }
      dialog=new ComplexTypeInputDialog(this,page.getSite().getShell(),schema,decl.getTypeDefinition(),types,isXSDModelGroup);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Dialog.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    if (!showDlg && !validateType()) {
      return Status.CANCEL_STATUS;
    }
    XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
    boolean anonymous=(typeName == null) || ("String_Node_Str".equals(typeName));
    boolean alreadyExists=false;
    XSDComplexTypeDefinition complexType=null;
    XSDParticle subParticle=null;
    XSDParticle groupParticle=null;
    XSDElementDeclaration subElement=null;
    XSDElementDeclaration parent=null;
    Object pObject=Util.getParent(decl);
    if (pObject instanceof XSDElementDeclaration)     parent=(XSDElementDeclaration)pObject;
 else     if (pObject instanceof XSDComplexTypeDefinition)     complexType=(XSDComplexTypeDefinition)pObject;
    if (!anonymous) {
      EList list=schema.getTypeDefinitions();
      String ns="String_Node_Str";
      if (typeName.lastIndexOf("String_Node_Str") != -1) {
        ns=typeName.substring(typeName.lastIndexOf("String_Node_Str") + 3);
        typeName=typeName.substring(0,typeName.lastIndexOf("String_Node_Str"));
      }
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDTypeDefinition td=(XSDTypeDefinition)iter.next();
        if ((td.getName().equals(typeName) && (td instanceof XSDComplexTypeDefinition))) {
          alreadyExists=true;
          complexType=(XSDComplexTypeDefinition)td;
          break;
        }
      }
    }
 else {
      if (parent != null && decl.getTypeDefinition() instanceof XSDComplexTypeDefinition)       complexType=(XSDComplexTypeDefinition)decl.getTypeDefinition();
      if (complexType != null && complexType.getName() == null) {
        alreadyExists=true;
      }
      if (decl.getTypeDefinition() instanceof XSDSimpleTypeDefinition)       alreadyExists=false;
    }
    if (complexType != null) {
      XSDParticleImpl partCnt=(XSDParticleImpl)complexType.getContentType();
      XSDModelGroupImpl mdlGrp=(XSDModelGroupImpl)partCnt.getTerm();
      if (isChoice)       mdlGrp.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll) {
        mdlGrp.setCompositor(XSDCompositor.ALL_LITERAL);
      }
 else {
        mdlGrp.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      }
      partCnt.unsetMaxOccurs();
      partCnt.unsetMinOccurs();
      XSDTypeDefinition superType=null;
      for (      XSDTypeDefinition type : types) {
        if (type.getName().equals(superTypeName)) {
          superType=type;
          break;
        }
      }
      complexType.setName(typeName);
      if (superType != null) {
        complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
        complexType.setBaseTypeDefinition(superType);
      }
      if (isAbstract)       complexType.setAbstract(isAbstract);
 else       complexType.unsetAbstract();
      if (parent != null)       parent.updateElement();
      if (complexType != null)       complexType.updateElement();
    }
    if (!alreadyExists) {
      subElement=factory.createXSDElementDeclaration();
      subElement.setName("String_Node_Str");
      subElement.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
      subParticle=factory.createXSDParticle();
      subParticle.unsetMaxOccurs();
      subParticle.unsetMinOccurs();
      subParticle.setContent(subElement);
      subParticle.updateElement();
      XSDModelGroup group=factory.createXSDModelGroup();
      if (isChoice)       group.setCompositor(XSDCompositor.CHOICE_LITERAL);
 else       if (isAll)       group.setCompositor(XSDCompositor.ALL_LITERAL);
 else       group.setCompositor(XSDCompositor.SEQUENCE_LITERAL);
      group.getContents().add(0,subParticle);
      group.updateElement();
      complexType=factory.createXSDComplexTypeDefinition();
      if (!anonymous) {
        XSDTypeDefinition superType=null;
        for (        XSDTypeDefinition type : types) {
          if (type.getName().equals(superTypeName)) {
            superType=type;
            break;
          }
        }
        complexType.setName(typeName);
        if (superType != null) {
          complexType.setDerivationMethod(XSDDerivationMethod.EXTENSION_LITERAL);
          complexType.setBaseTypeDefinition(superType);
        }
        if (isAbstract)         complexType.setAbstract(isAbstract);
 else         complexType.unsetAbstract();
        schema.getContents().add(complexType);
      }
      complexType.updateElement();
      groupParticle=factory.createXSDParticle();
      groupParticle.unsetMaxOccurs();
      groupParticle.unsetMinOccurs();
      groupParticle.setContent(group);
      groupParticle.updateElement();
      complexType.setContent(groupParticle);
      complexType.updateElement();
    }
    if (anonymous)     decl.setAnonymousTypeDefinition(complexType);
 else {
      decl.setTypeDefinition(complexType);
    }
    if (isConcept) {
      ArrayList keys=new ArrayList();
      EList list=decl.getIdentityConstraintDefinitions();
      for (Iterator iter=list.iterator(); iter.hasNext(); ) {
        XSDIdentityConstraintDefinition icd=(XSDIdentityConstraintDefinition)iter.next();
        if (icd.getIdentityConstraintCategory().equals(XSDIdentityConstraintCategory.UNIQUE_LITERAL))         keys.add(icd);
      }
      decl.getIdentityConstraintDefinitions().removeAll(keys);
      XSDElementDeclaration firstDecl=null;
      if (complexType.getContent() instanceof XSDParticle) {
        if (((XSDParticle)complexType.getContent()).getTerm() instanceof XSDModelGroup) {
          XSDModelGroup group=(XSDModelGroup)((XSDParticle)complexType.getContent()).getTerm();
          EList gpl=group.getContents();
          for (Iterator iter=gpl.iterator(); iter.hasNext(); ) {
            XSDParticle part=(XSDParticle)iter.next();
            if (part.getTerm() instanceof XSDElementDeclaration) {
              firstDecl=(XSDElementDeclaration)part.getTerm();
              break;
            }
          }
        }
      }
      if (firstDecl != null) {
        XSDIdentityConstraintDefinition uniqueKey=factory.createXSDIdentityConstraintDefinition();
        uniqueKey.setIdentityConstraintCategory(XSDIdentityConstraintCategory.UNIQUE_LITERAL);
        uniqueKey.setName(decl.getName());
        XSDXPathDefinition selector=factory.createXSDXPathDefinition();
        selector.setVariety(XSDXPathVariety.SELECTOR_LITERAL);
        selector.setValue("String_Node_Str");
        uniqueKey.setSelector(selector);
        XSDXPathDefinition field=factory.createXSDXPathDefinition();
        field.setVariety(XSDXPathVariety.FIELD_LITERAL);
        field.setValue(firstDecl.getAliasName());
        uniqueKey.getFields().add(field);
        decl.getIdentityConstraintDefinitions().add(uniqueKey);
      }
    }
    decl.updateElement();
    schema.update();
    page.refresh();
    declNew=null;
    page.markDirty();
  }
 catch (  Exception e) {
    log.error(e.getMessage(),e);
    MessageDialog.openError(page.getSite().getShell(),"String_Node_Str","String_Node_Str" + e.getLocalizedMessage());
    return Status.CANCEL_STATUS;
  }
  return Status.OK_STATUS;
}