Program: 11902
Project: 11902
Buggy source:
protected void addNamespaceDeclarations(){
  EPackage noNamespacePackage=helper.getNoNamespacePackage();
  EPackage[] packages=helper.packages();
  buffer.setLength(0);
  StringBuffer xsiSchemaLocation=buffer;
  String xsiNoNamespaceSchemaLocation=null;
  if (declareSchemaLocation) {
    Map handledBySchemaLocationMap=Collections.EMPTY_MAP;
    if (extendedMetaData != null) {
      Resource resource=helper.getResource();
      if (resource != null && resource.getContents().size() >= 1) {
        EObject root=getSchemaLocationRoot((EObject)resource.getContents().get(0));
        EClass eClass=root.eClass();
        EReference xsiSchemaLocationMapFeature=extendedMetaData.getXSISchemaLocationMapFeature(eClass);
        if (xsiSchemaLocationMapFeature != null) {
          EMap xsiSchemaLocationMap=(EMap)root.eGet(xsiSchemaLocationMapFeature);
          if (!xsiSchemaLocationMap.isEmpty()) {
            handledBySchemaLocationMap=xsiSchemaLocationMap.map();
            declareXSI=true;
            for (Iterator i=xsiSchemaLocationMap.entrySet().iterator(); i.hasNext(); ) {
              Map.Entry entry=(Map.Entry)i.next();
              String namespace=(String)entry.getKey();
              URI location=URI.createURI(entry.getValue().toString());
              if (namespace == null) {
                xsiNoNamespaceSchemaLocation=helper.deresolve(location).toString();
              }
 else {
                if (xsiSchemaLocation.length() > 0) {
                  xsiSchemaLocation.append(' ');
                }
                xsiSchemaLocation.append(namespace);
                xsiSchemaLocation.append(' ');
                xsiSchemaLocation.append(helper.deresolve(location).toString());
              }
            }
          }
        }
      }
    }
    for (int i=0; i < packages.length; i++) {
      EPackage ePackage=packages[i];
      String javaImplementationLocation=null;
      if (declareSchemaLocationImplementation) {
        try {
          Field field=ePackage.getClass().getField("String_Node_Str");
          javaImplementationLocation="String_Node_Str" + field.getDeclaringClass().getName();
        }
 catch (        Exception exception) {
        }
      }
      if (noNamespacePackage == ePackage) {
        if (ePackage.eResource() != null && !handledBySchemaLocationMap.containsKey(null)) {
          declareXSI=true;
          if (javaImplementationLocation != null) {
            xsiNoNamespaceSchemaLocation=javaImplementationLocation;
          }
 else {
            xsiNoNamespaceSchemaLocation=helper.getHREF(ePackage);
            if (xsiNoNamespaceSchemaLocation.endsWith("String_Node_Str")) {
              xsiNoNamespaceSchemaLocation=xsiNoNamespaceSchemaLocation.substring(0,xsiNoNamespaceSchemaLocation.length() - 2);
            }
          }
        }
      }
 else {
        Resource resource=ePackage.eResource();
        if (resource != null) {
          String nsURI=ePackage.getNsURI();
          if (!handledBySchemaLocationMap.containsKey(nsURI)) {
            URI uri=resource.getURI();
            if (javaImplementationLocation != null || (uri == null ? nsURI != null : !uri.toString().equals(nsURI))) {
              declareXSI=true;
              if (xsiSchemaLocation.length() > 0) {
                xsiSchemaLocation.append(' ');
              }
              xsiSchemaLocation.append(nsURI);
              xsiSchemaLocation.append(' ');
              String location=javaImplementationLocation == null ? helper.getHREF(ePackage) : javaImplementationLocation;
              if (location.endsWith("String_Node_Str")) {
                location=location.substring(0,location.length() - 2);
              }
              xsiSchemaLocation.append(location);
            }
          }
        }
      }
    }
  }
  if (declareXSI) {
    if (!toDOM) {
      doc.addAttribute(XSI_XMLNS,XMLResource.XSI_URI);
    }
 else {
      ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XSI_XMLNS,XMLResource.XSI_URI);
    }
  }
  for (int i=0; i < packages.length; i++) {
    EPackage ePackage=packages[i];
    if (ePackage != noNamespacePackage && ePackage != XMLNamespacePackage.eINSTANCE && !ExtendedMetaData.XMLNS_URI.equals(ePackage.getNsURI())) {
      String nsURI=extendedMetaData == null ? ePackage.getNsURI() : extendedMetaData.getNamespace(ePackage);
      if (ePackage == xmlSchemaTypePackage) {
        nsURI=XMLResource.XML_SCHEMA_URI;
      }
      if (nsURI != null && !isDuplicateURI(nsURI)) {
        List nsPrefixes=helper.getPrefixes(ePackage);
        for (Iterator j=nsPrefixes.iterator(); j.hasNext(); ) {
          String nsPrefix=(String)j.next();
          if (!toDOM) {
            if (nsPrefix != null && nsPrefix.length() > 0) {
              doc.addAttributeNS(XMLResource.XML_NS,nsPrefix,nsURI);
            }
 else {
              doc.addAttribute(XMLResource.XML_NS,nsURI);
            }
          }
 else {
            if (nsPrefix != null && nsPrefix.length() > 0) {
              ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XMLResource.XML_NS + "String_Node_Str" + nsPrefix,nsURI);
            }
 else {
              ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XMLResource.XML_NS,nsURI);
            }
          }
        }
      }
    }
  }
  if (xsiSchemaLocation.length() > 0) {
    if (!toDOM) {
      doc.addAttribute(XSI_SCHEMA_LOCATION,xsiSchemaLocation.toString());
    }
 else {
      ((Element)currentNode).setAttributeNS(XMLResource.XSI_URI,XSI_SCHEMA_LOCATION,xsiSchemaLocation.toString());
    }
  }
  if (xsiNoNamespaceSchemaLocation != null) {
    if (!toDOM) {
      doc.addAttribute(XSI_NO_NAMESPACE_SCHEMA_LOCATION,xsiNoNamespaceSchemaLocation);
    }
 else {
      ((Element)currentNode).setAttributeNS(XMLResource.XSI_URI,XSI_NO_NAMESPACE_SCHEMA_LOCATION,xsiNoNamespaceSchemaLocation);
    }
  }
}

