<commit_before>/*
 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.psi.impl.source.resolve.reference.impl.providers;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.psi.impl.source.jsp.jspJava.JspXmlTagBase;
import com.intellij.psi.impl.source.resolve.reference.PsiReferenceProviderBase;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.jsp.el.ELExpressionHolder;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlTag;
import com.intellij.lang.StdLanguages;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
 * @author peter
 */
public class IdReferenceProvider extends PsiReferenceProviderBase {
  @NonNls public static final String FOR_ATTR_NAME = "for";
  @NonNls public static final String ID_ATTR_NAME = "id";
  @NonNls public static final String NAME_ATTR_NAME = "name";
  private static THashSet<String> ourNamespacesWithoutIdRefs = new THashSet<String>();
  static {
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.JSP_URI );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_BEAN_URI );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_BEAN_URI2 );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_LOGIC_URI );
    ourNamespacesWithoutIdRefs.add( "http://struts.apache.org/tags-tiles" );
    for(String s: MetaRegistry.SCHEMA_URIS) ourNamespacesWithoutIdRefs.add( s );
  }

  public String[] getIdForAttributeNames() {
    return new String[]{FOR_ATTR_NAME, ID_ATTR_NAME, NAME_ATTR_NAME};
  }

  public ElementFilter getIdForFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        final PsiElement grandParent = ((PsiElement)element).getParent().getParent();
        if (grandParent instanceof XmlTag) {
          final XmlTag tag = (XmlTag)grandParent;
          final String nsPrefix = tag.getNamespacePrefix();

          if (nsPrefix.length() > 0) {
            final String s = tag.getNamespace();
            return !ourNamespacesWithoutIdRefs.contains(s) && grandParent.getLanguage() != StdLanguages.XML;
          }
        }
        return false;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  @NotNull
  public PsiReference[] getReferencesByElement(PsiElement element) {
    if (element instanceof XmlAttributeValue) {
      if (PsiTreeUtil.getChildOfAnyType(element, JspXmlTagBase.class, ELExpressionHolder.class) != null) {
        return PsiReference.EMPTY_ARRAY;
      }

      final PsiElement parentElement = element.getParent();
      if (!(parentElement instanceof XmlAttribute)) return PsiReference.EMPTY_ARRAY;
      final String name = ((XmlAttribute)parentElement).getName();

      if (FOR_ATTR_NAME.equals(name)) {
        return new PsiReference[]{new IdRefReference(element, 1)};
      }
      else if (ID_ATTR_NAME.equals(name) || NAME_ATTR_NAME.equals(name)) {
        return new PsiReference[]{new GlobalAttributeValueSelfReference(element)};
      }
    }
    return PsiReference.EMPTY_ARRAY;
  }

  public static class GlobalAttributeValueSelfReference extends AttributeValueSelfReference {

    public GlobalAttributeValueSelfReference(final PsiElement element) {
      super(element);<commit_msg>JSP validation problem when using struts form element and HTML <label> tag (IDEADEV-15917)<commit_after>/*
 * Copyright (c) 2000-2005 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.psi.impl.source.resolve.reference.impl.providers;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.filters.ElementFilter;
import com.intellij.psi.impl.source.jsp.jspJava.JspXmlTagBase;
import com.intellij.psi.impl.source.resolve.reference.PsiReferenceProviderBase;
import com.intellij.psi.impl.meta.MetaRegistry;
import com.intellij.psi.jsp.el.ELExpressionHolder;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlTag;
import com.intellij.lang.StdLanguages;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/**
 * @author peter
 */
public class IdReferenceProvider extends PsiReferenceProviderBase {
  @NonNls public static final String FOR_ATTR_NAME = "for";
  @NonNls public static final String ID_ATTR_NAME = "id";
  @NonNls public static final String STYLE_ID_ATTR_NAME = "styleId";
  @NonNls public static final String NAME_ATTR_NAME = "name";
  private static THashSet<String> ourNamespacesWithoutIdRefs = new THashSet<String>();
  static {
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.JSP_URI );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_BEAN_URI );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_BEAN_URI2 );
    ourNamespacesWithoutIdRefs.add( com.intellij.xml.util.XmlUtil.STRUTS_LOGIC_URI );
    ourNamespacesWithoutIdRefs.add( "http://struts.apache.org/tags-tiles" );
    for(String s: MetaRegistry.SCHEMA_URIS) ourNamespacesWithoutIdRefs.add( s );
  }

  public String[] getIdForAttributeNames() {
    return new String[]{FOR_ATTR_NAME, ID_ATTR_NAME, NAME_ATTR_NAME,STYLE_ID_ATTR_NAME};
  }

  public ElementFilter getIdForFilter() {
    return new ElementFilter() {
      public boolean isAcceptable(Object element, PsiElement context) {
        final PsiElement grandParent = ((PsiElement)element).getParent().getParent();
        if (grandParent instanceof XmlTag) {
          final XmlTag tag = (XmlTag)grandParent;
          final String nsPrefix = tag.getNamespacePrefix();

          if (nsPrefix.length() > 0) {
            final String s = tag.getNamespace();
            return !ourNamespacesWithoutIdRefs.contains(s) && grandParent.getLanguage() != StdLanguages.XML;
          }
        }
        return false;
      }

      public boolean isClassAcceptable(Class hintClass) {
        return true;
      }
    };
  }

  @NotNull
  public PsiReference[] getReferencesByElement(PsiElement element) {
    if (element instanceof XmlAttributeValue) {
      if (PsiTreeUtil.getChildOfAnyType(element, JspXmlTagBase.class, ELExpressionHolder.class) != null) {
        return PsiReference.EMPTY_ARRAY;
      }

      final PsiElement parentElement = element.getParent();
      if (!(parentElement instanceof XmlAttribute)) return PsiReference.EMPTY_ARRAY;
      final String name = ((XmlAttribute)parentElement).getName();

      if (FOR_ATTR_NAME.equals(name)) {
        return new PsiReference[]{new IdRefReference(element, 1)};
      }
      else if (ID_ATTR_NAME.equals(name) ||
               NAME_ATTR_NAME.equals(name) ||
               STYLE_ID_ATTR_NAME.equals(name)
              ) {
        return new PsiReference[]{new GlobalAttributeValueSelfReference(element)};
      }
    }
    return PsiReference.EMPTY_ARRAY;
  }

  public static class GlobalAttributeValueSelfReference extends AttributeValueSelfReference {

    public GlobalAttributeValueSelfReference(final PsiElement element) {
      super(element);
{'fraction_non_alphanumeric': 0.09977622745820719, 'fraction_numerical': 0.0035540344872976174, 'mean_word_length': 4.085676037483267, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '5836543', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2319, 'n_words': 445}