001 /** 002 * Copyright 2010-2012 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.license; 017 018 import org.apache.maven.model.License; 019 import org.codehaus.mojo.license.model.ProjectLicenseInfo; 020 import org.w3c.dom.Document; 021 import org.w3c.dom.Element; 022 import org.w3c.dom.Node; 023 import org.w3c.dom.NodeList; 024 import org.xml.sax.SAXException; 025 026 import javax.xml.parsers.DocumentBuilder; 027 import javax.xml.parsers.DocumentBuilderFactory; 028 import javax.xml.parsers.ParserConfigurationException; 029 import java.io.IOException; 030 import java.io.InputStream; 031 import java.util.ArrayList; 032 import java.util.List; 033 034 /** 035 * A LicenseSummaryReader. 036 * 037 * @author Paul Gier 038 * @version $Revision: 13529 $ 039 * @since 1.0 040 */ 041 public class LicenseSummaryReader 042 { 043 044 /** 045 * Read a component-info.xml from an input stream into a ComponentInfo object. 046 * 047 * @param licSummaryIS Input stream containing the license data 048 * @return List of DependencyProject objects 049 * @throws IOException if there is a problem reading the InputStream 050 * @throws ParserConfigurationException if there is a problem parsing the XML stream 051 * @throws SAXException if there is a problem parsing the XML stream 052 */ 053 public static List<ProjectLicenseInfo> parseLicenseSummary( InputStream licSummaryIS ) 054 throws IOException, ParserConfigurationException, SAXException 055 { 056 List<ProjectLicenseInfo> dependencies = new ArrayList<ProjectLicenseInfo>(); 057 058 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 059 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 060 Document doc = docBuilder.parse( licSummaryIS ); 061 062 // normalize text representation 063 doc.getDocumentElement().normalize(); 064 Element documentElement = doc.getDocumentElement(); 065 066 Node dependenciesNode = documentElement.getElementsByTagName( "dependencies" ).item( 0 ); 067 NodeList dependencyNodes = dependenciesNode.getChildNodes(); 068 069 for ( int i = 0; i < dependencyNodes.getLength(); ++i ) 070 { 071 Node dependencyNode = dependencyNodes.item( i ); 072 if ( dependencyNode.getNodeType() == Node.ELEMENT_NODE ) 073 { 074 dependencies.add( parseDependencyNode( dependencyNode ) ); 075 } 076 } 077 078 return dependencies; 079 080 } 081 082 private static ProjectLicenseInfo parseDependencyNode( Node dependencyNode ) 083 { 084 ProjectLicenseInfo dependency = new ProjectLicenseInfo(); 085 NodeList depElements = dependencyNode.getChildNodes(); 086 for ( int i = 0; i < depElements.getLength(); ++i ) 087 { 088 Node node = depElements.item( i ); 089 090 if ( node.getNodeName().equals( "groupId" ) ) 091 { 092 dependency.setGroupId( node.getTextContent() ); 093 } 094 else if ( node.getNodeName().equals( "artifactId" ) ) 095 { 096 dependency.setArtifactId( node.getTextContent() ); 097 } 098 else if ( node.getNodeName().equals( "version" ) ) 099 { 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 }