Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5e7b938d3f6e8d233ceb88a4e76b151b75bb8ae8 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 *(c) Copyright QNX Software Systems Ltd. 2002.
 * All Rights Reserved.
 * 
 */
package org.eclipse.cdt.launch.sourcelookup;

import java.io.IOException;
import java.io.StringReader;
import java.text.MessageFormat;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.xerces.dom.DocumentImpl;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.sourcelookup.ICSourceLocator;
import org.eclipse.cdt.debug.core.sourcelookup.ISourceMode;
import org.eclipse.cdt.debug.internal.core.CDebugUtils;
import org.eclipse.cdt.debug.ui.sourcelookup.CUISourceLocator;
import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.IPersistableSourceLocator;
import org.eclipse.debug.core.model.IStackFrame;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * The wrapper for the CUISourceLocator class.
 * 
 * @since: Dec 11, 2002
 */
public class DefaultSourceLocator implements IPersistableSourceLocator, IAdaptable
{
	private static final String ELEMENT_NAME = "PromptingSourceLocator";
	private static final String ATTR_PROJECT = "project";
	private static final String ATTR_MEMENTO = "memento";

	/**
	 * Identifier for the 'Default C/C++ Source Locator' extension
	 * (value <code>"org.eclipse.cdt.launch.DefaultSourceLocator"</code>).
	 */
	public static final String ID_DEFAULT_SOURCE_LOCATOR = LaunchUIPlugin.getUniqueIdentifier() + ".DefaultSourceLocator"; //$NON-NLS-1$

	private CUISourceLocator fSourceLocator = null;
	private final static int ERROR = 1000;   // ????

	/**
	 * Constructor for DefaultSourceLocator.
	 */
	public DefaultSourceLocator()
	{
	}

	/**
	 * Constructor for DefaultSourceLocator.
	 */
	public DefaultSourceLocator( CUISourceLocator locator )
	{
		fSourceLocator = locator;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#getMemento()
	 */
	public String getMemento() throws CoreException
	{
		if ( fSourceLocator != null )
		{
			Document doc = new DocumentImpl();
			Element node = doc.createElement( ELEMENT_NAME );
			doc.appendChild( node );
			node.setAttribute( ATTR_PROJECT, fSourceLocator.getProject().getName() );

			IPersistableSourceLocator psl = getPersistableSourceLocator();
			if ( psl != null )
			{
				node.setAttribute( ATTR_MEMENTO, psl.getMemento() );
			}
			try
			{
				return CDebugUtils.serializeDocument( doc, " " );
			}
			catch( IOException e )
			{
				abort( "Unable to create memento for C/C++ source locator.", e );
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeFromMemento(String)
	 */
	public void initializeFromMemento( String memento ) throws CoreException
	{
		Exception ex = null;
		try
		{
			Element root = null;
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			StringReader reader = new StringReader( memento );
			InputSource source = new InputSource( reader );
			root = parser.parse( source ).getDocumentElement();

			if ( !root.getNodeName().equalsIgnoreCase( ELEMENT_NAME ) )
			{
				abort( "Unable to restore prompting source locator - invalid format.", null );
			}

			String projectName = root.getAttribute( ATTR_PROJECT );
			String data = root.getAttribute( ATTR_MEMENTO );
			if ( isEmpty( projectName ) )
			{
				abort( "Unable to restore prompting source locator - invalid format.", null );
			}
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( projectName );
			if ( project != null )
			{
				fSourceLocator = new CUISourceLocator( project );
			}
			else
			{
				abort( MessageFormat.format( "Unable to restore prompting source locator - project {0} not found.", new String[] { projectName } ), null );
			}
			
			IPersistableSourceLocator psl = getPersistableSourceLocator();
			if ( psl != null )
			{
				psl.initializeFromMemento( data );
			}
			else
			{
				abort( "Unable to restore C/C++ source locator - invalid format.", null );
			}
			return;
		}
		catch( ParserConfigurationException e )
		{
			ex = e;
		}
		catch( SAXException e )
		{
			ex = e;
		}
		catch( IOException e )
		{
			ex = e;
		}
		abort( "Exception occurred initializing source locator.", ex );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IPersistableSourceLocator#initializeDefaults(ILaunchConfiguration)
	 */
	public void initializeDefaults( ILaunchConfiguration configuration ) throws CoreException
	{
		fSourceLocator = new CUISourceLocator( getProject( configuration ) );
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame)
	 */
	public Object getSourceElement( IStackFrame stackFrame )
	{
		return ( fSourceLocator != null ) ? fSourceLocator.getSourceElement( stackFrame ) : null;
	}
	
	private IProject getProject( ILaunchConfiguration configuration ) throws CoreException
	{
		String projectName = configuration.getAttribute( ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null );
		if ( !isEmpty( projectName ) )
		{
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject( projectName );
			if ( project.exists() )
			{
				return project;
			}
		}
		abort( MessageFormat.format( "Project \"{0}\" does not exist.", new String[] { projectName } ), null );
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(Class)
	 */
	public Object getAdapter( Class adapter )
	{
		if ( fSourceLocator != null )
		{
			if ( adapter.equals( ICSourceLocator.class ) )
			{
				return fSourceLocator.getAdapter( adapter );
			}
			if ( adapter.equals( IResourceChangeListener.class ) )
			{
				return fSourceLocator.getAdapter( adapter );
			}
			if ( adapter.equals( ISourceMode.class ) )
			{
				return fSourceLocator.getAdapter( adapter );
			}
		}
		return null;
	}	

	private ICSourceLocator getCSourceLocator()
	{
		if ( fSourceLocator != null )
		{
			return (ICSourceLocator)fSourceLocator.getAdapter( ICSourceLocator.class );
		}
		return null;
	}
	
	private IPersistableSourceLocator getPersistableSourceLocator()
	{
		ICSourceLocator sl = getCSourceLocator();
		return ( sl instanceof IPersistableSourceLocator ) ? (IPersistableSourceLocator)sl : null;
	}

	/**
	 * Throws an internal error exception
	 */
	private void abort( String message, Throwable e ) throws CoreException
	{
		IStatus s = new Status( IStatus.ERROR,
								LaunchUIPlugin.getUniqueIdentifier(),
								ERROR,
								message,
								e );
		throw new CoreException( s );
	}


	private boolean isEmpty( String string )
	{
		return string == null || string.length() == 0;
	}
}

Back to the top