/**
   * This method attempts to find a matching dependency quickly, by bypassing full resolution of the
   * classpath path group for a plugin. It assumes the following: 1. The dependendency is directly
   * defined by the plugin artifact (this should always be true when a plugin implements an abstract
   * method of another) 2. The plugin artifact defines no overrides that effect the dependency (this
   * should be true as the plugin should just define the version it wants directly). If the
   * performance of resolvePathGroup is improved this method could be replaced by the version below.
   */
  private RepoArtifactId fastFindMatchingDependency(Target target, RepoArtifactId id) {
    // Find the matching id that belongs to the classpath
    PathGroup pathGroup = target.getPathGroup("classpath");
    RepoArtifactId match = null;
    String matchingPath = null;
    for (Iterator i = target.getPlugin().getArtifact().getDependencies().iterator();
        i.hasNext(); ) {
      RepoDependency dependency = (RepoDependency) i.next();
      if (dependency.getId().matches(id)) {
        for (Iterator j = dependency.getPathSpecs().iterator(); j.hasNext(); ) {
          RepoPathSpec pathSpec = (RepoPathSpec) j.next();
          if (pathGroup.getPaths().contains("plugin." + pathSpec.getTo())) {
            match = dependency.getId();
            matchingPath = pathSpec.getTo();
            break;
          }
        }
      }
    }

    Assert.isTrue(
        match != null,
        target.getPlugin().getArtifact().getId().toShortString()
            + " does not declare a dependency that matches "
            + id);

    // Apply any project overrides
    for (Iterator i = overrides.iterator(); i.hasNext(); ) {
      ws.quokka.core.model.Override override = (ws.quokka.core.model.Override) i.next();
      if (override.getWithVersion() != null && override.matches(match)) {
        Set paths = override.matchingPluginPaths(target.getPlugin().getArtifact().getId());
        if (paths.contains(matchingPath) || ((paths.size() == 1) && paths.contains("*"))) {
          log.verbose("Overriding " + match.toShortString() + " to " + override.getWithVersion());
          if (log.isDebugEnabled()) {
            log.debug(
                "Applied "
                    + override
                    + (override.getLocator() == null ? "" : " from " + override.getLocator()));
          }
          return new RepoArtifactId(
              id.getGroup(), id.getName(), id.getType(), override.getWithVersion());
        }
      }
    }

    return match;

    // Slow code
    //        List artifacts = resolvePathGroup(target, "classpath");
    //        for (Iterator i = artifacts.iterator(); i.hasNext();) {
    //            RepoArtifact artifact = (RepoArtifact) i.next();
    //            if (artifact.getId().matches(id)) {
    //                return artifact.getId();
    //            }
    //        }
  }
  /**
   * Overrides anything that conflicts with the core to the core version This will allow old plugins
   * to potentially work without overriding them
   */
  private List getCoreOverrides() {
    List overrides = new ArrayList();

    for (Iterator i = corePath.getArtifacts().iterator(); i.hasNext(); ) {
      RepoArtifact artifact = (RepoArtifact) i.next();
      RepoArtifactId id = artifact.getId();
      overrides.add(
          new RepoOverride(
              Collections.singleton("*"),
              id.getGroup(),
              id.getName(),
              id.getType(),
              null,
              id.getVersion()));
    }

    return overrides;
  }
  public ResolvedPath _getResolvedPluginPath(
      Plugin plugin, String pathId, boolean mergeWithCore, boolean overrideCore, boolean flatten) {
    // Create a mock artifact for the resolver as a way to add user specified path specs and
    // overrides
    RepoArtifact artifact = new RepoArtifact();
    RepoDependency dependency = new RepoDependency();
    RepoArtifactId pluginId = plugin.getArtifact().getId();
    dependency.setId(pluginId);
    artifact.addDependency(dependency);

    String id = "plugin";
    artifact.addPath(new RepoPath(id, "Plugin path", true, true));

    // Add dependencies
    if (plugin.getDependency() != null) {
      for (Iterator j = plugin.getDependency().getPathSpecs().iterator(); j.hasNext(); ) {
        RepoPathSpec pluginPathSpec = (RepoPathSpec) j.next();

        if (pluginPathSpec.getFrom().equals(pathId)) {
          // Add user specifications. Ignore the mandatory flag and to paths as they
          // are not relevant. However, allow descend to be false in case the writer of the
          // plugin added bogus dependencies.
          dependency.addPathSpec(
              new RepoPathSpec(
                  pathId,
                  id,
                  pluginPathSpec.getOptions(),
                  (pluginPathSpec.isDescend() == null) ? Boolean.TRUE : pluginPathSpec.isDescend(),
                  Boolean.TRUE));
        }
      }
    }

    if (dependency.getPathSpecs().size() == 0) {
      // Add default ... user hasn't specified anything
      dependency.addPathSpec(new RepoPathSpec(pathId, id, null, Boolean.TRUE, Boolean.TRUE));
    }

    // Add core overrides if applicable
    if (overrideCore) {
      for (Iterator j = coreOverrides.iterator(); j.hasNext(); ) {
        RepoOverride override = (RepoOverride) j.next();
        artifact.addOverride(override);
      }
    }

    // Add overrides
    for (Iterator i = overrides.iterator(); i.hasNext(); ) {
      ws.quokka.core.model.Override override = (ws.quokka.core.model.Override) i.next();
      Set paths = override.matchingPluginPaths(plugin.getArtifact().getId());

      if (paths.contains(pathId) || ((paths.size() == 1) && paths.contains("*"))) {
        // Create a copy of the override, moving matching plugin paths to be standard paths
        RepoOverride copy =
            new RepoOverride(
                Collections.singleton("*"),
                override.getGroup(),
                override.getName(),
                override.getType(),
                override.getVersion(),
                override.getWithVersion(),
                override.getWithPathSpecs());
        artifact.addOverride(copy);
      }
    }

    // Remove the plugin itself from the path
    // TODO: Look into a better way of doing this, perhaps modifying Resolver so it has the option
    // of not adding the root in first place.
    ResolvedPath path = pathResolver.resolvePath(id, artifact);
    path.setId("Plugin path '" + pathId + "' from " + pluginId.toShortString());

    List artifacts = new ArrayList();

    for (Iterator i = path.getArtifacts().iterator(); i.hasNext(); ) {
      artifact = (RepoArtifact) i.next();

      if (!artifact.getId().equals(pluginId)) {
        artifacts.add(artifact);
      }

      if (pluginId.equals(artifact.getId().getAnnotations().get("declaredBy"))) {
        artifact.getId().getAnnotations().remove("declaredBy");
      }
    }

    path = new ResolvedPath(path.getId(), artifacts);
    path = handleMergeAndFlatten(mergeWithCore, flatten, path);

    return path;
  }
  /**
   * Returns a set of properties for the project. The precedence is: quokka.properties
   * quokka.properties of inherited projects plugin.properties No expansion or ordering of
   * properties is done
   */
  public AnnotatedProperties getProperties() {
    AnnotatedProperties resolvedProperties = new AnnotatedProperties();

    // Add the global defaults
    resolvedProperties.put("quokka.project.targetDir", "${basedir}/target");
    resolvedProperties.put("quokka.project.sourceDir", "${basedir}/src");
    resolvedProperties.put("quokka.project.resourcesDir", "${basedir}/resources");

    // Add artifact related properties
    if (project.getArtifacts().size() > 0) {
      // Add properties common to all (group & version)
      RepoArtifactId artifactId =
          ((Artifact) getProject().getArtifacts().iterator().next()).getId();
      resolvedProperties.put("quokka.project.artifact.group", artifactId.getGroup());
      resolvedProperties.put("quokka.project.artifact.version", artifactId.getVersion().toString());

      // Build up a list of names by type
      Map namesByType = new HashMap();

      for (Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) {
        Artifact artifact = (Artifact) i.next();
        artifactId = artifact.getId();

        List names = (List) namesByType.get(artifactId.getType());

        if (names == null) {
          names = new ArrayList();
          namesByType.put(artifactId.getType(), names);
        }

        names.add(artifactId.getName());
      }

      // Output the names
      for (Iterator i = namesByType.entrySet().iterator(); i.hasNext(); ) {
        Map.Entry entry = (Map.Entry) i.next();
        List names = (List) entry.getValue();
        resolvedProperties.put(
            "quokka.project.artifact.name[" + entry.getKey() + "]",
            Strings.join(names.iterator(), ","));
      }
    }

    // Put the plugin properties in first. Order is not important as plugin properties should be
    // unique to their plugin
    for (Iterator i = resolvedTargets.values().iterator(); i.hasNext(); ) {
      Target target = (Target) i.next();
      AnnotatedProperties targetProperties = target.getDefaultProperties();
      resolvedProperties.putAll(
          applyProfiles(targetProperties, project.getActiveProfiles().getElements()));
    }

    // Put in any properties defined in dependency sets (processed in reverse to ensure high levels
    // override low levels)
    resolveProperties(resolvedProperties, project.getDependencySet());

    resolvedProperties.putAll(
        applyProfiles(project.getProperties(), project.getActiveProfiles().getElements()));

    // Put the project paths as properties
    for (Iterator i = resolvedPaths.entrySet().iterator(); i.hasNext(); ) {
      Map.Entry entry = (Map.Entry) i.next();
      resolvedProperties.put(
          "quokka.project.path." + entry.getKey(),
          toAntPath(getProjectPath((String) entry.getKey(), false, true)).toString());
    }

    return resolvedProperties;
  }
  public void initialise() {
    pathResolver = new Resolver(repository, log);

    // Add ant-types path
    project
        .getDependencySet()
        .addPath(
            new Path(
                "ant-types",
                "Dependencies added to this path are available to ant optional tasks."));

    // Get dependency sets, applying profiles and overrides
    List sets = depthFirst(project.getDependencySet());

    // O pass: aggregate overrides
    for (Iterator i = sets.iterator(); i.hasNext(); ) {
      DependencySet set = (DependencySet) i.next();
      overrides.addAll(set.getOverrides());
    }

    // 1st pass: Define targets, paths, imported URLs
    Map targets = new HashMap();

    for (Iterator i = sets.iterator(); i.hasNext(); ) {
      DependencySet set = (DependencySet) i.next();

      // Get targets
      for (Iterator j = set.getDependencies().iterator(); j.hasNext(); ) {
        Dependency dependency = (Dependency) j.next();

        if (dependency instanceof PluginDependency) {
          resolveTargets(targets, (PluginDependency) dependency);
        }
      }

      // Get paths
      for (Iterator j = set.getPaths().values().iterator(); j.hasNext(); ) {
        addPath((Path) j.next());
      }

      if (set.getImportURL() != null) {
        resolvedImports.add(set.getImportURL());
      }

      // Find the unique names for all declared artifacts
      Set uniqueNames = new HashSet();

      for (Iterator j = project.getArtifacts().iterator(); j.hasNext(); ) {
        Artifact artifact = (Artifact) j.next();
        uniqueNames.add(artifact.getId().getName());
      }

      // Create artifacts for any licenses referenced by files
      for (Iterator j = set.getLicenses().iterator(); j.hasNext(); ) {
        License license = (License) j.next();

        if (license.getFile() != null) {
          Assert.isTrue(
              project.getArtifacts().size() != 0,
              license.getLocator(),
              "There are no artifacts defined for a license to be applied to.");

          Artifact artifact = (Artifact) project.getArtifacts().iterator().next();
          RepoArtifactId id = artifact.getId();

          // If no name is specified, default it to name of the artifact if it is unique,
          // otherwise use the default from the group
          String name = license.getId().getName();
          String defaultName = RepoArtifactId.defaultName(id.getGroup());

          if (name == null) {
            if (uniqueNames.size() == 1) {
              name = (String) uniqueNames.iterator().next();
            } else {
              name = defaultName;
            }
          }

          Artifact licenseArtifact = new Artifact(id.getGroup(), name, "license", id.getVersion());
          licenseArtifact.setDescription(
              "License for " + id.getGroup() + (name.equals(defaultName) ? "" : (":" + name)));
          project.addArtifact(licenseArtifact);
          license.setId(licenseArtifact.getId());
        }
      }
    }

    // 2nd pass: Define default paths specs, add imported targets
    for (Iterator i = sets.iterator(); i.hasNext(); ) {
      DependencySet set = (DependencySet) i.next();

      // Fill in path spec defaults now that the project paths have been defined
      addPathSpecDefaults(set);
    }

    // 3rd pass: Reverse order for build resources
    List reverse = new ArrayList(sets);
    Collections.reverse(reverse);

    for (Iterator i = reverse.iterator(); i.hasNext(); ) {
      DependencySet set = (DependencySet) i.next();
      buildResources.putAll(set.getBuildResources()); // Parent overrides children
    }

    resolvedTargets = targets;
    corePath = resolveCorePath();

    if ("true".equals(antProject.getProperty("quokka.project.overrideCore"))) {
      coreOverrides = getCoreOverrides();
    }
  }
  private void addTarget(Map resolved, Target target) {
    //        System.out.println("Adding " + target.getName() + " from " + parents);
    if (target.getPrefix() != null) {
      target.setDefaultProperties(expandPrefix(target.getPrefix(), target.getDefaultProperties()));
    }

    Target existing = (Target) resolved.get(target.getName());

    if (existing != null) {
      RepoArtifactId targetId = target.getPlugin().getArtifact().getId();
      RepoArtifactId existingId = existing.getPlugin().getArtifact().getId();
      Assert.isTrue(
          targetId.equals(existingId),
          target.getLocator(),
          "Multiple targets are defined with the name '"
              + target.getName()
              + "'. Declared in "
              + targetId
              + " and "
              + existingId);

      return;
    }

    resolved.put(target.getName(), target);
    registerTypes(target);
    registerProjectPaths(target);
    buildResources.putAll(target.getPlugin().getBuildResources());

    // PluginDependency declares the target
    addDependentTargets(resolved, target);

    if (target.getImplementsPlugin() != null) {
      // PluginDependency implements the target declared in another plugin
      String[] implementsPlugin = parseImplements(target);
      RepoArtifactId declaringPluginId =
          fastFindMatchingDependency(
              target,
              new RepoArtifactId(
                  implementsPlugin[0], implementsPlugin[1], "plugin", (Version) null));

      Plugin declaringPlugin = getPluginInstance(declaringPluginId);
      String declaringTargetName = declaringPlugin.getNameSpace() + ":" + implementsPlugin[2];

      // Get the declaring plugin and find the matching target
      Target declaringTarget = (Target) resolved.get(declaringTargetName);

      if (declaringTarget == null) {
        declaringTarget = declaringPlugin.getTarget(declaringTargetName);
        Assert.isTrue(
            declaringTarget != null,
            target.getLocator(),
            "'"
                + declaringTargetName
                + "' is not defined in '"
                + declaringPluginId.toShortString()
                + "'");
        addTarget(resolved, declaringTarget);
      }

      Assert.isTrue(
          declaringTarget.isAbstract(),
          target.getLocator(),
          "Target is attempting to implement a non-abstract target: target="
              + target.getName()
              + ", implements="
              + declaringTarget.getName());
      target.getPlugin().setDeclaringPlugin(declaringTarget.getPlugin());

      if (!declaringTarget.isImplemented()) {
        declaringTarget.setImplemented(true);
        declaringTarget.clearDependencies();
      }

      declaringTarget.addDependency(target.getName());

      // Add the declaring targets dependencies to ensure the implementation is executed before them
      for (Iterator i = declaringTarget.getOriginalDependencies().iterator(); i.hasNext(); ) {
        String dependency = (String) i.next();
        target.addDependency(dependency);
      }
    }
  }