View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.mojo.license;
17  
18  /* 
19   * Codehaus License Maven Plugin
20   *     
21   * This program is free software: you can redistribute it and/or modify
22   * it under the terms of the GNU Lesser General Public License as published by
23   * the Free Software Foundation, either version 3 of the License, or
24   * (at your option) any later version.
25   *
26   * This program is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29   * GNU Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public License
32   * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * A LicenseSummaryWriter.
51   *
52   * @author Paul Gier
53   * @version $Revision: 13519 $
54   * @since 1.0
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          // Prepare the output file File
76          Result result = new StreamResult( outputFile.toURI().getPath() );
77  
78          // Write the DOM document to the file
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 }