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          MetadataTreeNode node = null;
91  
92          for ( ArtifactMetadata md : classpath )
93          {
94              node = new MetadataTreeNode( md, parent, md.isResolved(), md.getArtifactScope() );
95              if ( tree == null )
96              {
97                  tree = node;
98              }
99  
100             if ( parent != null )
101             {
102                 parent.setNChildren( 1 );
103                 parent.addChild( 0, node );
104             }
105 
106             parent = node;
107 
108         }
109         return tree;
110     }
111 
112     public void setClasspath( List<ArtifactMetadata> classpath )
113     {
114         this.classpath = classpath;
115     }
116 
117     public ArtifactScopeEnum getScope()
118     {
119         return scope;
120     }
121 
122     public void setScope( ArtifactScopeEnum scope )
123     {
124         this.scope = scope;
125     }
126 
127     // -------------------------------------------------------------------------------------------
128     @Override
129     public String toString()
130     {
131         StringBuilder sb = new StringBuilder( 256 );
132         sb.append( "[scope=" + scope.getScope() );
133         if ( classpath != null )
134         {
135             for ( ArtifactMetadata md : classpath )
136             {
137                 sb.append( ": " + md.toString() + '{' + md.getArtifactUri() + '}' );
138             }
139         }
140         sb.append( ']' );
141         return sb.toString();
142     }
143     // -------------------------------------------------------------------------------------------
144     // -------------------------------------------------------------------------------------------
145 }