update copyright
[fedora-idea.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / importing / ArtifactExternalDependenciesImporter.java
blob560b79d7f452236bb1dfd6c024e4b9a630841387
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.maven.importing;
18 import com.intellij.openapi.roots.ui.configuration.artifacts.ManifestFilesInfo;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.packaging.artifacts.Artifact;
21 import com.intellij.packaging.artifacts.ModifiableArtifactModel;
22 import com.intellij.packaging.elements.CompositePackagingElement;
23 import com.intellij.packaging.elements.PackagingElement;
24 import com.intellij.packaging.elements.PackagingElementResolvingContext;
25 import com.intellij.packaging.impl.artifacts.ArtifactUtil;
26 import com.intellij.packaging.impl.artifacts.PackagingElementProcessor;
27 import com.intellij.packaging.impl.elements.ArtifactElementType;
28 import com.intellij.packaging.impl.elements.ArtifactPackagingElement;
29 import com.intellij.packaging.ui.ManifestFileConfiguration;
30 import org.jetbrains.annotations.NotNull;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
37 /**
38 * @author nik
40 public class ArtifactExternalDependenciesImporter {
41 private ManifestFilesInfo myManifestFiles = new ManifestFilesInfo();
42 private Map<Artifact, List<PackagingElement<?>>> myExternalDependencies = new HashMap<Artifact, List<PackagingElement<?>>>();
44 public ManifestFileConfiguration getManifestFile(@NotNull Artifact artifact,
45 @NotNull PackagingElementResolvingContext context) {
46 return myManifestFiles.getManifestFile(artifact.getRootElement(), artifact.getArtifactType(), context);
49 public List<PackagingElement<?>> getExternalDependenciesList(@NotNull Artifact artifact) {
50 List<PackagingElement<?>> elements = myExternalDependencies.get(artifact);
51 if (elements == null) {
52 elements = new ArrayList<PackagingElement<?>>();
53 myExternalDependencies.put(artifact, elements);
55 return elements;
58 public void applyChanges(ModifiableArtifactModel artifactModel, final PackagingElementResolvingContext context) {
59 myManifestFiles.saveManifestFiles();
60 final List<Pair<? extends CompositePackagingElement<?>, List<PackagingElement<?>>>> elementsToInclude = new ArrayList<Pair<? extends CompositePackagingElement<?>, List<PackagingElement<?>>>>();
61 for (Artifact artifact : artifactModel.getArtifacts()) {
62 ArtifactUtil.processPackagingElements(artifact, ArtifactElementType.ARTIFACT_ELEMENT_TYPE, new PackagingElementProcessor<ArtifactPackagingElement>() {
63 @Override
64 public boolean process(@NotNull List<CompositePackagingElement<?>> parents,
65 @NotNull ArtifactPackagingElement artifactPackagingElement) {
66 final Artifact included = artifactPackagingElement.findArtifact(context);
67 if (!parents.isEmpty() && included != null) {
68 final CompositePackagingElement<?> parent = parents.get(0);
69 final List<PackagingElement<?>> elements = myExternalDependencies.get(included);
70 if (elements != null) {
71 elementsToInclude.add(Pair.create(parent, elements));
74 return true;
76 }, context, false);
79 for (Pair<? extends CompositePackagingElement<?>, List<PackagingElement<?>>> pair : elementsToInclude) {
80 pair.getFirst().addOrFindChildren(pair.getSecond());