Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5dd69f82ab8d365bc68f76bbb5f6dbcd07c7f9df (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
import org.eclipse.cdt.core.settings.model.ICSettingEntry;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 * A settings processor that imports and exports symbols.
 *
 * @author Mike Kucera
 * @since 5.1
 *
 */
public class MacroSettingsProcessor extends SettingsProcessor {

	public static final String SECTION_NAME = "org.eclipse.cdt.internal.ui.wizards.settingswizards.Macros"; //$NON-NLS-1$

	private static final String MACRO_ELEMENT = "macro"; //$NON-NLS-1$
	private static final String NAME_ELEMENT = "name"; //$NON-NLS-1$
	private static final String VALUE_ELEMENT = "value"; //$NON-NLS-1$

	@Override
	public Image getIcon() {
		return CUIPlugin.getImageDescriptorRegistry()
				.get(CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_MACRO));
	}

	@Override
	public String getDisplayName() {
		return Messages.ProjectSettingsWizardPage_Processor_Macros;
	}

	@Override
	public String getSectionName() {
		return SECTION_NAME;
	}

	@Override
	protected int getSettingsType() {
		return ICSettingEntry.MACRO;
	}

	@Override
	protected void writeSettings(ContentHandler content, ICLanguageSettingEntry setting)
			throws SettingsImportExportException {
		char[] name = setting.getName().toCharArray();
		char[] value = setting.getValue().toCharArray();

		try {
			content.startElement(NONE, NONE, MACRO_ELEMENT, null);
			newline(content);

			content.startElement(NONE, NONE, NAME_ELEMENT, null);
			content.characters(name, 0, name.length);
			content.endElement(NONE, NONE, NAME_ELEMENT);

			content.startElement(NONE, NONE, VALUE_ELEMENT, null);
			content.characters(value, 0, value.length);
			content.endElement(NONE, NONE, VALUE_ELEMENT);
			newline(content);

			content.endElement(NONE, NONE, MACRO_ELEMENT);
			newline(content);

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

	@Override
	protected void readSettings(ICLanguageSetting setting, Element language) throws SettingsImportExportException {
		List<ICLanguageSettingEntry> macros = new ArrayList<>();

		List<Element> macrosNodes = XMLUtils.extractChildElements(language, MACRO_ELEMENT);

		for (Element macroElement : macrosNodes) {
			String name = null;
			String value = null;

			NodeList nodeList = macroElement.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				switch (node.getNodeType()) {
				case Node.TEXT_NODE:
					Text text = (Text) node;
					if (XMLUtils.isWhitespace(text.getData()))
						break;
					throw new SettingsImportExportException("Unknown text: '" + text.getData() + "'"); //$NON-NLS-1$ //$NON-NLS-2$
				case Node.ELEMENT_NODE:
					Element element = (Element) node;
					String tagName = element.getTagName();
					if (name == null && tagName.equals(NAME_ELEMENT))
						name = element.getTextContent();
					else if (value == null && tagName.equals(VALUE_ELEMENT))
						value = element.getTextContent();
					else
						throw new SettingsImportExportException("Unknown or extra tag: " + tagName); //$NON-NLS-1$
					break;
				default:
					throw new SettingsImportExportException("Unknown node: " + node.getNodeName()); //$NON-NLS-1$
				}
			}

			if (name == null)
				throw new SettingsImportExportException("There must be one <name> element"); //$NON-NLS-1$
			if (value == null)
				throw new SettingsImportExportException("There must be one <value> element"); //$NON-NLS-1$

			macros.add(CDataUtil.createCMacroEntry(name, value, 0));
		}

		if (macros.isEmpty())
			return;

		// need to do this or existing settings will disappear
		macros.addAll(setting.getSettingEntriesList(ICSettingEntry.MACRO));
		setting.setSettingEntries(ICSettingEntry.MACRO, macros);
	}

}

Back to the top