Bug 562484 - Auto-generated Automatic-Module-Name is not guaranteed to
be a legal module name 

Change-Id: I0afb510a77b78cd146ab37096ce008c1bbd84987
Signed-off-by: Vikas Chandra <Vikas.Chandra@in.ibm.com>
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddAutomaticModuleResolution.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddAutomaticModuleResolution.java
index 306aa51..1a9eae5 100644
--- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddAutomaticModuleResolution.java
+++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/correction/AddAutomaticModuleResolution.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- *  Copyright (c) 2017, 2019 IBM Corporation and others.
+ *  Copyright (c) 2017, 2020 IBM Corporation and others.
  *
  *  This program and the accompanying materials
  *  are made available under the terms of the Eclipse Public License 2.0
@@ -22,6 +22,7 @@
 import org.eclipse.pde.internal.core.text.bundle.Bundle;
 import org.eclipse.pde.internal.core.text.bundle.BundleModel;
 import org.eclipse.pde.internal.ui.PDEUIMessages;
+import org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation;
 import org.osgi.framework.Constants;
 
 public class AddAutomaticModuleResolution extends AbstractManifestMarkerResolution {
@@ -59,7 +60,8 @@
 				catch (Exception e) {
 					// for cases where ; not present
 				}
-				bundle.setHeader(ICoreConstants.AUTOMATIC_MODULE_NAME, val);
+				bundle.setHeader(ICoreConstants.AUTOMATIC_MODULE_NAME,
+						NewProjectCreationOperation.determineAutomaticModuleNameFromBSN(val));
 			}
 		}
 	}
diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/plugin/NewProjectCreationOperation.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/plugin/NewProjectCreationOperation.java
index 4177552..9b4945f 100644
--- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/plugin/NewProjectCreationOperation.java
+++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/plugin/NewProjectCreationOperation.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2019 IBM Corporation and others.
+ * Copyright (c) 2000, 2020 IBM Corporation and others.
  *
  * This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -204,7 +204,8 @@
 		// add Bundle Specific fields if applicable
 		if (pluginBase instanceof BundlePluginBase) {
 			IBundle bundle = ((BundlePluginBase) pluginBase).getBundle();
-			bundle.setHeader(ICoreConstants.AUTOMATIC_MODULE_NAME, bundle.getHeader(Constants.BUNDLE_SYMBOLICNAME));
+			String header = bundle.getHeader(Constants.BUNDLE_SYMBOLICNAME);
+			bundle.setHeader(ICoreConstants.AUTOMATIC_MODULE_NAME, determineAutomaticModuleNameFromBSN(header));
 
 			String value = getCommaValuesFromPackagesSet(getImportPackagesSet(), fData.getVersion());
 			if (value.length() > 0)
@@ -250,6 +251,65 @@
 		}
 	}
 
+	/**
+	 * copied and edited from jdt.core
+	 *
+	 * @see org.eclipse.jdt.internal.compiler.env.AutomaticModuleNaming#determineAutomaticModuleNameFromFileName(String,
+	 *      boolean, boolean)
+	 * @param name
+	 *            bundle symbolic name
+	 * @return automatic module name corresponding to BSN
+	 */
+
+	public static String determineAutomaticModuleNameFromBSN(String name) {
+		int index;
+		int start = 0;
+		int end = name.length();
+
+		// "If the name matches the regular expression "-(\\d+(\\.|$))" then the
+		// module name will be derived from the
+		// subsequence preceding the hyphen of the first occurrence. [...]"
+		dashLoop: for (index = start; index < end - 1; index++) {
+			if (name.charAt(index) == '-' && name.charAt(index + 1) >= '0' && name.charAt(index + 1) <= '9') {
+				for (int index2 = index + 2; index2 < end; index2++) {
+					final char c = name.charAt(index2);
+					if (c == '.') {
+						break;
+					}
+					if (c < '0' || c > '9') {
+						continue dashLoop;
+					}
+				}
+				end = index;
+				break;
+			}
+		}
+
+		// "All non-alphanumeric characters ([^A-Za-z0-9]) in the module name
+		// are replaced with a dot ("."), all
+		// repeating dots are replaced with one dot, and all leading and
+		// trailing dots are removed."
+		StringBuilder sb = new StringBuilder(end - start);
+		boolean needDot = false;
+		for (int i = start; i < end; i++) {
+			char c = name.charAt(i);
+			if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
+				if (needDot) {
+					sb.append('.');
+					needDot = false;
+				}
+				sb.append(c);
+			} else {
+				if (sb.length() > 0) {
+					needDot = true;
+				}
+			}
+		}
+		return sb.toString();
+	}
+
+
+
 	private IProject createProject() throws CoreException {
 		IProject project = fProjectProvider.getProject();
 		if (!project.exists()) {