1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.repository.metadata;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.maven.artifact.ArtifactScopeEnum;
26
27 /**
28 * classpath container that is aware of the classpath scope
29 *
30 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
31 *
32 */
33 public class ClasspathContainer implements Iterable<ArtifactMetadata> {
34 private List<ArtifactMetadata> classpath;
35
36 private ArtifactScopeEnum scope;
37
38 // -------------------------------------------------------------------------------------------
39 public ClasspathContainer(ArtifactScopeEnum scope) {
40 this.scope = ArtifactScopeEnum.checkScope(scope);
41 }
42
43 // -------------------------------------------------------------------------------------------
44 public ClasspathContainer(List<ArtifactMetadata> classpath, ArtifactScopeEnum scope) {
45 this(scope);
46 this.classpath = classpath;
47 }
48
49 // -------------------------------------------------------------------------------------------
50 public Iterator<ArtifactMetadata> iterator() {
51 return classpath == null ? null : classpath.iterator();
52 }
53
54 // -------------------------------------------------------------------------------------------
55 public ClasspathContainer add(ArtifactMetadata md) {
56 if (classpath == null) {
57 classpath = new ArrayList<>(16);
58 }
59
60 classpath.add(md);
61
62 return this;
63 }
64
65 // -------------------------------------------------------------------------------------------
66 public List<ArtifactMetadata> getClasspath() {
67 return classpath;
68 }
69
70 // -------------------------------------------------------------------------------------------
71 public MetadataTreeNode getClasspathAsTree() throws MetadataResolutionException {
72 if (classpath == null || classpath.size() < 1) {
73 return null;
74 }
75
76 MetadataTreeNode tree = null;
77 MetadataTreeNode parent = null;
78
79 for (ArtifactMetadata md : classpath) {
80 MetadataTreeNode node = new MetadataTreeNode(md, parent, md.isResolved(), md.getArtifactScope());
81 if (tree == null) {
82 tree = node;
83 }
84
85 if (parent != null) {
86 parent.setNChildren(1);
87 parent.addChild(0, node);
88 }
89
90 parent = node;
91 }
92 return tree;
93 }
94
95 public void setClasspath(List<ArtifactMetadata> classpath) {
96 this.classpath = classpath;
97 }
98
99 public ArtifactScopeEnum getScope() {
100 return scope;
101 }
102
103 public void setScope(ArtifactScopeEnum scope) {
104 this.scope = scope;
105 }
106
107 // -------------------------------------------------------------------------------------------
108 @Override
109 public String toString() {
110 StringBuilder sb = new StringBuilder(256);
111 sb.append("[scope=").append(scope.getScope());
112 if (classpath != null) {
113 for (ArtifactMetadata md : classpath) {
114 sb.append(": ")
115 .append(md.toString())
116 .append('{')
117 .append(md.getArtifactUri())
118 .append('}');
119 }
120 }
121 sb.append(']');
122 return sb.toString();
123 }
124 // -------------------------------------------------------------------------------------------
125 // -------------------------------------------------------------------------------------------
126 }