Skip to content

Commit bdbc765

Browse files
karypidromani
authored andcommitted
Fix #107: Write format/cleanup profiles to XML files within project
1 parent 17435dd commit bdbc765

File tree

2 files changed

+163
-132
lines changed

2 files changed

+163
-132
lines changed

net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java

Lines changed: 39 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,33 @@
2020

2121
package net.sf.eclipsecs.core.transformer;
2222

23-
import java.util.ArrayList;
24-
import java.util.List;
23+
import java.io.InputStream;
2524
import java.util.Map;
2625

26+
import javax.xml.parsers.ParserConfigurationException;
27+
import javax.xml.transform.TransformerException;
28+
2729
import net.sf.eclipsecs.core.util.CheckstyleLog;
2830

31+
import org.eclipse.core.resources.IFile;
2932
import org.eclipse.core.resources.IProject;
3033
import org.eclipse.core.runtime.CoreException;
31-
import org.eclipse.core.runtime.preferences.IScopeContext;
32-
import org.eclipse.jdt.core.JavaCore;
33-
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
34-
import org.eclipse.jdt.internal.corext.fix.CleanUpPreferenceUtil;
35-
import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess;
36-
import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileManager;
37-
import org.eclipse.jdt.internal.ui.preferences.cleanup.CleanUpProfileVersioner;
38-
import org.eclipse.jdt.internal.ui.preferences.formatter.FormatterProfileManager;
39-
import org.eclipse.jdt.internal.ui.preferences.formatter.FormatterProfileStore;
40-
import org.eclipse.jdt.internal.ui.preferences.formatter.IProfileVersioner;
41-
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager;
42-
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.CustomProfile;
43-
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileManager.Profile;
44-
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileStore;
45-
import org.eclipse.jdt.internal.ui.preferences.formatter.ProfileVersioner;
46-
import org.osgi.service.prefs.BackingStoreException;
4734

