1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
29 public class AdaptNbVersion
30 {
31
32 public static final String TYPE_SPECIFICATION = "spec";
33 public static final String TYPE_IMPLEMENTATION = "impl";
34 private static final String SNAPSHOT = "SNAPSHOT";
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
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
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 }