View Javadoc

1   package org.codehaus.mojo.license;
2   
3   /* 
4    * Codehaus License Maven Plugin
5    *     
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
18   */
19  
20  import org.apache.maven.model.License;
21  import org.codehaus.mojo.license.model.ProjectLicenseInfo;
22  import org.w3c.dom.Document;
23  import org.w3c.dom.Node;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.DocumentBuilderFactory;
27  import javax.xml.parsers.ParserConfigurationException;
28  import javax.xml.transform.*;
29  import javax.xml.transform.dom.DOMSource;
30  import javax.xml.transform.stream.StreamResult;
31  import java.io.File;
32  import java.util.List;
33  
34  /**
35   * A LicenseSummaryWriter.
36   *
37   * @author Paul Gier
38   * @version $Revision: 13519 $
39   * @since 1.0
40   */
41  public class LicenseSummaryWriter
42  {
43      public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, File outputFile )
44          throws ParserConfigurationException, TransformerException
45      {
46          DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
47          DocumentBuilder parser = fact.newDocumentBuilder();
48          Document doc = parser.newDocument();
49  
50          Node root = doc.createElement( "licenseSummary" );
51          doc.appendChild( root );
52          Node dependenciesNode = doc.createElement( "dependencies" );
53          root.appendChild( dependenciesNode );
54  
55          for ( ProjectLicenseInfo dep : dependencies )
56          {
57              dependenciesNode.appendChild( createDependencyNode( doc, dep ) );
58          }
59  
60          // Prepare the output file File
61          Result result = new StreamResult( outputFile.toURI().getPath() );
62  
63          // Write the DOM document to the file
64          Transformer xformer = TransformerFactory.newInstance().newTransformer();
65          xformer.setOutputProperty( OutputKeys.INDENT, "yes" );
66          xformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
67          xformer.transform( new DOMSource( doc ), result );
68      }
69  
70      public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
71      {
72          Node depNode = doc.createElement( "dependency" );
73  
74          Node groupIdNode = doc.createElement( "groupId" );
75          groupIdNode.appendChild( doc.createTextNode( dep.getGroupId() ) );
76          depNode.appendChild( groupIdNode );
77  
78          Node artifactIdNode = doc.createElement( "artifactId" );
79          artifactIdNode.appendChild( doc.createTextNode( dep.getArtifactId() ) );
80          depNode.appendChild( artifactIdNode );
81  
82          Node versionNode = doc.createElement( "version" );
83          versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
84          depNode.appendChild( versionNode );
85  
86          Node licensesNode = doc.createElement( "licenses" );
87          if ( dep.getLicenses() == null || dep.getLicenses().size() == 0 )
88          {
89              licensesNode.appendChild( doc.createComment( "No license information available. " ) );
90          }
91          else
92          {
93              for ( License lic : dep.getLicenses() )
94              {
95                  licensesNode.appendChild( createLicenseNode( doc, lic ) );
96              }
97          }
98          depNode.appendChild( licensesNode );
99          return depNode;
100 
101     }
102 
103     public static Node createLicenseNode( Document doc, License lic )
104     {
105         Node licenseNode = doc.createElement( "license" );
106 
107         if ( lic.getName() != null )
108         {
109             Node licNameNode = doc.createElement( "name" );
110             licNameNode.appendChild( doc.createTextNode( lic.getName() ) );
111             licenseNode.appendChild( licNameNode );
112         }
113 
114         if ( lic.getUrl() != null )
115         {
116             Node licUrlNode = doc.createElement( "url" );
117             licUrlNode.appendChild( doc.createTextNode( lic.getUrl() ) );
118             licenseNode.appendChild( licUrlNode );
119         }
120 
121         if ( lic.getDistribution() != null )
122         {
123             Node licDistNode = doc.createElement( "distribution" );
124             licDistNode.appendChild( doc.createTextNode( lic.getDistribution() ) );
125             licenseNode.appendChild( licDistNode );
126         }
127 
128         if ( lic.getComments() != null )
129         {
130             Node licCommentsNode = doc.createElement( "comments" );
131             licCommentsNode.appendChild( doc.createTextNode( lic.getComments() ) );
132             licenseNode.appendChild( licCommentsNode );
133         }
134 
135         return licenseNode;
136     }
137 
138 }