<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.inspections;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.Processor;
import com.intellij.util.containers.hash.HashSet;
import com.intellij.util.xml.DomFileElement;
import com.intellij.util.xml.highlighting.BasicDomElementsInspection;
import com.intellij.util.xml.highlighting.DomElementAnnotationHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.MavenDomBundle;
import org.jetbrains.idea.maven.dom.MavenDomProjectProcessorUtils;
import org.jetbrains.idea.maven.dom.MavenDomUtil;
import org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates;
import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
import org.jetbrains.idea.maven.model.MavenId;
import org.jetbrains.idea.maven.project.MavenProject;

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

public class MavenDuplicateDependenciesInspection extends BasicDomElementsInspection<MavenDomProjectModel> {
  public MavenDuplicateDependenciesInspection() {
    super(MavenDomProjectModel.class);
  }

  @Override
  public void checkFileElement(DomFileElement<MavenDomProjectModel> domFileElement,
                               DomElementAnnotationHolder holder) {

    final XmlFile xmlFile = domFileElement.getFile();
    MavenDomProjectModel projectModel = domFileElement.getRootElement();

    checkMavenProjectModel(projectModel, xmlFile, holder);
  }

  private static void checkMavenProjectModel(@NotNull MavenDomProjectModel projectModel,
                                             @NotNull XmlFile xmlFile,
                                             @NotNull DomElementAnnotationHolder holder) {
    final Map<MavenId, Set<MavenDomDependency>> allDuplicates = getDuplicateDependenciesMap(projectModel);

    for (MavenDomDependency dependency : projectModel.getDependencies().getDependencies()) {
      MavenId id = createId(dependency);
      if (id != null) {
        Set<MavenDomDependency> dependencies = allDuplicates.get(id);
        if (dependencies != null && dependencies.size() > 1) {
          addProblem(dependency, dependencies, holder);
        }
      }
    }
  }

  private static void addProblem(@NotNull MavenDomDependency dependency,
                                 @NotNull Set<MavenDomDependency> dependencies,
                                 @NotNull DomElementAnnotationHolder holder) {
    StringBuffer sb = new StringBuffer();
    Set<MavenDomProjectModel> processed = new HashSet<MavenDomProjectModel>();
    for (MavenDomDependency domDependency : dependencies) {
      if (dependency.equals(domDependency)) continue;
      MavenDomProjectModel model = domDependency.getParentOfType(MavenDomProjectModel.class, false);
      if (model != null && !processed.contains(model)) {
        if (processed.size() > 0) sb.append(", ");
        sb.append(createLinkText(model, domDependency));

        processed.add(model);
      }
    }
    holder.createProblem(dependency, HighlightSeverity.WARNING, MavenDomBundle.message("MavenDuplicateDependenciesInspection.has.duplicates", sb.toString()));
  }

  private static String createLinkText(@NotNull MavenDomProjectModel model,@NotNull MavenDomDependency dependency) {
    StringBuffer sb =new StringBuffer();

    XmlTag tag = dependency.getXmlTag();
    if (tag == null) return getProjectName(model);
    VirtualFile file = tag.getContainingFile().getVirtualFile();
    if (file == null) return getProjectName(model);

    sb.append("<a href ='#navigation/");
    sb.append(file.getPath());
    sb.append(":");
    sb.append(tag.getTextRange().getStartOffset());
    sb.append("'>");
    sb.append(getProjectName(model));
    sb.append("</a>");

    return sb.toString(); 
  }

  @NotNull
  private static String getProjectName(MavenDomProjectModel model) {
    MavenProject mavenProject = MavenDomUtil.findProject(model);
    if (mavenProject != null) {
      return mavenProject.getDisplayName();
    } else {
      String name = model.getName().getStringValue();
      if (!StringUtil.isEmptyOrSpaces(name)) {
        return name;
      }
      else {
        return "pom.xml"; // ?
      }
    }
  }

