Skip to content

Commit 045d103

Browse files
committed
Progress with Eclipse Stylesheet genertator
1 parent 21eb237 commit 045d103

File tree

9 files changed

+723
-22
lines changed

9 files changed

+723
-22
lines changed

java/pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,12 @@
135135
<version>${project.version}</version>
136136
<scope>test</scope>
137137
</dependency>
138+
139+
<dependency>
140+
<groupId>io.github.java-diff-utils</groupId>
141+
<artifactId>java-diff-utils</artifactId>
142+
<version>4.9</version>
143+
<scope>test</scope>
144+
</dependency>
138145
</dependencies>
139146
</project>

java/src/main/java/eu/solven/cleanthat/language/java/eclipse/EclipseJavaFormatter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ public String doFormat(String code, LineEnding ending) throws IOException {
5252
// Make a new formatter to enable thread-safety
5353
CodeFormatter formatter = makeFormatter();
5454

55-
TextEdit te;
55+
TextEdit textEdit;
5656
try {
57-
te = formatter.format(CodeFormatter.K_COMPILATION_UNIT
57+
textEdit = formatter.format(CodeFormatter.K_COMPILATION_UNIT
5858
| CodeFormatter.F_INCLUDE_COMMENTS, code, 0, code.length(), 0, ending.getChars());
59-
if (te == null) {
59+
if (textEdit == null) {
6060
LOGGER.warn("Code cannot be formatted. Possible cause is unmatched source/target/compliance version.");
6161
return null;
6262
}
6363
} catch (RuntimeException e) {
64-
LOGGER.warn("Code cannot be formatted for text -->" + code + "<--", e);
64+
LOGGER.warn("Code cannot be formatted", e);
6565
return null;
6666
}
6767
IDocument doc = new Document(code);
6868
try {
69-
te.apply(doc);
69+
textEdit.apply(doc);
7070
} catch (MalformedTreeException | BadLocationException e) {
7171
throw new IllegalArgumentException(e);
7272
}

java/src/main/java/eu/solven/cleanthat/language/java/eclipse/EclipseJavaFormatterConfiguration.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URL;
2121
import java.util.LinkedHashMap;
2222
import java.util.Map;
23+
import java.util.TreeMap;
2324

2425
import org.eclipse.jdt.core.JavaCore;
2526
import org.slf4j.Logger;
@@ -51,14 +52,13 @@ public class EclipseJavaFormatterConfiguration {
5152
private final Map<String, String> options;
5253

5354
public EclipseJavaFormatterConfiguration(Map<String, String> options) {
54-
this.options = ImmutableMap.copyOf(options);
55+
// Sorted for human-friendliness
56+
this.options = ImmutableMap.copyOf(new TreeMap<>(options));
5557
}
5658

5759
public static EclipseJavaFormatterConfiguration load(ICodeProvider codeProvider,
5860
ILanguageProperties languageProperties,
5961
EclipseJavaFormatterProcessorProperties processorConfig) {
60-
Map<String, String> options = new LinkedHashMap<>();
61-
6262
String javaConfigFile = processorConfig.getUrl();
6363

6464
// Eclipse default
@@ -71,27 +71,37 @@ public static EclipseJavaFormatterConfiguration load(ICodeProvider codeProvider,
7171
// LOGGER.warn("No value for {}. Defaulted to: {}", KEY_JDK_VERSION, DEFAULT_JDK_VERSION);
7272
// }
7373
// String jdkVersion = optJdkVersion.orElse();
74+
Map<String, String> options = new LinkedHashMap<>();
7475
options.put(JavaCore.COMPILER_SOURCE, jdkVersion);
7576
options.put(JavaCore.COMPILER_COMPLIANCE, jdkVersion);
7677
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, jdkVersion);
78+
79+
return new EclipseJavaFormatterConfiguration(options);
7780
} else {
7881
LOGGER.info("Loading Eclipse java formatting configuration from {}", javaConfigFile);
7982

8083
Resource resource = CleanthatUrlLoader.loadUrl(codeProvider, javaConfigFile);
8184

82-
try (InputStream is = resource.getInputStream()) {
83-
try {
84-
options = new ConfigReader().read(is);
85-
} catch (SAXException | ConfigReadException e) {
86-
throw new RuntimeException("Issue parsing config: " + javaConfigFile, e);
87-
}
88-
} catch (MalformedURLException e) {
89-
throw new IllegalArgumentException("Invalid java.config_uri: + javaConfigFile", e);
90-
} catch (IOException e) {
91-
throw new UncheckedIOException(e);
85+
try {
86+
return loadResource(resource);
87+
} catch (RuntimeException e) {
88+
throw new RuntimeException("Issue processing: " + javaConfigFile, e);
9289
}
9390
}
91+
}
9492

95-
return new EclipseJavaFormatterConfiguration(options);
93+
public static EclipseJavaFormatterConfiguration loadResource(Resource resource) {
94+
try (InputStream is = resource.getInputStream()) {
95+
try {
96+
Map<String, String> options = new ConfigReader().read(is);
97+
return new EclipseJavaFormatterConfiguration(options);
98+
} catch (SAXException | ConfigReadException e) {
99+
throw new RuntimeException("Issue parsing config", e);
100+
}
101+
} catch (MalformedURLException e) {
102+
throw new IllegalArgumentException("Invalid java.config_uri: + javaConfigFile", e);
103+
} catch (IOException e) {
104+
throw new UncheckedIOException(e);
105+
}
96106
}
97107
}

java/src/main/java/eu/solven/cleanthat/language/java/eclipse/GenerateEclipseStylesheet.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
package eu.solven.cleanthat.language.java.eclipse;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.Map;
9+
10+
import javax.xml.parsers.ParserConfigurationException;
11+
import javax.xml.transform.TransformerException;
12+
13+
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
314
import org.slf4j.Logger;
415
import org.slf4j.LoggerFactory;
516

17+
import com.google.common.io.ByteStreams;
18+
19+
import eu.solven.cleanthat.language.java.eclipse.checkstyle.XmlProfileWriter;
20+
621
/**
722
* This helps generating a proper Eclipse Stylesheet, based on the existing codebase: it will generate a stylesheet
823
* minifying impacts over the codebase (supposing the codebase is well formatted)
@@ -19,7 +34,27 @@ protected GenerateEclipseStylesheet() {
1934
// hidden
2035
}
2136

22-
public static void main(String[] args) {
23-
LOGGER.info("TODO");
37+
public Path writeInTmp() throws IOException {
38+
DefaultCodeFormatterOptions defaultSettings = DefaultCodeFormatterOptions.getDefaultSettings();
39+
40+
Map<String, String> defaultSettingsAsMap = defaultSettings.getMap();
41+
42+
Path tmpFile = writeConfigurationToTmpPath(defaultSettingsAsMap);
43+
44+
return tmpFile;
45+
}
46+
47+
public Path writeConfigurationToTmpPath(Map<String, String> defaultSettingsAsMap) throws IOException {
48+
Path tmpFile = Files.createTempFile("cleanthat-eclipse-formatter-", ".xml");
49+
LOGGER.info("About to write Eclipse formatter configuration in {}", tmpFile);
50+
51+
try (InputStream is = XmlProfileWriter.writeFormatterProfileToStream("cleanthat", defaultSettingsAsMap);
52+
OutputStream outputStream = Files.newOutputStream(tmpFile)) {
53+
ByteStreams.copy(is, outputStream);
54+
} catch (TransformerException | ParserConfigurationException e) {
55+
throw new IllegalArgumentException(e);
56+
}
57+
LOGGER.info("Done writing Eclipse formatter configuration in {}", tmpFile);
58+
return tmpFile;
2459
}
2560
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//============================================================================
2+
//
3+
// Copyright (C) 2009 Lukas Frena
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 eu.solven.cleanthat.language.java.eclipse.checkstyle;
22+
23+
import java.io.InputStream;
24+
import java.util.Map;
25+
26+
import javax.xml.parsers.ParserConfigurationException;
27+
import javax.xml.transform.TransformerException;
28+
29+
import org.eclipse.core.resources.IFile;
30+
import org.eclipse.core.resources.IProject;
31+
import org.eclipse.core.runtime.CoreException;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
34+
35+
/**
36+
* Class for writing a new eclipse-configuration-file. Gets used by class Transformer. Two eclipse-formatter-profile
37+
* files gets written to the project root.
38+
*
39+
* @author Alexandros Karypidis
40+
* @author Lukas Frena
41+
* @author Lars Ködderitzsch
42+
*/
43+
// https://github.com/checkstyle/eclipse-cs/blob/master/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java
44+
public class FormatterConfigWriter {
45+
private static final Logger LOGGER = LoggerFactory.getLogger(FormatterConfigWriter.class);
46+
47+
/** Constant for show generated code. */
48+
private static final String CS_GENERATED = "CheckStyle-Generated ";
49+
/** A eclipse-configuration. */
50+
private final FormatterConfiguration mConfiguration;
51+
52+
private IProject mProject;
53+
54+
/**
55+
* Constructor to create a new instance of class FormatterConfigWriter.
56+
*
57+
* @param project
58+
* the project whose formatter settings should be written
59+
* @param settings
60+
* A eclipse-configuration.
61+
*/
62+
public FormatterConfigWriter(IProject project, final FormatterConfiguration settings) {
63+
mConfiguration = settings;
64+
mProject = project;
65+
66+
writeSettings();
67+
}
68+
69+
/**
70+
* Method for persisting all settings to files.
71+
*/
72+
private void writeSettings() {
73+
writeCleanupSettings(mConfiguration.getCleanupSettings());
74+
writeFormatterSettings(mConfiguration.getFormatterSettings());
75+
}
76+
77+
/**
78+
* Method for writing all cleanup settings to disc.
79+
*
80+
* @param settings
81+
* All the settings.
82+
*/
83+
private void writeCleanupSettings(final Map<String, String> settings) {
84+
final IFile settingsFile = mProject.getFile(mProject.getName() + "-cs-cleanup.xml");
85+
try {
86+
final InputStream stream =
87+
XmlProfileWriter.writeCleanupProfileToStream(CS_GENERATED + mProject.getName(), settings);
88+
createOrUpdateFile(settingsFile, stream);
89+
} catch (CoreException | TransformerException | ParserConfigurationException exc) {
90+
LOGGER.warn("Error saving cleanup profile", exc);
91+
}
92+
}
93+
94+
/**
95+
* Method for writing all formatter settings to disc.
96+
*
97+
* @param settings
98+
* All the settings.
99+
*/
100+
private void writeFormatterSettings(final Map<String, String> settings) {
101+
final IFile settingsFile = mProject.getFile(mProject.getName() + "-cs-formatter.xml");
102+
try {
103+
final InputStream stream =
104+
XmlProfileWriter.writeFormatterProfileToStream(CS_GENERATED + mProject.getName(), settings);
105+
createOrUpdateFile(settingsFile, stream);
106+
} catch (CoreException | TransformerException | ParserConfigurationException exc) {
107+
LOGGER.warn("Error saving formatter profile", exc);
108+
}
109+
}
110+
111+
private static void createOrUpdateFile(IFile settingsFile, InputStream stream) throws CoreException {
112+
if (settingsFile.exists()) {
113+
settingsFile.setContents(stream, true, false, null);
114+
} else {
115+
settingsFile.create(stream, true, null);
116+
}
117+
}
118+
119+
}

0 commit comments

Comments
 (0)