/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat Middleware LLC, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.shrinkwrap.resolver.impl.maven;


import java.util.Set;
import java.util.Stack;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Assignable;

import org.jboss.shrinkwrap.resolver.api.maven.EffectivePomMavenDependencyResolver;

import org.jboss.shrinkwrap.resolver.api.maven.MavenImporter;
import org.jboss.shrinkwrap.resolver.api.maven.MavenImporter.EffectivePomMavenImporter;
import org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.CombinedFilter;
import org.jboss.shrinkwrap.resolver.api.maven.filter.ScopeFilter;

/**
 * Implementation of EffectivePomMavenImporter. This class is hidden to be instantiated by user, and it contains all the
 * functionality required to handle Maven packages.
 *
 * @author <a href="kpiwko@redhat.com>Karel Piwko</a>
 * @see MavenPackagingType
 */
class EffectivePomMavenImporterImpl implements MavenImporter.EffectivePomMavenImporter, MavenEnvironmentRetrieval {

    private Archive<?> archive;

    private EffectivePomMavenDependencyResolver effectivePomResolver;

    private final MavenPackagingType mpt;

    /**
     * Creates a EffectivePomMavenImporter based on information from POM model
     *
     * @param archive The archive to be modified
     * @param effectivePomResolver Effective pom in resolved state
     */
    public EffectivePomMavenImporterImpl(Archive<?> archive, EffectivePomMavenDependencyResolver effectivePomResolver) {

        this.archive = archive;
        this.effectivePomResolver = effectivePomResolver;
        this.mpt = MavenPackagingType.from(getMavenEnvironment().getModel().getPackaging());
    }

    @Override
    public <TYPE extends Assignable> TYPE as(Class<TYPE> archiveType) {
        return archive.as(archiveType);
    }

    @Override
    public EffectivePomMavenImporter importBuildOutput() {
        this.archive = mpt.enrichArchiveWithBuildOutput(archive, getMavenEnvironment().getModel());
        return this;
    }

    @Override
    public EffectivePomMavenImporter importTestBuildOutput() {
        this.archive = mpt.enrichArchiveWithTestOutput(archive, getMavenEnvironment().getModel());
        return this;
    }

    @Override
    public EffectivePomMavenImporter importTestDependencies() {
        return importAnyDependencies(new ScopeFilter("test"));
    }

