Move Java sources to UTF-8 and update the File encoding rules

Hi devs,

This is a follow-up of Status of File encoding rules + proposal to enforce them with Checkstyle, where I audited our sources against our File encoding rules. Thanks to @tmortagne’s feedback there, the proposal has changed a lot, so I’m starting a new thread to have a clean vote.

The main points from the previous thread:

  • Our rules say that Java sources must be ASCII-only, but we have 74 Java files with non-ASCII chars (22 in main code, 52 in tests, mostly intentional test data) and new ones keep being added (e.g. emojis in this PR). Java’s own default is now UTF-8.
  • Our rules say that translation files must be ASCII-only with \uXXXX escapes, but the rule we actually follow since a Weblate upgrade is ISO-8859-1, which is also the encoding we use to load them (Properties.load(InputStream)). The dev practices page was never updated.

Is it safe to move Java sources to UTF-8?

I checked that it can’t cause any regression:

  • The build already reads Java sources as UTF-8, on all branches. project.build.sourceEncoding=UTF-8 is set in the xwiki-commons root POM on master, stable-17.10.x and stable-16.10.x, and inherited by rendering, platform and the contrib parent POMs. It’s the default value of the encoding parameter of all the plugins that read our Java sources: maven-compiler-plugin (so javac always gets -encoding UTF-8, whatever the JDK version or the OS), aspectj-maven-plugin, maven-javadoc-plugin, maven-checkstyle-plugin, license-maven-plugin, jacoco-maven-plugin and Sonar. So we don’t depend on JEP 400 (Java 18+) and the 17.10.x/16.10.x branches are already covered. We could also set <encoding> explicitly in the compiler configuration for clarity, but it wouldn’t change anything.
  • The only exception is Spoon: spoon-maven-plugin has no encoding parameter and uses the JVM default charset. That’s UTF-8 with Java 21+ (master). On 16.10.x/17.10.x, built with Java 17 on a non-UTF-8 OS (e.g. Windows), it could misread non-ASCII chars, but it only runs our static checks (it doesn’t generate anything), so the impact is negligible.
  • It already works in practice: stable-16.10.x already has 50 Java files with non-ASCII chars in platform (20 in main code) and 6 in commons, and they’ve been built and tested on the CI for years without problems. And there’s no runtime impact: the compiled .class files store strings the same way whatever the source encoding was.
  • The remaining risk is on the editor side: someone using an editor configured with another encoding. IDEs importing the Maven project use project.build.sourceEncoding. For the other editors, we could add an .editorconfig at the root of our repos with charset = utf-8 by default and charset = latin1 for *.properties. It’s only a helper though: it’s honored by IntelliJ and by VS Code (with the EditorConfig extension), but not by the GitHub web editor or by AI coding agents, which write UTF-8 files. So the real safeguard is the Checkstyle rules below.

What to do with the existing escaped content?

  • Java: we currently have about 960 \uXXXX escapes in 48 Java files and some HTML entities in javadoc. I propose to keep them as they are and not do a mass conversion: they compile to exactly the same thing, and converting them would only add noise to git blame and conflicts when backporting to the stable branches. New code can use the actual chars. Converting existing escapes when modifying the code is fine, with one exception (see next point).
  • Invisible and ambiguous chars must stay escaped: non-breaking spaces (U+00A0, 37 of the existing escapes), zero-width chars (U+200B-U+200D, U+2060), control chars, bidirectional control chars, BOM, and lone surrogates used as test data (which can’t be written in UTF-8 anyway). Written as raw chars, they’re invisible in a code review, which can hide bugs and is also a known attack vector (“Trojan Source”, CVE-2021-42574). I propose to enforce this with Checkstyle. That rule would currently flag only 4 files (2 main, 2 test), all with raw non-breaking spaces.
  • Properties: 403 properties files contain \uXXXX escapes. Let’s keep them too: chars outside ISO-8859-1 (Cyrillic, Greek, CJK, etc.) must stay escaped anyway, and Weblate rewrites the files in its own format when it touches them, so no manual conversion is needed. Both forms are loaded the same way.
  • Properties encoding check: the real risk for properties files is now someone (or some AI agent) saving one as UTF-8, which would display café instead of café at runtime. Checkstyle can detect it: it reads files as UTF-8 (project.build.sourceEncoding), so in an ISO-8859-1 file every non-ASCII byte becomes U+FFFD, and the only way to get another non-ASCII char is a UTF-8 encoded file. None of our properties files is UTF-8 encoded today, so this check wouldn’t flag anything.

