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