<commit_before>/*
 * Copyright 2000-2014 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.html.impl.util;

import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.XmlRecursiveElementVisitor;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.PairFunction;
import com.intellij.util.text.StringTokenizer;
import com.intellij.xml.util.HtmlUtil;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author: Fedor.Korotkov
 */
public class MicrodataUtil {
  public static final Key<List<String>> ITEM_PROP_KEYS = Key.create("microdata.prop");
  public static final String ITEM_REF = "itemref";
  public static final String ITEM_SCOPE = "itemscope";
  public static final String ITEM_TYPE = "itemtype";
  public static final String ITEM_PROP = "itemprop";
  public static final String ITEM_ID = "itemid";

  public static boolean hasScopeTag(@Nullable XmlTag tag) {
    return findScopeTag(tag) != null;
  }

  @Nullable
  public static XmlTag findScopeTag(@Nullable XmlTag context) {
    Map<String, XmlTag> id2tag = findScopesWithItemRef(context != null ? context.getContainingFile() : null);
    XmlTag tag = context;
    while (tag != null) {
      if (tag != context && tag.getAttribute(ITEM_SCOPE) != null) return tag;
      final String id = getStripedAttributeValue(tag, HtmlUtil.ID_ATTRIBUTE_NAME);
      if (id != null && id2tag.containsKey(id)) return id2tag.get(id);
      tag = tag.getParentTag();
    }
    return null;
  }

  private static Map<String, XmlTag> findScopesWithItemRef(@Nullable PsiFile file) {
    if (!(file instanceof XmlFile)) return Collections.emptyMap();
    final Map<String, XmlTag> result = new THashMap<String, XmlTag>();
    file.accept(new XmlRecursiveElementVisitor() {
      @Override
      public void visitXmlTag(final XmlTag tag) {
        super.visitXmlTag(tag);
        XmlAttribute refAttr = tag.getAttribute(ITEM_REF);
        if (refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) {
          getReferencesForAttributeValue(refAttr.getValueElement(), new PairFunction<String, Integer, PsiReference>() {
            @Nullable
            @Override
            public PsiReference fun(String t, Integer v) {
              result.put(t, tag);
              return null;
            }
          });
        }
      }
    });
    return result;
  }

  public static List<String> extractProperties(PsiFile file, String type) {
    final VirtualFile virtualFile = file.getVirtualFile();
    List<String> result = virtualFile != null ? virtualFile.getUserData(ITEM_PROP_KEYS) : null;
    if (virtualFile != null && result == null) {
      result = collectNames(file, type);
      virtualFile.putUserData(ITEM_PROP_KEYS, result);
    }
    return result;
  }

  private static List<String> collectNames(PsiFile file, String type) {
    if (file instanceof XmlFile) {
      final CollectNamesVisitor collectNamesVisitor = getVisitorByType(type);
      file.accept(collectNamesVisitor);
      return collectNamesVisitor.getValues();
    }
    return Collections.emptyList();
  }

  private static CollectNamesVisitor getVisitorByType(String type) {<commit_msg>cache itemscope tag #WEB-19192 fixed<commit_after>/*
 * Copyright 2000-2014 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.html.impl.util;

import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.XmlRecursiveElementVisitor;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.DependentNSReference;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.URLReference;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.PairFunction;
import com.intellij.util.text.StringTokenizer;
import com.intellij.xml.util.HtmlUtil;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author: Fedor.Korotkov
 */
public class MicrodataUtil {
  public static final Key<List<String>> ITEM_PROP_KEYS = Key.create("microdata.prop");
  public static final String ITEM_REF = "itemref";
  public static final String ITEM_SCOPE = "itemscope";
  public static final String ITEM_TYPE = "itemtype";
  public static final String ITEM_PROP = "itemprop";
  public static final String ITEM_ID = "itemid";

  public static boolean hasScopeTag(@Nullable XmlTag tag) {
    return findScopeTag(tag) != null;
  }

  @Nullable
  public static XmlTag findScopeTag(@Nullable XmlTag context) {
    Map<String, XmlTag> id2tag = findScopesWithItemRef(context != null ? context.getContainingFile() : null);
    XmlTag tag = context;
    while (tag != null) {
      if (tag != context && tag.getAttribute(ITEM_SCOPE) != null) return tag;
      final String id = getStripedAttributeValue(tag, HtmlUtil.ID_ATTRIBUTE_NAME);
      if (id != null && id2tag.containsKey(id)) return id2tag.get(id);
      tag = tag.getParentTag();
    }
    return null;
  }

  private static Map<String, XmlTag> findScopesWithItemRef(@Nullable final PsiFile file) {
    if (!(file instanceof XmlFile)) return Collections.emptyMap();
    return CachedValuesManager.getCachedValue(file, new CachedValueProvider<Map<String, XmlTag>>() {
      @Nullable
      @Override
      public Result<Map<String, XmlTag>> compute() {
        final Map<String, XmlTag> result = new THashMap<String, XmlTag>();
        file.accept(new XmlRecursiveElementVisitor() {
          @Override
          public void visitXmlTag(final XmlTag tag) {
            super.visitXmlTag(tag);
            XmlAttribute refAttr = tag.getAttribute(ITEM_REF);
            if (refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) {
              getReferencesForAttributeValue(refAttr.getValueElement(), new PairFunction<String, Integer, PsiReference>() {
                @Nullable
                @Override
                public PsiReference fun(String t, Integer v) {
                  result.put(t, tag);
                  return null;
                }
              });
            }
          }
        });
        return Result.create(result, file);
      }
    });
  }

  public static List<String> extractProperties(PsiFile file, String type) {
    final VirtualFile virtualFile = file.getVirtualFile();
    List<String> result = virtualFile != null ? virtualFile.getUserData(ITEM_PROP_KEYS) : null;
    if (virtualFile != null && result == null) {
      result = collectNames(file, type);
      virtualFile.putUserData(ITEM_PROP_KEYS, result);
    }
    return result;
  }

  private static List<String> collectNames(PsiFile file, String type) {
    if (file instanceof XmlFile) {
      final CollectNamesVisitor collectNamesVisitor = getVisitorByType(type);
      file.accept(collectNamesVisitor);
      return collectNamesVisitor.getValues();
    }
    return Collections.emptyList();
  }

  private static CollectNamesVisitor getVisitorByType(String type) {
{'fraction_non_alphanumeric': 0.10233333333333333, 'fraction_numerical': 0.0038888888888888888, 'mean_word_length': 3.7548864236661386, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '938495', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2640, 'n_words': 679}