001    package org.codehaus.mojo.license;
002    
003    /* 
004     * Codehaus License Maven Plugin
005     *     
006     * This program is free software: you can redistribute it and/or modify
007     * it under the terms of the GNU Lesser General Public License as published by
008     * the Free Software Foundation, either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
018     */
019    
020    import org.apache.maven.model.License;
021    import org.codehaus.mojo.license.model.ProjectLicenseInfo;
022    import org.w3c.dom.Document;
023    import org.w3c.dom.Node;
024    
025    import javax.xml.parsers.DocumentBuilder;
026    import javax.xml.parsers.DocumentBuilderFactory;
027    import javax.xml.parsers.ParserConfigurationException;
028    import javax.xml.transform.*;
029    import javax.xml.transform.dom.DOMSource;
030    import javax.xml.transform.stream.StreamResult;
031    import java.io.File;
032    import java.util.List;
033    
034    /**
035     * A LicenseSummaryWriter.
036     *
037     * @author Paul Gier
038     * @version $Revision: 13519 $
039     * @since 1.0
040     */
041    public class LicenseSummaryWriter
042    {
043        public static void writeLicenseSummary( List<ProjectLicenseInfo> dependencies, File outputFile )
044            throws ParserConfigurationException, TransformerException
045        {
046            DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
047            DocumentBuilder parser = fact.newDocumentBuilder();
048            Document doc = parser.newDocument();
049    
050            Node root = doc.createElement( "licenseSummary" );
051            doc.appendChild( root );
052            Node dependenciesNode = doc.createElement( "dependencies" );
053            root.appendChild( dependenciesNode );
054    
055            for ( ProjectLicenseInfo dep : dependencies )
056            {
057                dependenciesNode.appendChild( createDependencyNode( doc, dep ) );
058            }
059    
060            // Prepare the output file File
061            Result result = new StreamResult( outputFile.toURI().getPath() );
062    
063            // Write the DOM document to the file
064            Transformer xformer = TransformerFactory.newInstance().newTransformer();
065            xformer.setOutputProperty( OutputKeys.INDENT, "yes" );
066            xformer.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount", "2" );
067            xformer.transform( new DOMSource( doc ), result );
068        }
069    
070        public static Node createDependencyNode( Document doc, ProjectLicenseInfo dep )
071        {
072            Node depNode = doc.createElement( "dependency" );
073    
074            Node groupIdNode = doc.createElement( "groupId" );
075            groupIdNode.appendChild( doc.createTextNode( dep.getGroupId() ) );
076            depNode.appendChild( groupIdNode );
077    
078            Node artifactIdNode = doc.createElement( "artifactId" );
079            artifactIdNode.appendChild( doc.createTextNode( dep.getArtifactId() ) );
080            depNode.appendChild( artifactIdNode );
081    
082            Node versionNode = doc.createElement( "version" );
083            versionNode.appendChild( doc.createTextNode( dep.getVersion() ) );
084            depNode.appendChild( versionNode );
085    
086            Node licensesNode = doc.createElement( "licenses" );
087            if ( dep.getLicenses() == null || dep.getLicenses().size() == 0 )
088            {
089                licensesNode.appendChild( doc.createComment( "No license information available. " ) );
090            }
091            else
092            {
093                for ( License lic : dep.getLicenses() )
094                {
095                    licensesNode.appendChild( createLicenseNode( doc, lic ) );
096                }
097            }
098            depNode.appendChild( licensesNode );
099            return depNode;
100    
101        }
102    
103        public static Node createLicenseNode( Document doc, License lic )
104        {
105            Node licenseNode = doc.createElement( "license" );
106    
107            if ( lic.getName() != null )
108            {
109                Node licNameNode = doc.createElement( "name" );
110                licNameNode.appendChild( doc.createTextNode( lic.getName() ) );
111                licenseNode.appendChild( licNameNode );
112            }
113    
114            if ( lic.getUrl() != null )
115            {
116                Node licUrlNode = doc.createElement( "url" );
117                licUrlNode.appendChild( doc.createTextNode( lic.getUrl() ) );
118                licenseNode.appendChild( licUrlNode );
119            }
120    
121            if ( lic.getDistribution() != null )
122            {
123                Node licDistNode = doc.createElement( "distribution" );
124                licDistNode.appendChild( doc.createTextNode( lic.getDistribution() ) );
125                licenseNode.appendChild( licDistNode );
126            }
127    
128            if ( lic.getComments() != null )
129            {
130                Node licCommentsNode = doc.createElement( "comments" );
131                licCommentsNode.appendChild( doc.createTextNode( lic.getComments() ) );
132                licenseNode.appendChild( licCommentsNode );
133            }
134    
135            return licenseNode;
136        }
137    
138    }