Coverage Report - org.apache.maven.plugin.doap.DoapUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
DoapUtil
86 %
75/87
67 %
28/42
2,769
 
 1  
 package org.apache.maven.plugin.doap;
 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.text.DateFormat;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Date;
 25  
 import java.util.HashMap;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.util.Locale;
 29  
 import java.util.Map;
 30  
 
 31  
 import org.apache.maven.model.Contributor;
 32  
 import org.apache.maven.model.Developer;
 33  
 import org.codehaus.plexus.i18n.I18N;
 34  
 import org.codehaus.plexus.util.StringUtils;
 35  
 import org.codehaus.plexus.util.xml.XMLWriter;
 36  
 import org.codehaus.plexus.util.xml.XmlWriterUtil;
 37  
 
 38  
 /**
 39  
  * Utility class for DOAP mojo.
 40  
  *
 41  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 42  
  * @version $Id$
 43  
  * @since 1.0
 44  
  */
 45  0
 public class DoapUtil
 46  
 {
 47  
     /** Magic number to repeat '=' */
 48  
     private static final int REPEAT_EQUALS = 21;
 49  
 
 50  
     /** RDF resource attribute */
 51  
     protected static final String RDF_RESOURCE = "rdf:resource";
 52  
 
 53  
     /**
 54  
      * Write comments in the DOAP file header
 55  
      *
 56  
      * @param writer not null
 57  
      */
 58  
     public static void writeHeader( XMLWriter writer )
 59  
     {
 60  2
         XmlWriterUtil.writeLineBreak( writer );
 61  
 
 62  2
         XmlWriterUtil.writeCommentLineBreak( writer );
 63  2
         XmlWriterUtil.writeComment( writer, StringUtils.repeat( "=", REPEAT_EQUALS ) + " - DO NOT EDIT THIS FILE! - "
 64  
             + StringUtils.repeat( "=", REPEAT_EQUALS ) );
 65  2
         XmlWriterUtil.writeCommentLineBreak( writer );
 66  2
         XmlWriterUtil.writeComment( writer, " " );
 67  2
         XmlWriterUtil.writeComment( writer, "Any modifications will be overwritten." );
 68  2
         XmlWriterUtil.writeComment( writer, " " );
 69  2
         DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.US );
 70  2
         XmlWriterUtil.writeComment( writer, "Generated by Maven Doap Plugin on "
 71  
             + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
 72  2
         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-doap-plugin/" );
 73  2
         XmlWriterUtil.writeComment( writer, " " );
 74  2
         XmlWriterUtil.writeCommentLineBreak( writer );
 75  
 
 76  2
         XmlWriterUtil.writeLineBreak( writer );
 77  2
     }
 78  
 
 79  
     /**
 80  
      * @param writer not null
 81  
      * @param name not null
 82  
      * @param value could be null. In this case, the element is not written.
 83  
      * @throws IllegalArgumentException if name is null or empty
 84  
      */
 85  
     public static void writeElement( XMLWriter writer, String name, String value )
 86  
         throws IllegalArgumentException
 87  
     {
 88  2
         if ( StringUtils.isEmpty( name ) )
 89  
         {
 90  1
             throw new IllegalArgumentException( "name should be defined" );
 91  
         }
 92  
 
 93  1
         if ( value != null )
 94  
         {
 95  1
             writer.startElement( name );
 96  1
             writer.writeText( value );
 97  1
             writer.endElement();
 98  
         }
 99  1
     }
 100  
 
 101  
     /**
 102  
      * @param writer not null
 103  
      * @param name not null
 104  
      * @param lang not null
 105  
      * @param value could be null. In this case, the element is not written.
 106  
      * @throws IllegalArgumentException if name is null or empty
 107  
      */
 108  
     public static void writeElement( XMLWriter writer, String name, String value, String lang )
 109  
         throws IllegalArgumentException
 110  
     {
 111  4
         if ( StringUtils.isEmpty( lang ) )
 112  
         {
 113  0
             writeElement( writer, name, value );
 114  0
             return;
 115  
         }
 116  
 
 117  4
         if ( StringUtils.isEmpty( name ) )
 118  
         {
 119  0
             throw new IllegalArgumentException( "name should be defined" );
 120  
         }
 121  
 
 122  4
         if ( value != null )
 123  
         {
 124  4
             writer.startElement( name );
 125  4
             writer.addAttribute( "xml:lang", lang );
 126  4
             writer.writeText( value );
 127  4
             writer.endElement();
 128  
         }
 129  4
     }
 130  
 
 131  
     /**
 132  
      * @param writer not null
 133  
      * @param name not null
 134  
      * @param value could be null. In this case, the element is not written.
 135  
      * @throws IllegalArgumentException if name is null or empty
 136  
      */
 137  
     public static void writeRdfResourceElement( XMLWriter writer, String name, String value )
 138  
         throws IllegalArgumentException
 139  
     {
 140  12
         if ( StringUtils.isEmpty( name ) )
 141  
         {
 142  1
             throw new IllegalArgumentException( "name should be defined" );
 143  
         }
 144  
 
 145  11
         if ( value != null )
 146  
         {
 147  11
             writer.startElement( name );
 148  11
             writer.addAttribute( RDF_RESOURCE, value );
 149  11
             writer.endElement();
 150  
         }
 151  11
     }
 152  
 
 153  
     /**
 154  
      * @param i18n the internationalization component
 155  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 156  
      * @return a none null list of developers or contributors which have a <code>developer</code> DOAP role.
 157  
      */
 158  
     public static List getDevelopersOrContributorsWithDeveloperRole( I18N i18n, List developersOrContributors )
 159  
     {
 160  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "developers" );
 161  
     }
 162  
 
 163  
     /**
 164  
      * @param i18n the internationalization component
 165  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 166  
      * @return a none null list of developers or contributors which have a <code>documenter</code> DOAP role.
 167  
      */
 168  
     public static List getDevelopersOrContributorsWithDocumenterRole( I18N i18n, List developersOrContributors )
 169  
     {
 170  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "documenters" );
 171  
     }
 172  
 
 173  
     /**
 174  
      * @param i18n the internationalization component
 175  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 176  
      * @return a none null list of developers or contributors which have an <code>helper</code> DOAP role.
 177  
      */
 178  
     public static List getDevelopersOrContributorsWithHelperRole( I18N i18n, List developersOrContributors )
 179  
     {
 180  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "helpers" );
 181  
     }
 182  
 
 183  
     /**
 184  
      * @param i18n the internationalization component
 185  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 186  
      * @return a none null list of developers or contributors which have a <code>maintainer</code> DOAP role.
 187  
      */
 188  
     public static List getDevelopersOrContributorsWithMaintainerRole( I18N i18n, List developersOrContributors )
 189  
     {
 190  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "maintainers" );
 191  
     }
 192  
 
 193  
     /**
 194  
      * @param i18n the internationalization component
 195  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 196  
      * @return a none null list of developers or contributors which have a <code>tester</code> DOAP role.
 197  
      */
 198  
     public static List getDevelopersOrContributorsWithTesterRole( I18N i18n, List developersOrContributors )
 199  
     {
 200  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "testers" );
 201  
     }
 202  
 
 203  
     /**
 204  
      * @param i18n the internationalization component
 205  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 206  
      * @return a none null list of developers or contributors which have a <code>translator</code> DOAP role.
 207  
      */
 208  
     public static List getDevelopersOrContributorsWithTranslatorRole( I18N i18n, List developersOrContributors )
 209  
     {
 210  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "translators" );
 211  
     }
 212  
 
 213  
     /**
 214  
      * @param i18n the internationalization component
 215  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 216  
      * @return a none null list of developers or contributors which have an <code>unknown</code> DOAP role.
 217  
      */
 218  
     public static List getDevelopersOrContributorsWithUnknownRole( I18N i18n, List developersOrContributors )
 219  
     {
 220  2
         return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "unknowns" );
 221  
     }
 222  
 
 223  
     // ----------------------------------------------------------------------
 224  
     // Private methods
 225  
     // ----------------------------------------------------------------------
 226  
 
 227  
     /**
 228  
      * Filter the developers/contributors roles by the keys from {@link I18N#getBundle()}.
 229  
      * <br/>
 230  
      * I18N roles supported in DOAP, i.e. <code>maintainer</code>, <code>developer</code>, <code>documenter</code>,
 231  
      * <code>translator</code>, <code>tester</code>, <code>helper</code>.
 232  
      * <br/>
 233  
      * <b>Note:</b> Actually, only English keys are used.
 234  
      *
 235  
      * @param i18n i18n component
 236  
      * @param developersOrContributors list of <code>{@link Developer}/{@link Contributor}</code>
 237  
      * @return a none null map with <code>maintainers</code>, <code>developers</code>, <code>documenters</code>,
 238  
      * <code>translators</code>, <code>testers</code>, <code>helpers</code>, <code>unknowns</code> as keys and list of
 239  
      * <code>{@link Developer}/{@link Contributor}</code> as value.
 240  
      */
 241  
     private static Map filterDevelopersOrContributorsByDoapRoles( I18N i18n, List developersOrContributors )
 242  
     {
 243  14
         Map returnMap = new HashMap( 7 );
 244  14
         returnMap.put( "maintainers", new ArrayList() );
 245  14
         returnMap.put( "developers", new ArrayList() );
 246  14
         returnMap.put( "documenters", new ArrayList() );
 247  14
         returnMap.put( "translators", new ArrayList() );
 248  14
         returnMap.put( "testers", new ArrayList() );
 249  14
         returnMap.put( "helpers", new ArrayList() );
 250  14
         returnMap.put( "unknowns", new ArrayList() );
 251  
 
 252  14
         if ( developersOrContributors == null || developersOrContributors.isEmpty() )
 253  
         {
 254  0
             return returnMap;
 255  
         }
 256  
 
 257  14
         for ( Iterator it = developersOrContributors.iterator(); it.hasNext(); )
 258  
         {
 259  14
             Object obj = it.next();
 260  
 
 261  
             List roles;
 262  15
             if ( Developer.class.isAssignableFrom( obj.getClass() ) )
 263  
             {
 264  14
                 Developer developer = (Developer) obj;
 265  14
                 roles = developer.getRoles();
 266  14
             }
 267  
             else
 268  
             {
 269  0
                 Contributor contributor = (Contributor) obj;
 270  0
                 roles = contributor.getRoles();
 271  
             }
 272  
 
 273  14
             if ( roles != null && roles.size() != 0 )
 274  
             {
 275  14
                 for ( Iterator it2 = roles.iterator(); it2.hasNext(); )
 276  
                 {
 277  35
                     String role = (String) it2.next();
 278  
 
 279  35
                     role = role.toLowerCase( Locale.ENGLISH );
 280  35
                     if ( role.indexOf( getLowerCaseString( i18n, "doap.maintainer" ) ) != -1 )
 281  
                     {
 282  14
                         ( (List) returnMap.get( "maintainers" ) ).add( obj );
 283  
                     }
 284  21
                     else if ( role.indexOf( getLowerCaseString( i18n, "doap.developer" ) ) != -1 )
 285  
                     {
 286  0
                         ( (List) returnMap.get( "developers" ) ).add( obj );
 287  
                     }
 288  21
                     else if ( role.indexOf( getLowerCaseString( i18n, "doap.documenter" ) ) != -1 )
 289  
                     {
 290  0
                         ( (List) returnMap.get( "documenters" ) ).add( obj );
 291  
                     }
 292  21
                     else if ( role.indexOf( getLowerCaseString( i18n, "doap.translator" ) ) != -1 )
 293  
                     {
 294  0
                         ( (List) returnMap.get( "translators" ) ).add( obj );
 295  
                     }
 296  21
                     else if ( role.indexOf( getLowerCaseString( i18n, "doap.tester" ) ) != -1 )
 297  
                     {
 298  7
                         ( (List) returnMap.get( "testers" ) ).add( obj );
 299  
                     }
 300  14
                     else if ( role.indexOf( getLowerCaseString( i18n, "doap.helper" ) ) != -1 )
 301  
                     {
 302  0
                         ( (List) returnMap.get( "helpers" ) ).add( obj );
 303  
                     }
 304  
                     else
 305  
                     {
 306  14
                         ( (List) returnMap.get( "unknowns" ) ).add( obj );
 307  
                     }
 308  35
                 }
 309  
             }
 310  
             else
 311  
             {
 312  0
                 ( (List) returnMap.get( "unknowns" ) ).add( obj );
 313  
             }
 314  14
         }
 315  
 
 316  14
         return returnMap;
 317  
     }
 318  
 
 319  
     /**
 320  
      * @param i18n not null
 321  
      * @param key not null
 322  
      * @return lower case value for the key in the i18n bundle.
 323  
      */
 324  
     private static String getLowerCaseString( I18N i18n, String key )
 325  
     {
 326  133
         return i18n.getString( "doap-person", Locale.ENGLISH, key ).toLowerCase( Locale.ENGLISH );
 327  
     }
 328  
 }