Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 898fc4fe1ef27e22ab57f3b5ae28dd8ab543bb44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**********************************************************************
 * Copyright (c) 2004 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 * QNX Software Systems - Initial API and implementation
 ***********************************************************************/ 
package org.eclipse.cdt.debug.core.sourcelookup; 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.internal.core.sourcelookup.MapEntrySourceContainer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer;
 
/**
 * The source container for path mappings.
 */
public class MappingSourceContainer extends AbstractSourceContainer {

	/**
	 * Unique identifier for the mapping source container type
	 * (value <code>org.eclipse.cdt.debug.core.containerType.mapping</code>).
	 */
	public static final String TYPE_ID = CDebugCorePlugin.getUniqueIdentifier() + ".containerType.mapping";	 //$NON-NLS-1$

	private String fName;
	private ArrayList fContainers;

	/** 
	 * Constructor for MappingSourceContainer. 
	 */
	public MappingSourceContainer( String name ) {
		fName = name;
		fContainers = new ArrayList();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getName()
	 */
	public String getName() {
		return fName;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.sourcelookup.ISourceContainer#getType()
	 */
	public ISourceContainerType getType() {
		return getSourceContainerType( TYPE_ID );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#isComposite()
	 */
	public boolean isComposite() {
		return !fContainers.isEmpty();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#findSourceElements(java.lang.String)
	 */
	public Object[] findSourceElements( String name ) throws CoreException {
		return findSourceElements( name, getSourceContainers() );
	}

	protected Object[] findSourceElements( String name, ISourceContainer[] containers ) throws CoreException {
		List results = null;
		CoreException single = null;
		MultiStatus multiStatus = null;
		if ( isFindDuplicates() ) {
			results = new ArrayList();
		}
		for( int i = 0; i < containers.length; i++ ) {
			ISourceContainer container = containers[i];
			try {
				Object[] objects = container.findSourceElements( name );
				if ( objects.length > 0 ) {
					if ( isFindDuplicates() ) {
						for( int j = 0; j < objects.length; j++ ) {
							results.add( objects[j] );
						}
					}
					else {
						if ( objects.length == 1 ) {
							return objects;
						}
						return new Object[]{ objects[0] };
					}
				}
			}
			catch( CoreException e ) {
				if ( single == null ) {
					single = e;
				}
				else if ( multiStatus == null ) {
					multiStatus = new MultiStatus( DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, new IStatus[]{ single.getStatus() }, SourceLookupMessages.getString( "MappingSourceContainer.0" ), null ); //$NON-NLS-1$
					multiStatus.add( e.getStatus() );
				}
				else {
					multiStatus.add( e.getStatus() );
				}
			}
		}
		if ( results == null ) {
			if ( multiStatus != null ) {
				throw new CoreException( multiStatus );
			}
			else if ( single != null ) {
				throw single;
			}
			return EMPTY;
		}
		return results.toArray();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainer#getSourceContainers()
	 */
	public ISourceContainer[] getSourceContainers() throws CoreException {
		return (MapEntrySourceContainer[])fContainers.toArray( new MapEntrySourceContainer[fContainers.size()] );
	}

	public void addMapEntry( MapEntrySourceContainer entry ) {
		fContainers.add( entry );
	}

	public void addMapEntries( MapEntrySourceContainer[] entries ) {
		fContainers.addAll( Arrays.asList( entries ) );
	}

	public void removeMapEntry( MapEntrySourceContainer entry ) {
		fContainers.remove( entry );
	}

	public void removeMapEntries( MapEntrySourceContainer[] entries ) {
		fContainers.removeAll( Arrays.asList( entries ) );
	}

	public void clear() {
		Iterator it = fContainers.iterator();
		while( it.hasNext() ) {
			((ISourceContainer)it.next()).dispose();
		}
		fContainers.clear();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainer#dispose()
	 */
	public void dispose() {
		super.dispose();
		Iterator it = fContainers.iterator();
		while( it.hasNext() ) {
			((ISourceContainer)it.next()).dispose();
		}
		fContainers.clear();
	}

	public MappingSourceContainer copy() {
		MappingSourceContainer copy = new MappingSourceContainer( fName );
		MapEntrySourceContainer[] entries = new MapEntrySourceContainer[fContainers.size()];
		for ( int i = 0; i < entries.length; ++i ) {
			copy.addMapEntry( ((MapEntrySourceContainer)fContainers.get( i )).copy() );
		}
		return copy;
	}
	
	public void setName( String name ) {
		fName = name;
	}	

	public IPath getCompilationPath( String sourceName ) {
		IPath path = new Path( sourceName );
		IPath result = null;
		try {
			ISourceContainer[] containers = getSourceContainers();
			for ( int i = 0; i < containers.length; ++i ) {
				MapEntrySourceContainer entry = (MapEntrySourceContainer)containers[i];
				IPath local = entry.getLocalPath();
				if ( local.isPrefixOf( path ) ) {
					result = entry.getBackendPath().append( path.removeFirstSegments( local.segmentCount() ) );
					break;
				}
			}
		}
		catch( CoreException e ) {
		}
		return result;
	}
}

Back to the top