Coverage Report - org.apache.maven.plugin.jxr.JxrReportUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
JxrReportUtil
0%
0/59
0%
0/38
17,5
 
 1  
 package org.apache.maven.plugin.jxr;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.parsers.FactoryConfigurationError;
 29  
 import javax.xml.parsers.ParserConfigurationException;
 30  
 import javax.xml.transform.TransformerException;
 31  
 
 32  
 import org.apache.maven.model.Plugin;
 33  
 import org.apache.maven.model.ReportPlugin;
 34  
 import org.apache.maven.model.Site;
 35  
 import org.apache.maven.project.MavenProject;
 36  
 import org.apache.maven.wagon.repository.Repository;
 37  
 import org.apache.xpath.XPathAPI;
 38  
 import org.apache.xpath.objects.XObject;
 39  
 import org.codehaus.plexus.util.StringInputStream;
 40  
 import org.codehaus.plexus.util.StringUtils;
 41  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 42  
 import org.w3c.dom.Document;
 43  
 import org.xml.sax.SAXException;
 44  
 
 45  
 /**
 46  
  * Utility class for the jxr report.
 47  
  *
 48  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 49  
  * @version $Id: JxrReportUtil.java 692686 2008-09-06 17:13:41Z hboutemy $
 50  
  */
 51  0
 public class JxrReportUtil
 52  
 {
 53  
     /**
 54  
      * Return the <code>optionName</code> value defined in a project for the "maven-javadoc-plugin" plugin.
 55  
      *
 56  
      * @param project not null
 57  
      * @param optionName the option name wanted
 58  
      * @param defaultValue a default value
 59  
      * @return the value for the option name or the default value. Could be null if not found.
 60  
      * @throws IOException if any
 61  
      */
 62  
     protected static String getMavenJavadocPluginBasicOption( MavenProject project, String optionName,
 63  
                                                               String defaultValue )
 64  
         throws IOException
 65  
     {
 66  0
         List plugins = new ArrayList();
 67  0
         for ( Iterator it = project.getModel().getReporting().getPlugins().iterator(); it.hasNext(); )
 68  
         {
 69  0
             plugins.add( it.next() );
 70  
         }
 71  0
         for ( Iterator it = project.getModel().getBuild().getPlugins().iterator(); it.hasNext(); )
 72  
         {
 73  0
             plugins.add( it.next() );
 74  
         }
 75  
 
 76  0
         String pluginArtifactId = "maven-javadoc-plugin";
 77  0
         for ( Iterator it = plugins.iterator(); it.hasNext(); )
 78  
         {
 79  0
             Object next = it.next();
 80  
 
 81  0
             Xpp3Dom pluginConf = null;
 82  
 
 83  0
             if ( next instanceof Plugin )
 84  
             {
 85  0
                 Plugin plugin = (Plugin) next;
 86  
 
 87  
                 // using out-of-box Maven plugins
 88  0
                 if ( !( ( plugin.getGroupId().equals( "org.apache.maven.plugins" ) ) && ( plugin.getArtifactId()
 89  
                     .equals( pluginArtifactId ) ) ) )
 90  
                 {
 91  0
                     continue;
 92  
                 }
 93  
 
 94  0
                 pluginConf = (Xpp3Dom) plugin.getConfiguration();
 95  
             }
 96  
 
 97  0
             if ( next instanceof ReportPlugin )
 98  
             {
 99  0
                 ReportPlugin reportPlugin = (ReportPlugin) next;
 100  
 
 101  
                 // using out-of-box Maven plugins
 102  0
                 if ( !( ( reportPlugin.getGroupId().equals( "org.apache.maven.plugins" ) ) && ( reportPlugin
 103  
                     .getArtifactId().equals( pluginArtifactId ) ) ) )
 104  
                 {
 105  0
                     continue;
 106  
                 }
 107  
 
 108  0
                 pluginConf = (Xpp3Dom) reportPlugin.getConfiguration();
 109  
             }
 110  
 
 111  0
             if ( pluginConf == null )
 112  
             {
 113  0
                 continue;
 114  
             }
 115  
 
 116  
             try
 117  
             {
 118  0
                 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
 119  
                     .parse( new StringInputStream( pluginConf.toString() ) );
 120  
 
 121  0
                 XObject obj = XPathAPI.eval( doc, "//configuration/" + optionName );
 122  
 
 123  0
                 if ( StringUtils.isNotEmpty( obj.toString() ) )
 124  
                 {
 125  0
                     return obj.toString();
 126  
                 }
 127  
             }
 128  0
             catch ( SAXException e )
 129  
             {
 130  0
                 throw new IOException( "SAXException: " + e.getMessage() );
 131  
             }
 132  0
             catch ( ParserConfigurationException e )
 133  
             {
 134  0
                 throw new IOException( "ParserConfigurationException: " + e.getMessage() );
 135  
             }
 136  0
             catch ( FactoryConfigurationError e )
 137  
             {
 138  0
                 throw new IOException( "FactoryConfigurationError: " + e.getMessage() );
 139  
             }
 140  0
             catch ( TransformerException e )
 141  
             {
 142  0
                 throw new IOException( "TransformerException: " + e.getMessage() );
 143  0
             }
 144  0
         }
 145  
 
 146  0
         return defaultValue;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Generates the site structure using the project hierarchy (project and its modules) or using the
 151  
      * distributionManagement elements from the pom.xml.
 152  
      *
 153  
      * @todo come from site plugin!
 154  
      * @see org.apache.maven.plugins.site.SiteStageMojo#getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
 155  
      *
 156  
      * @param project
 157  
      * @param ignoreMissingSiteUrl
 158  
      * @return the structure relative path
 159  
      * @throws IOException if any
 160  
      */
 161  
     protected static String getStructure( MavenProject project, boolean ignoreMissingSiteUrl )
 162  
         throws IOException
 163  
     {
 164  0
         if ( project.getDistributionManagement() == null )
 165  
         {
 166  0
             String hierarchy = project.getName();
 167  
 
 168  0
             MavenProject parent = project.getParent();
 169  0
             while ( parent != null )
 170  
             {
 171  0
                 hierarchy = parent.getName() + "/" + hierarchy;
 172  0
                 parent = parent.getParent();
 173  
             }
 174  
 
 175  0
             return hierarchy;
 176  
         }
 177  
 
 178  0
         Site site = project.getDistributionManagement().getSite();
 179  0
         if ( site == null )
 180  
         {
 181  0
             if ( !ignoreMissingSiteUrl )
 182  
             {
 183  0
                 throw new IOException(
 184  
                                        "Missing site information in the distribution management element in the project: '"
 185  
                                            + project.getName() + "'." );
 186  
             }
 187  
 
 188  0
             return null;
 189  
         }
 190  
 
 191  0
         if ( StringUtils.isEmpty( site.getUrl() ) )
 192  
         {
 193  0
             if ( !ignoreMissingSiteUrl )
 194  
             {
 195  0
                 throw new IOException( "The URL in the site is missing in the project descriptor." );
 196  
             }
 197  
 
 198  0
             return null;
 199  
         }
 200  
 
 201  0
         Repository repository = new Repository( site.getId(), site.getUrl() );
 202  0
         if ( StringUtils.isEmpty( repository.getBasedir() ) )
 203  
         {
 204  0
             return repository.getHost();
 205  
         }
 206  
 
 207  0
         if ( repository.getBasedir().startsWith( "/" ) )
 208  
         {
 209  0
             return repository.getHost() + repository.getBasedir();
 210  
         }
 211  
 
 212  0
         return repository.getHost() + "/" + repository.getBasedir();
 213  
     }
 214  
 }