<commit_before>/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.xml.util;

import com.intellij.openapi.util.NullableComputable;
import com.intellij.openapi.util.RecursionManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.xmlb.JDOMXIncluder;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.regex.Matcher;

/**
* @author peter
*/
class InclusionProvider implements CachedValueProvider<PsiElement[]> {
  private final XmlTag myXincludeTag;

  public InclusionProvider(XmlTag xincludeTag) {
    myXincludeTag = xincludeTag;
  }

  @Nonnull
  public static PsiElement[] getIncludedTags(XmlTag xincludeTag) {
    return CachedValuesManager.getManager(xincludeTag.getProject()).getCachedValue(xincludeTag, new InclusionProvider(xincludeTag));
  }

  public Result<PsiElement[]> compute() {
    PsiElement[] result = RecursionManager.doPreventingRecursion(myXincludeTag, true, new NullableComputable<PsiElement[]>() {
      @Override
      public PsiElement[] compute() {
        return computeInclusion(myXincludeTag);
      }
    });
    return Result.create(result == null ? PsiElement.EMPTY_ARRAY : result, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
  }

  private static XmlTag[] extractXpointer(@Nonnull XmlTag rootTag, @Nullable final String xpointer) {
    if (xpointer != null) {
      Matcher matcher = JDOMXIncluder.XPOINTER_PATTERN.matcher(xpointer);
      if (matcher.matches()) {
        String pointer = matcher.group(1);
        matcher = JDOMXIncluder.CHILDREN_PATTERN.matcher(pointer);
        if (matcher.matches() && matcher.group(1).equals(rootTag.getName())) {
          return rootTag.getSubTags();
        }
      }
    }

    return new XmlTag[]{rootTag};
  }

  @Nullable
  private static PsiElement[] computeInclusion(final XmlTag xincludeTag) {
    final XmlFile included = XmlIncludeHandler.resolveXIncludeFile(xincludeTag);
    final XmlDocument document = included != null ? included.getDocument() : null;
    final XmlTag rootTag = document != null ? document.getRootTag() : null;
    if (rootTag != null) {
      final String xpointer = xincludeTag.getAttributeValue("xpointer", XmlUtil.XINCLUDE_URI);
      final XmlTag[] includeTag = extractXpointer(rootTag, xpointer);
      PsiElement[] result = new PsiElement[includeTag.length];
      for (int i = 0; i < includeTag.length; i++) {
        result[i] = new IncludedXmlTag(includeTag[i], xincludeTag.getParentTag());
      }
      return result;
    }

    return null;
  }

}<commit_msg>drop usage of jdomincluder<commit_after>/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.xml.util;

import com.intellij.openapi.util.NullableComputable;
import com.intellij.openapi.util.RecursionManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.psi.xml.XmlDocument;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author peter
 */
class InclusionProvider implements CachedValueProvider<PsiElement[]>
{
	//xpointer($1)
	public static Pattern XPOINTER_PATTERN = Pattern.compile("xpointer\\((.*)\\)");

	// /$1(/$2)?/*
	public static Pattern CHILDREN_PATTERN = Pattern.compile("/([^/]*)(/[^/]*)?/\\*");

	private final XmlTag myXincludeTag;

	public InclusionProvider(XmlTag xincludeTag)
	{
		myXincludeTag = xincludeTag;
	}

	@Nonnull
	public static PsiElement[] getIncludedTags(XmlTag xincludeTag)
	{
		return CachedValuesManager.getManager(xincludeTag.getProject()).getCachedValue(xincludeTag, new InclusionProvider(xincludeTag));
	}

	public Result<PsiElement[]> compute()
	{
		PsiElement[] result = RecursionManager.doPreventingRecursion(myXincludeTag, true, new NullableComputable<PsiElement[]>()
		{
			@Override
			public PsiElement[] compute()
			{
				return computeInclusion(myXincludeTag);
			}
		});
		return Result.create(result == null ? PsiElement.EMPTY_ARRAY : result, PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
	}

	private static XmlTag[] extractXpointer(@Nonnull XmlTag rootTag, @Nullable final String xpointer)
	{
		if(xpointer != null)
		{
			Matcher matcher = XPOINTER_PATTERN.matcher(xpointer);
			if(matcher.matches())
			{
				String pointer = matcher.group(1);
				matcher = CHILDREN_PATTERN.matcher(pointer);
				if(matcher.matches() && matcher.group(1).equals(rootTag.getName()))
				{
					return rootTag.getSubTags();
				}
			}
		}

		return new XmlTag[]{rootTag};
	}

	@Nullable
	private static PsiElement[] computeInclusion(final XmlTag xincludeTag)
	{
		final XmlFile included = XmlIncludeHandler.resolveXIncludeFile(xincludeTag);
		final XmlDocument document = included != null ? included.getDocument() : null;
		final XmlTag rootTag = document != null ? document.getRootTag() : null;
		if(rootTag != null)
		{
			final String xpointer = xincludeTag.getAttributeValue("xpointer", XmlUtil.XINCLUDE_URI);
			final XmlTag[] includeTag = extractXpointer(rootTag, xpointer);
			PsiElement[] result = new PsiElement[includeTag.length];
			for(int i = 0; i < includeTag.length; i++)
			{
				result[i] = new IncludedXmlTag(includeTag[i], xincludeTag.getParentTag());
			}
			return result;
		}

		return null;
	}

}
{'fraction_non_alphanumeric': 0.10981308411214953, 'fraction_numerical': 0.0048189252336448595, 'mean_word_length': 4.487980769230769, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 9}, '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': '2655054', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2095, 'n_words': 500}