234905 Backport fix for bug 158409 to 1.5.5 patches
diff --git a/bundles/org.eclipse.wst.xsd.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.wst.xsd.ui/META-INF/MANIFEST.MF
index a0074ba..1eeb1b7 100644
--- a/bundles/org.eclipse.wst.xsd.ui/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.wst.xsd.ui/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %_UI_PLUGIN_NAME
 Bundle-SymbolicName: org.eclipse.wst.xsd.ui; singleton:=true
-Bundle-Version: 1.1.105.qualifier
+Bundle-Version: 1.1.106.qualifier
 Bundle-Activator: org.eclipse.wst.xsd.ui.internal.editor.XSDEditorPlugin
 Bundle-Vendor: %Bundle-Vendor.0
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/UpdateComponentReferenceAndManageDirectivesCommand.java b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/UpdateComponentReferenceAndManageDirectivesCommand.java
index fcb9b46..74b1099 100644
--- a/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/UpdateComponentReferenceAndManageDirectivesCommand.java
+++ b/bundles/org.eclipse.wst.xsd.ui/src-common/org/eclipse/wst/xsd/ui/internal/common/commands/UpdateComponentReferenceAndManageDirectivesCommand.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2001, 2006 IBM Corporation and others.
+ * Copyright (c) 2001, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -39,72 +39,89 @@
     XSDComponent result = null;
     XSDSchema schema = concreteComponent.getSchema();
     XSDSchemaDirective directive = null;
-    // TODO (cs) handle case where namespace==null
-    //
-    if (componentNamespace != null)
+
+    // lets see if the element is already visible to our schema
+    result = getDefinedComponent(schema, componentName, componentNamespace);
+    if (result == null)
     {
-      // lets see if the element is already visible to our schema
-      result = getDefinedComponent(schema, componentName, componentNamespace);
-      if (result == null)
+      // TODO (cs) we need to provide a separate command to do this part
+      //
+      // apparently the element is not yet visible, we need to add
+      // includes/imports to get to it
+      if (componentNamespace != null && componentNamespace.equals(schema.getTargetNamespace()))
       {
-        // TODO (cs) we need to provide a separate command to do this part
-        //
-        // apparently the element is not yet visible, we need to add
-        // includes/imports to get to it
-        if (componentNamespace.equals(schema.getTargetNamespace()))
+        // we need to add an include
+        // if the component's namespace is not null and matches the schema's
+        // target namespace
+        directive = XSDFactory.eINSTANCE.createXSDInclude();
+      }
+      else if (componentNamespace == null)
+      {
+        // we need to add an include
+        // if the component's namespace is null, then we can just add it
+        // only if the current namespace is not null
+        directive = XSDFactory.eINSTANCE.createXSDInclude();
+
+        // we have to ensure the schema for schema prefix is NOT null
+        if (schema.getSchemaForSchemaQNamePrefix() == null)
         {
-          // we need to add an include
-          directive = XSDFactory.eINSTANCE.createXSDInclude();
-        }
-        else
-        {
-          // we need to add an import
-          XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
-          xsdImport.setNamespace(componentNamespace);
-          directive = xsdImport;
-        }
-     
-        String location = computeNiceLocation(schema.getSchemaLocation(), file);       
-        directive.setSchemaLocation(location);
-        // TODO (cs) we should at the directive 'next' in the list of directives
-        // for now I'm just adding as the first thing in the schema :-(
-        //
-        schema.getContents().add(0, directive);
-        XSDSchema resolvedSchema = directive.getResolvedSchema();
-        if (resolvedSchema == null)
-        {
-          String platformLocation = "platform:/resource" + file.getFullPath();
-          Resource resource = concreteComponent.eResource().getResourceSet().createResource(URI.createURI(platformLocation));
-          if (resource instanceof XSDResourceImpl)
-          {
-            try
-            {
-              resource.load(null);
-              XSDResourceImpl resourceImpl = (XSDResourceImpl) resource;
-              resolvedSchema = resourceImpl.getSchema();
-              if (resolvedSchema != null)
-              {
-                directive.setResolvedSchema(resolvedSchema);
-              }
-            }
-            catch (Exception e)
-            {
-            }
-          }
-        }
-        if (resolvedSchema != null)
-        {
-          result = getDefinedComponent(resolvedSchema, componentName, componentNamespace);
-        }
-        else
-        {
-          // TODO (cs) consider setting some error state so that the client can
-          // provide a pop-dialog error
-          // we should also remove the import/include so save from cluttering
-          // the file with bogus directives
+          String targetNS = schema.getTargetNamespace();
+          if (targetNS == null)
+            targetNS = "";
+          // this will just update the schema for schema prefix to be, say, xsd
+          UpdateNamespaceInformationCommand command = new UpdateNamespaceInformationCommand("", schema, "", targetNS);
+          command.execute();
         }
       }
+      else
+      {
+        // we need to add an import
+        XSDImport xsdImport = XSDFactory.eINSTANCE.createXSDImport();
+        xsdImport.setNamespace(componentNamespace);
+        directive = xsdImport;
+      }
+
+      String location = computeNiceLocation(schema.getSchemaLocation(), file);
+      directive.setSchemaLocation(location);
+      // TODO (cs) we should at the directive 'next' in the list of directives
+      // for now I'm just adding as the first thing in the schema :-(
+      //
+      schema.getContents().add(0, directive);
+      XSDSchema resolvedSchema = directive.getResolvedSchema();
+      if (resolvedSchema == null)
+      {
+        String platformLocation = "platform:/resource" + file.getFullPath();
+        Resource resource = concreteComponent.eResource().getResourceSet().createResource(URI.createURI(platformLocation));
+        if (resource instanceof XSDResourceImpl)
+        {
+          try
+          {
+            resource.load(null);
+            XSDResourceImpl resourceImpl = (XSDResourceImpl) resource;
+            resolvedSchema = resourceImpl.getSchema();
+            if (resolvedSchema != null)
+            {
+              directive.setResolvedSchema(resolvedSchema);
+            }
+          }
+          catch (Exception e)
+          {
+          }
+        }
+      }
+      if (resolvedSchema != null)
+      {
+        result = getDefinedComponent(resolvedSchema, componentName, componentNamespace);
+      }
+      else
+      {
+        // TODO (cs) consider setting some error state so that the client can
+        // provide a pop-dialog error
+        // we should also remove the import/include so save from cluttering
+        // the file with bogus directives
+      }
     }
