1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license;
17
18 import org.apache.maven.model.License;
19 import org.codehaus.mojo.license.model.ProjectLicenseInfo;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24 import org.xml.sax.SAXException;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.List;
33
34
35
36
37
38
39
40
41 public class LicenseSummaryReader
42 {
43
44
45
46
47
48
49
50
51
52
53 public static List<ProjectLicenseInfo> parseLicenseSummary( InputStream licSummaryIS )
54 throws IOException, ParserConfigurationException, SAXException
55 {
56 List<ProjectLicenseInfo> dependencies = new ArrayList<ProjectLicenseInfo>();
57
58 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
59 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
60 Document doc = docBuilder.parse( licSummaryIS );
61
62
63 doc.getDocumentElement().normalize();
64 Element documentElement = doc.getDocumentElement();
65
66 Node dependenciesNode = documentElement.getElementsByTagName( "dependencies" ).item( 0 );
67 NodeList dependencyNodes = dependenciesNode.getChildNodes();
68
69 for ( int i = 0; i < dependencyNodes.getLength(); ++i )
70 {
71 Node dependencyNode = dependencyNodes.item( i );
72 if ( dependencyNode.getNodeType() == Node.ELEMENT_NODE )
73 {
74 dependencies.add( parseDependencyNode( dependencyNode ) );
75 }
76 }
77
78 return dependencies;
79
80 }
81
82 private static ProjectLicenseInfo parseDependencyNode( Node dependencyNode )
83 {
84 ProjectLicenseInfo dependency = new ProjectLicenseInfo();
85 NodeList depElements = dependencyNode.getChildNodes();
86 for ( int i = 0; i < depElements.getLength(); ++i )
87 {
88 Node node = depElements.item( i );
89
90 if ( node.getNodeName().equals( "groupId" ) )
91 {
92 dependency.setGroupId( node.getTextContent() );
93 }
94 else if ( node.getNodeName().equals( "artifactId" ) )
95 {
96 dependency.setArtifactId( node.getTextContent() );
97 }
98 else if ( node.getNodeName().equals( "version" ) )
99 {
100 dependency.setVersion( node.getTextContent() );
101 }
102 else if ( node.getNodeName().equals( "licenses" ) )
103 {
104 NodeList licensesChildNodes = node.getChildNodes();
105 for ( int j = 0; j < licensesChildNodes.getLength(); ++j )
106 {
107 Node licensesChildNode = licensesChildNodes.item( j );
108 if ( licensesChildNode.getNodeName().equals( "license" ) )
109 {
110 License license = parseLicense( licensesChildNode );
111 dependency.addLicense( license );
112 }
113 }
114 }
115 }
116 return dependency;
117 }
118
119 private static License parseLicense( Node licenseNode )
120 {
121 License license = new License();
122 NodeList licenseElements = licenseNode.getChildNodes();
123 for ( int i = 0; i < licenseElements.getLength(); ++i )
124 {
125 Node node = licenseElements.item( i );
126 if ( node.getNodeName().equals( "name" ) )
127 {
128 license.setName( node.getTextContent() );
129 }
130 else if ( node.getNodeName().equals( "url" ) )
131 {
132 license.setUrl( node.getTextContent() );
133 }
134 else if ( node.getNodeName().equals( "distribution" ) )
135 {
136 license.setDistribution( node.getTextContent() );
137 }
138 else if ( node.getNodeName().equals( "comments" ) )
139 {
140 license.setComments( node.getTextContent() );
141 }
142 }
143 return license;
144 }
145
146 }