Merge change 24047 into eclair

* changes:
  ADT: fix an NPE that can happen when an XML file resource is being refreshed by Eclipse whilst the SDK is not finished loading.
This commit is contained in:
Android (Google) Code Review
2009-09-04 17:56:34 -07:00

View File

@@ -37,7 +37,7 @@ import org.eclipse.ui.part.FileEditorInput;
import org.w3c.dom.Document; import org.w3c.dom.Document;
/** /**
* Multi-page form editor for /res/xml XML files. * Multi-page form editor for /res/xml XML files.
*/ */
public class XmlEditor extends AndroidEditor { public class XmlEditor extends AndroidEditor {
@@ -69,7 +69,7 @@ public class XmlEditor extends AndroidEditor {
* <p/> * <p/>
* The {@link XmlEditor} can handle XML files that have a <searchable> or * The {@link XmlEditor} can handle XML files that have a <searchable> or
* <Preferences> root XML element with the adequate xmlns:android attribute. * <Preferences> root XML element with the adequate xmlns:android attribute.
* *
* @return True if the {@link XmlEditor} can handle that file. * @return True if the {@link XmlEditor} can handle that file.
*/ */
public static boolean canHandleFile(IFile file) { public static boolean canHandleFile(IFile file) {
@@ -77,14 +77,17 @@ public class XmlEditor extends AndroidEditor {
IProject project = file.getProject(); IProject project = file.getProject();
IAndroidTarget target = Sdk.getCurrent().getTarget(project); IAndroidTarget target = Sdk.getCurrent().getTarget(project);
if (target != null) { if (target != null) {
// Note: the target data can be null when an SDK is not finished loading yet.
// We can potentially arrive here when Eclipse is started with a file previously
// open and the resource gets refreshed -- at that point we may not have the SDK yet.
AndroidTargetData data = Sdk.getCurrent().getTargetData(target); AndroidTargetData data = Sdk.getCurrent().getTargetData(target);
FirstElementParser.Result result = FirstElementParser.parse( FirstElementParser.Result result = FirstElementParser.parse(
file.getLocation().toOSString(), file.getLocation().toOSString(),
SdkConstants.NS_RESOURCES); SdkConstants.NS_RESOURCES);
if (result != null) { if (result != null && data != null) {
String name = result.getElement(); String name = result.getElement();
if (name != null && result.getXmlnsPrefix() != null) { if (name != null && result.getXmlnsPrefix() != null) {
DocumentDescriptor desc = data.getXmlDescriptors().getDescriptor(); DocumentDescriptor desc = data.getXmlDescriptors().getDescriptor();
for (ElementDescriptor elem : desc.getChildren()) { for (ElementDescriptor elem : desc.getChildren()) {
@@ -96,7 +99,7 @@ public class XmlEditor extends AndroidEditor {
} }
} }
} }
return false; return false;
} }
@@ -106,7 +109,7 @@ public class XmlEditor extends AndroidEditor {
* Returns whether the "save as" operation is supported by this editor. * Returns whether the "save as" operation is supported by this editor.
* <p/> * <p/>
* Save-As is a valid operation for the ManifestEditor since it acts on a * Save-As is a valid operation for the ManifestEditor since it acts on a
* single source file. * single source file.
* *
* @see IEditorPart * @see IEditorPart
*/ */
@@ -125,7 +128,7 @@ public class XmlEditor extends AndroidEditor {
} catch (PartInitException e) { } catch (PartInitException e) {
AdtPlugin.log(e, "Error creating nested page"); //$NON-NLS-1$ AdtPlugin.log(e, "Error creating nested page"); //$NON-NLS-1$
} }
} }
/* (non-java doc) /* (non-java doc)
@@ -140,10 +143,10 @@ public class XmlEditor extends AndroidEditor {
setPartName(String.format("%1$s", file.getName())); setPartName(String.format("%1$s", file.getName()));
} }
} }
/** /**
* Processes the new XML Model, which XML root node is given. * Processes the new XML Model, which XML root node is given.
* *
* @param xml_doc The XML document, if available, or null if none exists. * @param xml_doc The XML document, if available, or null if none exists.
*/ */
@Override @Override
@@ -152,10 +155,10 @@ public class XmlEditor extends AndroidEditor {
initUiRootNode(false /*force*/); initUiRootNode(false /*force*/);
mUiRootNode.loadFromXmlNode(xml_doc); mUiRootNode.loadFromXmlNode(xml_doc);
super.xmlModelChanged(xml_doc); super.xmlModelChanged(xml_doc);
} }
/** /**
* Creates the initial UI Root Node, including the known mandatory elements. * Creates the initial UI Root Node, including the known mandatory elements.
* @param force if true, a new UiRootNode is recreated even if it already exists. * @param force if true, a new UiRootNode is recreated even if it already exists.
@@ -198,5 +201,5 @@ public class XmlEditor extends AndroidEditor {
mUiRootNode.reloadFromXmlNode(mUiRootNode.getXmlNode()); mUiRootNode.reloadFromXmlNode(mUiRootNode.getXmlNode());
} }
} }
} }