+
     return result;
   }
   
diff --git a/features/org.eclipse.wst.xml_ui.feature.patch/buildnotes_org.eclipse.wst.xml_ui.feature.patch.html b/features/org.eclipse.wst.xml_ui.feature.patch/buildnotes_org.eclipse.wst.xml_ui.feature.patch.html
index 5254ae0..d793c39 100644
--- a/features/org.eclipse.wst.xml_ui.feature.patch/buildnotes_org.eclipse.wst.xml_ui.feature.patch.html
+++ b/features/org.eclipse.wst.xml_ui.feature.patch/buildnotes_org.eclipse.wst.xml_ui.feature.patch.html
@@ -16,4 +16,5 @@
 <p>Bug <a href='https://bugs.eclipse.org/bugs/show_bug.cgi?id=199053'>199053</a>. Syntax errors outside of scripting areas not reported</p>
 <p>Bug <a href='https://bugs.eclipse.org/bugs/show_bug.cgi?id=186444'>186444</a>. some actions/commands in the source menu do not work</p>
 <p>Bug <a href='https://bugs.eclipse.org/bugs/show_bug.cgi?id=220005'>220005</a>. Incorrect prefix handling on XSD to XML Generator</p>
+<p>Bug <a href='https://bugs.eclipse.org/bugs/show_bug.cgi?id=234905'>234905</a>. Backport fix for bug 158409 to 1.5.5 patches</p>
 </body></html>
\ No newline at end of file
diff --git a/features/org.eclipse.wst.xml_ui.feature.patch/feature.properties b/features/org.eclipse.wst.xml_ui.feature.patch/feature.properties
index 78dcee4..acdd072 100644
--- a/features/org.eclipse.wst.xml_ui.feature.patch/feature.properties
+++ b/features/org.eclipse.wst.xml_ui.feature.patch/feature.properties
@@ -33,7 +33,7 @@
 Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=199053 Syntax errors outside of scripting areas not reported  \n\
 Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=186444 Some actions/commands in the source menu do not work \n\
 Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=220005 Incorrect prefix handling on XSD to XML Generator \n\
-
+Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=234905 Backport fix for bug 158409 to 1.5.5 patches\n\
 \n\
 
 # "copyright" property - text of the "Feature Update Copyright"