345617 - JAXB class generation doesn't work with JDK-shipped JAXB libraries
diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/ClassesGeneratorUi.java b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/ClassesGeneratorUi.java
index 9ff9af8..090e0ab 100644
--- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/ClassesGeneratorUi.java
+++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/ClassesGeneratorUi.java
@@ -9,6 +9,8 @@
 *******************************************************************************/
 package org.eclipse.jpt.jaxb.ui.internal;
 
+import java.util.HashMap;
+import java.util.Map;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.WorkspaceJob;
 import org.eclipse.emf.common.util.URI;
@@ -17,13 +19,19 @@
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.window.Window;
 import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.jpt.jaxb.core.JaxbProject;
+import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
+import org.eclipse.jpt.jaxb.core.SchemaLibrary;
 import org.eclipse.jpt.jaxb.core.internal.gen.ClassesGeneratorExtensionOptions;
 import org.eclipse.jpt.jaxb.core.internal.gen.ClassesGeneratorOptions;
 import org.eclipse.jpt.jaxb.core.internal.gen.GenerateJaxbClassesJob;
+import org.eclipse.jpt.jaxb.core.xsd.XsdUtil;
 import org.eclipse.jpt.jaxb.ui.JptJaxbUiPlugin;
 import org.eclipse.jpt.jaxb.ui.internal.wizards.classesgen.ClassesGeneratorWizard;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
+import org.eclipse.wst.xsd.contentmodel.internal.XSDImpl;
+import org.eclipse.xsd.XSDSchema;
 
 /**
  *  ClassesGeneratorUi
@@ -31,8 +39,8 @@
 public class ClassesGeneratorUi {
 	
 	private final IJavaProject javaProject;
-	private final URI absoluteLocalXsdUri;
-
+	private final IFile xsdFile;
+	
 	// ********** static methods **********
 	
 	public static void generate(IFile xsdFile) {
@@ -40,19 +48,18 @@
 		if (javaProject == null) {
 			throw new NullPointerException();
 		}
-		URI xsdUri = URI.createURI(xsdFile.getLocation().toString());
 		
-		new ClassesGeneratorUi(javaProject, xsdUri).generate();
+		new ClassesGeneratorUi(javaProject, xsdFile).generate();
 	}
 
 	// ********** constructors **********
-	private ClassesGeneratorUi(IJavaProject javaProject, URI absoluteLocalXsdUri) {
+	private ClassesGeneratorUi(IJavaProject javaProject, IFile xsdFile) {
 		super();
 		if (javaProject == null) {
 			throw new NullPointerException();
 		}
 		this.javaProject = javaProject;
-		this.absoluteLocalXsdUri = absoluteLocalXsdUri;
+		this.xsdFile = xsdFile;
 	}
 
 	// ********** generate **********
@@ -60,7 +67,7 @@
 	 * prompt the user with a wizard
 	 */
 	protected void generate() {
-		ClassesGeneratorWizard wizard = new ClassesGeneratorWizard(this.javaProject, this.absoluteLocalXsdUri);
+		ClassesGeneratorWizard wizard = new ClassesGeneratorWizard(this.javaProject, this.xsdFile);
 		wizard.setWindowTitle(JptJaxbUiMessages.ClassesGeneratorWizard_title);
 		WizardDialog dialog = new WizardDialog(this.getCurrentShell(), wizard);
 		dialog.create();
@@ -68,41 +75,44 @@
 		if (returnCode != Window.OK) {
 			return;
 		}
-		String outputDir = wizard.getDestinationFolder();
-		String targetPackage = wizard.getTargetPackage();
-		String catalog = wizard.getCatalog();
-		boolean usesMoxy = wizard.usesMoxy();
-		String[] bindingsFileNames = wizard.getBindingsFileNames();
-		ClassesGeneratorOptions generatorOptions = wizard.getGeneratorOptions();
-		ClassesGeneratorExtensionOptions generatorExtensionOptions = wizard.getGeneratorExtensionOptions();
-
-		if(this.displayOverridingClassesWarning(generatorOptions)) {
-			this.generateJaxbClasses(outputDir, targetPackage, catalog, usesMoxy, bindingsFileNames, generatorOptions, generatorExtensionOptions);
+		
+		if(this.displayOverridingClassesWarning(wizard.getGeneratorOptions())) {
+			this.generateJaxbClasses(
+					wizard.getLocalSchemaUri(), 
+					wizard.getDestinationFolder(), 
+					wizard.getTargetPackage(), 
+					wizard.getCatalog(), 
+					wizard.usesMoxy(), 
+					wizard.getBindingsFileNames(),
+					wizard.getGeneratorOptions(),
+					wizard.getGeneratorExtensionOptions());
+			addSchemaToLibrary(wizard.getSchemaLocation());
 		}
 	}
 
 	// ********** internal methods **********
 
 	private void generateJaxbClasses(
-		String outputDir,
-		String targetPackage, 
-		String catalog, 
-		boolean usesMoxyGenerator,
-		String[] bindingsFileNames,
-		ClassesGeneratorOptions generatorOptions,
-		ClassesGeneratorExtensionOptions generatorExtensionOptions) {
+			URI schemaUri,
+			String outputDir,
+			String targetPackage, 
+			String catalog, 
+			boolean usesMoxyGenerator,
+			String[] bindingsFileNames,
+			ClassesGeneratorOptions generatorOptions,
+			ClassesGeneratorExtensionOptions generatorExtensionOptions) {
 		
 		try {
 			WorkspaceJob job = new GenerateJaxbClassesJob(
-				this.javaProject, 
-				this.absoluteLocalXsdUri.toString(), 
-				outputDir, 
-				targetPackage, 
-				catalog, 
-				usesMoxyGenerator,
-				bindingsFileNames,
-				generatorOptions,
-				generatorExtensionOptions);
+					this.javaProject, 
+					schemaUri.toString(),
+					outputDir, 
+					targetPackage, 
+					catalog, 
+					usesMoxyGenerator,
+					bindingsFileNames,
+					generatorOptions,
+					generatorExtensionOptions);
 			job.schedule();
 		}
 		catch(RuntimeException re) {
@@ -114,6 +124,28 @@
 		}
 	}
 	
