View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.dependency.graph.internal;
20  
21  import java.util.List;
22  
23  import org.apache.maven.artifact.Artifact;
24  import org.apache.maven.model.Exclusion;
25  import org.apache.maven.shared.dependency.graph.DependencyNode;
26  
27  class VerboseDependencyNode extends DefaultDependencyNode {
28  
29      private final ConflictData data;
30  
31      VerboseDependencyNode(
32              DependencyNode parent,
33              Artifact artifact,
34              String premanagedVersion,
35              String premanagedScope,
36              String versionConstraint,
37              Boolean optional,
38              List<Exclusion> exclusions,
39              ConflictData data) {
40          super(parent, artifact, premanagedVersion, premanagedScope, versionConstraint, optional, exclusions);
41  
42          this.data = data;
43      }
44  
45      @Override
46      public String toNodeString() {
47          StringBuilder buffer = new StringBuilder();
48  
49          boolean included = (data.getWinnerVersion() == null);
50  
51          if (!included) {
52              buffer.append('(');
53          }
54  
55          buffer.append(getArtifact());
56  
57          ItemAppender appender = new ItemAppender(buffer, included ? " (" : " - ", "; ", included ? ")" : "");
58  
59          if (getPremanagedVersion() != null) {
60              appender.append("version managed from ", getPremanagedVersion());
61          }
62  
63          if (getPremanagedScope() != null) {
64              appender.append("scope managed from ", getPremanagedScope());
65          }
66  
67          if (data.getOriginalScope() != null) {
68              appender.append("scope updated from ", data.getOriginalScope());
69          }
70  
71          if (data.getIgnoredScope() != null) {
72              appender.append("scope not updated to ", data.getIgnoredScope());
73          }
74  
75          //        if ( getVersionSelectedFromRange() != null )
76          //        {
77          //            appender.append( "version selected from range ", getVersionSelectedFromRange().toString() );
78          //            appender.append( "available versions ", getAvailableVersions().toString() );
79          //        }
80  
81          if (!included) {
82              String winnerVersion = data.getWinnerVersion();
83              if (winnerVersion.equals(getArtifact().getVersion())) {
84                  appender.append("omitted for duplicate");
85              } else {
86                  appender.append("omitted for conflict with ", winnerVersion);
87              }
88          }
89  
90          appender.flush();
91  
92          if (!included) {
93              buffer.append(')');
94          }
95  
96          return buffer.toString();
97      }
98  
99      /**
100      * Utility class to concatenate a number of parameters with separator tokens.
101      */
102     private static class ItemAppender {
103         private StringBuilder buffer;
104 
105         private String startToken;
106 
107         private String separatorToken;
108 
109         private String endToken;
110 
111         private boolean appended;
112 
113         ItemAppender(StringBuilder buffer, String startToken, String separatorToken, String endToken) {
114             this.buffer = buffer;
115             this.startToken = startToken;
116             this.separatorToken = separatorToken;
117             this.endToken = endToken;
118 
119             appended = false;
120         }
121 
122         public ItemAppender append(String item1) {
123             appendToken();
124 
125             buffer.append(item1);
126 
127             return this;
128         }
129 
130         public ItemAppender append(String item1, String item2) {
131             appendToken();
132 
133             buffer.append(item1).append(item2);
134 
135             return this;
136         }
137 
138         public void flush() {
139             if (appended) {
140                 buffer.append(endToken);
141 
142                 appended = false;
143             }
144         }
145 
146         private void appendToken() {
147             buffer.append(appended ? separatorToken : startToken);
148 
149             appended = true;
150         }
151     }
152 }