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