/*
 * The MIT License
 *
 * Copyright (c) 2016, CloudBees, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.jenkinsci.plugins.pipeline.maven.eventspy.handler;

import org.apache.maven.project.DependencyResolutionResult;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.jenkinsci.plugins.pipeline.maven.eventspy.reporter.MavenEventReporter;

/**
 * @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
 */
public class DependencyResolutionResultHandler extends AbstractMavenEventHandler<DependencyResolutionResult> {
    public DependencyResolutionResultHandler(MavenEventReporter reporter) {
        super(reporter);
    }

    @Override
    protected boolean _handle(DependencyResolutionResult result) {

        Xpp3Dom root = new Xpp3Dom("DependencyResolutionResult");
        root.setAttribute("class", result.getClass().getName());

        /*
        Xpp3Dom dependenciesElt = new Xpp3Dom("resolvedDependencies");
        root.addChild(dependenciesElt);
        // don't cast to Dependency to not have a classloading issue at runtime
        for (Object dependency: result.getResolvedDependencies()) {
            dependenciesElt.addChild(newElement("dependency", dependency.toString()));
        }*/

        reporter.print(root);
        return true;
    }
}

@@ -1306,16 +1306,105 @@
 pp3Dom;%0A
+import org.eclipse.aether.artifact.Artifact;%0Aimport org.eclipse.aether.graph.Dependency;%0A
 import o
@@ -1478,16 +1478,84 @@
 orter;%0A%0A
+import javax.annotation.Nonnull;%0Aimport javax.annotation.Nullable;%0A%0A
 /**%0A * @
@@ -2063,19 +2063,8 @@
 );%0A%0A
-        /*%0A
     
@@ -2170,24 +2170,25 @@
 esElt);%0A
+%0A
         
 // don't
@@ -2183,147 +2183,828 @@
     
-// don't cast to Dependency to not have a classloading issue at runtime%0A        for (Object dependency: result.getResolvedDependencies()) %7B
+for (Dependency dependency: result.getResolvedDependencies()) %7B%0A            Artifact artifact = dependency.getArtifact();%0A%0A            if(!artifact.isSnapshot()) %7B%0A                continue;%0A            %7D%0A%0A            Xpp3Dom dependencyElt = new Xpp3Dom(%22dependency%22);%0A%0A            dependencyElt.addChild(newElement(%22file%22, artifact.getFile().getAbsolutePath()));%0A%0A            dependencyElt.setAttribute(%22name%22, artifact.getFile().getName());%0A%0A            dependencyElt.setAttribute(%22groupId%22, artifact.getGroupId());%0A            dependencyElt.setAttribute(%22artifactId%22, artifact.getArtifactId());%0A            dependencyElt.setAttribute(%22version%22, artifact.getVersion());%0A            if (artifact.getClassifier() != null) %7B%0A                dependencyElt.setAttribute(%22classifier%22, artifact.getClassifier());%0A            %7D
 %0A   
@@ -3025,71 +3025,258 @@
 denc
-ies
+y
 Elt.
-addChild(newElement(%22dependency%22, dependency.toString())
+setAttribute(%22type%22, artifact.getExtension());%0A            dependencyElt.setAttribute(%22id%22, artifact.getVersion());%0A            dependencyElt.setAttribute(%22extension%22, artifact.getExtension());%0A%0A            dependenciesElt.addChild(dependencyElt
 );%0A 
@@ -3283,18 +3283,16 @@
        %7D
-*/
 %0A%0A      

Add dependencies to maven spy output