View Javadoc

1   package org.apache.maven.shared.dependency.graph.internal;
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.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.shared.dependency.graph.DependencyNode;
26  import org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
27  
28  public class DefaultDependencyNode
29      implements DependencyNode
30  {
31      private final Artifact artifact;
32  
33      private final DependencyNode parent;
34  
35      private final String premanagedVersion;
36  
37      private final String premanagedScope;
38  
39      private final String versionConstraint;
40  
41      private List<DependencyNode> children;
42  
43      public DefaultDependencyNode( DependencyNode parent, Artifact artifact, String premanagedVersion,
44                                    String premanagedScope, String versionConstraint )
45      {
46          this.parent = parent;
47          this.artifact = artifact;
48          this.premanagedVersion = premanagedVersion;
49          this.premanagedScope = premanagedScope;
50          this.versionConstraint = versionConstraint;
51      }
52  
53      /**
54       * Applies the specified dependency node visitor to this dependency node and its children.
55       * 
56       * @param visitor
57       *            the dependency node visitor to use
58       * @return the visitor result of ending the visit to this node
59       * @since 1.1
60       */
61      public boolean accept( DependencyNodeVisitor visitor )
62      {
63          if ( visitor.visit( this ) )
64          {
65              for ( DependencyNode child : getChildren() )
66              {
67                  if ( !child.accept( visitor ) )
68                  {
69                      break;
70                  }
71              }
72          }
73  
74          return visitor.endVisit( this );
75      }
76  
77      public Artifact getArtifact()
78      {
79          return artifact;
80      }
81  
82      public void setChildren( List<DependencyNode> children )
83      {
84          this.children = children;
85      }
86  
87      public List<DependencyNode> getChildren()
88      {
89          return children;
90      }
91  
92      public DependencyNode getParent()
93      {
94          return parent;
95      }
96  
97      public String getPremanagedVersion()
98      {
99          return premanagedVersion;
100     }
101 
102     public String getPremanagedScope()
103     {
104         return premanagedScope;
105     }
106 
107     public String getVersionConstraint()
108     {
109         return versionConstraint;
110     }
111 
112     public String toNodeString()
113     {
114         StringBuffer buffer = new StringBuffer();
115 
116         buffer.append( artifact );
117         
118         ItemAppender appender = new ItemAppender( buffer, " (", "; ", ")" );
119 
120         if ( getPremanagedVersion() != null )
121         {
122             appender.append( "version managed from ", getPremanagedVersion() );
123         }
124             
125         if ( getPremanagedScope() != null )
126         {
127             appender.append( "scope managed from ", getPremanagedScope() );
128         }
129         
130         if ( getVersionConstraint() != null )
131         {
132             appender.append( "version selected from constraint ", getVersionConstraint() );
133         }
134         
135         appender.flush();
136 
137         return buffer.toString();
138     }
139 
140     /**
141      * Utility class to concatenate a number of parameters with separator tokens.   
142      */
143     private static class ItemAppender
144     {
145         private StringBuffer buffer;
146         
147         private String startToken;
148         
149         private String separatorToken;
150         
151         private String endToken;
152         
153         private boolean appended;
154         
155         public ItemAppender( StringBuffer buffer, String startToken, String separatorToken, String endToken )
156         {
157             this.buffer = buffer;
158             this.startToken = startToken;
159             this.separatorToken = separatorToken;
160             this.endToken = endToken;
161             
162             appended = false;
163         }
164 
165         public ItemAppender append( String item1, String item2 )
166         {
167             appendToken();
168             
169             buffer.append( item1 ).append( item2 );
170             
171             return this;
172         }
173         
174         public void flush()
175         {
176             if ( appended )
177             {
178                 buffer.append( endToken );
179                 
180                 appended = false;
181             }
182         }
183         
184         private void appendToken()
185         {
186             buffer.append( appended ? separatorToken : startToken );
187             
188             appended = true;
189         }
190     }
191 }