4835
/**
49-
* Class for writing a new eclipse-configuration-file. Gets used by class Transformer. A new
50-
* eclipse-formatter-profile gets added.
36+
* Class for writing a new eclipse-configuration-file. Gets used by class Transformer. Two
37+
* eclipse-formatter-profile files gets written to the project root.
5138
*
39+
* @author Alexandros Karypidis
5240
* @author Lukas Frena
5341
* @author Lars Ködderitzsch
5442
*/
55-
@SuppressWarnings("restriction")
5643
public class FormatterConfigWriter {
5744

58-
private static final String JDT_UI_PLUGINID = "org.eclipse.jdt.ui";
59-
45+
/** Constant for show generated code. */
46+
private static final String CS_GENERATED = "CheckStyle-Generated ";
6047
/** A eclipse-configuration. */
6148
private final FormatterConfiguration mConfiguration;
6249

63-
/** Name of new createt profile. */
64-
private final String mNewProfileName;
65-
6650
private IProject mProject;
6751

6852
/**
@@ -77,135 +61,58 @@ public FormatterConfigWriter(IProject project, final FormatterConfiguration sett
7761
mConfiguration = settings;
7862
mProject = project;
7963

80-
mNewProfileName = "eclipse-cs " + mProject.getName();
8164
writeSettings();
8265
}
8366

8467
/**
85-
* Method for writing all settings to disc. Also activates new profile.
68+
* Method for persisting all settings to files.
8669
*/
8770
private void writeSettings() {
88-
// read the Eclipse-Preferences for manipulation
8971
writeCleanupSettings(mConfiguration.getCleanupSettings());
9072
writeFormatterSettings(mConfiguration.getFormatterSettings());
9173
}
9274

75+
/**
76+
* Method for writing all cleanup settings to disc.
77+
*
78+
* @param settings
79+
* All the settings.
80+
*/
9381
private void writeCleanupSettings(final Map<String, String> settings) {
94-
95-
PreferencesAccess access = PreferencesAccess.getOriginalPreferences();
96-
97-
IScopeContext instanceScope = access.getInstanceScope();
98-
IScopeContext scope = access.getProjectScope(mProject);
99-
100-
IProfileVersioner versioner = new CleanUpProfileVersioner();
101-
ProfileStore profilesStore = new ProfileStore(CleanUpConstants.CLEANUP_PROFILES, versioner);
82+
final IFile settingsFile = mProject.getFile(mProject.getName() + "-cs-cleanup.xml");
10283
try {
103-
104-
List<Profile> profiles = profilesStore.readProfiles(instanceScope);
105-
106-
if (profiles == null) {
107-
profiles = new ArrayList<>();
108-
}
109-
profiles.addAll(CleanUpPreferenceUtil.getBuiltInProfiles());
110-
111-
ProfileManager manager = new CleanUpProfileManager(profiles, scope, access, versioner);
112-
113-
CustomProfile myProfile = (CustomProfile) manager
114-
.getProfile(ProfileManager.ID_PREFIX + mNewProfileName);
115-
116-
if (myProfile == null) {
117-
// take current settings and create new profile
118-
Profile current = manager.getSelected();
119-
myProfile = new CustomProfile(mNewProfileName, current.getSettings(),
120-
versioner.getCurrentVersion(), versioner.getProfileKind());
121-
manager.addProfile(myProfile);
122-
}
123-
124-
Map<String, String> joinedSettings = myProfile.getSettings();
125-
joinedSettings.putAll(settings);
126-
127-
myProfile.setSettings(joinedSettings);
128-
manager.setSelected(myProfile);
129-
130-
// writes profiles to the workspace profile store
131-
profilesStore.writeProfiles(manager.getSortedProfiles(), instanceScope);
132-
133-
// commits changes to the project profile settings
134-
manager.commitChanges(scope);
135-
136-
scope.getNode(JDT_UI_PLUGINID).flush();
137-
scope.getNode(JavaCore.PLUGIN_ID).flush();
138-
if (scope != instanceScope) {
139-
instanceScope.getNode(JDT_UI_PLUGINID).flush();
140-
instanceScope.getNode(JavaCore.PLUGIN_ID).flush();
141-
}
142-
143-
} catch (CoreException e) {
144-
CheckstyleLog.log(e, "Error storing cleanup profile");
145-
} catch (BackingStoreException e) {
146-
CheckstyleLog.log(e, "Error storing cleanup profile");
84+
final InputStream stream = XmlProfileWriter.writeCleanupProfileToStream(
85+
CS_GENERATED + mProject.getName(), settings);
86+
createOrUpdateFile(settingsFile, stream);
87+
} catch (CoreException | TransformerException | ParserConfigurationException exc) {
88+
CheckstyleLog.log(exc, "Error saving cleanup profile");
14789
}
14890
}
14991

15092
/**
151-
* Method for writing all formatter-settings to disc.
93+
* Method for writing all formatter settings to disc.
15294
*
15395
* @param settings
15496
* All the settings.
15597
*/
15698
private void writeFormatterSettings(final Map<String, String> settings) {
157-
158-
PreferencesAccess access = PreferencesAccess.getOriginalPreferences();
159-
160-
IScopeContext instanceScope = access.getInstanceScope();
161-
IScopeContext scope = access.getProjectScope(mProject);
162-
163-
IProfileVersioner versioner = new ProfileVersioner();
164-
ProfileStore profilesStore = new FormatterProfileStore(versioner);
99+
final IFile settingsFile = mProject.getFile(mProject.getName() + "-cs-formatter.xml");
165100
try {
101+
final InputStream stream = XmlProfileWriter.writeFormatterProfileToStream(
102+
CS_GENERATED + mProject.getName(), settings);
103+
createOrUpdateFile(settingsFile, stream);
104+
} catch (CoreException | TransformerException | ParserConfigurationException exc) {
105+
CheckstyleLog.log(exc, "Error saving formatter profile");
106+
}
107+
}
166108

167-
List<Profile> profiles = profilesStore.readProfiles(instanceScope);
168-
169-
if (profiles == null) {
170-
profiles = new ArrayList<>();
171-
}
172-
173-
ProfileManager manager = new FormatterProfileManager(profiles, scope, access, versioner);
174-
175-
CustomProfile myProfile = (CustomProfile) manager
176-
.getProfile(ProfileManager.ID_PREFIX + mNewProfileName);
177-
178-
if (myProfile == null) {
179-
// take current settings and create new profile
180-
Profile current = manager.getSelected();
181-
myProfile = new CustomProfile(mNewProfileName, current.getSettings(),
182-
versioner.getCurrentVersion(), versioner.getProfileKind());
183-
manager.addProfile(myProfile);
184-
}
185-
186-
Map<String, String> joinedSettings = myProfile.getSettings();
187-
joinedSettings.putAll(settings);
188-
189-
myProfile.setSettings(joinedSettings);
190-
manager.setSelected(myProfile);
191-
192-
// writes profiles to the workspace profile store
193-
profilesStore.writeProfiles(manager.getSortedProfiles(), instanceScope);
194-
195-
// commits changes to the project profile settings
196-
manager.commitChanges(scope);
197-
198-
scope.getNode(JDT_UI_PLUGINID).flush();
199-
scope.getNode(JavaCore.PLUGIN_ID).flush();
200-
if (scope != instanceScope) {
201-
instanceScope.getNode(JDT_UI_PLUGINID).flush();
202-
instanceScope.getNode(JavaCore.PLUGIN_ID).flush();
203-
}
204-
205-
} catch (CoreException e) {
206-
CheckstyleLog.log(e, "Error storing formatter profile");
207-
} catch (BackingStoreException e) {
208-
CheckstyleLog.log(e, "Error storing formatter profile");
109+
private static void createOrUpdateFile(IFile settingsFile, InputStream stream)
110+
throws CoreException {
111+
if (settingsFile.exists()) {
112+
settingsFile.setContents(stream, true, false, null);
113+
} else {
114+
settingsFile.create(stream, true, null);
209115
}
210116
}
117+
211118
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
//============================================================================
2+
//
3+
// Copyright (C) 2002-2016 David Schneider, Lars Ködderitzsch
4+
//
5+
// This library is free software; you can redistribute it and/or
6+
// modify it under the terms of the GNU Lesser General Public
7+
// License as published by the Free Software Foundation; either
8+
// version 2.1 of the License, or (at your option) any later version.
9+
//
10+
// This library is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
// Lesser General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Lesser General Public
16+
// License along with this library; if not, write to the Free Software
17+
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
//
19+
//============================================================================
20+
21+
package net.sf.eclipsecs.core.transformer;
22+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.InputStream;
25+
import java.io.StringWriter;
26+
import java.nio.charset.StandardCharsets;
27+
import java.util.Iterator;
28+
import java.util.Map;
29+
30+
import javax.xml.parsers.DocumentBuilder;
31+
import javax.xml.parsers.DocumentBuilderFactory;
32+
import javax.xml.parsers.ParserConfigurationException;
33+
import javax.xml.transform.OutputKeys;
34+
import javax.xml.transform.Transformer;
35+
import javax.xml.transform.TransformerException;
36+
import javax.xml.transform.TransformerFactory;
37+
import javax.xml.transform.dom.DOMSource;
38+
import javax.xml.transform.stream.StreamResult;
39+
40+
import net.sf.eclipsecs.core.util.CheckstyleLog;
41+
42+
import org.w3c.dom.Document;
43+
import org.w3c.dom.Element;
44+
45+
/**
46+
* Utility class to write eclipse formatter/cleanup profile XML files.
47+
*
48+
* @author Alexandros Karypidis
49+
*
50+
*/
51+
public final class XmlProfileWriter {
52+
private static final String XML_NODE_ROOT = "profiles";
53+
private static final String XML_NODE_PROFILE = "profile";
54+
private static final String XML_NODE_SETTING = "setting";
55+
56+
private static final String XML_ATTRIBUTE_VERSION = "version";
57+
private static final String XML_ATTRIBUTE_ID = "id";
58+
private static final String XML_ATTRIBUTE_NAME = "name";
59+
private static final String XML_ATTRIBUTE_PROFILE_KIND = "kind";
60+
private static final String XML_ATTRIBUTE_VALUE = "value";
61+
62+
private static final String CLEANUP_PROFILE_VERSION = "2";
63+
private static final String CLEANUP_PROFILE_KIND = "CleanUpProfile";
64+
65+
private static final String FORMATTER_PROFILE_VERSION = "10";
66+
private static final String FORMATTER_PROFILE_KIND = "CodeFormatterProfile";
67+
68+
private XmlProfileWriter() {
69+
// no code
70+
}
71+
72+
public static InputStream writeCleanupProfileToStream(String name,
73+
Map<String, String> settings) throws TransformerException, ParserConfigurationException {
74+
return writeProfileToStream(name, CLEANUP_PROFILE_VERSION, CLEANUP_PROFILE_KIND, settings);
75+
}
76+
77+
public static InputStream writeFormatterProfileToStream(String name,
78+
Map<String, String> settings) throws TransformerException, ParserConfigurationException {
79+
return writeProfileToStream(name, FORMATTER_PROFILE_VERSION, FORMATTER_PROFILE_KIND, settings);
80+
}
81+
82+
private static InputStream writeProfileToStream(String name, String profileVersion,
83+
String profileKind, Map<String, String> settings) throws TransformerException,
84+
ParserConfigurationException {
85+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
86+
final DocumentBuilder builder = factory.newDocumentBuilder();
87+
final Document document = builder.newDocument();
88+
89+
final Element rootElement = document.createElement(XML_NODE_ROOT);
90+
rootElement.setAttribute(XML_ATTRIBUTE_VERSION, profileVersion);
91+
92+
document.appendChild(rootElement);
93+
94+
final Element profileElement = document.createElement(XML_NODE_PROFILE);
95+
profileElement.setAttribute(XML_ATTRIBUTE_NAME, name);
96+
profileElement.setAttribute(XML_ATTRIBUTE_VERSION, profileVersion);
97+
profileElement.setAttribute(XML_ATTRIBUTE_PROFILE_KIND, profileKind);
98+
99+
final Iterator<String> keyIter = settings.keySet().iterator();
100+
101+
while (keyIter.hasNext()) {
102+
final String key = keyIter.next();
103+
final String value = settings.get(key);
104+
if (value != null) {
105+
final Element setting = document.createElement(XML_NODE_SETTING);
106+
setting.setAttribute(XML_ATTRIBUTE_ID, key);
107+
setting.setAttribute(XML_ATTRIBUTE_VALUE, value);
108+
profileElement.appendChild(setting);
109+
} else {
110+
CheckstyleLog.log(null, String.format("Profile is missing value for [key=%s]", key));
111+
}
112+
}
113+
rootElement.appendChild(profileElement);
114+
115+
final Transformer transformer = TransformerFactory.newInstance().newTransformer();
116+
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
117+
transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
118+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
119+
final StringWriter writer = new StringWriter();
120+
transformer.transform(new DOMSource(document), new StreamResult(writer));
121+
return new ByteArrayInputStream(writer.toString().getBytes());
122+
}
123+
124+
}

0 commit comments

Comments
 (0)