doc.addAttribute(XMLResource.XML_NS, nsURI); } } } } }  if (xsiSchemaLocation != null) { doc.addAttribute(XSI_SCHEMA_LOCATION, xsiSchemaLocation.toString());
if (nsPrefix != null && nsPrefix.length() > 0) { ((Element)currentNode).setAttributeNS(ExtendedMetaData.XMLNS_URI, XMLResource.XML_NS +":" + 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()); }
  protected void addNamespaceDeclarations()  {    EPackage noNamespacePackage = helper.getNoNamespacePackage();    EPackage[] packages = helper.packages();    StringBuffer xsiSchemaLocation = null;    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 = (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 == null)                  {                    xsiSchemaLocation = new StringBuffer();                  }                  else                  {                    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)        {          // First try to see if this package's implementation class has an eInstance.          //          try          {            Field field = ePackage.getClass().getField("eINSTANCE");            javaImplementationLocation = "java://" + 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("#/"))              {                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 == null)                {                  xsiSchemaLocation = new StringBuffer();                }                else                {                  xsiSchemaLocation.append(' ');                }                xsiSchemaLocation.append(nsURI);                xsiSchemaLocation.append(' ');                String location = javaImplementationLocation == null ? helper.getHREF(ePackage) : javaImplementationLocation;                if (location.endsWith("#/"))                {                  location = location.substring(0, location.length() - 2);                }                xsiSchemaLocation.append(location);              }            }          }        }      }    }    if (declareXSI)    {      doc.addAttribute(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 (nsPrefix != null && nsPrefix.length() > 0)            {              doc.addAttributeNS(XMLResource.XML_NS, nsPrefix, nsURI);            }            else            {              doc.addAttribute(XMLResource.XML_NS, nsURI);            }          }        }      }    }    if (xsiSchemaLocation != null)    {      doc.addAttribute(XSI_SCHEMA_LOCATION, xsiSchemaLocation.toString());    }    if (xsiNoNamespaceSchemaLocation != null)    {      doc.addAttribute(XSI_NO_NAMESPACE_SCHEMA_LOCATION, xsiNoNamespaceSchemaLocation);    }  }