View Javadoc
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  import org.apache.maven.artifact.ArtifactScopeEnum;
25  
26  /**
27   * classpath container that is aware of the classpath scope
28   *
29   * @author <a href="oleg@codehaus.org">Oleg Gusakov</a>
30   *
31   */
32  public class ClasspathContainer implements Iterable<ArtifactMetadata> {
33      private List<ArtifactMetadata> classpath;
34  
35      private ArtifactScopeEnum scope;
36  
37      // -------------------------------------------------------------------------------------------
38      public ClasspathContainer(ArtifactScopeEnum scope) {
39          this.scope = ArtifactScopeEnum.checkScope(scope);
40      }
41  
42      // -------------------------------------------------------------------------------------------
43      public ClasspathContainer(List<ArtifactMetadata> classpath, ArtifactScopeEnum scope) {
44          this(scope);
45          this.classpath = classpath;
46      }
47  
48      // -------------------------------------------------------------------------------------------
49      public Iterator<ArtifactMetadata> iterator() {
50          return classpath == null ? null : classpath.iterator();
51      }
52  
53      // -------------------------------------------------------------------------------------------
54      public ClasspathContainer add(ArtifactMetadata md) {
55          if (classpath == null) {
56              classpath = new ArrayList<>(16);
57          }
58  
59          classpath.add(md);
60  
61          return this;
62      }
63  
64      // -------------------------------------------------------------------------------------------
65      public List<ArtifactMetadata> getClasspath() {
66          return classpath;
67      }
68  
69      // -------------------------------------------------------------------------------------------
70      public MetadataTreeNode getClasspathAsTree() throws MetadataResolutionException {
71          if (classpath == null || classpath.size() < 1) {
72              return null;
73          }
74  
75          MetadataTreeNode tree = null;
76          MetadataTreeNode parent = null;
77  
78          for (ArtifactMetadata md : classpath) {
79              MetadataTreeNode node = new MetadataTreeNode(md, parent, md.isResolved(), md.getArtifactScope());
80              if (tree == null) {
81                  tree = node;
82              }
83  
84              if (parent != null) {
85                  parent.setNChildren(1);
86                  parent.addChild(0, node);
87              }
88  
89              parent = node;
90          }
91          return tree;
92      }
93  
94      public void setClasspath(List<ArtifactMetadata> classpath) {
95          this.classpath = classpath;
96      }
97  
98      public ArtifactScopeEnum getScope() {
99          return scope;
100     }
101 
102     public void setScope(ArtifactScopeEnum scope) {
103         this.scope = scope;
104     }
105 
106     // -------------------------------------------------------------------------------------------
107     @Override
108     public String toString() {
109         StringBuilder sb = new StringBuilder(256);
110         sb.append("[scope=").append(scope.getScope());
111         if (classpath != null) {
112             for (ArtifactMetadata md : classpath) {
113                 sb.append(": ")
114                         .append(md.toString())
115                         .append('{')
116                         .append(md.getArtifactUri())
117                         .append('}');
118             }
119         }
120         sb.append(']');
121         return sb.toString();
122     }
123     // -------------------------------------------------------------------------------------------
124     // -------------------------------------------------------------------------------------------
125 }