001package org.apache.maven.repository.metadata; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026import org.apache.maven.artifact.ArtifactScopeEnum; 027 028/** 029 * classpath container that is aware of the classpath scope 030 * 031 * @author <a href="oleg@codehaus.org">Oleg Gusakov</a> 032 * 033 */ 034public class ClasspathContainer 035implements Iterable<ArtifactMetadata> 036{ 037 private List<ArtifactMetadata> classpath; 038 039 private ArtifactScopeEnum scope; 040 041 // ------------------------------------------------------------------------------------------- 042 public ClasspathContainer( ArtifactScopeEnum scope ) 043 { 044 this.scope = ArtifactScopeEnum.checkScope( scope ); 045 } 046 047 // ------------------------------------------------------------------------------------------- 048 public ClasspathContainer( List<ArtifactMetadata> classpath, ArtifactScopeEnum scope ) 049 { 050 this( scope ); 051 this.classpath = classpath; 052 } 053 054 // ------------------------------------------------------------------------------------------- 055 public Iterator<ArtifactMetadata> iterator() 056 { 057 return classpath == null ? null : classpath.iterator(); 058 } 059 060 // ------------------------------------------------------------------------------------------- 061 public ClasspathContainer add( ArtifactMetadata md ) 062 { 063 if ( classpath == null ) 064 { 065 classpath = new ArrayList<ArtifactMetadata>( 16 ); 066 } 067 068 classpath.add( md ); 069 070 return this; 071 } 072 073 // ------------------------------------------------------------------------------------------- 074 public List<ArtifactMetadata> getClasspath() 075 { 076 return classpath; 077 } 078 079 // ------------------------------------------------------------------------------------------- 080 public MetadataTreeNode getClasspathAsTree() 081 throws MetadataResolutionException 082 { 083 if ( classpath == null || classpath.size() < 1 ) 084 { 085 return null; 086 } 087 088 MetadataTreeNode tree = null; 089 MetadataTreeNode parent = null; 090 091 for ( ArtifactMetadata md : classpath ) 092 { 093 MetadataTreeNode node = new MetadataTreeNode( md, parent, md.isResolved(), md.getArtifactScope() ); 094 if ( tree == null ) 095 { 096 tree = node; 097 } 098 099 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}