Program: 148394
Project: 148394
Buggy source:
public IStatus doAction(){
  try {
    XSDElementDeclaration decl=null;
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    if (declNew != null)     decl=declNew;
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration) {
      isConcept=true;
      decl=(XSDElementDeclaration)selection.getFirstElement();
    }
 else {
      isConcept=false;
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    ArrayList customTypes=new ArrayList();
    for (Iterator iter=schema.getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition) {
        if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null)         customTypes.add(type.getName());
      }
    }
    ArrayList builtInTypes=new ArrayList();
    for (Iterator iter=schema.getSchemaForSchema().getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition)       builtInTypes.add(type.getName());
    }
    if (showDlg) {
      dialog=new SimpleTypeInputDialog(this,page.getSite().getShell(),schema,"String_Node_Str",customTypes,builtInTypes);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Window.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    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);
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      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("String_Node_Str");
      uniqueKey.getFields().add(field);
      decl.getIdentityConstraintDefinitions().add(uniqueKey);
    }
    XSDTypeDefinition current=decl.getTypeDefinition();
    if (builtIn) {
      decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),typeName));
    }
 else {
      if (typeName != null && typeName.length() > 0) {
        XSDSimpleTypeDefinition std=null;
        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 (        XSDTypeDefinition typeDef : schema.getTypeDefinitions()) {
          if (typeDef instanceof XSDSimpleTypeDefinition) {
            if (typeDef.getName().equals(typeName)) {
              std=(XSDSimpleTypeDefinition)typeDef;
              break;
            }
          }
        }
        if (std == null) {
          std=schema.resolveSimpleTypeDefinition(typeName);
          std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
          schema.getContents().add(std);
        }
        decl.setTypeDefinition(std);
      }
 else {
        XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
        simpleType=factory.createXSDSimpleTypeDefinition();
        simpleType.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
        decl.setAnonymousTypeDefinition(simpleType);
      }
    }
    decl.updateElement();
    declNew=null;
    page.refresh();
    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 {
    XSDElementDeclaration decl=null;
    IStructuredSelection selection=(IStructuredSelection)page.getTreeViewer().getSelection();
    if (declNew != null)     decl=declNew;
 else     if (selection.getFirstElement() instanceof XSDElementDeclaration) {
      isConcept=true;
      decl=(XSDElementDeclaration)selection.getFirstElement();
    }
 else {
      isConcept=false;
      if (selection.getFirstElement() != null) {
        decl=(XSDElementDeclaration)((XSDParticle)selection.getFirstElement()).getTerm();
      }
    }
    ArrayList customTypes=new ArrayList();
    for (Iterator iter=schema.getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition) {
        if (type.getTargetNamespace() != null && !type.getTargetNamespace().equals(XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001) || type.getTargetNamespace() == null)         customTypes.add(type.getName());
      }
    }
    ArrayList builtInTypes=new ArrayList();
    for (Iterator iter=schema.getSchemaForSchema().getTypeDefinitions().iterator(); iter.hasNext(); ) {
      XSDTypeDefinition type=(XSDTypeDefinition)iter.next();
      if (type instanceof XSDSimpleTypeDefinition)       builtInTypes.add(type.getName());
    }
    if (showDlg) {
      dialog=new SimpleTypeInputDialog(this,page.getSite().getShell(),schema,"String_Node_Str",customTypes,builtInTypes);
      dialog.setBlockOnOpen(true);
      int ret=dialog.open();
      if (ret == Window.CANCEL) {
        return Status.CANCEL_STATUS;
      }
    }
    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);
      XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
      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("String_Node_Str");
      uniqueKey.getFields().add(field);
      decl.getIdentityConstraintDefinitions().add(uniqueKey);
    }
    XSDTypeDefinition current=decl.getTypeDefinition();
    if (builtIn) {
      decl.setTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),typeName));
    }
 else {
      if (typeName != null && typeName.length() > 0) {
        XSDSimpleTypeDefinition std=null;
        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 (        XSDTypeDefinition typeDef : schema.getTypeDefinitions()) {
          if (typeDef instanceof XSDSimpleTypeDefinition) {
            if (typeDef.getName().equals(typeName)) {
              std=(XSDSimpleTypeDefinition)typeDef;
              break;
            }
          }
        }
        if (std == null) {
          std=schema.resolveSimpleTypeDefinition(typeName);
          std.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
          schema.getContents().add(std);
        }
        decl.setTypeDefinition(std);
      }
 else {
        XSDFactory factory=XSDSchemaBuildingTools.getXSDFactory();
        simpleType=factory.createXSDSimpleTypeDefinition();
        simpleType.setBaseTypeDefinition(schema.resolveSimpleTypeDefinition(schema.getSchemaForSchemaNamespace(),"String_Node_Str"));
        decl.setAnonymousTypeDefinition(simpleType);
      }
    }
    decl.updateElement();
    declNew=null;
    page.refresh();
    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;
}