View Javadoc

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  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          StringTokenizer tok = new StringTokenizer( version, "." );
39          if ( SNAPSHOT.equals( version ) && TYPE_IMPLEMENTATION.equals( type ) )
40          {
41              return "0.0.0." + generateSnapshotValue( date );
42          }
43          StringBuffer toReturn = new StringBuffer();
44          while ( tok.hasMoreTokens() )
45          {
46              String token = tok.nextToken();
47              if ( TYPE_IMPLEMENTATION.equals( type ) )
48              {
49                  int snapshotIndex = token.indexOf( SNAPSHOT );
50                  if ( snapshotIndex > 0 )
51                  {
52                      String repl = token.substring( 0, snapshotIndex ) + generateSnapshotValue( date );
53                      if ( token.length() > snapshotIndex + SNAPSHOT.length() )
54                      {
55                          repl = token.substring(
56                                  snapshotIndex + SNAPSHOT.length() );
57                      }
58                      token = repl;
59                  }
60              }
61              if ( TYPE_SPECIFICATION.equals( type ) )
62              {
63                  // strip the trailing -RC1, -BETA5, -SNAPSHOT
64                  if ( token.indexOf( '-' ) > 0 )
65                  {
66                      token = token.substring( 0, token.indexOf( '-' ) );
67                  } else if ( token.indexOf( '_' ) > 0 )
68                  {
69                      token = token.substring( 0, token.indexOf( '_' ) );
70                  }
71                  try
72                  {
73                      Integer intValue = Integer.valueOf( token );
74                      token = intValue.toString();
75                  }
76                  catch ( NumberFormatException exc )
77                  {
78                      // ignore, will just not be added to the
79                      token = "";
80                  }
81              }
82              if ( token.length() > 0 )
83              {
84                  if ( toReturn.length() != 0 )
85                  {
86                      toReturn.append( "." );
87                  }
88                  toReturn.append( token );
89              }
90  
91          }
92          if ( toReturn.length() == 0 )
93          {
94              toReturn.append( "0.0.0" );
95          }
96          return toReturn.toString();
97      }
98  
99      private static String generateSnapshotValue( Date date )
100     {
101         SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMdd" );
102         dateFormat.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
103         return dateFormat.format( date );
104     }
105 }