Applied patch (FELIX-304) to factor out some common functionality.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@546946 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/pom.xml b/bundleplugin/pom.xml
index 819ee3e..e1ea380 100644
--- a/bundleplugin/pom.xml
+++ b/bundleplugin/pom.xml
@@ -75,5 +75,10 @@
       <version>1.0</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-osgi</artifactId>
+      <version>0.1.0-SNAPSHOT</version>
+    </dependency>
  </dependencies>
 </project>
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
index a20704e..d9ae31b 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundleAllPlugin.java
@@ -324,14 +324,9 @@
         return getBundleName( project.getArtifact() );
     }
 
-    private String getBundleNameFirstPart( Artifact artifact )
-    {
-        return artifact.getGroupId() + "." + artifact.getArtifactId();
-    }
-
     private String getBundleName( Artifact artifact )
     {
-        return getBundleNameFirstPart( artifact ) + "_" + convertVersionToOsgi( artifact.getVersion() ) + ".jar";
+        return getMaven2OsgiConverter().getBundleFileName( artifact );
     }
 
     private boolean alreadyBundled( Artifact artifact )
diff --git a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
index 6211b88..4f06e35 100644
--- a/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
+++ b/bundleplugin/src/main/java/org/apache/felix/bundleplugin/BundlePlugin.java
@@ -28,6 +28,7 @@
 import org.apache.maven.model.*;
 import org.apache.maven.plugin.*;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.osgi.Maven2OsgiConverter;
  
 import aQute.lib.osgi.*;
  
@@ -40,15 +41,6 @@
  */
 public class BundlePlugin extends AbstractMojo {
  
- /** Bundle-Version must match this pattern */
- private static final Pattern OSGI_VERSION_PATTERN = Pattern.compile("[0-9]+(\\.[0-9]+(\\.[0-9]+(\\.[0-9A-Za-z_-]+)?)?)?");
-
- /** pattern used to change - to . */
- //private static final Pattern P_VERSION = Pattern.compile("([0-9]+(\\.[0-9])*)-(.*)");
-
- /** pattern that matches strings that contain only numbers */
- private static final Pattern ONLY_NUMBERS = Pattern.compile("[0-9]+");
-
  private static final Collection SUPPORTED_PROJECT_TYPES = Arrays.asList(new String[]{"jar","bundle"});
 
  /**
@@ -89,7 +81,13 @@
   * @parameter
   */
  private Map    instructions = new HashMap();
- 
+
+ private Maven2OsgiConverter maven2OsgiConverter = new Maven2OsgiConverter();
+
+ protected Maven2OsgiConverter getMaven2OsgiConverter() {
+  return maven2OsgiConverter;
+ }
+
  protected MavenProject getProject() {
   return project;
  }
@@ -316,89 +314,12 @@
   */
  protected String convertVersionToOsgi(String version)
  {
-     String osgiVersion;
-
-//     Matcher m = P_VERSION.matcher(version);
-//     if (m.matches()) {
-//         osgiVersion = m.group(1) + "." + m.group(3);
-//     }
-
-     /* TODO need a regexp guru here */
-
-     Matcher m;
-     
-     /* if it's already OSGi compliant don't touch it */
-     m = OSGI_VERSION_PATTERN.matcher(version);
-     if (m.matches()) {
-         return version;
-     }
-
-     osgiVersion = version;
-
-     /* check for dated snapshot versions with only major or major and minor */
-     Pattern DATED_SNAPSHOT = Pattern.compile("([0-9])(\\.([0-9]))?(\\.([0-9]))?\\-([0-9]{8}\\.[0-9]{6}\\-[0-9]*)");
-     m = DATED_SNAPSHOT.matcher(osgiVersion);
-     if (m.matches()) {
-         String major = m.group(1);
-         String minor = (m.group(3) != null) ? m.group(3) : "0";
-         String service = (m.group(5) != null) ? m.group(5) : "0";
-         String qualifier = m.group(6).replaceAll( "-", "_" ).replaceAll( "\\.", "_" );
-         osgiVersion = major + "." + minor + "." + service + "." + qualifier;
-     }
-
-     /* else transform first - to . and others to _ */
-     osgiVersion = osgiVersion.replaceFirst( "-", "\\." );
-     osgiVersion = osgiVersion.replaceAll( "-", "_" );
-     m = OSGI_VERSION_PATTERN.matcher(osgiVersion);
-     if (m.matches()) {
-         return osgiVersion;
-     }
-
-     /* remove dots in the middle of the qualifier */
-     Pattern DOTS_IN_QUALIFIER = Pattern.compile("([0-9])(\\.[0-9])?\\.([0-9A-Za-z_-]+)\\.([0-9A-Za-z_-]+)");
-     m = DOTS_IN_QUALIFIER.matcher(osgiVersion);
-     if (m.matches()) {
-         String s1 = m.group(1);
-         String s2 = m.group(2);
-         String s3 = m.group(3);
-         String s4 = m.group(4);
-
-         Matcher qualifierMatcher = ONLY_NUMBERS.matcher( s3 );
-         /* if last portion before dot is only numbers then it's not in the middle of the qualifier */
-         if (!qualifierMatcher.matches()) {
-             osgiVersion = s1 + s2 + "." + s3 + "_" + s4;
-         }
-     }
-
-     /* convert 1.string into 1.0.0.string and 1.2.string into 1.2.0.string */
-     Pattern NEED_TO_FILL_ZEROS = Pattern.compile("([0-9])(\\.([0-9]))?\\.([0-9A-Za-z_-]+)");
-     m = NEED_TO_FILL_ZEROS.matcher(osgiVersion);
-     if (m.matches()) {
-         String major = m.group(1);
-         String minor = ( m.group( 3 ) != null ) ? m.group( 3 ) : "0";
-         String service = "0";
-         String qualifier = m.group(4);
-
-         Matcher qualifierMatcher = ONLY_NUMBERS.matcher( qualifier );
-         /* if last portion is only numbers then it's not a qualifier */
-         if (!qualifierMatcher.matches()) {
-             osgiVersion = major + "." + minor + "." + service + "." + qualifier;
-         }
-     }
-
-     m = OSGI_VERSION_PATTERN.matcher(osgiVersion);
-     /* if still its not OSGi version then add everything as qualifier */
-     if (!m.matches()) {
-         String major = "0";
-         String minor = "0";
-         String service = "0";
-         String qualifier = osgiVersion.replaceAll( "\\.", "_" );
-         osgiVersion = major + "." + minor + "." + service + "." + qualifier;
-     }
-
-     return osgiVersion;
+     return getMaven2OsgiConverter().getVersion( version );
  }
 
+ /**
+  * TODO this should return getMaven2Osgi().getBundleFileName( project.getArtifact() )  
+  */
  protected String getBundleName(MavenProject project) {
   return project.getBuild().getFinalName() + ".jar";
  }
@@ -436,7 +357,7 @@
      properties.put(Analyzer.BUNDLE_SYMBOLICNAME, bsn);
      properties.put(Analyzer.IMPORT_PACKAGE, "*");
 
-     String version = convertVersionToOsgi(project.getVersion());
+     String version = getMaven2OsgiConverter().getVersion( project.getVersion() );
      
      properties.put(Analyzer.BUNDLE_VERSION, version);
      header(properties, Analyzer.BUNDLE_DESCRIPTION, project