  @NotNull
  private static Map<MavenId, Set<MavenDomDependency>> getDuplicateDependenciesMap(MavenDomProjectModel projectModel) {
    final Map<MavenId, Set<MavenDomDependency>> allDependencies = new HashMap<MavenId, Set<MavenDomDependency>>();

    Processor<MavenDomProjectModel> collectProcessor = new Processor<MavenDomProjectModel>() {
      public boolean process(MavenDomProjectModel model) {
        for (MavenDomDependency dependency : model.getDependencies().getDependencies()) {
          MavenId mavenId = createId(dependency);
          if (mavenId != null) {
            if (allDependencies.containsKey(mavenId)) {
              allDependencies.get(mavenId).add(dependency);
            }
            else {
              Set<MavenDomDependency> dependencies = new HashSet<MavenDomDependency>();
              dependencies.add(dependency);
              allDependencies.put(mavenId, dependencies);
            }
          }
        }
        return false;
      }
    };

    MavenDomProjectProcessorUtils.processChildrenRecursively(projectModel, collectProcessor, true);
    MavenDomProjectProcessorUtils.processParentProjects(projectModel, collectProcessor);

    return allDependencies;
  }

  @Nullable
  private static MavenId createId(MavenDomArtifactCoordinates coordinates) {
    String groupId = coordinates.getGroupId().getStringValue();
    String artifactId = coordinates.getArtifactId().getStringValue();
    String version = coordinates.getVersion().getStringValue();

    if (StringUtil.isEmptyOrSpaces(groupId) || StringUtil.isEmptyOrSpaces(artifactId)) return null;

    return new MavenId(groupId, artifactId, version);

  }

  @NotNull
  public String getGroupDisplayName() {
    return MavenDomBundle.message("inspection.group");
  }

