From 86f5038595415f88aed0f4c867e938651eb893f8 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Mon, 28 Feb 2011 01:53:06 +0000 Subject: [PATCH] Fixed NPE. --- .../core/language/LanguageMappingStore.java | 54 +++++++++---------- .../internal/ui/text/doctools/ProjectMap.java | 39 +++++++------- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java index 0a8ec1d3ee5..2f46ca220f7 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java @@ -1,13 +1,14 @@ /******************************************************************************* - * Copyright (c) 2007, 2011 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 - * http://www.eclipse.org/legal/epl-v10.html + * Copyright (c) 2007, 2011 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 + * http://www.eclipse.org/legal/epl-v10.html * - * Contributors: - * IBM Corporation - Initial API and implementation - * James Blackburn (Broadcom Corp.) + * Contributors: + * IBM Corporation - Initial API and implementation + * James Blackburn (Broadcom Corp.) + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.core.language; @@ -53,21 +54,13 @@ import org.xml.sax.SAXException; */ public class LanguageMappingStore { private static final String LANGUAGE_MAPPING_ID = "org.eclipse.cdt.core.language.mapping"; //$NON-NLS-1$ - private static final String PROJECT_MAPPINGS = "project-mappings"; //$NON-NLS-1$ - private static final String WORKSPACE_MAPPINGS = "workspace-mappings"; //$NON-NLS-1$ - private static final String CONTENT_TYPE_MAPPING = "content-type-mapping"; //$NON-NLS-1$ - private static final String FILE_MAPPING = "file-mapping"; //$NON-NLS-1$ - private static final String ATTRIBUTE_PATH = "path"; //$NON-NLS-1$ - private static final String ATTRIBUTE_CONTENT_TYPE = "content-type"; //$NON-NLS-1$ - private static final String ATTRIBUTE_LANGUAGE = "language"; //$NON-NLS-1$ - private static final String ATTRIBUTE_CONFIGURATION = "configuration"; //$NON-NLS-1$ public LanguageMappingStore() { @@ -76,16 +69,16 @@ public class LanguageMappingStore { public ProjectLanguageConfiguration decodeMappings(IProject project) throws CoreException { ProjectLanguageConfiguration config = new ProjectLanguageConfiguration(); ICProjectDescription descriptor = getProjectDescription(project, false); - ICStorageElement rootElement = descriptor.getStorage(LANGUAGE_MAPPING_ID, false); - if (rootElement == null) { - return config; - } - - ICStorageElement[] mappingElements = rootElement.getChildrenByName(PROJECT_MAPPINGS); - if (mappingElements.length > 0) { - ICStorageElement element = mappingElements[0]; - config.setContentTypeMappings(decodeProjectContentTypeMappings(element)); - config.setFileMappings(decodeFileMappings(element)); + if (descriptor != null) { + ICStorageElement rootElement = descriptor.getStorage(LANGUAGE_MAPPING_ID, false); + if (rootElement != null) { + ICStorageElement[] mappingElements = rootElement.getChildrenByName(PROJECT_MAPPINGS); + if (mappingElements.length > 0) { + ICStorageElement element = mappingElements[0]; + config.setContentTypeMappings(decodeProjectContentTypeMappings(element)); + config.setFileMappings(decodeFileMappings(element)); + } + } } return config; } @@ -195,7 +188,7 @@ public class LanguageMappingStore { serializer.transform(source, result); String encodedMappings = buffer.getBuffer().toString(); - IEclipsePreferences node = new InstanceScope().getNode(CCorePlugin.PLUGIN_ID); + IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); node.put(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, encodedMappings); node.flush(); } catch (ParserConfigurationException e) { @@ -208,8 +201,8 @@ public class LanguageMappingStore { } public WorkspaceLanguageConfiguration decodeWorkspaceMappings() throws CoreException { - IEclipsePreferences node = new InstanceScope().getNode(CCorePlugin.PLUGIN_ID); - IEclipsePreferences defaultNode = new DefaultScope().getNode(CCorePlugin.PLUGIN_ID); + IEclipsePreferences node = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); + IEclipsePreferences defaultNode = DefaultScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID); String encodedMappings = defaultNode.get(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, null); if (encodedMappings == null) { encodedMappings = node.get(CCorePreferenceConstants.WORKSPACE_LANGUAGE_MAPPINGS, null); @@ -245,7 +238,8 @@ public class LanguageMappingStore { } } - private void addMappings(Map mappings, Element rootElement, String category, String keyName, String valueName) { + private void addMappings(Map mappings, Element rootElement, String category, + String keyName, String valueName) { Document document = rootElement.getOwnerDocument(); Iterator> entries = mappings.entrySet().iterator(); while (entries.hasNext()) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/doctools/ProjectMap.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/doctools/ProjectMap.java index eb46aa7d26e..9652b51f77b 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/doctools/ProjectMap.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/doctools/ProjectMap.java @@ -6,7 +6,8 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Andrew Ferguson (Symbian) - Initial implementation + * Andrew Ferguson (Symbian) - Initial implementation + * Sergey Prigogin (Google) *******************************************************************************/ package org.eclipse.cdt.internal.ui.text.doctools; @@ -72,13 +73,13 @@ class ProjectMap { */ public String getOwnerID(IResource resource) { String id= null; - if(resource!=null) { - for(IPath p= resource.getProjectRelativePath(); ; p= p.removeLastSegments(1)) { - if(fMap.containsKey(p)) { + if (resource != null) { + for (IPath p= resource.getProjectRelativePath(); ; p= p.removeLastSegments(1)) { + if (fMap.containsKey(p)) { id= fMap.get(p); break; } - if(p.isEmpty()) + if (p.isEmpty()) break; } } @@ -94,9 +95,9 @@ class ProjectMap { */ public void setCommentOwner(IResource resource, IDocCommentOwner owner) { Assert.isNotNull(resource); - if(ResourcesPlugin.getWorkspace().getRoot().equals(resource)) + if (ResourcesPlugin.getWorkspace().getRoot().equals(resource)) throw new IllegalStateException(); - if(owner!=null) { + if (owner != null) { fMap.put(resource.getProjectRelativePath(), owner.getID()); } else { fMap.remove(resource.getProjectRelativePath()); @@ -115,15 +116,17 @@ class ProjectMap { private static Map load(IProject project) throws CoreException { Map result= new HashMap(); ICProjectDescription pd= CCorePlugin.getDefault().getProjectDescription(project, false); - ICStorageElement e = pd.getStorage(ATTRVAL_STORAGEID, false); - if (e != null) { - for (ICStorageElement node : e.getChildrenByName(ELEMENT_DOC_COMMENT_OWNER)) { - String commentOwnerID = node.getAttribute(ATTRKEY_DCO_ID); - if(commentOwnerID != null) { - for (ICStorageElement path : node.getChildrenByName(ELEMENT_PATH)) { - String pathValue= path.getAttribute(ATTRKEY_PATH_VALUE); - if(pathValue != null) { - result.put(Path.fromPortableString(pathValue), commentOwnerID); + if (pd != null) { + ICStorageElement element = pd.getStorage(ATTRVAL_STORAGEID, false); + if (element != null) { + for (ICStorageElement node : element.getChildrenByName(ELEMENT_DOC_COMMENT_OWNER)) { + String commentOwnerID = node.getAttribute(ATTRKEY_DCO_ID); + if (commentOwnerID != null) { + for (ICStorageElement path : node.getChildrenByName(ELEMENT_PATH)) { + String pathValue= path.getAttribute(ATTRKEY_PATH_VALUE); + if(pathValue != null) { + result.put(Path.fromPortableString(pathValue), commentOwnerID); + } } } } @@ -144,11 +147,11 @@ class ProjectMap { data.removeChild(child); // invert and persist associations - for(Iterator i= fMap.values().iterator(); i.hasNext();) { + for (Iterator i= fMap.values().iterator(); i.hasNext();) { String cid= i.next(); ICStorageElement commentNode = data.createChild(ELEMENT_DOC_COMMENT_OWNER); commentNode.setAttribute(ATTRKEY_DCO_ID, cid); - for(Iterator j= fMap.keySet().iterator(); j.hasNext(); ) { + for (Iterator j= fMap.keySet().iterator(); j.hasNext(); ) { IPath path= j.next(); String ccid= fMap.get(path); if(cid.equals(ccid)) {