Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 36cc7c2f7220737b22db6e86656a7f98074c7a9d (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
/*******************************************************************************
 * Copyright (c) 2008, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards.settingswizards;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.settings.model.ICFolderDescription;
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.w3c.dom.Element;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Base class implementing standard import and export functionality
 * for a section of the file.
 *
 * @author Mike Kucera
 * @since 5.1
 */
public abstract class SettingsProcessor implements ISettingsProcessor {

	protected static final String NONE = ""; //$NON-NLS-1$
	protected static final String CDATA = "CDATA"; //$NON-NLS-1$

	protected static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
	protected static final String LANGUAGE_ELEMENT = "language"; //$NON-NLS-1$

	/**
	 * Returns a constant from the ICSettingEntry interface.
	 */
	protected abstract int getSettingsType();

	protected abstract void writeSettings(ContentHandler content, ICLanguageSettingEntry setting)
			throws SettingsImportExportException;

	protected abstract void readSettings(ICLanguageSetting setting, Element language)
			throws SettingsImportExportException;

	/**
	 * Outputs a newline (\n) to the given ContentHandler.
	 */
	protected static void newline(ContentHandler handler) throws SAXException {
		handler.ignorableWhitespace("\n".toCharArray(), 0, 1); //$NON-NLS-1$
	}

	@Override
	public void writeSectionXML(ICFolderDescription projectRoot, ContentHandler content)
			throws SettingsImportExportException {
		ICLanguageSetting[] languages = projectRoot.getLanguageSettings();
		AttributesImpl attributes = new AttributesImpl();

		try {
			for (ICLanguageSetting language : languages) {
				//TODO for some reason language.getLanguageId() is returning null
				String languageName = language.getName();
				attributes.clear();
				attributes.addAttribute(NONE, NONE, NAME_ATTRIBUTE, CDATA, languageName);
				content.startElement(NONE, NONE, LANGUAGE_ELEMENT, attributes);
				newline(content);

				ICLanguageSettingEntry[] settings = language.getSettingEntries(getSettingsType());

				for (ICLanguageSettingEntry setting : settings) {
					if (!setting.isBuiltIn()) {
						writeSettings(content, setting);
					}
				}

				newline(content);
				content.endElement(NONE, NONE, LANGUAGE_ELEMENT);
				newline(content);
			}

		} catch (SAXException e) {
			throw new SettingsImportExportException(e);
		}
	}

	@Override
	public void readSectionXML(ICFolderDescription projectRoot, Element section) throws SettingsImportExportException {
		ICLanguageSetting[] languageSettings = projectRoot.getLanguageSettings();

		Map<String, ICLanguageSetting> languageMap = new HashMap<String, ICLanguageSetting>();
		for (ICLanguageSetting language : languageSettings) {
			languageMap.put(language.getName(), language);
		}

		List<Element> elements = XMLUtils.extractChildElements(section, LANGUAGE_ELEMENT); // throws SettingsImportExportException
		for (Element languageElement : elements) {
			String languageName = languageElement.getAttribute(NAME_ATTRIBUTE);
			ICLanguageSetting setting = languageMap.get(languageName);
			if (setting != null)
				readSettings(setting, languageElement);
		}
	}

}

Back to the top