cdt/cdt_6_0_x/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/sourcelookup/AbsolutePathSourceContainer.java
author ryall
Fri, 07 Aug 2009 10:37:37 -0500
changeset 57 b9108837a1fc
parent 37 c2bce6dd59e7
permissions -rw-r--r--
Bug 9565, fix NPE.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     1
/*******************************************************************************
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     2
 * Copyright (c) 2006, 2008 Nokia and others.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     3
 * All rights reserved. This program and the accompanying materials
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     4
 * are made available under the terms of the Eclipse Public License v1.0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     5
 * which accompanies this distribution, and is available at
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     6
 * http://www.eclipse.org/legal/epl-v10.html
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     7
 *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     8
 * Contributors:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     9
 *     Nokia - Initial implementation (159833)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    10
 *     Broadcom - http://bugs.eclipse.org/247853
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    11
 *******************************************************************************/
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    12
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    13
package org.eclipse.cdt.debug.core.sourcelookup;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    14
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    15
import java.io.File;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    16
import java.io.IOException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    17
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    18
import org.eclipse.cdt.core.model.CoreModel;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    19
import org.eclipse.cdt.core.model.ICProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    20
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    21
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    22
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    23
import org.eclipse.cdt.internal.core.resources.ResourceLookup;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    24
import org.eclipse.core.resources.IFile;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    25
import org.eclipse.core.resources.IProject;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    26
import org.eclipse.core.resources.ResourcesPlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    27
import org.eclipse.core.runtime.CoreException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    28
import org.eclipse.core.runtime.IPath;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    29
import org.eclipse.core.runtime.Path;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    30
import org.eclipse.debug.core.ILaunchConfiguration;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    31
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    32
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    33
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    34
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    35
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    36
public class AbsolutePathSourceContainer extends AbstractSourceContainer {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    37
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    38
	 * Unique identifier for the absolute source container type
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    39
	 * (value <code>org.eclipse.cdt.debug.core.containerType.absolutePath</code>).
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    40
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    41
	public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.absolutePath";	 //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    42
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    43
	private Object[] findSourceElementByFile(File file) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    44
		IFile[] wfiles = ResourceLookup.findFilesForLocation(new Path(file.getAbsolutePath()));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    45
		if (wfiles.length > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    46
			ResourceLookup.sortFilesByRelevance(wfiles, getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    47
			return wfiles;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    48
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    49
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    50
		try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    51
			// Check the canonical path as well to support case insensitive file
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    52
			// systems like Windows.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    53
			wfiles = ResourceLookup.findFilesForLocation(new Path(file.getCanonicalPath()));
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    54
			if (wfiles.length > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    55
				ResourceLookup.sortFilesByRelevance(wfiles, getProject());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    56
				return wfiles;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    57
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    58
			
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    59
			// The file is not already in the workspace so try to create an external translation unit for it.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    60
			ISourceLookupDirector director = getDirector();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    61
			if (director != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    62
			{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    63
				ILaunchConfiguration launchConfiguration = director.getLaunchConfiguration();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    64
				if (launchConfiguration != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    65
				{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    66
					String projectName = launchConfiguration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    67
					if (projectName.length() > 0) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    68
						ICProject project = CoreModel.getDefault().getCModel().getCProject(projectName);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    69
						if (project != null)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    70
						{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    71
							IPath path = Path.fromOSString(file.getCanonicalPath());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    72
							String id = CoreModel.getRegistedContentTypeId(project.getProject(), path.lastSegment());
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    73
							return new ExternalTranslationUnit[] { new ExternalTranslationUnit(project, path, id) };
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    74
						}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    75
					}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    76
				}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    77
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    78
		} catch (IOException e) { // ignore if getCanonicalPath throws
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    79
		} catch (CoreException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    80
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    81
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    82
		// If we can't create an ETU then fall back on LocalFileStorage.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    83
		return new LocalFileStorage[] { new LocalFileStorage(file) };
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    84
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    85
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    86
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    87
	 * Find the project associated with the current launch configuration
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    88
	 * @return IProject or null
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    89
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    90
	private IProject getProject() {
57
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    91
		ISourceLookupDirector director = getDirector();
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    92
		if (director != null)
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    93
		{
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    94
			ILaunchConfiguration config = director.getLaunchConfiguration();
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    95
			if (config != null) {
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    96
				try {
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    97
					String name = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    98
					if (name.length() > 0)
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
    99
						return ResourcesPlugin.getWorkspace().getRoot().getProject(name);
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
   100
				} catch (CoreException e) {
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
   101
					// Don't care carry on search using other heuristics
b9108837a1fc Bug 9565, fix NPE.
ryall
parents: 37
diff changeset
   102
				}
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   103
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   104
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   105
		return null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   106
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   107
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   108
	public boolean isValidAbsoluteFilePath( String name )
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   109
	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   110
		return isValidAbsoluteFilePath( new File(name) );	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   111
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   112
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   113
	public boolean isValidAbsoluteFilePath( File file )
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   114
	{
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   115
		return file.isAbsolute() && file.exists() && file.isFile();	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   116
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   117
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   118
	public Object[] findSourceElements( String name ) throws CoreException {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   119
		if ( name != null ) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   120
			File file = new File( name );
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   121
			if ( isValidAbsoluteFilePath(file) ) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   122
				return findSourceElementByFile( file );
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   123
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   124
		}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   125
		return new Object[0];
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   126
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   127
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   128
	public String getName() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   129
		return SourceLookupMessages.getString( "AbsolutePathSourceContainer.0" ); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   130
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   131
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   132
	/* (non-Javadoc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   133
	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   134
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   135
	public ISourceContainerType getType() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   136
		return getSourceContainerType( TYPE_ID );
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   137
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   138
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   139
	@Override
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   140
	public int hashCode() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   141
		return TYPE_ID.hashCode();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   142
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   143
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   144
	@Override
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   145
	public boolean equals(Object obj) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   146
	    if (!(obj instanceof AbsolutePathSourceContainer))
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   147
		    return false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   148
	    return true;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   149
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   150
}