001 /*
002 * #%L
003 * License Maven Plugin
004 * $Id: LicenseSummaryReader.java 13529 2011-02-07 11:05:33Z tchemit $
005 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/LicenseSummaryReader.java $
006 *
007 * %%
008 * Copyright (C) 2010 - 2011 CodeLutin, Codehaus, Tony Chemit
009 * %%
010 * This program is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU Lesser General Public License as
012 * published by the Free Software Foundation, either version 3 of the
013 * License, or (at your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018 * GNU General Lesser Public License for more details.
019 *
020 * You should have received a copy of the GNU General Lesser Public
021 * License along with this program. If not, see
022 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023 * #L%
024 */
025 package org.codehaus.mojo.license;
026
027 import org.apache.maven.model.License;
028 import org.codehaus.mojo.license.model.ProjectLicenseInfo;
029 import org.w3c.dom.Document;
030 import org.w3c.dom.Element;
031 import org.w3c.dom.Node;
032 import org.w3c.dom.NodeList;
033 import org.xml.sax.SAXException;
034
035 import javax.xml.parsers.DocumentBuilder;
036 import javax.xml.parsers.DocumentBuilderFactory;
037 import javax.xml.parsers.ParserConfigurationException;
038 import java.io.IOException;
039 import java.io.InputStream;
040 import java.util.ArrayList;
041 import java.util.List;
042
043 /**
044 * A LicenseSummaryReader.
045 *
046 * @author Paul Gier
047 * @version $Revision: 13529 $
048 * @since 1.0
049 */
050 public class LicenseSummaryReader
051 {
052
053 /**
054 * Read a component-info.xml from an input stream into a ComponentInfo object.
055 *
056 * @param licSummaryIS Input stream containing the license data
057 * @return List of DependencyProject objects
058 * @throws IOException if there is a problem reading the InputStream
059 * @throws ParserConfigurationException if there is a problem parsing the XML stream
060 * @throws SAXException if there is a problem parsing the XML stream
061 */
062 public static List<ProjectLicenseInfo> parseLicenseSummary( InputStream licSummaryIS )
063 throws IOException, ParserConfigurationException, SAXException
064 {
065 List<ProjectLicenseInfo> dependencies = new ArrayList<ProjectLicenseInfo>();
066
067 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
068 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
069 Document doc = docBuilder.parse( licSummaryIS );
070
071 // normalize text representation
072 doc.getDocumentElement().normalize();
073 Element documentElement = doc.getDocumentElement();
074
075 Node dependenciesNode = documentElement.getElementsByTagName( "dependencies" ).item( 0 );
076 NodeList dependencyNodes = dependenciesNode.getChildNodes();
077
078 for ( int i = 0; i < dependencyNodes.getLength(); ++i )
079 {
080 Node dependencyNode = dependencyNodes.item( i );
081 if ( dependencyNode.getNodeType() == Node.ELEMENT_NODE )
082 {
083 dependencies.add( parseDependencyNode( dependencyNode ) );
084 }
085 }
086
087 return dependencies;
088
089 }
090
091 private static ProjectLicenseInfo parseDependencyNode( Node dependencyNode )
092 {
093 ProjectLicenseInfo dependency = new ProjectLicenseInfo();
094 NodeList depElements = dependencyNode.getChildNodes();
095 for ( int i = 0; i < depElements.getLength(); ++i )
096 {
097 Node node = depElements.item( i );
098
099 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 }