I’ve tested both Checkstyle rules locally with Checkstyle 14.3.0 (visible chars like é are accepted, a raw non-breaking space or zero-width space is rejected while its escaped form is accepted, ISO-8859-1 and escaped properties files are accepted, a UTF-8 properties file is rejected):

<!-- Invisible or ambiguous characters must be written as unicode escapes -->
<module name="RegexpSingleline">
  <property name="format"
    value="[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}\x{007F}\x{00A0}\x{00AD}\x{200B}-\x{200F}\x{202A}-\x{202E}\x{2060}-\x{2064}\x{2066}-\x{2069}\x{FEFF}]"/>
  <property name="fileExtensions" value="java"/>
  <property name="message" value="Invisible or ambiguous character: use a unicode escape instead."/>
</module>
<!-- Properties files must be encoded in ISO-8859-1 -->
<module name="RegexpSingleline">
  <property name="format" value="[^\x{0000}-\x{007F}\x{FFFD}]"/>
  <property name="fileExtensions" value="properties"/>
  <property name="message" value="Properties files must be encoded in ISO-8859-1, not UTF-8."/>
</module>

Proposal

  1. Java sources (main and tests) are UTF-8 and may contain non-ASCII chars, except invisible/ambiguous chars which must be escaped (Checkstyle rule in both checkstyle.xml and checkstyle-test.xml).
  2. Existing escapes and HTML entities are kept as they are (no mass conversion).
  3. Properties files are ISO-8859-1, with chars outside ISO-8859-1 escaped (handled by Weblate), and a Checkstyle rule to catch UTF-8 encoded properties files.
  4. Optional: add an .editorconfig to commons, rendering and platform, as a helper for editors that support it.
  5. No change for XML and other resources (UTF-8, fix the few remaining issues opportunistically).
  6. Replace the File encoding section of the dev practices page with the following (xwiki/2.0 syntax):
= File encoding =

The following rules apply to the text files stored in the XWiki source repositories:

* **Java source files** (main and test sources, including AspectJ files) must be encoded in UTF-8 and can contain non-ASCII characters. The encoding is configured for the whole build by the ##project.build.sourceEncoding## Maven property.
** However, invisible or ambiguous characters must be written as Unicode escapes ({{{\uXXXX}}}): control characters (except tab, line feed and carriage return), non-breaking spaces (U+00A0), soft hyphens (U+00AD), zero-width characters (U+200B to U+200F and U+2060 to U+2064), bidirectional control characters (U+202A to U+202E and U+2066 to U+2069) and byte order marks (U+FEFF). They can't be seen during code reviews and can hide bugs or malicious code. This rule is enforced by Checkstyle.
** Existing Unicode escapes, and HTML entities in javadoc, don't need to be converted. They can be converted to the actual characters when modifying the code, except for the characters listed above.
* **Java properties files**, including translation files (##ApplicationResources*.properties##), must be encoded in ISO-8859-1, which is the encoding used to load them. Characters outside of ISO-8859-1 must be written as Unicode escapes ({{{\uXXXX}}}). This is handled automatically by Weblate for translation files, and Checkstyle fails the build if a properties file is encoded in UTF-8.
* **Wiki pages** (XML files in XAR modules) must be encoded in UTF-8.
* **Other XML files** should specify their encoding in the **{{{<?xml>}}}** header, and it should be UTF-8 whenever possible.
* **All other text files** must be encoded in UTF-8, minimizing the use of non-ASCII characters.

Here’s my +1.

Thanks

+1

+1

+1

Thanks,
Alex

Thanks for the votes, the proposal is accepted and is now implemented:

  • Checkstyle now enforces the rules in XWiki 18.9.0RC1+ (Loading...):
    • invisible or ambiguous characters must be written as unicode escapes in Java sources (main and test)
    • properties files must be encoded in ISO-8859-1, not UTF-8
  • The existing violations have been fixed, and .editorconfig files have been added to commons, rendering and platform.
  • The dev practices have been updated: File encoding and Automatic Checks.

One correction to my first post: I wrote that none of our properties files was UTF-8 encoded, but the new rule found one. The French CSRF translation file had a non-breaking space encoded in UTF-8, displayed as  at runtime. It’s fixed in Loading... (18.9.0RC1, 18.4.7 and 17.10.14).

Thanks

PS: This post was co-written with Claude Code.