<commit_before>/*
 * Copyright 2000-2010 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 org.jetbrains.idea.maven.dom;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.hash.HashSet;
import com.intellij.util.xml.DomUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.model.*;
import org.jetbrains.idea.maven.project.*;
import org.jetbrains.idea.maven.utils.MavenUtil;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

public class MavenDomProjectProcessorUtils {
  private MavenDomProjectProcessorUtils() {
  }

  @Nullable
  public static XmlTag searchProperty(@NotNull final String propertyName,
                                      @NotNull MavenDomProjectModel projectDom,
                                      @NotNull final Project project) {
    final XmlTag[] property = new XmlTag[]{null};

    Processor<MavenDomProperties> searchProcessor = new Processor<MavenDomProperties>() {
      public boolean process(MavenDomProperties mavenDomProperties) {
        XmlTag propertiesTag = mavenDomProperties.getXmlTag();
        if (propertiesTag != null) {
          for (XmlTag each : propertiesTag.getSubTags()) {
            if (each.getName().equals(propertyName)) {
              property[0] = each;
              return true;
            }
          }
        }
        return false;
      }
    };

    processProperties(projectDom, searchProcessor, project);

    return property[0];
  }

  @Nullable
  public static Set<XmlTag> collectProperties(@NotNull MavenDomProjectModel projectDom, @NotNull final Project project) {
    final Set<XmlTag> properties = new HashSet<XmlTag>();

    Processor<MavenDomProperties> collectProcessor = new Processor<MavenDomProperties>() {
      public boolean process(MavenDomProperties mavenDomProperties) {
        XmlTag propertiesTag = mavenDomProperties.getXmlTag();
        if (propertiesTag != null) {
          properties.addAll(Arrays.asList(propertiesTag.getSubTags()));
        }
        return false;
      }
    };

    processProperties(projectDom, collectProcessor, project);

    return properties;
  }

  @NotNull
  public static Set<MavenDomDependency> searchDependencyUsages(@NotNull final MavenDomDependency dependency,
                                                               @NotNull final Project project) {
    final Set<MavenDomDependency> usages = new HashSet<MavenDomDependency>();

    final MavenDomProjectModel model = dependency.getParentOfType(MavenDomProjectModel.class, false);
    if (model != null) {
      final String artifactId = dependency.getArtifactId().getStringValue();
      final String groupId = dependency.getGroupId().getStringValue();
      if (artifactId != null && groupId != null) {
        Processor<MavenDomProjectModel> collectProcessor = new Processor<MavenDomProjectModel>() {
          public boolean process(MavenDomProjectModel mavenDomProjectModel) {
            if (!model.equals(mavenDomProjectModel)) {
              for (MavenDomDependency domDependency : mavenDomProjectModel.getDependencies().getDependencies()) {
                if (domDependency.equals(dependency)) continue;
                if (artifactId.equals(domDependency.getArtifactId().getStringValue()) &&
                    groupId.equals(domDependency.getGroupId().getStringValue())) {
                  usages.add(domDependency);
                }
              }
            }
            return false;
          }
        };

        processProjectDependenciesRecursively(model, collectProcessor, project);
      }
    }

    return usages;
  }

  private static void processProjectDependenciesRecursively(@Nullable MavenDomProjectModel model,
                                                            @NotNull Processor<MavenDomProjectModel> processor,
                                                            @NotNull Project project) {
    if (model != null) {
      if (processor.process(model)) return;

      MavenProject mavenProject = MavenDomUtil.findProject(model);
      if (mavenProject != null) {
        for (MavenProject childProject : MavenProjectsManager.getInstance(project).findInheritors(mavenProject)) {
          MavenDomProjectModel childProjectModel = MavenDomUtil.getMavenDomProjectModel(project, childProject.getFile());

          processProjectDependenciesRecursively(childProjectModel, processor, project);
        }
      }
    }
  }

  @NotNull
  public static Set<MavenDomDependency> collectManagingDependencies(@NotNull MavenDomProjectModel model) {
    final Set<MavenDomDependency> dependencies = new HashSet<MavenDomDependency>();

    Processor<MavenDomDependencies> collectProcessor = new Processor<MavenDomDependencies>() {
      public boolean process(MavenDomDependencies mavenDomDependencies) {
        dependencies.addAll(mavenDomDependencies.getDependencies());
        return false;
      }
    };

    processDependenciesInDependencyManagement(model, collectProcessor, model.getManager().getProject());

    return dependencies;
  }<commit_msg>maven:IDEA-52894<commit_after>/*
 * Copyright 2000-2010 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 org.jetbrains.idea.maven.dom;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.Function;
import com.intellij.util.Processor;
import com.intellij.util.containers.hash.HashSet;
import com.intellij.util.xml.DomUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.model.*;
import org.jetbrains.idea.maven.project.*;
import org.jetbrains.idea.maven.utils.MavenUtil;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

public class MavenDomProjectProcessorUtils {
  private MavenDomProjectProcessorUtils() {
  }

  @Nullable
  public static XmlTag searchProperty(@NotNull final String propertyName,
                                      @NotNull MavenDomProjectModel projectDom,
                                      @NotNull final Project project) {
    final XmlTag[] property = new XmlTag[]{null};

    Processor<MavenDomProperties> searchProcessor = new Processor<MavenDomProperties>() {
      public boolean process(MavenDomProperties mavenDomProperties) {
        XmlTag propertiesTag = mavenDomProperties.getXmlTag();
        if (propertiesTag != null) {
          for (XmlTag each : propertiesTag.getSubTags()) {
            if (each.getName().equals(propertyName)) {
              property[0] = each;
              return true;
            }
          }
        }
        return false;
      }
    };

    processProperties(projectDom, searchProcessor, project);

    return property[0];
  }

  @Nullable
  public static Set<XmlTag> collectProperties(@NotNull MavenDomProjectModel projectDom, @NotNull final Project project) {
    final Set<XmlTag> properties = new HashSet<XmlTag>();

    Processor<MavenDomProperties> collectProcessor = new Processor<MavenDomProperties>() {
      public boolean process(MavenDomProperties mavenDomProperties) {
        XmlTag propertiesTag = mavenDomProperties.getXmlTag();
        if (propertiesTag != null) {
          properties.addAll(Arrays.asList(propertiesTag.getSubTags()));
        }
        return false;
      }
    };

    processProperties(projectDom, collectProcessor, project);

    return properties;
  }

  @NotNull
  public static Set<MavenDomDependency> searchDependencyUsages(@NotNull final MavenDomDependency dependency,
                                                               @NotNull final Project project) {
    final Set<MavenDomDependency> usages = new HashSet<MavenDomDependency>();

    final MavenDomProjectModel model = dependency.getParentOfType(MavenDomProjectModel.class, false);
    if (model != null) {
      final String artifactId = dependency.getArtifactId().getStringValue();
      final String groupId = dependency.getGroupId().getStringValue();
      if (artifactId != null && groupId != null) {
        Processor<MavenDomProjectModel> collectProcessor = new Processor<MavenDomProjectModel>() {
          public boolean process(MavenDomProjectModel mavenDomProjectModel) {
            if (!model.equals(mavenDomProjectModel)) {
              for (MavenDomDependency domDependency : mavenDomProjectModel.getDependencies().getDependencies()) {
                if (domDependency.equals(dependency)) continue;
                if (artifactId.equals(domDependency.getArtifactId().getStringValue()) &&
                    groupId.equals(domDependency.getGroupId().getStringValue())) {
                  usages.add(domDependency);
                }
              }
            }
            return false;
          }
        };

        processProjectDependenciesRecursively(model, collectProcessor, project, new HashSet<MavenDomProjectModel>());
      }
    }

    return usages;
  }

  private static void processProjectDependenciesRecursively(@Nullable MavenDomProjectModel model,
                                                            @NotNull Processor<MavenDomProjectModel> processor,
                                                            @NotNull Project project,
                                                            @NotNull Set<MavenDomProjectModel> processedModels) {
    if (model != null && !processedModels.contains(model)) {
      processedModels.add(model);

      if (processor.process(model)) return;

      MavenProject mavenProject = MavenDomUtil.findProject(model);
      if (mavenProject != null) {
        for (MavenProject childProject : MavenProjectsManager.getInstance(project).findInheritors(mavenProject)) {
          MavenDomProjectModel childProjectModel = MavenDomUtil.getMavenDomProjectModel(project, childProject.getFile());

          processProjectDependenciesRecursively(childProjectModel, processor, project, processedModels);
        }
      }
    }
  }

  @NotNull
  public static Set<MavenDomDependency> collectManagingDependencies(@NotNull MavenDomProjectModel model) {
    final Set<MavenDomDependency> dependencies = new HashSet<MavenDomDependency>();

    Processor<MavenDomDependencies> collectProcessor = new Processor<MavenDomDependencies>() {
      public boolean process(MavenDomDependencies mavenDomDependencies) {
        dependencies.addAll(mavenDomDependencies.getDependencies());
        return false;
      }
    };

    processDependenciesInDependencyManagement(model, collectProcessor, model.getManager().getProject());

    return dependencies;
  }
{'fraction_non_alphanumeric': 0.08737864077669903, 'fraction_numerical': 0.002761968530297958, 'mean_word_length': 3.0313765182186234, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4945036', 'n_tokens_mistral': 3086, 'n_tokens_neox': 2928, 'n_words': 688}