<commit_before>package com.intellij.psi.impl.source.xml;

import com.intellij.ant.impl.dom.xmlBridge.AntDOMNSDescriptor;
import com.intellij.j2ee.ExternalResourceManager;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.pom.PomModel;
import com.intellij.pom.event.PomModelEvent;
import com.intellij.pom.impl.PomTransactionBase;
import com.intellij.pom.xml.XmlAspect;
import com.intellij.pom.xml.impl.events.XmlDocumentChangedImpl;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.impl.source.html.dtd.HtmlNSDescriptorImpl;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.impl.CachedValueImpl;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.xml.*;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.XmlNSDescriptor;
import com.intellij.xml.util.XmlNSDescriptorSequence;
import com.intellij.xml.util.XmlUtil;
import gnu.trove.TObjectIntHashMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mike
 */
public class XmlDocumentImpl extends XmlElementImpl implements XmlDocument {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.xml.XmlDocumentImpl");

  public XmlDocumentImpl() {
    this(XML_DOCUMENT);
  }

  protected XmlDocumentImpl(IElementType type) {
    super(type);
  }

  public void accept(PsiElementVisitor visitor) {
    visitor.visitXmlDocument(this);
  }

  public int getChildRole(ASTNode child) {
    LOG.assertTrue(child.getTreeParent() == this);
    IElementType i = child.getElementType();
    if (i == XML_PROLOG) {
      return ChildRole.XML_PROLOG;
    }
    else if (i == XML_TAG) {
      return ChildRole.XML_TAG;
    }
    else {
      return ChildRole.NONE;
    }
  }

  public XmlProlog getProlog() {
    return (XmlProlog)findElementByTokenType(XML_PROLOG);
  }

  public XmlTag getRootTag() {
    return (XmlTag)findElementByTokenType(XML_TAG);
  }

  public XmlNSDescriptor getRootTagNSDescriptor() {
    XmlTag rootTag = getRootTag();
    return rootTag != null ? rootTag.getNSDescriptor(rootTag.getNamespace(), false) : null;
  }

  private final Map<String, CachedValue<XmlNSDescriptor>> myDefaultDescriptorsCache = new HashMap<String, CachedValue<XmlNSDescriptor>>();

  public void clearCaches() {
    myDefaultDescriptorsCache.clear();
    super.clearCaches();
  }

  public XmlNSDescriptor getDefaultNSDescriptor(final String namespace, final boolean strict) {
    if(myDefaultDescriptorsCache.containsKey(namespace)) return myDefaultDescriptorsCache.get(namespace).getValue();
    final XmlNSDescriptor defaultNSDescriptorInner = getDefaultNSDescriptorInner(namespace, strict);
    myDefaultDescriptorsCache.put(namespace, new CachedValueImpl<XmlNSDescriptor>(getManager(), new CachedValueProvider<XmlNSDescriptor>(){
      public Result<XmlNSDescriptor> compute() {
        return new Result<XmlNSDescriptor>(defaultNSDescriptorInner, defaultNSDescriptorInner.getDependences());
      }
    }, false));
    return defaultNSDescriptorInner;
  }

