001    /**
002     * Copyright 2010-2013 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    /* 
019     * Codehaus License Maven Plugin
020     *     
021     * This program is free software: you can redistribute it and/or modify
022     * it under the terms of the GNU Lesser General Public License as published by
023     * the Free Software Foundation, either version 3 of the License, or
024     * (at your option) any later version.
025     *
026     * This program is distributed in the hope that it will be useful,
027     * but WITHOUT ANY WARRANTY; without even the implied warranty of
028     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
029     * GNU Lesser General Public License for more details.
030     *
031     * You should have received a copy of the GNU Lesser General Public License
032     * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
033     */
034    
035    import org.apache.maven.model.License;
036    import org.codehaus.mojo.license.model.ProjectLicenseInfo;
037    import org.w3c.dom.Document;
038    import org.w3c.dom.Node;
039    
040    import javax.xml.parsers.DocumentBuilder;
041    import javax.xml.parsers.DocumentBuilderFactory;
042    import javax.xml.parsers.ParserConfigurationException;
043    import javax.xml.transform.*;
044    import javax.xml.transform.dom.DOMSource;
045    import javax.xml.transform.stream.StreamResult;
046    import java.io.File;
047    import java.util.List;
048    
049    /**
050     * A LicenseSummaryWriter.
051     *
052     * @author Paul Gier
053     * @version $Revision: 13519 $
054     * @since 1.0
055     */
056    public class LicenseSummaryWriter
057    {
058        public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, File outputFile )
059            throws ParserConfigurationException, TransformerException
060        {
061            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
062            DocumentBuilder parser = fact.newDocumentBuilder();
063            Document doc = parser.newDocument();
064    
065            Node root = doc.createElement( "licenseSummary" );
066            doc.appendChild( root );
067            Node dependenciesNode = doc.createElement( "dependencies" );
068            root.appendChild( dependenciesNode );
069    
070            for ( ProjectLicenseInfo dep : dependencies )
071            {
072                dependenciesNode.appendChild( createDependencyNode( doc, dep ) );
073            }
074    
075            // Prepare the output file File
076            Result result = new StreamResult( outputFile.toURI().getPath() );
077    
078            // Write the DOM document to the file
079            Transformer xformer = TransformerFactory.newInstance().newTransformer();
080            xformer.setOutputProperty( OutputKeys.INDENT, "yes" );
081            xformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
082            xformer.transform( new DOMSource( doc ), result );
083        }
084    
085        public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
086        {
087            Node depNode = doc.createElement( "dependency" );
088    
089            Node groupIdNode = doc.createElement( "groupId" );
090            groupIdNode.appendChild( doc.createTextNode( dep.getGroupId() ) );
091            depNode.appendChild( groupIdNode );
092    
093            Node artifactIdNode = doc.createElement( "artifactId" );
094            artifactIdNode.appendChild( doc.createTextNode( dep.getArtifactId() ) );
095            depNode.appendChild( artifactIdNode );
096    
097            Node versionNode = doc.createElement( "version" );
098            versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
099            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    }