1
/*******************************************************************************
2
* Copyright (c) 2008 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.ui.wizards.settingswizards;
13
import java.util.HashMap;
14
import java.util.List;
17
import org.w3c.dom.Element;
18
import org.xml.sax.ContentHandler;
19
import org.xml.sax.SAXException;
20
import org.xml.sax.helpers.AttributesImpl;
22
import org.eclipse.cdt.core.settings.model.ICFolderDescription;
23
import org.eclipse.cdt.core.settings.model.ICLanguageSetting;
24
import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry;
27
* Base class implementing standard import and export functionality
28
* for a section of the file.
33
public abstract class SettingsProcessor implements ISettingsProcessor {
35
protected static final String NONE = ""; //$NON-NLS-1$
36
protected static final String CDATA = "CDATA"; //$NON-NLS-1$
38
protected static final String NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
39
protected static final String LANGUAGE_ELEMENT = "language"; //$NON-NLS-1$
43
* Returns a constant from the ICSettingEntry interface.
45
protected abstract int getSettingsType();
48
protected abstract void writeSettings(ContentHandler content, ICLanguageSettingEntry setting)
49
throws SettingsImportExportException;
52
protected abstract void readSettings(ICLanguageSetting setting, Element language)
53
throws SettingsImportExportException;
57
* Outputs a newline (\n) to the given ContentHandler.
59
protected static void newline(ContentHandler handler) throws SAXException {
60
handler.ignorableWhitespace("\n".toCharArray(), 0, 1); //$NON-NLS-1$
65
public void writeSectionXML(ICFolderDescription projectRoot, ContentHandler content) throws SettingsImportExportException {
66
ICLanguageSetting[] languages = projectRoot.getLanguageSettings();
67
AttributesImpl attributes = new AttributesImpl();
70
for(ICLanguageSetting language : languages) {
71
//TODO for some reason language.getLanguageId() is returning null
72
String languageName = language.getName();
74
attributes.addAttribute(NONE, NONE, NAME_ATTRIBUTE, CDATA, languageName);
75
content.startElement(NONE, NONE, LANGUAGE_ELEMENT, attributes);
78
ICLanguageSettingEntry[] settings = language.getSettingEntries(getSettingsType());
80
for(ICLanguageSettingEntry setting : settings) {
81
if(!setting.isBuiltIn()) {
82
writeSettings(content, setting);
87
content.endElement(NONE, NONE, LANGUAGE_ELEMENT);
91
} catch(SAXException e) {
92
throw new SettingsImportExportException(e);
98
public void readSectionXML(ICFolderDescription projectRoot, Element section) throws SettingsImportExportException {
99
ICLanguageSetting[] languageSettings = projectRoot.getLanguageSettings();
101
Map<String,ICLanguageSetting> languageMap = new HashMap<String,ICLanguageSetting>();
102
for(ICLanguageSetting language : languageSettings) {
103
languageMap.put(language.getName(), language);
106
List<Element> elements = XMLUtils.extractChildElements(section, LANGUAGE_ELEMENT); // throws SettingsImportExportException
107
for(Element languageElement : elements) {
108
String languageName = languageElement.getAttribute(NAME_ATTRIBUTE);
109
ICLanguageSetting setting = languageMap.get(languageName);
111
readSettings(setting, languageElement);