Coverage Report - org.codehaus.mojo.nbm.AdaptNbVersion
 
Classes in this File Line Coverage Branch Coverage Complexity
AdaptNbVersion
0%
0/35
0%
0/24
8
 
 1  
 /* ==========================================================================
 2  
  * Copyright 2003-2004 Mevenide Team
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  *  Unless required by applicable law or agreed to in writing, software
 11  
  *  distributed under the License is distributed on an "AS IS" BASIS,
 12  
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  *  See the License for the specific language governing permissions and
 14  
  *  limitations under the License.
 15  
  * =========================================================================
 16  
  */
 17  
 package org.codehaus.mojo.nbm;
 18  
 
 19  
 import java.text.SimpleDateFormat;
 20  
 import java.util.Date;
 21  
 import java.util.StringTokenizer;
 22  
 import java.util.TimeZone;
 23  
 
 24  
 /**
 25  
  *  will try to convert the maven version number to a NetBeans friendly version number.
 26  
  * @author <a href="mailto:mkleint@codehaus.org">Milos Kleint</a>
 27  
  *
 28  
  */
 29  0
 public class AdaptNbVersion
 30  
 {
 31  
 
 32  
     public static final String TYPE_SPECIFICATION = "spec"; //NOI18N
 33  
     public static final String TYPE_IMPLEMENTATION = "impl"; //NOI18N
 34  
     private static final String SNAPSHOT = "SNAPSHOT"; //NOI18N
 35  
 
 36  
     public static String adaptVersion( String version, Object type, Date date )
 37  
     {
 38  0
         StringTokenizer tok = new StringTokenizer( version, "." );
 39  0
         if ( SNAPSHOT.equals( version ) && TYPE_IMPLEMENTATION.equals( type ) )
 40  
         {
 41  0
             return "0.0.0." + generateSnapshotValue( date );
 42  
         }
 43  0
         StringBuffer toReturn = new StringBuffer();
 44  0
         while ( tok.hasMoreTokens() )
 45  
         {
 46  0
             String token = tok.nextToken();
 47  0
             if ( TYPE_IMPLEMENTATION.equals( type ) )
 48  
             {
 49  0
                 int snapshotIndex = token.indexOf( SNAPSHOT );
 50  0
                 if ( snapshotIndex > 0 )
 51  
                 {
 52  0
                     String repl = token.substring( 0, snapshotIndex ) + generateSnapshotValue( date );
 53  0
                     if ( token.length() > snapshotIndex + SNAPSHOT.length() )
 54  
                     {
 55  0
                         repl = token.substring(
 56  
                                 snapshotIndex + SNAPSHOT.length() );
 57  
                     }
 58  0
                     token = repl;
 59  
                 }
 60  
             }
 61  0
             if ( TYPE_SPECIFICATION.equals( type ) )
 62  
             {
 63  
                 // strip the trailing -RC1, -BETA5, -SNAPSHOT
 64  0
                 if ( token.indexOf( '-' ) > 0 )
 65  
                 {
 66  0
                     token = token.substring( 0, token.indexOf( '-' ) );
 67  0
                 } else if ( token.indexOf( '_' ) > 0 )
 68  
                 {
 69  0
                     token = token.substring( 0, token.indexOf( '_' ) );
 70  
                 }
 71  
                 try
 72  
                 {
 73  0
                     Integer intValue = Integer.valueOf( token );
 74  0
                     token = intValue.toString();
 75  
                 }
 76  0
                 catch ( NumberFormatException exc )
 77  
                 {
 78  
                     // ignore, will just not be added to the
 79  0
                     token = "";
 80  0
                 }
 81  
             }
 82  0
             if ( token.length() > 0 )
 83  
             {
 84  0
                 if ( toReturn.length() != 0 )
 85  
                 {
 86  0
                     toReturn.append( "." );
 87  
                 }
 88  0
                 toReturn.append( token );
 89  
             }
 90  
 
 91  0
         }
 92  0
         if ( toReturn.length() == 0 )
 93  
         {
 94  0
             toReturn.append( "0.0.0" );
 95  
         }
 96  0
         return toReturn.toString();
 97  
     }
 98  
 
 99  
     private static String generateSnapshotValue( Date date )
 100  
     {
 101  0
         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" );
 102  0
         dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
 103  0
         return dateFormat.format( date );
 104  
     }
 105  
 }