private static void addSubstitutionGroups(Map<String, XmlElementDescriptor> result,
                                            XmlNSDescriptorImpl nsDescriptor,
                                            Set<? super XmlNSDescriptorImpl> visited) {
    mainLoop: while (true) {
      for (final XmlElementDescriptor xmlElementDescriptor : result.values()) {
        XmlElementDescriptorImpl descriptor = (XmlElementDescriptorImpl)xmlElementDescriptor;

        final XmlElementDescriptor[] substitutes = nsDescriptor.getSubstitutes(descriptor.getName(), descriptor.getNamespace());
        boolean toContinue = false;

        for (XmlElementDescriptor substitute : substitutes) {
          if (result.get(substitute.getName()) == null) {
            toContinue = true;
            result.put(substitute.getName(), substitute);
          }
        }

        if (toContinue) continue mainLoop;
      }

      break;
    }

    visited.add(nsDescriptor);
    for (XmlTag tag : nsDescriptor.getTag().getSubTags()) {
      if (XmlNSDescriptorImpl.equalsToSchemaName(tag, "include") ||
          XmlNSDescriptorImpl.equalsToSchemaName(tag, "import")) {
        XmlAttribute location = tag.getAttribute("schemaLocation");
        if (location != null) {
          XmlAttributeValue valueElement = location.getValueElement();
          if (valueElement != null) {
            PsiElement element = new FileReferenceSet(valueElement).resolve();
            if (element instanceof XmlFile) {
              XmlDocument document = ((XmlFile)element).getDocument();
              if (document != null) {
                PsiMetaData metaData = document.getMetaData();
                if (metaData instanceof XmlNSDescriptorImpl && !visited.contains(metaData)) {
                  addSubstitutionGroups(result, (XmlNSDescriptorImpl)metaData, visited);
                }
              }
            }
          }
        }
      }
    }
  }
private static void addSubstitutionGroups(Map<String, XmlElementDescriptor> result,
                                            XmlNSDescriptorImpl nsDescriptor,
                                            Set<? super XmlNSDescriptorImpl> visited)