/*-
 * Copyright (c) 2013-2015 Red Hat, Inc.
 *
 * 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.fedoraproject.xmvn.mojo;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.ArtifactType;

import org.fedoraproject.xmvn.artifact.Artifact;
import org.fedoraproject.xmvn.artifact.DefaultArtifact;
import org.fedoraproject.xmvn.utils.ArtifactTypeRegistry;

/**
 * @author Mikolaj Izdebski
 */
class Utils
{
    public static Artifact aetherArtifact( org.apache.maven.artifact.Artifact mavenArtifact )
    {
        String groupId = mavenArtifact.getGroupId();
        String artifactId = mavenArtifact.getArtifactId();
        String version = mavenArtifact.getVersion();

        ArtifactHandler handler = mavenArtifact.getArtifactHandler();
        String extension = handler.getExtension();
        String classifier = handler.getClassifier();
        if ( StringUtils.isNotEmpty( mavenArtifact.getClassifier() ) )
            classifier = mavenArtifact.getClassifier();

        File artifactFile = mavenArtifact.getFile();
        Path artifactPath = artifactFile != null ? artifactFile.toPath() : null;

        Artifact artifact = new DefaultArtifact( groupId, artifactId, extension, classifier, version );
        artifact = artifact.setPath( artifactPath );
        return artifact;
    }

    public static Artifact dependencyArtifact( RepositorySystemSession repoSession, Dependency dependency )
    {
        ArtifactTypeRegistry registry = ArtifactTypeRegistry.getDefaultRegistry();

        if ( repoSession != null && dependency.getType() != null )
        {
            ArtifactType type = repoSession.getArtifactTypeRegistry().get( dependency.getType() );

            if ( type != null )
            {
                registry =
                    registry.registerStereotype( dependency.getType(), type.getExtension(), type.getClassifier() );
            }
        }

        return registry.createTypedArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getType(),
                                             dependency.getClassifier(), dependency.getVersion() );
    }

    private static void writeModel( Model model, Path path )
        throws IOException
    {
        try (Writer writer = Files.newBufferedWriter( path, StandardCharsets.UTF_8 ))
        {
            MavenXpp3Writer pomWriter = new MavenXpp3Writer();
            pomWriter.write( writer, model );
        }
    }

    public static Path saveEffectivePom( Model model )
        throws IOException
    {
        Path source = Files.createTempFile( "xmvn", ".pom.xml" );
        writeModel( model, source );
        return source;
    }
}

@@ -663,129 +663,8 @@
 le;%0A
-import java.io.IOException;%0Aimport java.io.Writer;%0Aimport java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0A
 impo
@@ -791,100 +791,8 @@
 cy;%0A
-import org.apache.maven.model.Model;%0Aimport org.apache.maven.model.io.xpp3.MavenXpp3Writer;%0A
 impo
@@ -2857,544 +2857,7 @@
    %7D
-%0A%0A    private static void writeModel( Model model, Path path )%0A        throws IOException%0A    %7B%0A        try (Writer writer = Files.newBufferedWriter( path, StandardCharsets.UTF_8 ))%0A        %7B%0A            MavenXpp3Writer pomWriter = new MavenXpp3Writer();%0A            pomWriter.write( writer, model );%0A        %7D%0A    %7D%0A%0A    public static Path saveEffectivePom( Model model )%0A        throws IOException%0A    %7B%0A        Path source = Files.createTempFile( %22xmvn%22, %22.pom.xml%22 );%0A        writeModel( model, source );%0A        return source;%0A    %7D
 %0A%7D%0A

Remove unused code from xmvn-mojo Utils