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  /**
29   * Default implementation of a DependencyNode.
30   */
31  public class DefaultDependencyNode implements DependencyNode
32  {
33      private final Artifact artifact;
34  
35      private final DependencyNode parent;
36  
37      private final String premanagedVersion;
38  
39      private final String premanagedScope;
40  
41      private final String versionConstraint;
42  
43      private List<DependencyNode> children;
44  
45      private Boolean optional;
46  
47      /**
48       * Constructs the DefaultDependencyNode.
49       *
50       * @param parent            Parent node, may be {@code null}.
51       * @param artifact          Artifact associated with this dependency.
52       * @param premanagedVersion the premanaged version, may be {@code null}.
53       * @param premanagedScope   the premanaged scope, may be {@code null}.
54       * @param versionConstraint the version constraint, may be {@code null.}
55       */
56      public DefaultDependencyNode( DependencyNode parent, Artifact artifact, String premanagedVersion,
57                                    String premanagedScope, String versionConstraint )
58      {
59          this.parent = parent;
60          this.artifact = artifact;
61          this.premanagedVersion = premanagedVersion;
62          this.premanagedScope = premanagedScope;
63          this.versionConstraint = versionConstraint;
64      }
65  
66      public DefaultDependencyNode( DependencyNode parent, Artifact artifact, String premanagedVersion,
67                                    String premanagedScope, String versionConstraint, Boolean optional )
68      {
69          this.parent = parent;
70          this.artifact = artifact;
71          this.premanagedVersion = premanagedVersion;
72          this.premanagedScope = premanagedScope;
73          this.versionConstraint = versionConstraint;
74          this.optional = optional;
75      }
76  
77      /**
78       * Applies the specified dependency node visitor to this dependency node and its children.
79       * 
80       * @param visitor the dependency node visitor to use
81       * @return the visitor result of ending the visit to this node
82       * @since 1.1
83       */
84      @Override
85      public boolean accept( DependencyNodeVisitor visitor )
86      {
87          if ( visitor.visit( this ) )
88          {
89              for ( DependencyNode child : getChildren() )
90              {
91                  if ( !child.accept( visitor ) )
92                  {
93                      break;
94                  }
95              }
96          }
97  
98          return visitor.endVisit( this );
99      }
100 
101     /**
102      * @return Artifact for this DependencyNode.
103      */
104     @Override
105     public Artifact getArtifact()
106     {
107         return artifact;
108     }
109 
110     /**
111      *
112      * @param children  List of DependencyNode to set as child nodes.
113      */
114     public void setChildren( List<DependencyNode> children )
115     {
116         this.children = children;
117     }
118 
119     /**
120      * @return List of child nodes for this DependencyNode.
121      */
122     @Override
123     public List<DependencyNode> getChildren()
124     {
125         return children;
126     }
127 
128     /**
129      * @return Parent of this DependencyNode.
130      */
131     @Override
132     public DependencyNode getParent()
133     {
134         return parent;
135     }
136 
137     @Override
138     public String getPremanagedVersion()
139     {
140         return premanagedVersion;
141     }
142 
143     @Override
144     public String getPremanagedScope()
145     {
146         return premanagedScope;
147     }
148 
149     @Override
150     public String getVersionConstraint()
151     {
152         return versionConstraint;
153     }
154 
155     @Override
156     public Boolean getOptional()
157     {
158         return optional;
159     }
160 
161     /**
162      * @return Stringified representation of this DependencyNode.
163      */
164     @Override
165     public String toNodeString()
166     {
167         StringBuffer buffer = new StringBuffer();
168 
169         buffer.append( artifact );
170 
171         ItemAppender appender = new ItemAppender( buffer, " (", "; ", ")" );
172 
173         if ( getPremanagedVersion() != null )
174         {
175             appender.append( "version managed from ", getPremanagedVersion() );
176         }
177 
178         if ( getPremanagedScope() != null )
179         {
180             appender.append( "scope managed from ", getPremanagedScope() );
181         }
182 
183         if ( getVersionConstraint() != null )
184         {
185             appender.append( "version selected from constraint ", getVersionConstraint() );
186         }
187 
188 
189         appender.flush();
190         if ( optional != null && optional )
191         {
192             buffer.append( " (optional) " );
193         }
194 
195         return buffer.toString();
196     }
197 
198     /**
199      * Utility class to concatenate a number of parameters with separator tokens.
200      */
201     private static class ItemAppender
202     {
203         private StringBuffer buffer;
204 
205         private String startToken;
206 
207         private String separatorToken;
208 
209         private String endToken;
210 
211         private boolean appended;
212 
213         public ItemAppender( StringBuffer buffer, String startToken, String separatorToken, String endToken )
214         {
215             this.buffer = buffer;
216             this.startToken = startToken;
217             this.separatorToken = separatorToken;
218             this.endToken = endToken;
219 
220             appended = false;
221         }
222 
223         public ItemAppender append( String item1, String item2 )
224         {
225             appendToken();
226 
227             buffer.append( item1 ).append( item2 );
228 
229             return this;
230         }
231 
232         public void flush()
233         {
234             if ( appended )
235             {
236                 buffer.append( endToken );
237 
238                 appended = false;
239             }
240         }
241 
242         private void appendToken()
243         {
244             buffer.append( appended ? separatorToken : startToken );
245 
246             appended = true;
247         }
248     }
249 }