Fixed version (ground truth):
protected void addNamespaceDeclarations(){
  EPackage noNamespacePackage=helper.getNoNamespacePackage();
  EPackage[] packages=helper.packages();
  buffer.setLength(0);
  StringBuffer xsiSchemaLocation=buffer;
  String xsiNoNamespaceSchemaLocation=null;
  if (declareSchemaLocation) {
    Map handledBySchemaLocationMap=Collections.EMPTY_MAP;
    if (extendedMetaData != null) {
      Resource resource=helper.getResource();
      if (resource != null && resource.getContents().size() >= 1) {
        EObject root=getSchemaLocationRoot((EObject)resource.getContents().get(0));
        EClass eClass=root.eClass();
        EReference xsiSchemaLocationMapFeature=extendedMetaData.getXSISchemaLocationMapFeature(eClass);
        if (xsiSchemaLocationMapFeature != null) {
          EMap xsiSchemaLocationMap=(EMap)root.eGet(xsiSchemaLocationMapFeature);
          if (!xsiSchemaLocationMap.isEmpty()) {
            handledBySchemaLocationMap=xsiSchemaLocationMap.map();
            declareXSI=true;
            for (Iterator i=xsiSchemaLocationMap.entrySet().iterator(); i.hasNext(); ) {
              Map.Entry entry=(Map.Entry)i.next();
              String namespace=(String)entry.getKey();
              URI location=URI.createURI(entry.getValue().toString());
              if (namespace == null) {
                xsiNoNamespaceSchemaLocation=helper.deresolve(location).toString();
              }
 else {
                if (xsiSchemaLocation.length() > 0) {
                  xsiSchemaLocation.append(' ');
                }
                xsiSchemaLocation.append(namespace);
                xsiSchemaLocation.append(' ');
                xsiSchemaLocation.append(helper.deresolve(location).toString());
              }
            }
          }
        }
      }
    }
    for (int i=0; i < packages.length; i++) {
      EPackage ePackage=packages[i];
      String javaImplementationLocation=null;
      if (declareSchemaLocationImplementation) {
        try {
          Field field=ePackage.getClass().getField("String_Node_Str");
          javaImplementationLocation="String_Node_Str" + field.getDeclaringClass().getName();
        }
 catch (        Exception exception) {
        }
      }
      if (noNamespacePackage == ePackage) {
        if (ePackage.eResource() != null && !handledBySchemaLocationMap.containsKey(null)) {
          declareXSI=true;
          if (javaImplementationLocation != null) {
            xsiNoNamespaceSchemaLocation=javaImplementationLocation;
          }
 else {
            xsiNoNamespaceSchemaLocation=helper.getHREF(ePackage);
            if (xsiNoNamespaceSchemaLocation.endsWith("String_Node_Str")) {
              xsiNoNamespaceSchemaLocation=xsiNoNamespaceSchemaLocation.substring(0,xsiNoNamespaceSchemaLocation.length() - 2);
            }
          }
        }
      }
 else {
        Resource resource=ePackage.eResource();
        if (resource != null) {
          String nsURI=ePackage.getNsURI();
          if (!handledBySchemaLocationMap.containsKey(nsURI)) {
            URI uri=resource.getURI();
            if (javaImplementationLocation != null || (uri == null ? nsURI != null : !uri.toString().equals(nsURI))) {
              declareXSI=true;
              if (xsiSchemaLocation.length() > 0) {
                xsiSchemaLocation.append(' ');
              }
              xsiSchemaLocation.append(nsURI);
              xsiSchemaLocation.append(' ');
              String location=javaImplementationLocation == null ? helper.getHREF(ePackage) : javaImplementationLocation;
              if (location.endsWith("String_Node_Str")) {
                location=location.substring(0,location.length() - 2);
                if (uri.hasFragment()) {
                  location+="String_Node_Str" + uri.fragment();
                }
              }
              xsiSchemaLocation.append(location);
            }
          }
        }
      }
    }
  }
  if (declareXSI) {
    if (!toDOM) {
      doc.addAttribute(XSI_XMLNS,XMLResource.XSI_URI);
    }
 else {
      ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XSI_XMLNS,XMLResource.XSI_URI);
    }
  }
  for (int i=0; i < packages.length; i++) {
    EPackage ePackage=packages[i];
    if (ePackage != noNamespacePackage && ePackage != XMLNamespacePackage.eINSTANCE && !ExtendedMetaData.XMLNS_URI.equals(ePackage.getNsURI())) {
      String nsURI=extendedMetaData == null ? ePackage.getNsURI() : extendedMetaData.getNamespace(ePackage);
      if (ePackage == xmlSchemaTypePackage) {
        nsURI=XMLResource.XML_SCHEMA_URI;
      }
      if (nsURI != null && !isDuplicateURI(nsURI)) {
        List nsPrefixes=helper.getPrefixes(ePackage);
        for (Iterator j=nsPrefixes.iterator(); j.hasNext(); ) {
          String nsPrefix=(String)j.next();
          if (!toDOM) {
            if (nsPrefix != null && nsPrefix.length() > 0) {
              doc.addAttributeNS(XMLResource.XML_NS,nsPrefix,nsURI);
            }
 else {
              doc.addAttribute(XMLResource.XML_NS,nsURI);
            }
          }
 else {
            if (nsPrefix != null && nsPrefix.length() > 0) {
              ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XMLResource.XML_NS + "String_Node_Str" + nsPrefix,nsURI);
            }
 else {
              ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI,XMLResource.XML_NS,nsURI);
            }
          }
        }
      }
    }
  }
  if (xsiSchemaLocation.length() > 0) {
    if (!toDOM) {
      doc.addAttribute(XSI_SCHEMA_LOCATION,xsiSchemaLocation.toString());
    }
 else {
      ((Element)currentNode).setAttributeNS(XMLResource.XSI_URI,XSI_SCHEMA_LOCATION,xsiSchemaLocation.toString());
    }
  }
  if (xsiNoNamespaceSchemaLocation != null) {
    if (!toDOM) {
      doc.addAttribute(XSI_NO_NAMESPACE_SCHEMA_LOCATION,xsiNoNamespaceSchemaLocation);
    }
 else {
      ((Element)currentNode).setAttributeNS(XMLResource.XSI_URI,XSI_NO_NAMESPACE_SCHEMA_LOCATION,xsiNoNamespaceSchemaLocation);
    }
  }
}