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  import org.apache.maven.model.License;
19  import org.codehaus.mojo.license.model.ProjectLicenseInfo;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  import org.w3c.dom.NodeList;
24  import org.xml.sax.SAXException;
25  
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.ParserConfigurationException;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * A LicenseSummaryReader.
36   *
37   * @author Paul Gier
38   * @version $Revision: 13529 $
39   * @since 1.0
40   */
41  public class LicenseSummaryReader
42  {
43  
44      /**
45       * Read a component-info.xml from an input stream into a ComponentInfo object.
46       *
47       * @param licSummaryIS Input stream containing the license data
48       * @return List of DependencyProject objects
49       * @throws IOException                  if there is a problem reading the InputStream
50       * @throws ParserConfigurationException if there is a problem parsing the XML stream
51       * @throws SAXException                 if there is a problem parsing the XML stream
52       */
53      public static List<ProjectLicenseInfo> parseLicenseSummary( InputStream licSummaryIS )
54          throws IOException, ParserConfigurationException, SAXException
55      {
56          List<ProjectLicenseInfo> dependencies = new ArrayList<ProjectLicenseInfo>();
57  
58          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
59          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
60          Document doc = docBuilder.parse( licSummaryIS );
61  
62          // normalize text representation
63          doc.getDocumentElement().normalize();
64          Element documentElement = doc.getDocumentElement();
65  
66          Node dependenciesNode = documentElement.getElementsByTagName( "dependencies" ).item( 0 );
67          NodeList dependencyNodes = dependenciesNode.getChildNodes();
68  
69          for ( int i = 0; i < dependencyNodes.getLength(); ++i )
70          {
71              Node dependencyNode = dependencyNodes.item( i );
72              if ( dependencyNode.getNodeType() == Node.ELEMENT_NODE )
73              {
74                  dependencies.add( parseDependencyNode( dependencyNode ) );
75              }
76          }
77  
78          return dependencies;
79  
80      }
81  
82      private static ProjectLicenseInfo parseDependencyNode( Node dependencyNode )
83      {
84          ProjectLicenseInfo dependency = new ProjectLicenseInfo();
85          NodeList depElements = dependencyNode.getChildNodes();
86          for ( int i = 0; i < depElements.getLength(); ++i )
87          {
88              Node node = depElements.item( i );
89  
90              if ( node.getNodeName().equals( "groupId" ) )
91              {
92                  dependency.setGroupId( node.getTextContent() );
93              }
94              else if ( node.getNodeName().equals( "artifactId" ) )
95              {
96                  dependency.setArtifactId( node.getTextContent() );
97              }
98              else if ( node.getNodeName().equals( "version" ) )
99              {
100                 dependency.setVersion( node.getTextContent() );
101             }
102             else if ( node.getNodeName().equals( "licenses" ) )
103             {
104                 NodeList licensesChildNodes = node.getChildNodes();
105                 for ( int j = 0; j < licensesChildNodes.getLength(); ++j )
106                 {
107                     Node licensesChildNode = licensesChildNodes.item( j );
108                     if ( licensesChildNode.getNodeName().equals( "license" ) )
109                     {
110                         License license = parseLicense( licensesChildNode );
111                         dependency.addLicense( license );
112                     }
113                 }
114             }
115         }
116         return dependency;
117     }
118 
119     private static License parseLicense( Node licenseNode )
120     {
121         License license = new License();
122         NodeList licenseElements = licenseNode.getChildNodes();
123         for ( int i = 0; i < licenseElements.getLength(); ++i )
124         {
125             Node node = licenseElements.item( i );
126             if ( node.getNodeName().equals( "name" ) )
127             {
128                 license.setName( node.getTextContent() );
129             }
130             else if ( node.getNodeName().equals( "url" ) )
131             {
132                 license.setUrl( node.getTextContent() );
133             }
134             else if ( node.getNodeName().equals( "distribution" ) )
135             {
136                 license.setDistribution( node.getTextContent() );
137             }
138             else if ( node.getNodeName().equals( "comments" ) )
139             {
140                 license.setComments( node.getTextContent() );
141             }
142         }
143         return license;
144     }
145 
146 }