Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 90ce5f2bbeb4fe12aa53ba83c46f28eec9430187 (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
/*******************************************************************************
 * Copyright (c) 2003, 2005 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
 *******************************************************************************/
package org.eclipse.debug.internal.ui.sourcelookup;

import java.io.IOException;
import java.io.StringReader;

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

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.AbstractSourceContainerTypeDelegate;
import org.eclipse.debug.ui.sourcelookup.WorkingSetSourceContainer;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PlatformUI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * The factory for creating/restoring working set source containers.
 *
 * @since 3.0
 */
public class WorkingSetSourceContainerType extends AbstractSourceContainerTypeDelegate {
	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainerTypeDelegate#getMemento(org.eclipse.debug.internal.core.sourcelookup.ISourceContainer)
	 */
	@Override
	public String getMemento(ISourceContainer container) throws CoreException {
		WorkingSetSourceContainer workingSet = (WorkingSetSourceContainer) container;
		Document doc = newDocument();
		Element node = doc.createElement("workingSet"); //$NON-NLS-1$
		node.setAttribute("name", workingSet.getName()); //$NON-NLS-1$
		doc.appendChild(node);
		return serializeDocument(doc);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.internal.core.sourcelookup.ISourceContainerType#createSourceContainer(java.lang.String)
	 */
	@Override
	public ISourceContainer createSourceContainer(String memento)throws CoreException {
		IWorkingSet workingSet = null;
		Exception ex = null;
		try {
			Element root = null;
			DocumentBuilder parser =
				DocumentBuilderFactory.newInstance().newDocumentBuilder();
			parser.setErrorHandler(new DefaultHandler());
			StringReader reader = new StringReader(memento);
			InputSource source = new InputSource(reader);
			root = parser.parse(source).getDocumentElement();

			String name = root.getAttribute("name");//$NON-NLS-1$

			if (isEmpty(name)) {
				abort(SourceLookupUIMessages.sourceSearch_initError,null);
			}
			workingSet = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name);
			//check that set still exists
			if (workingSet == null) {
				abort(SourceLookupUIMessages.sourceSearch_initError, null);
			}
			return new WorkingSetSourceContainer(workingSet);

		} catch (ParserConfigurationException e) {
			ex = e;
		} catch (SAXException e) {
			ex = e;
		} catch (IOException e) {
			ex = e;
		}

		abort(SourceLookupUIMessages.sourceSearch_initError, ex);
		return null;
	}

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


}

Back to the top