[ 
https://issues.apache.org/jira/browse/MDEP-435?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17923085#comment-17923085
 ] 

ASF GitHub Bot commented on MDEP-435:
-------------------------------------

elharo commented on code in PR #24:
URL: 
https://github.com/apache/maven-dependency-plugin/pull/24#discussion_r1938469416


##########
src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java:
##########
@@ -0,0 +1,187 @@
+package org.apache.maven.plugins.dependency.tree;
+
+/*
+ * 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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import 
org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import java.io.Writer;
+import java.util.List;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A dependency node visitor that serializes visited nodes to
+ * <a href="https://en.wikipedia.org/wiki/XML";>XML format</a>

Review Comment:
   period at end



##########
src/main/java/org/apache/maven/plugins/dependency/tree/XMLDependencyNodeVisitor.java:
##########
@@ -0,0 +1,187 @@
+package org.apache.maven.plugins.dependency.tree;
+
+/*
+ * 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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.shared.dependency.graph.DependencyNode;
+import 
org.apache.maven.shared.dependency.graph.traversal.DependencyNodeVisitor;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import java.io.Writer;
+import java.util.List;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A dependency node visitor that serializes visited nodes to
+ * <a href="https://en.wikipedia.org/wiki/XML";>XML format</a>
+ *
+ * @author <a href="mailto:sikora.bog...@webscope.io";>Bogdan Sikora</a>
+ * @since 3.1.2
+ */
+public class XMLDependencyNodeVisitor
+    extends AbstractSerializingVisitor
+    implements DependencyNodeVisitor
+{
+    /**
+     * Constructor.
+     *
+     * @param writer the writer to write to.
+     */
+    public XMLDependencyNodeVisitor( Writer writer )
+    {
+        super( writer );
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean visit( DependencyNode node )
+    {
+        try
+        {
+            if ( node.getParent() == null || node.getParent() == node )
+            {
+                DocumentBuilderFactory dbFactory = 
DocumentBuilderFactory.newInstance();
+                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
+                Document doc = dBuilder.newDocument();
+                Element rootElement = getNode( doc, node, true );
+                doc.appendChild( rootElement );
+
+                List<DependencyNode> children = node.getChildren();
+                for ( DependencyNode child : children )
+                {
+                    handleChild( doc, child, rootElement );
+                }
+
+                TransformerFactory transformerFactory = 
TransformerFactory.newInstance();
+                Transformer transformer = transformerFactory.newTransformer();
+                transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
+                transformer.setOutputProperty( 
"{http://xml.apache.org/xslt}indent-amount";, "2" );
+                DOMSource source = new DOMSource( doc );
+
+                StreamResult console = new StreamResult( writer );
+
+                transformer.transform( source, console );
+            }
+        }
+        catch ( ParserConfigurationException | TransformerException e )
+        {
+            
LoggerFactory.getLogger(XMLDependencyNodeVisitor.class).error(e.getMessage());
+        }
+
+        return true;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean endVisit( DependencyNode node )
+    {
+        return true;
+    }
+
+    /**
+     * Render child with its children recursively
+     *
+     * @param doc Docuemnt to use
+     * @param node child node to handle
+     * @param depth depth of the child
+     */
+    private void handleChild( Document doc, DependencyNode node, Element 
parent )
+    {
+        Element element = getNode( doc, node, false );
+        Node dependencies = parent.getElementsByTagName( "dependencies" 
).item( 0 );
+        dependencies.appendChild( element );
+
+        List<DependencyNode> children = node.getChildren();
+        for ( DependencyNode child : children )
+        {
+            handleChild( doc, child, element );
+        }
+    }
+
+    /**
+     * Get element from node
+     *
+     * @param doc Docuemnt to use
+     * @param node Node to get data from
+     */
+    private Element getNode( Document doc, DependencyNode node, boolean root )
+    {
+        Artifact artifact = node.getArtifact();
+        Element element = null;
+
+        if ( root )
+        {
+            element = doc.createElement( "project" );

Review Comment:
   That feels pretty heavyweight, and it wouldn't be used. Add one if you like, 
but I don't think it's a requirement. If you don add a schema, Relax NG is 
preferable to XSD. 





> improve mvn dependency:tree - add optional xml output of the results
> --------------------------------------------------------------------
>
>                 Key: MDEP-435
>                 URL: https://issues.apache.org/jira/browse/MDEP-435
>             Project: Maven Dependency Plugin
>          Issue Type: New Feature
>          Components: tree
>         Environment: all
>            Reporter: Kow Unk
>            Priority: Major
>              Labels: close-pending
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> The output of mvn dependency:tree would be more useful to me if it was in a 
> format that is machine readable.  I would like to create some tooling to be 
> able to easily determine what is causing a particular jar to be included.
> for example:
> >mymvntool -why org.springframework:org.springframework.beans:3.0.5
> org.springframework.beans-3.0.5 is being *dragged* in by: 
> my.otherproject.core:1.1
> %end of search
> another use:
> >mymvntool -makeexclusionfor 
> >org.springframework:org.springframework.beans:3.0.5
> <exclusions>
>  <exclusion>
>    <groupId>org.springframework</groupId>
>    <artifactId>org.springframework.beans</artifactId>
>  <exclusion>
> </exclusions>
> or even:
> >mymvntool -makeexclusionfor org.springframework:*:3.0.5
> this would pickup all the included jars in the project and build exclusions 
> for them. 
> It may have been dumb to pick spring as the exclusion resource, but this is 
> just an example.  having xml output would be valuable to me and probably 
> others too.
> -K



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to