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