Coverage Report - org.apache.maven.archetype.old.descriptor.ArchetypeDescriptorBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ArchetypeDescriptorBuilder
71 %
86/121
77 %
42/54
8,714
 
 1  
 package org.apache.maven.archetype.old.descriptor;
 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 org.codehaus.plexus.util.xml.Xpp3Dom;
 23  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 24  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 25  
 
 26  
 import java.io.IOException;
 27  
 import java.io.Reader;
 28  
 import java.nio.charset.IllegalCharsetNameException;
 29  
 import java.nio.charset.UnsupportedCharsetException;
 30  
 
 31  
 /**
 32  
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
 33  
  * @version $Id: ArchetypeDescriptorBuilder.java 925928 2010-03-21 23:08:39Z hboutemy $
 34  
  */
 35  5
 public class ArchetypeDescriptorBuilder
 36  
 {
 37  
     public ArchetypeDescriptor build( Reader reader )
 38  
         throws IOException, XmlPullParserException
 39  
     {
 40  5
         ArchetypeDescriptor descriptor = new ArchetypeDescriptor();
 41  
 
 42  5
         Xpp3Dom dom = Xpp3DomBuilder.build( reader );
 43  
 
 44  5
         descriptor.setId( dom.getChild( "id" ).getValue() );
 45  
 
 46  5
         Xpp3Dom allowPartialDom = dom.getChild( "allowPartial" );
 47  
 
 48  5
         if ( allowPartialDom != null )
 49  
         {
 50  0
             String allowPartial = allowPartialDom.getValue();
 51  
 
 52  0
             if ( "true".equals( allowPartial ) || "1".equals( allowPartial ) || "on".equals( allowPartial ) )
 53  
             {
 54  0
                 descriptor.setAllowPartial( true );
 55  
             }
 56  
         }
 57  
 
 58  
         // ----------------------------------------------------------------------
 59  
         // Main
 60  
         // ----------------------------------------------------------------------
 61  
 
 62  5
         Xpp3Dom sources = dom.getChild( "sources" );
 63  
 
 64  5
         if ( sources != null )
 65  
         {
 66  5
             Xpp3Dom[] sourceList = sources.getChildren( "source" );
 67  
 
 68  12
             for ( int i = 0; i < sourceList.length; i++ )
 69  
             {
 70  7
                 addSourceToDescriptor( sourceList[i], descriptor );
 71  
             }
 72  
         }
 73  
 
 74  5
         Xpp3Dom resources = dom.getChild( "resources" );
 75  
 
 76  5
         if ( resources != null )
 77  
         {
 78  4
             Xpp3Dom[] resourceList = resources.getChildren( "resource" );
 79  
 
 80  10
             for ( int i = 0; i < resourceList.length; i++ )
 81  
             {
 82  6
                 addResourceToDescriptor( resourceList[i], descriptor );
 83  
             }
 84  
         }
 85  
 
 86  
         // ----------------------------------------------------------------------
 87  
         // Test
 88  
         // ----------------------------------------------------------------------
 89  
 
 90  5
         Xpp3Dom testSources = dom.getChild( "testSources" );
 91  
 
 92  5
         if ( testSources != null )
 93  
         {
 94  3
             Xpp3Dom[] testSourceList = testSources.getChildren( "source" );
 95  
 
 96  9
             for ( int i = 0; i < testSourceList.length; i++ )
 97  
             {
 98  6
                 addTestSourceToDescriptor( testSourceList[i], descriptor );
 99  
             }
 100  
         }
 101  
 
 102  5
         Xpp3Dom testResources = dom.getChild( "testResources" );
 103  
 
 104  5
         if ( testResources != null )
 105  
         {
 106  2
             Xpp3Dom[] testResourceList = testResources.getChildren( "resource" );
 107  
 
 108  6
             for ( int i = 0; i < testResourceList.length; i++ )
 109  
             {
 110  4
                 addTestResourceToDescriptor( testResourceList[i], descriptor );
 111  
             }
 112  
         }
 113  
 
 114  
         // ----------------------------------------------------------------------
 115  
         // Site
 116  
         // ----------------------------------------------------------------------
 117  
 
 118  5
         Xpp3Dom siteResources = dom.getChild( "siteResources" );
 119  
 
 120  5
         if ( siteResources != null )
 121  
         {
 122  3
             Xpp3Dom[] siteResourceList = siteResources.getChildren( "resource" );
 123  
 
 124  7
             for ( int i = 0; i < siteResourceList.length; i++ )
 125  
             {
 126  4
                 addSiteResourceToDescriptor( siteResourceList[i], descriptor );
 127  
             }
 128  
         }
 129  
 
 130  5
         return descriptor;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Adds the source element <code>source</code> to the list of sources in the
 135  
      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 136  
      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
 137  
      * attribute or the Java virtual machine's default if it is not defined.
 138  
      *
 139  
      * @param source     a <code>&lt;source&gt;</code> element from the <code>&lt;sources&gt;</code>
 140  
      * @param descriptor the <code>ArchetypeDescriptor</code> to add the source template to.
 141  
      * @throws XmlPullParserException if the encoding specified is not valid or supported.
 142  
      */
 143  
     private static void addSourceToDescriptor( Xpp3Dom source, ArchetypeDescriptor descriptor )
 144  
         throws XmlPullParserException
 145  
     {
 146  7
         descriptor.addSource( source.getValue() );
 147  
 
 148  7
         TemplateDescriptor sourceDesc = descriptor.getSourceDescriptor( source.getValue() );
 149  
 
 150  7
         sourceDesc.setFiltered( true );
 151  
 
 152  7
         if ( source.getAttribute( "encoding" ) != null )
 153  
         {
 154  
             try
 155  
             {
 156  2
                 sourceDesc.setEncoding( source.getAttribute( "encoding" ) );
 157  
             }
 158  0
             catch ( IllegalCharsetNameException icne )
 159  
             {
 160  0
                 throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a valid encoding." );
 161  
             }
 162  0
             catch ( UnsupportedCharsetException uce )
 163  
             {
 164  0
                 throw new XmlPullParserException( source.getAttribute( "encoding" ) + " is not a supported encoding." );
 165  2
             }
 166  
         }
 167  7
     }
 168  
 
 169  
     /**
 170  
      * Adds the resource element <code>resource</code> to the list of resources in the
 171  
      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 172  
      * <i>filtered</i> if the attribute <code>filtered</code> was not
 173  
      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
 174  
      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
 175  
      * in the <code>encoding</code> attribute or the Java virtual machine's default if
 176  
      * it is not defined. If the <code>resource</code> is a property file (ends in
 177  
      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
 178  
      * even if some other encoding is specified in the attribute.
 179  
      *
 180  
      * @param resource   a <code>&lt;resource&gt;</code> element from the <code>&lt;resources&gt;</code>
 181  
      * @param descriptor the <code>ArchetypeDescriptor</code> to add the resource template to.
 182  
      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
 183  
      *                                value of the attribute <code>filtered</code> is no valid.
 184  
      */
 185  
     private static void addResourceToDescriptor( Xpp3Dom resource, ArchetypeDescriptor descriptor )
 186  
         throws XmlPullParserException
 187  
     {
 188  6
         descriptor.addResource( resource.getValue() );
 189  
 
 190  6
         if ( resource.getAttribute( "filtered" ) != null )
 191  
         {
 192  1
             TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
 193  
 
 194  
             try
 195  
             {
 196  1
                 resourceDesc.setFiltered( getValueFilteredAttribute( resource.getAttribute( "filtered" ) ) );
 197  
             }
 198  0
             catch ( IllegalArgumentException iae )
 199  
             {
 200  0
                 throw new XmlPullParserException( iae.getMessage() );
 201  1
             }
 202  
         }
 203  
 
 204  6
         if ( resource.getAttribute( "encoding" ) != null )
 205  
         {
 206  1
             TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
 207  
 
 208  
             try
 209  
             {
 210  1
                 resourceDesc.setEncoding( resource.getAttribute( "encoding" ) );
 211  
             }
 212  0
             catch ( IllegalCharsetNameException icne )
 213  
             {
 214  0
                 throw new XmlPullParserException( resource.getAttribute( "encoding" ) + " is not a valid encoding." );
 215  
             }
 216  0
             catch ( UnsupportedCharsetException uce )
 217  
             {
 218  0
                 throw new XmlPullParserException(
 219  
                     resource.getAttribute( "encoding" ) + " is not a supported encoding." );
 220  1
             }
 221  
         }
 222  
 
 223  6
         if ( resource.getValue().endsWith( ".properties" ) )
 224  
         {
 225  2
             TemplateDescriptor resourceDesc = descriptor.getResourceDescriptor( resource.getValue() );
 226  
 
 227  2
             resourceDesc.setEncoding( "iso-8859-1" );
 228  
         }
 229  6
     }
 230  
 
 231  
     /**
 232  
      * Adds the test-source element <code>source</code> to the list of sources in the
 233  
      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 234  
      * <i>filtered</i> and with the encoding specified in the <code>encoding</code>
 235  
      * attribute or the Java virtual machine's default if it is not defined.
 236  
      *
 237  
      * @param testSource a <code>&lt;source&gt;</code> element from the <code>&lt;testSources&gt;</code>
 238  
      * @param descriptor the <code>ArchetypeDescriptor</code> to add the test-source template to.
 239  
      * @throws XmlPullParserException if the encoding specified is not valid or supported.
 240  
      */
 241  
     private static void addTestSourceToDescriptor( Xpp3Dom testSource, ArchetypeDescriptor descriptor )
 242  
         throws XmlPullParserException
 243  
     {
 244  6
         descriptor.addTestSource( testSource.getValue() );
 245  6
         TemplateDescriptor testSourceDesc = descriptor.getTestSourceDescriptor( testSource.getValue() );
 246  6
         testSourceDesc.setFiltered( true );
 247  6
         if ( testSource.getAttribute( "encoding" ) != null )
 248  
         {
 249  
             try
 250  
             {
 251  2
                 testSourceDesc.setEncoding( testSource.getAttribute( "encoding" ) );
 252  
             }
 253  0
             catch ( IllegalCharsetNameException icne )
 254  
             {
 255  0
                 throw new XmlPullParserException( testSource.getAttribute( "encoding" ) + " is not a valid encoding." );
 256  
             }
 257  0
             catch ( UnsupportedCharsetException uce )
 258  
             {
 259  0
                 throw new XmlPullParserException(
 260  
                     testSource.getAttribute( "encoding" ) + " is not a supported encoding." );
 261  2
             }
 262  
         }
 263  6
     }
 264  
 
 265  
     /**
 266  
      * Adds the test-resource element <code>resource</code> to the list of test-resources in the
 267  
      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 268  
      * <i>filtered</i> if the attribute <code>filtered</code> was not
 269  
      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
 270  
      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
 271  
      * in the <code>encoding</code> attribute or the Java virtual machine's default if
 272  
      * it is not defined. If the <code>resource</code> is a property file (ends in
 273  
      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
 274  
      * even if some other encoding is specified in the attribute.
 275  
      *
 276  
      * @param testResource a <code>&lt;resource&gt;</code> element from the <code>&lt;testResources&gt;</code>
 277  
      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the test-resource template to.
 278  
      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
 279  
      *                                value of the attribute <code>filtered</code> is no valid.
 280  
      */
 281  
     private static void addTestResourceToDescriptor( Xpp3Dom testResource, ArchetypeDescriptor descriptor )
 282  
         throws XmlPullParserException
 283  
     {
 284  4
         descriptor.addTestResource( testResource.getValue() );
 285  
 
 286  4
         if ( testResource.getAttribute( "filtered" ) != null )
 287  
         {
 288  1
             TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
 289  
 
 290  
             try
 291  
             {
 292  1
                 testResourceDesc.setFiltered( getValueFilteredAttribute( testResource.getAttribute( "filtered" ) ) );
 293  
             }
 294  0
             catch ( IllegalArgumentException iae )
 295  
             {
 296  0
                 throw new XmlPullParserException( iae.getMessage() );
 297  1
             }
 298  
         }
 299  
 
 300  4
         if ( testResource.getAttribute( "encoding" ) != null )
 301  
         {
 302  1
             TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
 303  
 
 304  
             try
 305  
             {
 306  1
                 testResourceDesc.setEncoding( testResource.getAttribute( "encoding" ) );
 307  
 
 308  
             }
 309  0
             catch ( IllegalCharsetNameException icne )
 310  
             {
 311  0
                 throw new XmlPullParserException(
 312  
                     testResource.getAttribute( "encoding" ) + " is not a valid encoding." );
 313  
             }
 314  0
             catch ( UnsupportedCharsetException uce )
 315  
             {
 316  0
                 throw new XmlPullParserException(
 317  
                     testResource.getAttribute( "encoding" ) + " is not a supported encoding." );
 318  1
             }
 319  
         }
 320  
 
 321  4
         if ( testResource.getValue().endsWith( ".properties" ) )
 322  
         {
 323  0
             TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor( testResource.getValue() );
 324  
 
 325  0
             testResourceDesc.setEncoding( "iso-8859-1" );
 326  
         }
 327  4
     }
 328  
 
 329  
     /**
 330  
      * Adds the site-resource element <code>resource</code> to the list of site-resources in the
 331  
      * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 332  
      * <i>filtered</i> if the attribute <code>filtered</code> was not
 333  
      * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
 334  
      * if its value is <code>&quot;false&quot;</code>, and the encoding specified
 335  
      * in the <code>encoding</code> attribute or the Java virtual machine's default if
 336  
      * it is not defined. If the <code>resource</code> is a property file (ends in
 337  
      * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
 338  
      * even if some other encoding is specified in the attribute.
 339  
      *
 340  
      * @param siteResource a <code>&lt;resource&gt;</code> element from the <code>&lt;siteResources&gt;</code>
 341  
      * @param descriptor   the <code>ArchetypeDescriptor</code> to add the site-resource template to.
 342  
      * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
 343  
      *                                value of the attribute <code>filtered</code> is no valid.
 344  
      */
 345  
     private static void addSiteResourceToDescriptor( Xpp3Dom siteResource, ArchetypeDescriptor descriptor )
 346  
         throws XmlPullParserException
 347  
     {
 348  4
         descriptor.addSiteResource( siteResource.getValue() );
 349  
 
 350  4
         if ( siteResource.getAttribute( "filtered" ) != null )
 351  
         {
 352  1
             TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
 353  
 
 354  
             try
 355  
             {
 356  1
                 siteResourceDesc.setFiltered( getValueFilteredAttribute( siteResource.getAttribute( "filtered" ) ) );
 357  
             }
 358  0
             catch ( IllegalArgumentException iae )
 359  
             {
 360  0
                 throw new XmlPullParserException( iae.getMessage() );
 361  1
             }
 362  
         }
 363  4
         if ( siteResource.getAttribute( "encoding" ) != null )
 364  
         {
 365  1
             TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
 366  
 
 367  
             try
 368  
             {
 369  1
                 siteResourceDesc.setEncoding( siteResource.getAttribute( "encoding" ) );
 370  
             }
 371  0
             catch ( IllegalCharsetNameException icne )
 372  
             {
 373  0
                 throw new XmlPullParserException(
 374  
                     siteResource.getAttribute( "encoding" ) + " is not a valid encoding." );
 375  
             }
 376  0
             catch ( UnsupportedCharsetException uce )
 377  
             {
 378  0
                 throw new XmlPullParserException(
 379  
                     siteResource.getAttribute( "encoding" ) + " is not a supported encoding." );
 380  1
             }
 381  
         }
 382  4
         if ( siteResource.getValue().endsWith( ".properties" ) )
 383  
         {
 384  0
             TemplateDescriptor siteResourceDesc = descriptor.getSiteResourceDescriptor( siteResource.getValue() );
 385  
 
 386  0
             siteResourceDesc.setEncoding( "iso-8859-1" );
 387  
         }
 388  4
     }
 389  
 
 390  
     private static boolean getValueFilteredAttribute( String str )
 391  
         throws IllegalArgumentException
 392  
     {
 393  3
         boolean ret = false;
 394  
 
 395  3
         if ( str.equals( "true" ) )
 396  
         {
 397  0
             ret = true;
 398  
         }
 399  3
         else if ( str.equals( "false" ) )
 400  
         {
 401  3
             ret = false;
 402  
         }
 403  
         else
 404  
         {
 405  0
             throw new IllegalArgumentException( str + " is not an accepted value for the attribute 'filtered'" );
 406  
         }
 407  3
         return ret;
 408  
     }
 409  
 }