+	private void addSchemaToLibrary(String schemaLocation) {
+		JaxbProject jaxbProject = getJaxbProject();
+		
+		if (jaxbProject == null) {
+			return;
+		}
+		
+		String resolvedUri = XsdUtil.getResolvedUri(null, schemaLocation);
+		XSDSchema schema = XSDImpl.buildXSDModel(resolvedUri);
+		if (schema != null) {
+			String schemaNamespace = 
+				((schema.getTargetNamespace()) == null ? 
+						""
+						: schema.getTargetNamespace());
+			
+			SchemaLibrary schemaLib = jaxbProject.getSchemaLibrary();
+			Map<String, String> schemas = new HashMap<String, String>(schemaLib.getSchemaLocations());
+			schemas.put(schemaNamespace, schemaLocation);
+			schemaLib.setSchemaLocations(schemas);
+		}
+	}
+	
 	private void logError(String message) {
 		this.displayError(message);
 	}
@@ -137,6 +169,11 @@
 		return shell;
 	}
 	
+	/* may be null */
+	private JaxbProject getJaxbProject() {
+		return JptJaxbCorePlugin.getJaxbProject(this.javaProject.getProject());
+	}
+	
 	private boolean isOverridingClasses(ClassesGeneratorOptions generatorOptions) {
 		if(generatorOptions == null) {
 			throw new NullPointerException();
diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizard.java b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizard.java
index 24d7535..364abbb 100644
--- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizard.java
+++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizard.java
@@ -9,6 +9,8 @@
 *******************************************************************************/
 package org.eclipse.jpt.jaxb.ui.internal.wizards.classesgen;
 
+import java.util.HashMap;
+import java.util.Map;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
@@ -16,7 +18,6 @@
 import org.eclipse.core.resources.WorkspaceJob;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.emf.common.CommonPlugin;
 import org.eclipse.emf.common.util.URI;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jdt.core.IJavaProject;
@@ -25,23 +26,31 @@
 import org.eclipse.jface.wizard.Wizard;
 import org.eclipse.jface.wizard.WizardPage;
 import org.eclipse.jpt.common.ui.internal.wizards.JavaProjectWizardPage;
+import org.eclipse.jpt.jaxb.core.JaxbProject;
+import org.eclipse.jpt.jaxb.core.JptJaxbCorePlugin;
+import org.eclipse.jpt.jaxb.core.SchemaLibrary;
 import org.eclipse.jpt.jaxb.core.internal.gen.ClassesGeneratorExtensionOptions;
 import org.eclipse.jpt.jaxb.core.internal.gen.ClassesGeneratorOptions;
 import org.eclipse.jpt.jaxb.core.internal.gen.GenerateJaxbClassesJob;
+import org.eclipse.jpt.jaxb.core.xsd.XsdUtil;
 import org.eclipse.jpt.jaxb.ui.JptJaxbUiPlugin;
 import org.eclipse.jpt.jaxb.ui.internal.JptJaxbUiIcons;
 import org.eclipse.jpt.jaxb.ui.internal.JptJaxbUiMessages;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.ui.IWorkbench;
 import org.eclipse.ui.IWorkbenchWizard;
+import org.eclipse.wst.xsd.contentmodel.internal.XSDImpl;
+import org.eclipse.xsd.XSDSchema;
 
 /**
  *  ClassesGeneratorWizard
  */
-public class ClassesGeneratorWizard extends Wizard implements IWorkbenchWizard {
-
+public class ClassesGeneratorWizard
+		extends Wizard
+		implements IWorkbenchWizard {
+	
 	private IJavaProject javaProject;
-	private URI absoluteLocalXsdUri;  // the URI must be absolutely defined in the local file system
+	private IFile preselectedXsdFile;
 	protected IStructuredSelection selection;
 	
 	private String destinationFolder;
@@ -52,7 +61,7 @@
 	
 	private ClassesGeneratorOptions generatorOptions;
 	private ClassesGeneratorExtensionOptions generatorExtensionOptions;
-
+	
 	private JavaProjectWizardPage projectWizardPage;
 	private SchemaWizardPage schemaWizardPage;
 	
@@ -60,7 +69,8 @@
 	private ClassesGeneratorOptionsWizardPage optionsPage;
 	private ClassesGeneratorExtensionOptionsWizardPage extensionOptionsPage;
 	private boolean performsGeneration;
-
+	
+	
 	// ********** constructor **********
 	
 	public ClassesGeneratorWizard() {
@@ -68,16 +78,16 @@
 		this.performsGeneration = true;
 	}
 	
-	public ClassesGeneratorWizard(IJavaProject javaProject, URI absoluteLocalXsdUri) {
+	public ClassesGeneratorWizard(IJavaProject javaProject, IFile xsdFile) {
 		super();
 		this.javaProject = javaProject;
-		this.absoluteLocalXsdUri = absoluteLocalXsdUri;
-
+		this.preselectedXsdFile = xsdFile;
 		this.performsGeneration = false;
 	}
-
+	
+	
 	// ********** IWorkbenchWizard implementation  **********
-
+	
 	public void init(IWorkbench workbench, IStructuredSelection selection) {
 		this.selection = selection;
 		
@@ -85,14 +95,15 @@
 		this.setDefaultPageImageDescriptor(JptJaxbUiPlugin.getImageDescriptor(JptJaxbUiIcons.CLASSES_GEN_WIZ_BANNER));
 		this.setNeedsProgressMonitor(true);
 	}
-
+	
+	
 	// ********** IWizard implementation  **********
 	
 	@Override
 	public void addPages() {
 		super.addPages();
-
-		if(this.selection != null) {
+		
+		if (this.selection != null) {
 			this.javaProject = this.getJavaProjectFromSelection(this.selection);
 			
 			this.projectWizardPage = new JavaProjectWizardPage(this.javaProject);
@@ -100,17 +111,18 @@
 			this.projectWizardPage.setDescription(JptJaxbUiMessages.ClassesGeneratorProjectWizardPage_desc);
 			this.projectWizardPage.setDestinationLabel(JptJaxbUiMessages.JavaProjectWizardPage_destinationProject);
 			this.addPage(this.projectWizardPage);
-
+			
 			// SchemaWizardPage
-			IFile schemaSelected = SchemaWizardPage.getSourceSchemaFromSelection(this.selection);
-			if (schemaSelected == null) {
+			if (this.preselectedXsdFile == null) {
+				this.preselectedXsdFile = SchemaWizardPage.getSourceSchemaFromSelection(this.selection);
+			}
+			
+			if (this.preselectedXsdFile == null) {
 				this.schemaWizardPage = new SchemaWizardPage(this.selection);
 				this.addPage(this.schemaWizardPage);
 			}
-			else {
-				this.absoluteLocalXsdUri = URI.createFileURI(schemaSelected.getLocation().toString());
-			}
 		}
+		
 		this.settingsPage = this.buildClassesGeneratorPage();
 		this.optionsPage = this.buildClassesGeneratorOptionsPage();
 		this.extensionOptionsPage = this.buildExtensionOptionsPage();
@@ -146,6 +158,7 @@
 		if (this.performsGeneration) {
 			if (displayOverridingClassesWarning(this.generatorOptions)) {
 				generateJaxbClasses();
+				addSchemaToLibrary();
 			}
 		}
 
@@ -160,19 +173,32 @@
 		}
     	return this.javaProject;
     }
-
-	public URI getAbsoluteLocalXsdUri() {
-		if (this.schemaWizardPage != null) {
-			IFile schemaFile = this.schemaWizardPage.getSourceSchema();
-			if(schemaFile != null) {
-				return URI.createFileURI(schemaFile.getLocation().toString());
-			}
-			else {
-				URI uri = CommonPlugin.asLocalURI(URI.createURI(this.schemaWizardPage.getSourceURI()));
-				return uri;
-			}
+	
+	/* may be null */
+	private JaxbProject getJaxbProject() {
+		return JptJaxbCorePlugin.getJaxbProject(getJavaProject().getProject());
+	}
+	
+	/* return the physical location of the schema */
+	public URI getLocalSchemaUri() {
+		if (this.preselectedXsdFile != null) {
+			return URI.createFileURI(this.preselectedXsdFile.getLocation().toString());
 		}
-		return this.absoluteLocalXsdUri;
+		else if (this.schemaWizardPage != null) {
+			return this.schemaWizardPage.getLocalSchemaURI();
+		}
+		return null;
+	}
+	
+	/* return the uri or file platform resource uri used for schema resolution */
+	public String getSchemaLocation() {
+		if (this.preselectedXsdFile != null) {
+			return URI.createPlatformResourceURI(this.preselectedXsdFile.getFullPath().toString(), false).toString();
+		}
+		else if (this.schemaWizardPage != null) {
+			return this.schemaWizardPage.getSchemaLocation();
+		}
+		return null;
 	}
 	
 
@@ -323,7 +349,7 @@
 			WorkspaceJob job = 
 					new GenerateJaxbClassesJob(
 						this.getJavaProject(),
-						this.getAbsoluteLocalXsdUri().toString(),
+						this.getLocalSchemaUri().toString(),
 						this.destinationFolder,
 						this.targetPackage,
 						this.catalog,
@@ -342,6 +368,29 @@
 		}
 	}
 	
+	private void addSchemaToLibrary() {
+		JaxbProject jaxbProject = getJaxbProject();
+		
+		if (jaxbProject == null) {
+			return;
+		}
+		
+		String schemaLocation = getSchemaLocation();
+		String resolvedUri = XsdUtil.getResolvedUri(null, schemaLocation);
+		XSDSchema schema = XSDImpl.buildXSDModel(resolvedUri);
+		if (schema != null) {
+			String schemaNamespace = 
+				((schema.getTargetNamespace()) == null ? 
+						""
+						: schema.getTargetNamespace());
+			
+			SchemaLibrary schemaLib = jaxbProject.getSchemaLibrary();
+			Map<String, String> schemas = new HashMap<String, String>(schemaLib.getSchemaLocations());
+			schemas.put(schemaNamespace, schemaLocation);
+			schemaLib.setSchemaLocations(schemas);
+		}
+	}
+	
 	protected void logError(String message) {
 			this.displayError(message);
 	}
diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizardPage.java b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizardPage.java
index 6c115dd..505a65f 100644
--- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizardPage.java
+++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/ClassesGeneratorWizardPage.java
@@ -367,7 +367,7 @@
 			}
 			this.validateProjectClasspath();
 
-			String schemaName = ((ClassesGeneratorWizard) getWizard()).getAbsoluteLocalXsdUri().lastSegment();
+			String schemaName = ((ClassesGeneratorWizard) getWizard()).getLocalSchemaUri().lastSegment();
 			this.setTitle(NLS.bind(JptJaxbUiMessages.ClassesGeneratorWizardPage_title, schemaName));
 		}
 	}
diff --git a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/SchemaWizardPage.java b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/SchemaWizardPage.java
index 7030f1c..cb1a1d6 100644
--- a/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/SchemaWizardPage.java
+++ b/jaxb/plugins/org.eclipse.jpt.jaxb.ui/src/org/eclipse/jpt/jaxb/ui/internal/wizards/classesgen/SchemaWizardPage.java
@@ -12,6 +12,8 @@
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.emf.common.CommonPlugin;
+import org.eclipse.emf.common.util.URI;
 import org.eclipse.jdt.core.IJavaElement;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -92,8 +94,8 @@
 		super.setVisible(visible);
 		if(visible) {
 			
-			if(this.getSourceSchema() != null) {
-	    		this.selectSourcePanel.setSingleFileViewDefaultSelection(new StructuredSelection(this.getSourceSchema()));
+			if(this.getSchemaFile() != null) {
+	    		this.selectSourcePanel.setSingleFileViewDefaultSelection(new StructuredSelection(this.getSchemaFile()));
 	    	}
 	    	else {
 	    		this.updateTargetProject();
@@ -120,31 +122,46 @@
     @Override
 	public boolean isPageComplete() {
     	
-		return this.schemaOrUriSelected() && (this.getErrorMessage() == null);
+		return this.fileOrXmlCatalogEntrySelected() && (this.getErrorMessage() == null);
 	}
     
     
 	// ********** intra-wizard methods **********
 	
-	public IFile getSourceSchema() {
+    /* return the file, if a file is selected */
+	public IFile getSchemaFile() {
 		return this.selectSourcePanel.getFile();
 	}
 	
-	public String getSourceURI() {
-		String uri = this.selectSourcePanel.getXMLCatalogURI();
-		if (uri == null) {
-			IFile file = this.selectSourcePanel.getFile();
-			if (file != null) {
-				uri = URIHelper.getPlatformURI(file);
-			}
-		}
-		return uri;
-	}
-	
+	/* return xml catalog id, if a catalog entry is selected */
 	public String getXMLCatalogId() {
 		return this.selectSourcePanel.getXMLCatalogId();
 	}
 	
+	/* return identifier of schema location, whether using file or xml catalog */
+	public String getSchemaLocation() {
+		IFile file = this.selectSourcePanel.getFile();
+		if (file != null) {
+			return URI.createPlatformResourceURI(file.getFullPath().toString(), false).toString();
+		}
+		return this.selectSourcePanel.getXMLCatalogId();
+	}
+	
+	/* return local uri of schema, whether using file or xml catalog */
+	public URI getLocalSchemaURI() {
+		IFile file = this.selectSourcePanel.getFile();
+		if (file != null) {
+			return URI.createFileURI(file.getLocation().toString());
+		}
+		
+		String uri = this.selectSourcePanel.getXMLCatalogURI();
+		if (uri != null) {
+			return CommonPlugin.asLocalURI(URI.createURI(uri));
+		}
+		
+		return null;
+	}
+	
 	
 	// ********** internal methods **********
 	
@@ -175,15 +192,15 @@
 		return null;
     }
     
-	private boolean schemaOrUriSelected() {
-		return ((this.getSourceSchema() != null) || (this.getSourceURI() != null));
+	private boolean fileOrXmlCatalogEntrySelected() {
+		return this.getSchemaFile() != null || this.getXMLCatalogId() != null;
 	}
 	
 	private String computeErrorMessage() {
 		String errorMessage = null;
-		String uri = this.getSourceURI();
-		if(uri != null) {
-			if( ! URIHelper.isReadableURI(uri, false)) {
+		URI uri = this.getLocalSchemaURI();
+		if (uri != null) {
+			if (! URIHelper.isReadableURI(uri.toString(), false)) {
 				errorMessage = JptJaxbUiMessages.SchemaWizardPage_errorUriCannotBeLocated;
 			}
 		}