  @NotNull
  public String getDisplayName() {
    return MavenDomBundle.message("inspection.duplicate.dependencies.name");<commit_msg>maven: erroneous 'duplicate dependency' fixed<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.inspections;

import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.Processor;
import com.intellij.util.containers.hash.HashSet;
import com.intellij.util.xml.DomFileElement;
import com.intellij.util.xml.highlighting.BasicDomElementsInspection;
import com.intellij.util.xml.highlighting.DomElementAnnotationHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.dom.MavenDomBundle;
import org.jetbrains.idea.maven.dom.MavenDomProjectProcessorUtils;
import org.jetbrains.idea.maven.dom.MavenDomUtil;
import org.jetbrains.idea.maven.dom.model.MavenDomDependency;
import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
import org.jetbrains.idea.maven.project.MavenProject;

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

public class MavenDuplicateDependenciesInspection extends BasicDomElementsInspection<MavenDomProjectModel> {
  public MavenDuplicateDependenciesInspection() {
    super(MavenDomProjectModel.class);
  }

  @Override
  public void checkFileElement(DomFileElement<MavenDomProjectModel> domFileElement,
                               DomElementAnnotationHolder holder) {

    final XmlFile xmlFile = domFileElement.getFile();
    MavenDomProjectModel projectModel = domFileElement.getRootElement();

    checkMavenProjectModel(projectModel, xmlFile, holder);
  }

  private static void checkMavenProjectModel(@NotNull MavenDomProjectModel projectModel,
                                             @NotNull XmlFile xmlFile,
                                             @NotNull DomElementAnnotationHolder holder) {
    final Map<String, Set<MavenDomDependency>> allDuplicates = getDuplicateDependenciesMap(projectModel);

    for (MavenDomDependency dependency : projectModel.getDependencies().getDependencies()) {
      String id = createId(dependency);
      if (id != null) {
        Set<MavenDomDependency> dependencies = allDuplicates.get(id);
        if (dependencies != null && dependencies.size() > 1) {
          addProblem(dependency, dependencies, holder);
        }
      }
    }
  }

  private static void addProblem(@NotNull MavenDomDependency dependency,
                                 @NotNull Set<MavenDomDependency> dependencies,
                                 @NotNull DomElementAnnotationHolder holder) {
    StringBuffer sb = new StringBuffer();
    Set<MavenDomProjectModel> processed = new HashSet<MavenDomProjectModel>();
    for (MavenDomDependency domDependency : dependencies) {
      if (dependency.equals(domDependency)) continue;
      MavenDomProjectModel model = domDependency.getParentOfType(MavenDomProjectModel.class, false);
      if (model != null && !processed.contains(model)) {
        if (processed.size() > 0) sb.append(", ");
        sb.append(createLinkText(model, domDependency));

        processed.add(model);
      }
    }
    holder.createProblem(dependency, HighlightSeverity.WARNING, MavenDomBundle.message("MavenDuplicateDependenciesInspection.has.duplicates", sb.toString()));
  }

  private static String createLinkText(@NotNull MavenDomProjectModel model,@NotNull MavenDomDependency dependency) {
    StringBuffer sb =new StringBuffer();

    XmlTag tag = dependency.getXmlTag();
    if (tag == null) return getProjectName(model);
    VirtualFile file = tag.getContainingFile().getVirtualFile();
    if (file == null) return getProjectName(model);

    sb.append("<a href ='#navigation/");
    sb.append(file.getPath());
    sb.append(":");
    sb.append(tag.getTextRange().getStartOffset());
    sb.append("'>");
    sb.append(getProjectName(model));
    sb.append("</a>");

    return sb.toString(); 
  }

  @NotNull
  private static String getProjectName(MavenDomProjectModel model) {
    MavenProject mavenProject = MavenDomUtil.findProject(model);
    if (mavenProject != null) {
      return mavenProject.getDisplayName();
    } else {
      String name = model.getName().getStringValue();
      if (!StringUtil.isEmptyOrSpaces(name)) {
        return name;
      }
      else {
        return "pom.xml"; // ?
      }
    }
  }

  @NotNull
  private static Map<String, Set<MavenDomDependency>> getDuplicateDependenciesMap(MavenDomProjectModel projectModel) {
    final Map<String, Set<MavenDomDependency>> allDependencies = new HashMap<String, Set<MavenDomDependency>>();

    Processor<MavenDomProjectModel> collectProcessor = new Processor<MavenDomProjectModel>() {
      public boolean process(MavenDomProjectModel model) {
        for (MavenDomDependency dependency : model.getDependencies().getDependencies()) {
          String mavenId = createId(dependency);
          if (mavenId != null) {
            if (allDependencies.containsKey(mavenId)) {
              allDependencies.get(mavenId).add(dependency);
            }
            else {
              Set<MavenDomDependency> dependencies = new HashSet<MavenDomDependency>();
              dependencies.add(dependency);
              allDependencies.put(mavenId, dependencies);
            }
          }
        }
        return false;
      }
    };

    MavenDomProjectProcessorUtils.processChildrenRecursively(projectModel, collectProcessor, true);
    MavenDomProjectProcessorUtils.processParentProjects(projectModel, collectProcessor);

    return allDependencies;
  }

  @Nullable
  private static String createId(MavenDomDependency coordinates) {
    String groupId = coordinates.getGroupId().getStringValue();
    String artifactId = coordinates.getArtifactId().getStringValue();

    if (StringUtil.isEmptyOrSpaces(groupId) || StringUtil.isEmptyOrSpaces(artifactId)) return null;

    String version = coordinates.getVersion().getStringValue();
    String type = coordinates.getType().getStringValue();
    String classifier = coordinates.getClassifier().getStringValue();

    return groupId +":" + artifactId + ":" + version + ":" + type + ":" + classifier;

  }

  @NotNull
  public String getGroupDisplayName() {
    return MavenDomBundle.message("inspection.group");
  }

  @NotNull
  public String getDisplayName() {
    return MavenDomBundle.message("inspection.duplicate.dependencies.name");
{'fraction_non_alphanumeric': 0.09971006293755745, 'fraction_numerical': 0.0019800579874124885, 'mean_word_length': 3.824974411463664, 'pattern_counts': {'":': 6, '<': 43, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4946185', 'n_tokens_mistral': 3973, 'n_tokens_neox': 3773, 'n_words': 796}