Skip to content

Edit the .idea XML files with the JDK's DOM instead of Groovy's XmlParser - #266

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/edit-the-.idea-xml-files-with-the-jdks-dom-inste
Nov 23, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/edit-the-.idea-xml-files-with-the-jdks-dom-inste

Conversation

@abashev

@abashev abashev commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Closes #262. Based on 21.X.X; there are no other open pull requests.

XmlUtils and ConfigureJavaFormatterXml read and wrote .idea/open-java-format.xml, workspace.xml and externalDependencies.xml with Groovy's XmlParser, XmlNodePrinter and groovy.util.Node. The plugin does not declare Groovy: the classes came with gradleApi() at compile time and, at run time, from the Groovy that the build's Gradle bundles, which once already dropped groovy.util.XmlParser (palantir#1536). The plugin's classes now reference no groovy class at all.

How the files are read and written

  • Parsing goes through DocumentBuilderFactory.newDefaultInstance(), the JDK's own parser even when a build brings another one, and the plugin edits the DOM. DTDs are refused, as Groovy's XmlParser refused them.
  • The order of the attributes. The DOM keeps an element's attributes sorted by name, so a second pass with SAX records the order of the file as user data on each element. The printer writes those first, in that order, then the ones the plugin added, by name.
  • A printer of the plugin's own writes the files as IntelliJ does: the XML declaration, two spaces of indentation, a space before />, " in text, 
 and 	 in attributes, and no line break at the end. Whitespace between elements gives way to that indentation; text and CDATA come out as they are, and so does the rest of their element. The JDK's Transformer, tried in Edit the .idea XML files with the JDK's DOM instead of Groovy's XmlParser #262, indents on top of the whitespace the parser kept, sorts the attributes and writes standalone="no".
  • UTF-8, which IntelliJ reads, rather than Charset.defaultCharset().
  • A file is written only when the printed text changes, so a file with nothing to change keeps its bytes, however it was written. Before, each task rewrote its file every time it ran. This covers open-java-format.xml, whose classpath list configureJavaFormat empties and fills again on every sync.

What changes for users

  • A file the plugin changes or creates now looks as IntelliJ writes it, so IntelliJ's next save no longer rewrites the whole file. Up to now the plugin dropped the declaration and the space before />.
  • A file the plugin wrote before keeps its old text until there is something to change in it.
  • min-version="" on the plugin's entry in externalDependencies.xml counts as no min-version; Version.parse("") failed the task before.

Tests

  • XmlUtilsTest, new, works on files:
    • a new file comes out as IntelliJ writes it;
    • an externalDependencies.xml with nothing to change keeps its bytes when written by IntelliJ, by the plugin before, or by hand with four spaces, CRLF, a comment and single quotes; so does an open-java-format.xml with the same classpath;
    • adding OptimizeOnSaveOptions to an IntelliJ workspace.xml with CDATA, " in text, 
 in an attribute and non-ASCII text leaves the rest of the file byte for byte;
    • the order of the attributes is kept, and the added ones go after them;
    • a DTD is refused.
  • ConfigureJavaFormatterXmlTest runs on the DOM, and its expected text has the space before /> and no line break at the end. RequiredIdeaPluginTest expects the space too.
  • With the "only when the text changes" check taken out, three of the new tests fail. The case IntelliJ wrote passes either way, because the printer reproduces IntelliJ's bytes.
  • IntelliJ's own files. I parsed and printed every workspace.xml, open-java-format.xml, palantir-java-format.xml and externalDependencies.xml in the .idea directories on my machine, 42 files. The 38 that IntelliJ wrote come out byte for byte, CDATA and character references included. The 4 that differ are ones the plugin wrote before.

./gradlew :gradle-open-java-format:check passes on JDK 21: 156 tests, Checkstyle and forbidden-apis. PalantirJavaFormatPluginTest, PalantirJavaFormatSpotlessPluginTest and FormatDiffTest need the native image and are left to CI's native jobs.

Not tried in a running IDE.

…rser

XmlUtils and ConfigureJavaFormatterXml read and wrote
.idea/open-java-format.xml, workspace.xml and externalDependencies.xml
with Groovy's XmlParser, XmlNodePrinter and groovy.util.Node. The
plugin does not declare Groovy: it came with gradleApi() at compile
time and, at run time, from the Groovy that the build's Gradle
bundles, which once already dropped groovy.util.XmlParser.

They parse with the JDK's DocumentBuilder now and edit the DOM. The
DOM keeps an element's attributes sorted by name, so a second pass
with SAX records the order of the file. A printer of the plugin's own
writes the files as IntelliJ does: the XML declaration, two spaces of
indentation, a space before "/>", the attributes in the order of the
file and those added after them, and no line break at the end. Text
and CDATA come out as they are. The files are written in UTF-8, which
IntelliJ reads, rather than in the JVM's default charset.

A file is written only when what the printer makes of it changes, so
a file with nothing to change keeps its bytes, however it was written.
Before, the plugin rewrote each file every time its task ran.

A min-version="" counts as none now; Version.parse("") failed the
task.

Fixes #262.
@abashev
abashev merged commit 8d8a534 into 21.X.X Nov 23, 2026
16 checks passed
@abashev
abashev deleted the abashev/edit-the-.idea-xml-files-with-the-jdks-dom-inste branch November 23, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Edit the .idea XML files with the JDK's DOM instead of Groovy's XmlParser

1 participant