  public XmlNSDescriptor getDefaultNSDescriptorInner(final String namespace, final boolean strict) {
    final XmlFile containingFile = XmlUtil.getContainingFile(this);
    if (XmlUtil.ANT_URI.equals(namespace)){
      final AntDOMNSDescriptor antDOMNSDescriptor = new AntDOMNSDescriptor();
      antDOMNSDescriptor.init(this);
      return antDOMNSDescriptor;
    }
    else if(XmlUtil.HTML_URI.equals(namespace)){
      final XmlNSDescriptor xhtmlNSDescriptor = getDefaultNSDescriptor(XmlUtil.XHTML_URI, false);
      final XmlNSDescriptor htmlDescriptor = new HtmlNSDescriptorImpl(xhtmlNSDescriptor);
      return htmlDescriptor;
    }
    else if(namespace != XmlUtil.EMPTY_URI){
      final XmlFile xmlFile = XmlUtil.findXmlFile(containingFile,
                                                  ExternalResourceManager.getInstance().getResourceLocation(namespace));
      if(xmlFile != null){
        final XmlNSDescriptor descriptor = (XmlNSDescriptor)xmlFile.getDocument().getMetaData();
        return descriptor;
      }
    }
    if(strict) return null;

    final XmlDoctype doctype = getProlog().getDoctype();

    if (doctype != null){
      XmlNSDescriptor descr = null;<commit_msg>for ant files we have to support full change tracking mechanism.<commit_after>package com.intellij.psi.impl.source.xml;

import com.intellij.ant.impl.dom.xmlBridge.AntDOMNSDescriptor;
import com.intellij.j2ee.ExternalResourceManager;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.pom.PomModel;
import com.intellij.pom.event.PomModelEvent;
import com.intellij.pom.impl.PomTransactionBase;
import com.intellij.pom.xml.XmlAspect;
import com.intellij.pom.xml.impl.events.XmlDocumentChangedImpl;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiRecursiveElementVisitor;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.impl.source.html.dtd.HtmlNSDescriptorImpl;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.TreeElement;
import com.intellij.psi.impl.CachedValueImpl;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.xml.*;
import com.intellij.util.IncorrectOperationException;
import com.intellij.xml.XmlNSDescriptor;
import com.intellij.xml.util.XmlNSDescriptorSequence;
import com.intellij.xml.util.XmlUtil;
import gnu.trove.TObjectIntHashMap;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mike
 */
public class XmlDocumentImpl extends XmlElementImpl implements XmlDocument {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.xml.XmlDocumentImpl");

  public XmlDocumentImpl() {
    this(XML_DOCUMENT);
  }

  protected XmlDocumentImpl(IElementType type) {
    super(type);
  }

  public void accept(PsiElementVisitor visitor) {
    visitor.visitXmlDocument(this);
  }

  public int getChildRole(ASTNode child) {
    LOG.assertTrue(child.getTreeParent() == this);
    IElementType i = child.getElementType();
    if (i == XML_PROLOG) {
      return ChildRole.XML_PROLOG;
    }
    else if (i == XML_TAG) {
      return ChildRole.XML_TAG;
    }
    else {
      return ChildRole.NONE;
    }
  }

  public XmlProlog getProlog() {
    return (XmlProlog)findElementByTokenType(XML_PROLOG);
  }

  public XmlTag getRootTag() {
    return (XmlTag)findElementByTokenType(XML_TAG);
  }

  public XmlNSDescriptor getRootTagNSDescriptor() {
    XmlTag rootTag = getRootTag();
    return rootTag != null ? rootTag.getNSDescriptor(rootTag.getNamespace(), false) : null;
  }

  private final Map<String, CachedValue<XmlNSDescriptor>> myDefaultDescriptorsCache = new HashMap<String, CachedValue<XmlNSDescriptor>>();

  public void clearCaches() {
    myDefaultDescriptorsCache.clear();
    super.clearCaches();
  }

  public XmlNSDescriptor getDefaultNSDescriptor(final String namespace, final boolean strict) {
    if(myDefaultDescriptorsCache.containsKey(namespace)) return myDefaultDescriptorsCache.get(namespace).getValue();
    myDefaultDescriptorsCache.put(namespace, new CachedValueImpl<XmlNSDescriptor>(getManager(), new CachedValueProvider<XmlNSDescriptor>(){
      public Result<XmlNSDescriptor> compute() {
        final XmlNSDescriptor defaultNSDescriptorInner = getDefaultNSDescriptorInner(namespace, strict);
        return new Result<XmlNSDescriptor>(defaultNSDescriptorInner, defaultNSDescriptorInner.getDependences());
      }
    }, false));
    return getDefaultNSDescriptor(namespace, strict);
  }

  public XmlNSDescriptor getDefaultNSDescriptorInner(final String namespace, final boolean strict) {
    final XmlFile containingFile = XmlUtil.getContainingFile(this);
    if (XmlUtil.ANT_URI.equals(namespace)){
      final AntDOMNSDescriptor antDOMNSDescriptor = new AntDOMNSDescriptor();
      antDOMNSDescriptor.init(this);
      return antDOMNSDescriptor;
    }
    else if(XmlUtil.HTML_URI.equals(namespace)){
      final XmlNSDescriptor xhtmlNSDescriptor = getDefaultNSDescriptor(XmlUtil.XHTML_URI, false);
      final XmlNSDescriptor htmlDescriptor = new HtmlNSDescriptorImpl(xhtmlNSDescriptor);
      return htmlDescriptor;
    }
    else if(namespace != XmlUtil.EMPTY_URI){
      final XmlFile xmlFile = XmlUtil.findXmlFile(containingFile,
                                                  ExternalResourceManager.getInstance().getResourceLocation(namespace));
      if(xmlFile != null){
        final XmlNSDescriptor descriptor = (XmlNSDescriptor)xmlFile.getDocument().getMetaData();
        return descriptor;
      }
    }
    if(strict) return null;

    final XmlDoctype doctype = getProlog().getDoctype();

    if (doctype != null){
      XmlNSDescriptor descr = null;
{'fraction_non_alphanumeric': 0.0982970467773227, 'fraction_numerical': 0.0002155636990730761, 'mean_word_length': 4.682180036742192, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676262', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2897, 'n_words': 492}