    @Override
    public EffectivePomMavenImporter importAnyDependencies(MavenResolutionFilter filter) {

        ArtifactTypeRegistry stereotypes = effectivePomResolver.getDelegate().getSystem()
                .getArtifactTypeRegistry(effectivePomResolver.getDelegate().getSession());

        // store all dependency information to be able to retrieve versions later
        Stack<MavenDependency> pomDefinedDependencies = MavenConverter.fromDependencies(effectivePomResolver.getModel()
                .getDependencies(), stereotypes);

    /**
     * {@inheritDoc}
     * @see org.jboss.shrinkwrap.resolver.api.maven.MavenImporter.EffectivePomMavenImporter#importTestDependencies(org.jboss.shrinkwrap.resolver.api.maven.MavenResolutionFilter)
     */
   @Override
   public EffectivePomMavenImporter importTestDependencies(final MavenResolutionFilter filter) throws IllegalArgumentException {
      // Precondition checks
      if (filter == null) {
         throw new IllegalArgumentException("At least one filter must be defined");
      }

      return this.importAnyDependencies(new CombinedFilter(new ScopeFilter("test"), filter));
   }

   private MavenDependencyResolver getMavenDependencyResolver(Stack<MavenDependency> dependencies,
            Set<MavenDependency> versionManagement) {
        // configure filter
        MavenResolutionFilter configuredFilter = filter.configure(pomDefinedDependencies);

        for (MavenDependency candidate : pomDefinedDependencies) {
            if (configuredFilter.accept(candidate)) {
                effectivePomResolver.getDelegate().getDependencies().push(candidate);
            }
        }

        this.effectivePomResolver = (EffectivePomMavenDependencyResolverInternal) effectivePomResolver
                .importAnyDependencies(filter);

        this.effectivePomResolver = effectivePomResolver.importAnyDependencies(filter);
        this.archive = mpt.enrichArchiveWithTestArtifacts(archive, effectivePomResolver, filter);
        return this;
    }

    @Override
    public MavenEnvironment getMavenEnvironment() {
        if (!(effectivePomResolver instanceof MavenEnvironmentRetrieval)) {
            throw new UnsupportedOperationException(
                    "Incompatible instance of EffectivePomDependencyResolver, unable to get MavenEnvironment object");
        }

        return ((MavenEnvironmentRetrieval) effectivePomResolver).getMavenEnvironment();
    }
}

@@ -833,56 +833,8 @@
 n;%0A%0A
-%0Aimport java.util.Set;%0Aimport java.util.Stack;%0A%0A
 impo
@@ -914,17 +914,16 @@
 gnable;%0A
-%0A
 import o
@@ -998,17 +998,16 @@
 solver;%0A
-%0A
 import o
@@ -2000,16 +2000,34 @@
  archive
+%0A     *           
  The arc
@@ -2080,16 +2080,34 @@
 Resolver
+%0A     *           
  Effecti
@@ -3141,549 +3141,8 @@
  %7D%0A%0A
-    @Override%0A    public EffectivePomMavenImporter importAnyDependencies(MavenResolutionFilter filter) %7B%0A%0A        ArtifactTypeRegistry stereotypes = effectivePomResolver.getDelegate().getSystem()%0A                .getArtifactTypeRegistry(effectivePomResolver.getDelegate().getSession());%0A%0A        // store all dependency information to be able to retrieve versions later%0A        Stack%3CMavenDependency%3E pomDefinedDependencies = MavenConverter.fromDependencies(effectivePomResolver.getModel()%0A                .getDependencies(), stereotypes);%0A%0A
     
@@ -3166,16 +3166,23 @@
 ritDoc%7D%0A
+     *%0A
      * @
@@ -3362,16 +3362,17 @@
   */%0A   
+ 
 @Overrid
@@ -3373,16 +3373,17 @@
 verride%0A
+ 
    publi
@@ -3468,16 +3468,24 @@
  filter)
+%0A       
  throws 
@@ -3517,16 +3517,18 @@
 %7B%0A      
+  
 // Preco
@@ -3544,24 +3544,26 @@
 hecks%0A      
+  
 if (filter =
@@ -3568,24 +3568,27 @@
  == null) %7B%0A
+   
          thr
@@ -3661,24 +3661,26 @@
 ed%22);%0A      
+  
 %7D%0A%0A      ret
@@ -3672,24 +3672,26 @@
    %7D%0A%0A      
+ 
+ 
 return this.
@@ -3773,667 +3773,116 @@
 %0A   
+ 
 %7D%0A%0A   
-private MavenDependencyResolver getMavenDependencyResolver(Stack%3CMavenDependency%3E dependencies,%0A            Set%3CMavenDependency%3E versionManagement) %7B%0A        // configure filter%0A        MavenResolutionFilter configuredFilter = filter.configure(pomDefinedDependencies);%0A%0A        for (MavenDependency candidate : pomDefinedDependencies) %7B%0A            if (configuredFilter.accept(candidate)) %7B%0A                effectivePomResolver.getDelegate().getDependencies().push(candidate);%0A            %7D%0A        %7D%0A%0A        this.effectivePomResolver = (EffectivePomMavenDependencyResolverInternal) effectivePomResolver%0A                .importAnyDependencies(
+ @Override%0A    public EffectivePomMavenImporter importAnyDependencies(MavenResolutionFilter 
 filter)
-;%0A
+ %7B
 %0A   
@@ -4303,20 +4303,16 @@
         
-    
 %22Incompa
@@ -4509,9 +4509,8 @@
 %0A    %7D%0A%7D
-%0A

Refactor to fix merge errors