View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    * $Id: LicenseSummaryReader.java 13529 2011-02-07 11:05:33Z tchemit $
5    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/LicenseSummaryReader.java $
6    *
7    * %%
8    * Copyright (C) 2010 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
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   * A LicenseSummaryReader.
45   *
46   * @author Paul Gier
47   * @version $Revision: 13529 $
48   * @since 1.0
49   */
50  public class LicenseSummaryReader
51  {
52  
53      /**
54       * Read a component-info.xml from an input stream into a ComponentInfo object.
55       *
56       * @param licSummaryIS Input stream containing the license data
57       * @return List of DependencyProject objects
58       * @throws IOException                  if there is a problem reading the InputStream
59       * @throws ParserConfigurationException if there is a problem parsing the XML stream
60       * @throws SAXException                 if there is a problem parsing the XML stream
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          // normalize text representation
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 }