added last missing API for maven-dependency-plugin:tree
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1364772 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java
new file mode 100644
index 0000000..be69437
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java
@@ -0,0 +1,142 @@
+package org.apache.maven.shared.dependency.graph.traversal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Stack;
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import org.apache.maven.shared.dependency.graph.internal.DefaultDependencyNode;
+
+/**
+ * A dependency node visitor that clones visited nodes into a new dependency tree. This can be used in conjunction with
+ * a dependency node filter to construct subtrees.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ */
+public class BuildingDependencyNodeVisitor
+ implements DependencyNodeVisitor
+{
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The dependency node visitor to apply on the resultant dependency tree, or <code>null</code> for none.
+ */
+ private final DependencyNodeVisitor visitor;
+
+ /**
+ * The resultant tree parent nodes for the currently visited node.
+ */
+ private final Stack<DependencyNode> parentNodes;
+
+ /**
+ * The root node of the resultant tree.
+ */
+ private DependencyNode rootNode;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node visitor that clones visited nodes into a new dependency tree.
+ */
+ public BuildingDependencyNodeVisitor()
+ {
+ this( null );
+ }
+
+ /**
+ * Creates a dependency node visitor that clones visited nodes into a new dependency tree, and then applies the
+ * specified dependency node visitor on the resultant dependency tree.
+ *
+ * @param visitor
+ * the dependency node visitor to apply on the resultant dependency tree, or <code>null</code> for none
+ */
+ public BuildingDependencyNodeVisitor( DependencyNodeVisitor visitor )
+ {
+ this.visitor = visitor;
+
+ parentNodes = new Stack<DependencyNode>();
+ }
+
+ // DependencyNodeVisitor methods ------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ // clone the node
+ DependencyNode newNode =
+ new DefaultDependencyNode( parentNodes.isEmpty() ? null : parentNodes.peek(), node.getArtifact(),
+ node.getPremanagedScope(), node.getPremanagedVersion(),
+ node.getVersionConstraint() );
+
+ if ( parentNodes.empty() )
+ {
+ rootNode = newNode;
+ }
+ else
+ {
+ DependencyNode parentNode = parentNodes.peek();
+ parentNode.getChildren().add( newNode );
+ }
+
+ parentNodes.push( newNode );
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean endVisit( DependencyNode node )
+ {
+ parentNodes.pop();
+
+ // apply the visitor to the resultant tree on the last visit
+ if ( parentNodes.empty() && visitor != null )
+ {
+ rootNode.accept( visitor );
+ }
+
+ return true;
+ }
+
+ // public methods ---------------------------------------------------------
+
+ /**
+ * Gets the dependency node visitor that this visitor applies on the resultant dependency tree.
+ *
+ * @return the dependency node visitor, or <code>null</code> for none
+ */
+ public DependencyNodeVisitor getDependencyNodeVisitor()
+ {
+ return visitor;
+ }
+
+ /**
+ * Gets the root node of the resultant dependency tree constructed by this visitor.
+ *
+ * @return the root node, or <code>null</code> if the source tree has not yet been visited
+ */
+ public DependencyNode getDependencyTree()
+ {
+ return rootNode;
+ }
+}
diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
new file mode 100644
index 0000000..7db07d0
--- /dev/null
+++ b/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java
@@ -0,0 +1,241 @@
+package org.apache.maven.shared.dependency.graph.traversal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.List;
+
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer.
+ *
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ */
+public class SerializingDependencyNodeVisitor
+ implements DependencyNodeVisitor
+{
+ // classes ----------------------------------------------------------------
+
+ /**
+ * Provides tokens to use when serializing the dependency tree.
+ */
+ public static class TreeTokens
+ {
+ private final String nodeIndent;
+
+ private final String lastNodeIndent;
+
+ private final String fillIndent;
+
+ private final String lastFillIndent;
+
+ public TreeTokens( String nodeIndent, String lastNodeIndent, String fillIndent, String lastFillIndent )
+ {
+ this.nodeIndent = nodeIndent;
+ this.lastNodeIndent = lastNodeIndent;
+ this.fillIndent = fillIndent;
+ this.lastFillIndent = lastFillIndent;
+ }
+
+ public String getNodeIndent( boolean last )
+ {
+ return last ? lastNodeIndent : nodeIndent;
+ }
+
+ public String getFillIndent( boolean last )
+ {
+ return last ? lastFillIndent : fillIndent;
+ }
+ }
+
+ // constants --------------------------------------------------------------
+
+ /**
+ * Whitespace tokens to use when outputing the dependency tree.
+ */
+ public static final TreeTokens WHITESPACE_TOKENS = new TreeTokens( " ", " ", " ", " " );
+
+ /**
+ * The standard ASCII tokens to use when outputing the dependency tree.
+ */
+ public static final TreeTokens STANDARD_TOKENS = new TreeTokens( "+- ", "\\- ", "| ", " " );
+
+ /**
+ * The extended ASCII tokens to use when outputing the dependency tree.
+ */
+ public static final TreeTokens EXTENDED_TOKENS =
+ new TreeTokens( "\u00c3\u00c4 ", "\u00c0\u00c4 ", "\u00b3 ", " " );
+
+ // fields -----------------------------------------------------------------
+
+ /**
+ * The writer to serialize to.
+ */
+ private final PrintWriter writer;
+
+ /**
+ * The tokens to use when serializing the dependency tree.
+ */
+ private final TreeTokens tokens;
+
+ /**
+ * The depth of the currently visited dependency node.
+ */
+ private int depth;
+
+ // constructors -----------------------------------------------------------
+
+ /**
+ * Creates a dependency node visitor that serializes visited nodes to the specified writer using whitespace tokens.
+ *
+ * @param writer
+ * the writer to serialize to
+ */
+ public SerializingDependencyNodeVisitor( Writer writer )
+ {
+ this( writer, WHITESPACE_TOKENS );
+ }
+
+ /**
+ * Creates a dependency node visitor that serializes visited nodes to the specified writer using the specified
+ * tokens.
+ *
+ * @param writer
+ * the writer to serialize to
+ * @param tokens
+ * the tokens to use when serializing the dependency tree
+ */
+ public SerializingDependencyNodeVisitor( Writer writer, TreeTokens tokens )
+ {
+ if ( writer instanceof PrintWriter )
+ {
+ this.writer = (PrintWriter) writer;
+ }
+ else
+ {
+ this.writer = new PrintWriter( writer, true );
+ }
+
+ this.tokens = tokens;
+
+ depth = 0;
+ }
+
+ // DependencyNodeVisitor methods ------------------------------------------
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit( DependencyNode node )
+ {
+ indent( node );
+
+ writer.println( node.toNodeString() );
+
+ depth++;
+
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean endVisit( DependencyNode node )
+ {
+ depth--;
+
+ return true;
+ }
+
+ // private methods --------------------------------------------------------
+
+ /**
+ * Writes the necessary tokens to indent the specified dependency node to this visitor's writer.
+ *
+ * @param node
+ * the dependency node to indent
+ */
+ private void indent( DependencyNode node )
+ {
+ for ( int i = 1; i < depth; i++ )
+ {
+ writer.write( tokens.getFillIndent( isLast( node, i ) ) );
+ }
+
+ if ( depth > 0 )
+ {
+ writer.write( tokens.getNodeIndent( isLast( node ) ) );
+ }
+ }
+
+ /**
+ * Gets whether the specified dependency node is the last of its siblings.
+ *
+ * @param node
+ * the dependency node to check
+ * @return <code>true</code> if the specified dependency node is the last of its last siblings
+ */
+ private boolean isLast( DependencyNode node )
+ {
+ // TODO: remove node argument and calculate from visitor calls only
+
+ DependencyNode parent = node.getParent();
+
+ boolean last;
+
+ if ( parent == null )
+ {
+ last = true;
+ }
+ else
+ {
+ List<DependencyNode> siblings = parent.getChildren();
+
+ last = ( siblings.indexOf( node ) == siblings.size() - 1 );
+ }
+
+ return last;
+ }
+
+ /**
+ * Gets whether the specified dependency node ancestor is the last of its siblings.
+ *
+ * @param node
+ * the dependency node whose ancestor to check
+ * @param ancestorDepth
+ * the depth of the ancestor of the specified dependency node to check
+ * @return <code>true</code> if the specified dependency node ancestor is the last of its siblings
+ */
+ private boolean isLast( DependencyNode node, int ancestorDepth )
+ {
+ // TODO: remove node argument and calculate from visitor calls only
+
+ int distance = depth - ancestorDepth;
+
+ while ( distance-- > 0 )
+ {
+ node = node.getParent();
+ }
+
+ return isLast( node );
+ }
+}