<commit_before>package com.intellij.xml.impl.dtd;

import com.intellij.codeInsight.daemon.Validator;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLock;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.filters.ClassFilter;
import com.intellij.psi.scope.ElementClassHint;
import com.intellij.psi.scope.NameHint;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.processor.FilterElementProcessor;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.xml.*;
import com.intellij.xml.XmlElementDescriptor;
import com.intellij.xml.XmlNSDescriptor;
import com.intellij.xml.impl.ExternalDocumentValidator;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mike
 */
public class XmlNSDescriptorImpl implements XmlNSDescriptor,Validator {
  private XmlElement myElement;
  private XmlFile myDescriptorFile;
  private volatile CachedValue<Map<String, XmlElementDescriptor>> myCachedDecls;

  public XmlNSDescriptorImpl() {
  }

  public XmlNSDescriptorImpl(XmlElement element) {
    init(element);
  }

  public XmlFile getDescriptorFile() {
    return myDescriptorFile;
  }

  public boolean isHierarhyEnabled() {
    return false;
  }


  private XmlElementDecl getElement(String elementName) {
    return (XmlElementDecl)buildDeclarationMap().get(elementName).getDeclaration();
  }


  public XmlElementDescriptor[] getElements() {
    return buildDeclarationMap().values().toArray(XmlElementDescriptor.EMPTY_ARRAY);
  }

  private Map<String,XmlElementDescriptor> buildDeclarationMap() {
    if (myCachedDecls == null) {
      synchronized (PsiLock.LOCK) {
        if (myCachedDecls == null) {
          myCachedDecls = myElement.getManager().getCachedValuesManager().createCachedValue(new CachedValueProvider<Map<String, XmlElementDescriptor>>() {
            public Result<Map<String, XmlElementDescriptor>> compute() {
              final List<XmlElementDecl> result = new ArrayList<XmlElementDecl>();
              myElement.processElements(new FilterElementProcessor(new ClassFilter(XmlElementDecl.class), result), getDeclaration());
              final Map<String, XmlElementDescriptor> ret = new LinkedHashMap<String, XmlElementDescriptor>((int)(result.size() * 1.5));
              for (final XmlElementDecl xmlElementDecl : result) {
                final XmlElement nameElement = xmlElementDecl.getNameElement();
                if (nameElement != null) {
                  String text = nameElement.getText();
                  if (!ret.containsKey(text)) {
                    ret.put(text, new XmlElementDescriptorImpl(xmlElementDecl));
                  }
                }
              }
              return new Result<Map<String, XmlElementDescriptor>>(ret, myDescriptorFile);
             }
           }, false);
        }
      }
    }
    
    return myCachedDecls.getValue();
  }

  public XmlElementDescriptor getElementDescriptor(XmlTag tag) {
    String name = tag.getName();
    return getElementDescriptor(name);
  }

  public XmlElementDescriptor[] getRootElementsDescriptors(final XmlDocument document) {
    return getElements();
  }
<commit_msg>Spring: code sense should not suggest all possible elements outside <beans> tag (IDEADEV-13514)<commit_after>package com.intellij.xml.impl.dtd;

import com.intellij.codeInsight.daemon.Validator;
import com.intellij.javaee.ExternalResourceManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLock;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.filters.ClassFilter;
import com.intellij.psi.scope.ElementClassHint;
import com.intellij.psi.scope.NameHint;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.processor.FilterElementProcessor;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.xml.*;
import com.intellij.xml.XmlElementDescriptor;
import com.intellij.xml.XmlNSDescriptor;
import com.intellij.xml.impl.ExternalDocumentValidator;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mike
 */
public class XmlNSDescriptorImpl implements XmlNSDescriptor,Validator {
  private XmlElement myElement;
  private XmlFile myDescriptorFile;
  private volatile CachedValue<Map<String, XmlElementDescriptor>> myCachedDecls;

  public XmlNSDescriptorImpl() {
  }

  public XmlNSDescriptorImpl(XmlElement element) {
    init(element);
  }

  public XmlFile getDescriptorFile() {
    return myDescriptorFile;
  }

  public boolean isHierarhyEnabled() {
    return false;
  }


  private XmlElementDecl getElement(String elementName) {
    return (XmlElementDecl)buildDeclarationMap().get(elementName).getDeclaration();
  }


  public XmlElementDescriptor[] getElements() {
    return buildDeclarationMap().values().toArray(XmlElementDescriptor.EMPTY_ARRAY);
  }

  private Map<String,XmlElementDescriptor> buildDeclarationMap() {
    if (myCachedDecls == null) {
      synchronized (PsiLock.LOCK) {
        if (myCachedDecls == null) {
          myCachedDecls = myElement.getManager().getCachedValuesManager().createCachedValue(new CachedValueProvider<Map<String, XmlElementDescriptor>>() {
            public Result<Map<String, XmlElementDescriptor>> compute() {
              final List<XmlElementDecl> result = new ArrayList<XmlElementDecl>();
              myElement.processElements(new FilterElementProcessor(new ClassFilter(XmlElementDecl.class), result), getDeclaration());
              final Map<String, XmlElementDescriptor> ret = new LinkedHashMap<String, XmlElementDescriptor>((int)(result.size() * 1.5));
              for (final XmlElementDecl xmlElementDecl : result) {
                final XmlElement nameElement = xmlElementDecl.getNameElement();
                if (nameElement != null) {
                  String text = nameElement.getText();
                  if (!ret.containsKey(text)) {
                    ret.put(text, new XmlElementDescriptorImpl(xmlElementDecl));
                  }
                }
              }
              return new Result<Map<String, XmlElementDescriptor>>(ret, myDescriptorFile);
             }
           }, false);
        }
      }
    }
    
    return myCachedDecls.getValue();
  }

  public XmlElementDescriptor getElementDescriptor(XmlTag tag) {
    String name = tag.getName();
    return getElementDescriptor(name);
  }

  public XmlElementDescriptor[] getRootElementsDescriptors(final XmlDocument document) {
    // Suggest more appropriate variant if DOCTYPE <element_name> exists
    final XmlProlog prolog = document != null ? document.getProlog():null;

    if (prolog != null) {
      final XmlDoctype doctype = prolog.getDoctype();

      if (doctype != null) {
        final XmlElement element = doctype.getNameElement();

        if (element != null) {
          final XmlElementDescriptor descriptor = getElementDescriptor(element.getText());

          if (descriptor != null) return new XmlElementDescriptor[] {descriptor};
        }
      }
    }

    return getElements();
  }

{'fraction_non_alphanumeric': 0.09921563231044447, 'fraction_numerical': 0.0012384752992981973, 'mean_word_length': 3.676962676962677, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3399242', 'n_tokens_mistral': 2070, 'n_tokens_neox': 1999, 'n_words': 390}