1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import org.apache.maven.model.License;
36 import org.codehaus.mojo.license.model.ProjectLicenseInfo;
37 import org.w3c.dom.Document;
38 import org.w3c.dom.Node;
39
40 import javax.xml.parsers.DocumentBuilder;
41 import javax.xml.parsers.DocumentBuilderFactory;
42 import javax.xml.parsers.ParserConfigurationException;
43 import javax.xml.transform.*;
44 import javax.xml.transform.dom.DOMSource;
45 import javax.xml.transform.stream.StreamResult;
46 import java.io.File;
47 import java.util.List;
48
49
50
51
52
53
54
55
56 public class LicenseSummaryWriter
57 {
58 public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, File outputFile )
59 throws ParserConfigurationException, TransformerException
60 {
61 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
62 DocumentBuilder parser = fact.newDocumentBuilder();
63 Document doc = parser.newDocument();
64
65 Node root = doc.createElement( "licenseSummary" );
66 doc.appendChild( root );
67 Node dependenciesNode = doc.createElement( "dependencies" );
68 root.appendChild( dependenciesNode );
69
70 for ( ProjectLicenseInfo dep : dependencies )
71 {
72 dependenciesNode.appendChild( createDependencyNode( doc, dep ) );
73 }
74
75
76 Result result = new StreamResult( outputFile.toURI().getPath() );
77
78
79 Transformer xformer = TransformerFactory.newInstance().newTransformer();
80 xformer.setOutputProperty( OutputKeys.INDENT, "yes" );
81 xformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
82 xformer.transform( new DOMSource( doc ), result );
83 }
84
85 public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
86 {
87 Node depNode = doc.createElement( "dependency" );
88
89 Node groupIdNode = doc.createElement( "groupId" );
90 groupIdNode.appendChild( doc.createTextNode( dep.getGroupId() ) );
91 depNode.appendChild( groupIdNode );
92
93 Node artifactIdNode = doc.createElement( "artifactId" );
94 artifactIdNode.appendChild( doc.createTextNode( dep.getArtifactId() ) );
95 depNode.appendChild( artifactIdNode );
96
97 Node versionNode = doc.createElement( "version" );
98 versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
99 depNode.appendChild( versionNode );
100
101 Node licensesNode = doc.createElement( "licenses" );
102 if ( dep.getLicenses() == null || dep.getLicenses().size() == 0 )
103 {
104 licensesNode.appendChild( doc.createComment( "No license information available. " ) );
105 }
106 else
107 {
108 for ( License lic : dep.getLicenses() )
109 {
110 licensesNode.appendChild( createLicenseNode( doc, lic ) );
111 }
112 }
113 depNode.appendChild( licensesNode );
114 return depNode;
115
116 }
117
118 public static Node createLicenseNode( Document doc, License lic )
119 {
120 Node licenseNode = doc.createElement( "license" );
121
122 if ( lic.getName() != null )
123 {
124 Node licNameNode = doc.createElement( "name" );
125 licNameNode.appendChild( doc.createTextNode( lic.getName() ) );
126 licenseNode.appendChild( licNameNode );
127 }
128
129 if ( lic.getUrl() != null )
130 {
131 Node licUrlNode = doc.createElement( "url" );
132 licUrlNode.appendChild( doc.createTextNode( lic.getUrl() ) );
133 licenseNode.appendChild( licUrlNode );
134 }
135
136 if ( lic.getDistribution() != null )
137 {
138 Node licDistNode = doc.createElement( "distribution" );
139 licDistNode.appendChild( doc.createTextNode( lic.getDistribution() ) );
140 licenseNode.appendChild( licDistNode );
141 }
142
143 if ( lic.getComments() != null )
144 {
145 Node licCommentsNode = doc.createElement( "comments" );
146 licCommentsNode.appendChild( doc.createTextNode( lic.getComments() ) );
147 licenseNode.appendChild( licCommentsNode );
148 }
149
150 return licenseNode;
151 }
152
153 }