Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rough idea on how to preserve newlines #31

Closed
wants to merge 1 commit into from
Closed

Conversation

aaime
Copy link
Contributor

@aaime aaime commented Mar 22, 2021

This PR shows a possible approach to address empty line preservation.

At this stage, it's not at all meant for merge, but only for discussion. The idea is to subclass the XML writer and add special handling of subsequent newlines, the code is detecting the presence of text blocks with white space and multiple newlines, and tracking them. When mlutiple newlines are detected, they are not fully skipped, but a single newline is issued in output.

You can see how that works by checking existing test failures, in particular, FormatUtilsTest.test2 would fail returning this output (which is not what the tests expects, but what I'd like to get given its input file):

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.sonatype.oss</groupId>

    <artifactId>oss-parent</artifactId>
    <version>9</version>
  </parent>
  <groupId>au.com.acegi.xmlformat</groupId>

  <artifactId>xml-format-maven-plugin</artifactId>
  <version>1.0.0</version>

  <packaging>maven-plugin</packaging>

</project>

Behavior becomes a bit more similar to a Java code formatters, where new lines are not fully removed.

In order to go from here to something usable, I was thinking to do the following:

  • Adding a new configuration option, off by default, that enables this behavior. sortpom is using a keepBlankLines option for this behavior, maybe it could be named the same way. Documentation for the option says: "Should blank lines in the pom-file be perserved. A maximum of one line is preserved between each tag.".
  • Create a separate subclass of XMLWriter, BlankLinesXMLWriter, that works as the anonymous inner class I have in this draft PR
  • Based on the configuration, decide whether to use the normal XMLWriter, or the BlankLinesXMLWriter
  • Add tests and documentation accordingly.
  • Actually test it in anger against GeoTools and GeoServer (300 maven modules in total, lots of XML files, should be a good test)

@aaime aaime marked this pull request as draft March 22, 2021 08:07
@aaime
Copy link
Contributor Author

aaime commented Apr 3, 2021

Cough cough? :-D

I'm happy to do the development work, was hoping to get some feedback on the approach chosen, the configuration name to control this behavior, and well, if this new feature would be welcomed at all?

@benalexau
Copy link
Member

I welcome a PR that can resolve this issue, as the whitespace handling issue does come up from time to time. Please feel free to complete the PR and I will take a proper look at it.

@aaime
Copy link
Contributor Author

aaime commented Apr 18, 2021

Superceded by #33

@aaime aaime closed this Apr 18, 2021
hazendaz added a commit that referenced this pull request Sep 21, 2024
…ges-4.x

Update peaceiris/actions-gh-pages action to v4
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.

2 participants