Skip to content

Commit 8818804

Browse files
authored
XML Extension (#293)
XML extension for Gradle and Maven.
1 parent c00d1cc commit 8818804

File tree

42 files changed

+1285
-920
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1285
-920
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ lib('kotlin.KtLintStep') +'{{yes}} | {{yes}}
4848
lib('markdown.FreshMarkStep') +'{{yes}} | {{no}} | {{no}} |',
4949
lib('scala.ScalaFmtStep') +'{{yes}} | {{yes}} | {{no}} |',
5050
lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
51+
extra('wtp.WtpEclipseFormatterStep') +'{{yes}} | {{yes}} | {{no}} |',
5152
'| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | {{no}} | {{no}} | {{no}} |',
5253
'| Fast up-to-date checking | {{yes}} | {{no}} | {{no}} |',
5354
'| Automatic idempotency safeguard | {{yes}} | {{no}} | {{no}} |',
@@ -72,6 +73,7 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}
7273
| [`markdown.FreshMarkStep`](lib/src/main/java/com/diffplug/spotless/markdown/FreshMarkStep.java) | :+1: | :white_large_square: | :white_large_square: |
7374
| [`scala.ScalaFmtStep`](lib/src/main/java/com/diffplug/spotless/scala/ScalaFmtStep.java) | :+1: | :+1: | :white_large_square: |
7475
| [`sql.DBeaverSQLFormatterStep`](lib/src/main/java/com/diffplug/spotless/sql/DBeaverSQLFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
76+
| [`wtp.WtpEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java) | :+1: | :+1: | :white_large_square: |
7577
| [(Your FormatterStep here)](CONTRIBUTING.md#how-to-add-a-new-formatterstep) | :white_large_square: | :white_large_square: | :white_large_square: |
7678
| Fast up-to-date checking | :+1: | :white_large_square: | :white_large_square: |
7779
| Automatic idempotency safeguard | :+1: | :white_large_square: | :white_large_square: |

_ext/eclipse-wtp/src/test/java/com/diffplug/spotless/extra/eclipse/wtp/EclipseXmlFormatterStepImplTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,88 +41,88 @@ public static void initializeStatic() throws Exception {
4141

4242
@Test
4343
public void simpleDefaultFormat() throws Throwable {
44-
String output = format(TEST_DATA.input("xml_space.xml"), config -> {});
44+
String output = format(TEST_DATA.input("xml_space.test"), config -> {});
4545
assertEquals("Unexpected formatting with default preferences.",
46-
TEST_DATA.expected("xml_space.xml"), output);
46+
TEST_DATA.expected("xml_space.test"), output);
4747
}
4848

4949
@Test
5050
public void invalidXmlFormat() throws Throwable {
51-
String[] input = TEST_DATA.input("xml_space.xml");
51+
String[] input = TEST_DATA.input("xml_space.test");
5252
input[0] += INCOMPLETE;
5353
String output = format(input, config -> {});
54-
String expected = TEST_DATA.expected("xml_space.xml") + LINE_DELIMITER + INCOMPLETE;
54+
String expected = TEST_DATA.expected("xml_space.test") + LINE_DELIMITER + INCOMPLETE;
5555
assertEquals("Incomplete XML not formatted on best effort basis.",
5656
expected, output);
5757
}
5858

5959
@Test
6060
public void illegalXmlCharater() throws Throwable {
61-
String[] input = TEST_DATA.input("xml_space.xml");
61+
String[] input = TEST_DATA.input("xml_space.test");
6262
input[0] = ILLEGAL_CHAR + input[0];
6363
String output = format(input, config -> {});
64-
String expected = LINE_DELIMITER + LINE_DELIMITER + TEST_DATA.expected("xml_space.xml");
64+
String expected = LINE_DELIMITER + LINE_DELIMITER + TEST_DATA.expected("xml_space.test");
6565
assertEquals("Illegal character not replaced by line delimiter.", expected, output);
6666
}
6767

6868
@Test
6969
public void multipleConfigurations() throws Throwable {
70-
String output = format(TEST_DATA.input("xml_space.xml"), config -> {
70+
String output = format(TEST_DATA.input("xml_space.test"), config -> {
7171
config.setProperty(INDENTATION_SIZE, "2");
7272
config.setProperty(INDENTATION_CHAR, SPACE);
7373
});
74-
String expected = TEST_DATA.expected("xml_space.xml").replace("\t", " ");
74+
String expected = TEST_DATA.expected("xml_space.test").replace("\t", " ");
7575
assertEquals("Custom indentation configuration not applied.", expected, output);
7676

77-
output = format(TEST_DATA.input("xml_space.xml"), config -> {
77+
output = format(TEST_DATA.input("xml_space.test"), config -> {
7878
config.setProperty(SPLIT_MULTI_ATTRS, Boolean.toString(true));
7979
});
80-
expected = TEST_DATA.expected("xml_space.xml");
80+
expected = TEST_DATA.expected("xml_space.test");
8181
expected = expected.replace(" a=", LINE_DELIMITER + "\ta=");
8282
expected = expected.replace(" b=", LINE_DELIMITER + "\tb=");
8383
assertEquals("Custom indentation configuration not reverted or custom multiple argument configuration not applied.", expected, output);
8484
}
8585

8686
@Test
8787
public void invalidConfiguration() throws Throwable {
88-
String output = format(TEST_DATA.input("xml_space.xml"), config -> {
88+
String output = format(TEST_DATA.input("xml_space.test"), config -> {
8989
config.setProperty(INDENTATION_SIZE, "Not an integer");
9090
config.setProperty(INDENTATION_CHAR, SPACE);
9191
});
9292
assertEquals("Invalid indentation configuration not replaced by default value (0 spaces)",
93-
TEST_DATA.expected("xml_space.xml").replace("\t", ""), output);
93+
TEST_DATA.expected("xml_space.test").replace("\t", ""), output);
9494
}
9595

9696
@Test
9797
public void dtdRelativePath() throws Throwable {
98-
String output = format(TEST_DATA.input("dtd_relative.xml"), config -> {});
98+
String output = format(TEST_DATA.input("dtd_relative.test"), config -> {});
9999
assertEquals("Relative DTD not resolved. Restrictions are not applied by formatter.",
100-
TEST_DATA.expected("dtd_relative.xml"), output);
100+
TEST_DATA.expected("dtd_relative.test"), output);
101101
}
102102

103103
@Test
104104
public void xsdRelativePath() throws Throwable {
105-
String output = format(TEST_DATA.input("xsd_relative.xml"), config -> {});
105+
String output = format(TEST_DATA.input("xsd_relative.test"), config -> {});
106106
assertEquals("Relative XSD not resolved. Restrictions are not applied by formatter.",
107-
TEST_DATA.expected("xsd_relative.xml"), output);
107+
TEST_DATA.expected("xsd_relative.test"), output);
108108
}
109109

110110
@Test
111111
public void xsdNotFound() throws Throwable {
112-
String output = format(TEST_DATA.input("xsd_not_found.xml"), config -> {});
112+
String output = format(TEST_DATA.input("xsd_not_found.test"), config -> {});
113113
assertEquals("Unresolved XSD/DTD not silently ignored.",
114-
TEST_DATA.expected("xsd_not_found.xml"), output);
114+
TEST_DATA.expected("xsd_not_found.test"), output);
115115
}
116116

117117
@Test
118118
public void catalogLookup() throws Throwable {
119-
String output = format(TEST_DATA.input("xsd_not_found.xml"), config -> {
119+
String output = format(TEST_DATA.input("xsd_not_found.test"), config -> {
120120
config.setProperty(
121121
SpotlessPreferences.USER_CATALOG,
122122
TEST_DATA.getRestrictionsPath("catalog.xml").toString());
123123
});
124124
assertEquals("XSD not resolved by catalog. Restrictions are not applied by formatter.",
125-
TEST_DATA.expected("xsd_not_found.xml").replace(" remove spaces ", "remove spaces"), output);
125+
TEST_DATA.expected("xsd_not_found.test").replace(" remove spaces ", "remove spaces"), output);
126126
}
127127

128128
private static String format(final String[] input, final Consumer<Properties> config) throws Exception {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
3-
<!-- OASIS: If the catalog entry file is specified with a relative URI, it is relative to the base URI of the document that contains the processing instruction. -->
4-
<system systemId="http://foo.bar/test.xsd" uri="./test.xsd"/>
3+
<!-- OASIS: If the catalog entry file is specified with a relative URI, it is relative to the base URI of the document that contains the processing instruction. -->
4+
<system systemId="http://foo.bar/test.xsd" uri="./test.xsd" />
55
</catalog>

_ext/eclipse-wtp/src/test/resources/xml/restrictions/test.xsd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<xs:element name="x">
77
<xs:simpleType>
88
<xs:restriction base="xs:string">
9-
<xs:whiteSpace value="collapse"/>
9+
<xs:whiteSpace value="collapse" />
1010
</xs:restriction>
1111
</xs:simpleType>
12-
</xs:element>
12+
</xs:element>
1313
<xs:element name="y">
1414
<xs:simpleType>
1515
<xs:restriction base="xs:string">
16-
<xs:whiteSpace value="preserve"/>
16+
<xs:whiteSpace value="preserve" />
1717
</xs:restriction>
1818
</xs:simpleType>
1919
</xs:element>

lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/WtpEclipseFormatterStep.java renamed to lib-extra/src/main/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
2626

2727
/** Formatter step which calls out to the Groovy-Eclipse formatter. */
28-
public final class WtpEclipseFormatterStep {
28+
public final class EclipseWtpFormatterStep {
2929
// prevent direct instantiation
30-
private WtpEclipseFormatterStep() {}
30+
private EclipseWtpFormatterStep() {}
3131

3232
private static final String NAME = "eclipse wtp formatters";
3333
private static final String FORMATTER_PACKAGE = "com.diffplug.spotless.extra.eclipse.wtp.";

lib-extra/src/test/java/com/diffplug/spotless/extra/wtp/EclipseWtpFormatterStepTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ private enum WTP {
4545
// @formatter:off
4646
CSS( "body {\na: v; b: \nv;\n} \n",
4747
"body {\n\ta: v;\n\tb: v;\n}",
48-
WtpEclipseFormatterStep::createCssBuilder),
48+
EclipseWtpFormatterStep::createCssBuilder),
4949
HTML( "<!DOCTYPE html> <html>\t<head> <meta charset=\"UTF-8\"></head>\n</html> ",
5050
"<!DOCTYPE html>\n<html>\n<head>\n<meta charset=\"UTF-8\">\n</head>\n</html>\n",
51-
WtpEclipseFormatterStep::createHtmlBuilder),
51+
EclipseWtpFormatterStep::createHtmlBuilder),
5252
JS( "function f( ) {\na.b(1,\n2);}",
5353
"function f() {\n a.b(1, 2);\n}",
54-
WtpEclipseFormatterStep::createJsBuilder),
54+
EclipseWtpFormatterStep::createJsBuilder),
5555
JSON( "{\"a\": \"b\", \"c\": { \"d\": \"e\",\"f\": \"g\"}}",
5656
"{\n\t\"a\": \"b\",\n\t\"c\": {\n\t\t\"d\": \"e\",\n\t\t\"f\": \"g\"\n\t}\n}",
57-
WtpEclipseFormatterStep::createJsonBuilder),
57+
EclipseWtpFormatterStep::createJsonBuilder),
5858
XML( "<a><b> c</b></a>", "<a>\n\t<b> c</b>\n</a>",
59-
WtpEclipseFormatterStep::createXmlBuilder);
59+
EclipseWtpFormatterStep::createXmlBuilder);
6060
// @formatter:on
6161

6262
public final String input;
@@ -132,7 +132,7 @@ private FormatterStep createStepForDefaultVersion(Consumer<Properties> config) t
132132
OutputStream tempOut = new FileOutputStream(tempFile);
133133
configProps.store(tempOut, "test properties");
134134
EclipseBasedStepBuilder builder = wtp.builderMethod.apply(TestProvisioner.mavenCentral());
135-
builder.setVersion(WtpEclipseFormatterStep.defaultVersion());
135+
builder.setVersion(EclipseWtpFormatterStep.defaultVersion());
136136
builder.setPreferences(Arrays.asList(tempFile));
137137
return builder.build();
138138
}

lib/src/main/java/com/diffplug/spotless/cpp/CppDefaults.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public class CppDefaults {
2525
//Prevent instantiation
2626
private CppDefaults() {};
2727

28+
/** Filtering based on Eclipse-CDT <code>org.eclipse.core.contenttype.contentTypes</code> */
29+
/**
30+
* Filter based on Eclipse-CDT <code>org.eclipse.core.contenttype.contentTypes</code>
31+
* extension <code>cSource</code>, <code>cHeader</code>, <code>cxxSource</code> and <code>cxxHeader</code>.
32+
*/
2833
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
2934
Arrays.asList("c", "h", "C", "cpp", "cxx", "cc", "c++", "h", "hpp", "hh", "hxx", "inc")
3035
.stream().map(s -> {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2016 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.xml;
17+
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.stream.Collectors;
22+
23+
/** Common utilities for XML */
24+
public class XmlDefaults {
25+
//Prevent instantiation
26+
private XmlDefaults() {};
27+
28+
/**
29+
* Filter based on Eclipse-WTP <code>org.eclipse.core.contenttype.contentTypes</code>
30+
* extension <code>org.eclipse.wst.xml.core.xmlsource</code>
31+
*/
32+
public static final List<String> FILE_FILTER = Collections.unmodifiableList(
33+
Arrays.asList("xml", "xsl", "xslt", "wsdl", "xsd", "exsd", "xmi")
34+
.stream().map(s -> "**/*." + s).collect(Collectors.toList()));
35+
36+
/** Delimiter covers beginning of elements and processing instructions. */
37+
public static final String DELIMITER_EXPR = "\\<[a-zA-Z\\?]";
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@ParametersAreNonnullByDefault
2+
@ReturnValuesAreNonnullByDefault
3+
package com.diffplug.spotless.xml;
4+
5+
import javax.annotation.ParametersAreNonnullByDefault;
6+
7+
import com.diffplug.spotless.annotations.ReturnValuesAreNonnullByDefault;

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Version 3.15.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-plugin-gradle/))
44

5+
* Added `xml` support ([#140](https://github.com/diffplug/spotless/issues/140)) using formatter of Eclipse WTP 3.9.5 ([#241](https://github.com/diffplug/spotless/pull/241)).
56
* Added C/C++ support using formatter of Eclipse CDT 9.4.3 ([#232](https://github.com/diffplug/spotless/issues/232)).
67
* Updated default groovy-eclipse from 4.8.0 to 4.8.1 ([#288](https://github.com/diffplug/spotless/pull/288)). New version is based on [Groovy-Eclipse 3.0.0](https://github.com/groovy/groovy-eclipse/wiki/3.0.0-Release-Notes).
78
* LicenseHeaderStep now wont attempt to add license to `module-info.java` ([#272](https://github.com/diffplug/spotless/pull/272)).

plugin-gradle/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Spotless can check and apply formatting to any plain-text file, using simple rul
7979

8080
* Eclipse's [CDT](#eclipse-cdt) C/C++ code formatter
8181
* Eclipse's java code formatter (including style and import ordering)
82+
* Eclipse's [WTP-XML](#eclipse-wtp-xml) XML code formatter
8283
* Google's [google-java-format](https://github.com/google/google-java-format)
8384
* [Groovy Eclipse](#groovy-eclipse)'s groovy code formatter
8485
* [FreshMark](https://github.com/diffplug/freshmark) (markdown with variables)
@@ -317,6 +318,32 @@ spotless {
317318

318319
Use the Eclipse to define the *Code Style preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). Within the preferences *Edit...* dialog, you can export your configuration as XML file, which can be used as a `configFile`. If no `configFile` is provided, the CDT default configuration is used.
319320

321+
<a name="xml-wtp"></a>
322+
323+
## Applying to XML sources
324+
325+
```gradle
326+
spotless {
327+
xml {
328+
target '**/*.xml' // Change file filter. By default files with 'xml', 'xsl', 'xslt', 'wsdl', 'xsd', 'exsd' and 'xmi' extension are supported
329+
eclipse().configFile './xml-formatter.prefs' // Properties file of the Eclipse WTP formatter
330+
// Use for example eclipse('4.7.3a') to specify a specific version of Eclipse,
331+
// available versions are: https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_wtp_formatters
332+
// also supports license headers
333+
licenseHeader '<!-- Licensed under Apache-2.0 -->' // License header
334+
licenseHeaderFile './license.txt' // License header file
335+
}
336+
}
337+
```
338+
339+
<a name="eclipse-wtp-xml"></a>
340+
341+
### Eclipse [WTP](https://www.eclipse.org/webtools/) XML formatter
342+
Use Eclipse to define the *XML editor preferences* (see [Eclipse documentation](https://www.eclipse.org/documentation/)). The preferences are stored below your Eclipse workspace directory in `.metadata/.plugins/org.eclipse.core.runtime/org.eclipse.wst.xml.core.prefs`. Note that only the differences to the default configuration are stored within the file. Omit the 'configFile' entirely to use the default Eclipse configuration.
343+
344+
The Eclipse WTP formatter supports DTD/XSD restrictions on white spaces. For XSD/DTD lookup, relative and absolute XSD/DTD URIs are supported. Furthermore a user catalog can be configured using the `userCatalog` property key. Add the property to the preference file or add an additional preference or properties files as an additional argument to the `configFile`.
345+
346+
320347
<a name="license-header"></a>
321348

322349
## License header options

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/CppExtension.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ public void configFile(Object... configFiles) {
5656

5757
}
5858

59-
/** If the user hasn't specified the files yet, we'll assume he/she means all of the C/C++ files. */
6059
@Override
6160
protected void setupTask(SpotlessTask task) {
6261
if (target == null) {
6362
/*
6463
* The org.gradle.language.c and org.gradle.language.cpp source sets are seldom used.
6564
* Most Gradle C/C++ use external CMake builds (so the source location is unknown to Gradle).
66-
* Hence file extension based filtering is used in line with the org.eclipse.core.contenttype.contentTypes
65+
* Hence file extension based filtering is used in line with the org.eclipse.core.contenttype.contentTypes<
6766
* defined by the CDT plugin.
6867
*/
6968
target(CppDefaults.FILE_FILTER.toArray());

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public void sql(Action<SqlExtension> closure) {
118118
configure(SqlExtension.NAME, SqlExtension.class, closure);
119119
}
120120

121+
/** Configures the special xml-specific extension for XML/XSL/... files (XHTML is excluded). */
122+
public void xml(Action<XmlExtension> closure) {
123+
configure(XmlExtension.NAME, XmlExtension.class, closure);
124+
}
125+
121126
/** Configures the special C/C++-specific extension. */
122127
public void cpp(Action<CppExtension> closure) {
123128
configure(CppExtension.NAME, CppExtension.class, closure);

0 commit comments

Comments
 (0)