Ekryd / sortpom

Maven plugin that helps the user sort pom.xml.
https://github.com/Ekryd/sortpom/wiki/
BSD 3-Clause "New" or "Revised" License
334 stars 178 forks source link

Xml formatting #267

Closed delanym closed 1 year ago

delanym commented 1 year ago

hi. I have my IDE format xml to make it more readable, but the changes keep being overridden by sortpom plugin

-<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">
+<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">

Can this be configurable?

delanym commented 1 year ago

image

Ekryd commented 1 year ago

Have you tried the indentSchemaLocation parameter?

delanym commented 1 year ago

That results in

-<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">
+<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">

I need each attribute on its own line because, although not applicable for poms, other xml files sometimes have many attributes on a tag.

Ekryd commented 1 year ago

Let me have a look

create-issue-branch[bot] commented 1 year ago

Branch issue-267-Xml_formatting created!

Ekryd commented 1 year ago

I did some experiments with this. I have some form of functionality, but it breaks existing tests. The test case is that somebody adds an xml namespace at a lower level of the POM file. Not very realistic, but it is possible. The indentation of the namespace will also be (normal indent * 2) + 1 space character which is not ideal. Feel free to check out the branch and try out the code for yourself.

delanym commented 1 year ago

@Ekryd how do I configure it though?

Ekryd commented 1 year ago

Check out the branch, compile it and use the parameter indentSchemaLocation to get the extra line breaks (this is just a concept code)

delanym commented 1 year ago

You asked me to try out indentSchemaLocation parameter earlier. What have you done on the branch to get each attribute on its own line, or leave the line breaks alone?

Ekryd commented 1 year ago

The branch makes sure that each attribute gets its own line:

<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">

I cannot preserve line breaks, as the whole xml file is parsed as a DOM tree and recreated completely during sorting.

delanym commented 1 year ago

Hi Björn. I don't know how you got that output. Using the released 3.2.1, I get either all attributes on one line or, with indentSchemaLocation to true, 2 lines. I don't get 3 lines - each attribute on its own line.

I tried changing

diff --git a/sorter/src/main/java/sortpom/output/PatchedXMLWriter.java b/sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
index 67dc29d..e0b3cd7 100644
--- a/sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
+++ b/sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
@@ -99 +99 @@ class PatchedXMLWriter extends XMLWriter {
-    if (indentSchemaLocation && "xsi:schemaLocation".equals(qualifiedName)) {
+    if (indentSchemaLocation) {

the test failed with

[ERROR] Failures: 
[ERROR]   IndentationTest.otherAttributeShouldNotBeIndented:103 expected: <<?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">
  <Gurka xmlns="" key="value"></Gurka>
</project>
> but was: <<?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">
  <Gurka xmlns=""
     key="value"></Gurka>
</project>
>

Since it breaks before key but not xmlns:xsi Im led to believes its only processing attributes in the default namespace?

Ekryd commented 1 year ago

You have to use git to checkout the branch issue-267-Xml_formatting in order to see the changes. The branch should compile, although some tests will fail on it.