1 package com.simpligility.maven.plugins.android.configuration;
2
3 import static java.lang.String.format;
4
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 import org.apache.maven.plugin.MojoExecutionException;
9
10
11
12
13
14
15
16 public class RegexVersionElementParser implements VersionElementParser
17 {
18
19 private Pattern namingPattern;
20
21 public RegexVersionElementParser( String pattern )
22 {
23 namingPattern = Pattern.compile( pattern );
24 }
25
26 @Override
27 public int[] parseVersionElements( final String versionName ) throws MojoExecutionException
28 {
29 final Matcher matcher = namingPattern.matcher( versionName );
30 if ( ! matcher.find() )
31 {
32 throw new MojoExecutionException( format(
33 "The version naming pattern failed to match version name: %s against %s",
34 namingPattern, versionName ) );
35 }
36
37 int elementCount = matcher.groupCount();
38 int[] result = new int[elementCount];
39
40 for ( int i = 0; i < elementCount; i++ )
41 {
42
43 try
44 {
45 result[i] = Integer.valueOf( matcher.group( i + 1 ) );
46 }
47 catch ( NumberFormatException ignored )
48 {
49
50 result[i] = 0;
51 }
52 }
53
54 return result;
55 }
56 }