Return-Path: X-Original-To: apmail-maven-commits-archive@www.apache.org Delivered-To: apmail-maven-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 17BC1DA9F for ; Mon, 23 Jul 2012 19:49:23 +0000 (UTC) Received: (qmail 71096 invoked by uid 500); 23 Jul 2012 19:49:23 -0000 Delivered-To: apmail-maven-commits-archive@maven.apache.org Received: (qmail 71008 invoked by uid 500); 23 Jul 2012 19:49:22 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 70998 invoked by uid 99); 23 Jul 2012 19:49:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2012 19:49:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 23 Jul 2012 19:49:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B31152388860; Mon, 23 Jul 2012 19:48:58 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1364772 - in /maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal: BuildingDependencyNodeVisitor.java SerializingDependencyNodeVisitor.java Date: Mon, 23 Jul 2012 19:48:58 -0000 To: commits@maven.apache.org From: hboutemy@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120723194858.B31152388860@eris.apache.org> Author: hboutemy Date: Mon Jul 23 19:48:58 2012 New Revision: 1364772 URL: http://svn.apache.org/viewvc?rev=1364772&view=rev Log: added last missing API for maven-dependency-plugin:tree Added: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java (with props) maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java (with props) Added: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java?rev=1364772&view=auto ============================================================================== --- maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java (added) +++ maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java Mon Jul 23 19:48:58 2012 @@ -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 Mark Hobson + */ +public class BuildingDependencyNodeVisitor + implements DependencyNodeVisitor +{ + // fields ----------------------------------------------------------------- + + /** + * The dependency node visitor to apply on the resultant dependency tree, or null for none. + */ + private final DependencyNodeVisitor visitor; + + /** + * The resultant tree parent nodes for the currently visited node. + */ + private final Stack 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 null for none + */ + public BuildingDependencyNodeVisitor( DependencyNodeVisitor visitor ) + { + this.visitor = visitor; + + parentNodes = new Stack(); + } + + // 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 null 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 null if the source tree has not yet been visited + */ + public DependencyNode getDependencyTree() + { + return rootNode; + } +} Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/BuildingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java?rev=1364772&view=auto ============================================================================== --- maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java (added) +++ maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java Mon Jul 23 19:48:58 2012 @@ -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 Mark Hobson + */ +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 true 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 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 true 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 ); + } +} Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Propchange: maven/shared/trunk/maven-dependency-tree/src/main/java/org/apache/maven/shared/dependency/graph/traversal/SerializingDependencyNodeVisitor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain