felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mccu...@apache.org
Subject svn commit: r810655 [2/2] - in /felix/trunk/bundleplugin: ./ src/main/java/org/apache/maven/shared/dependency/ src/main/java/org/apache/maven/shared/dependency/tree/ src/main/java/org/apache/maven/shared/dependency/tree/filter/ src/main/java/org/apache...
Date Wed, 02 Sep 2009 18:33:42 GMT
Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/DependencyNodeFilter.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/DependencyNodeFilter.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/DependencyNodeFilter.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/DependencyNodeFilter.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,41 @@
+package org.apache.maven.shared.dependency.tree.filter;
+
+/*
+ * 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 org.apache.maven.shared.dependency.tree.DependencyNode;
+
+/**
+ * Defines a filter for dependency nodes.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: DependencyNodeFilter.java 661727 2008-05-30 14:21:49Z bentmann $
+ * @since 1.1
+ */
+public interface DependencyNodeFilter
+{
+    /**
+     * Gets whether this filter accepts the specified dependency node.
+     * 
+     * @param node
+     *            the dependency node to check
+     * @return <code>true</code> if this filter accepts the specified dependency
node
+     */
+    boolean accept( DependencyNode node );
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/DependencyNodeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/StateDependencyNodeFilter.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/StateDependencyNodeFilter.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/StateDependencyNodeFilter.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/StateDependencyNodeFilter.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,90 @@
+package org.apache.maven.shared.dependency.tree.filter;
+
+/*
+ * 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 org.apache.maven.shared.dependency.tree.DependencyNode;
+
+/**
+ * A dependency node filter that accepts nodes depending on their state.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: StateDependencyNodeFilter.java 661727 2008-05-30 14:21:49Z bentmann $
+ * @since 1.1
+ */
+public class StateDependencyNodeFilter implements DependencyNodeFilter
+{
+    // constants --------------------------------------------------------------
+
+    /**
+     * A dependency node filter that only accepts included nodes.
+     */
+    public static final StateDependencyNodeFilter INCLUDED = new StateDependencyNodeFilter(
DependencyNode.INCLUDED );
+
+    // fields -----------------------------------------------------------------
+
+    /**
+     * The state of dependency nodes to accept.
+     * 
+     * @see DependencyNode
+     */
+    private final int state;
+
+    // constructors -----------------------------------------------------------
+
+    /**
+     * Creates a dependency node filter that only accepts dependency nodes of the specified
state.
+     * 
+     * @param state
+     *            the state of dependency nodes to accept
+     * @throws IllegalArgumentException
+     *             if the specified state is invalid
+     */
+    public StateDependencyNodeFilter( int state )
+    {
+        if ( state < DependencyNode.INCLUDED || state > DependencyNode.OMITTED_FOR_CYCLE
)
+        {
+            throw new IllegalArgumentException( "Unknown state: " + state );
+        }
+
+        this.state = state;
+    }
+
+    // DependencyNodeFilter methods -------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean accept( DependencyNode node )
+    {
+        return node.getState() == state;
+    }
+
+    // public methods ---------------------------------------------------------
+
+    /**
+     * Gets the dependency node state that this filter accepts.
+     * 
+     * @return the dependency node state that this filter accepts
+     */
+    public int getState()
+    {
+        return state;
+    }
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/filter/StateDependencyNodeFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/BuildingDependencyNodeVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/BuildingDependencyNodeVisitor.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/BuildingDependencyNodeVisitor.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/BuildingDependencyNodeVisitor.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,143 @@
+package org.apache.maven.shared.dependency.tree.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.tree.DependencyNode;
+
+/**
+ * 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>
+ * @version $Id: BuildingDependencyNodeVisitor.java 661727 2008-05-30 14:21:49Z bentmann
$
+ * @since 1.1
+ */
+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 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();
+    }
+
+    // DependencyNodeVisitor methods ------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean visit( DependencyNode node )
+    {
+        // clone the node
+        DependencyNode newNode = new DependencyNode( node.getArtifact(), node.getState(),
node.getRelatedArtifact() );
+        newNode.setOriginalScope( node.getOriginalScope() );
+        newNode.setFailedUpdateScope( node.getFailedUpdateScope() );
+        newNode.setPremanagedVersion( node.getPremanagedVersion() );
+        newNode.setPremanagedScope( node.getPremanagedScope() );
+
+        if ( parentNodes.empty() )
+        {
+            rootNode = newNode;
+        }
+        else
+        {
+            DependencyNode parentNode = (DependencyNode) parentNodes.peek();
+            parentNode.addChild( 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;
+    }
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/BuildingDependencyNodeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/CollectingDependencyNodeVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/CollectingDependencyNodeVisitor.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/CollectingDependencyNodeVisitor.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/CollectingDependencyNodeVisitor.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,86 @@
+package org.apache.maven.shared.dependency.tree.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.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.shared.dependency.tree.DependencyNode;
+
+/**
+ * A dependency node visitor that collects visited nodes for further processing.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: CollectingDependencyNodeVisitor.java 661727 2008-05-30 14:21:49Z bentmann
$
+ * @since 1.1
+ */
+public class CollectingDependencyNodeVisitor implements DependencyNodeVisitor
+{
+    // fields -----------------------------------------------------------------
+
+    /**
+     * The collected list of nodes.
+     */
+    private final List nodes;
+
+    // constructors -----------------------------------------------------------
+
+    /**
+     * Creates a dependency node visitor that collects visited nodes for further processing.
+     */
+    public CollectingDependencyNodeVisitor()
+    {
+        nodes = new ArrayList();
+    }
+
+    // DependencyNodeVisitor methods ------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean visit( DependencyNode node )
+    {
+        // collect node
+        nodes.add( node );
+
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean endVisit( DependencyNode node )
+    {
+        return true;
+    }
+
+    // public methods ---------------------------------------------------------
+
+    /**
+     * Gets the list of collected dependency nodes.
+     * 
+     * @return the list of collected dependency nodes
+     */
+    public List getNodes()
+    {
+        return Collections.unmodifiableList( nodes );
+    }
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/CollectingDependencyNodeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/DependencyNodeVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/DependencyNodeVisitor.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/DependencyNodeVisitor.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/DependencyNodeVisitor.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,52 @@
+package org.apache.maven.shared.dependency.tree.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 org.apache.maven.shared.dependency.tree.DependencyNode;
+
+/**
+ * Defines a hierarchical visitor for processing dependency node trees.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: DependencyNodeVisitor.java 661727 2008-05-30 14:21:49Z bentmann $
+ * @since 1.1
+ */
+public interface DependencyNodeVisitor
+{
+    /**
+     * Starts the visit to the specified dependency node.
+     * 
+     * @param node
+     *            the dependency node to visit
+     * @return <code>true</code> to visit the specified dependency node's children,
<code>false</code> to skip the
+     *         specified dependency node's children and proceed to its next sibling
+     */
+    boolean visit( DependencyNode node );
+
+    /**
+     * Ends the visit to to the specified dependency node.
+     * 
+     * @param node
+     *            the dependency node to visit
+     * @return <code>true</code> to visit the specified dependency node's next
sibling, <code>false</code> to skip
+     *         the specified dependency node's next siblings and proceed to its parent
+     */
+    boolean endVisit( DependencyNode node );
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/DependencyNodeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,124 @@
+package org.apache.maven.shared.dependency.tree.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 org.apache.maven.shared.dependency.tree.DependencyNode;
+import org.apache.maven.shared.dependency.tree.filter.DependencyNodeFilter;
+
+/**
+ * A dependency node visitor that filters nodes and delegates to another visitor.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: FilteringDependencyNodeVisitor.java 661727 2008-05-30 14:21:49Z bentmann
$
+ * @since 1.1
+ */
+public class FilteringDependencyNodeVisitor implements DependencyNodeVisitor
+{
+    // fields -----------------------------------------------------------------
+
+    /**
+     * The dependency node visitor to delegate to.
+     */
+    private final DependencyNodeVisitor visitor;
+
+    /**
+     * The dependency node filter to apply before delegation.
+     */
+    private final DependencyNodeFilter filter;
+
+    // constructors -----------------------------------------------------------
+
+    /**
+     * Creates a dependency node visitor that delegates nodes that are accepted by the specified
filter to the specified
+     * visitor.
+     * 
+     * @param visitor
+     *            the dependency node visitor to delegate to
+     * @param filter
+     *            the dependency node filter to apply before delegation
+     */
+    public FilteringDependencyNodeVisitor( DependencyNodeVisitor visitor, DependencyNodeFilter
filter )
+    {
+        this.visitor = visitor;
+        this.filter = filter;
+    }
+
+    // DependencyNodeVisitor methods ------------------------------------------
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean visit( DependencyNode node )
+    {
+        boolean visit;
+
+        if ( filter.accept( node ) )
+        {
+            visit = visitor.visit( node );
+        }
+        else
+        {
+            visit = true;
+        }
+
+        return visit;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean endVisit( DependencyNode node )
+    {
+        boolean visit;
+
+        if ( filter.accept( node ) )
+        {
+            visit = visitor.endVisit( node );
+        }
+        else
+        {
+            visit = true;
+        }
+
+        return visit;
+    }
+
+    // public methods ---------------------------------------------------------
+
+    /**
+     * Gets the dependency node visitor that this visitor delegates to.
+     * 
+     * @return the dependency node visitor
+     */
+    public DependencyNodeVisitor getDependencyNodeVisitor()
+    {
+        return visitor;
+    }
+
+    /**
+     * Gets the dependency node filter that this visitor applies before delegation.
+     * 
+     * @return the dependency node filter
+     */
+    public DependencyNodeFilter getDependencyNodeFilter()
+    {
+        return filter;
+    }
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/FilteringDependencyNodeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java
URL: http://svn.apache.org/viewvc/felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java?rev=810655&view=auto
==============================================================================
--- felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java
(added)
+++ felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java
Wed Sep  2 18:33:41 2009
@@ -0,0 +1,242 @@
+package org.apache.maven.shared.dependency.tree.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.tree.DependencyNode;
+
+/**
+ * A dependency node visitor that serializes visited nodes to a writer.
+ * 
+ * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
+ * @version $Id: SerializingDependencyNodeVisitor.java 661727 2008-05-30 14:21:49Z bentmann
$
+ * @since 1.1
+ */
+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 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 );
+    }
+}

Propchange: felix/trunk/bundleplugin/src/main/java/org/apache/maven/shared/dependency/tree/traversal/SerializingDependencyNodeVisitor.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message