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

Xml formatting #267

Closed
delanym opened this issue Dec 9, 2022 · 12 comments
Closed

Xml formatting #267

delanym opened this issue Dec 9, 2022 · 12 comments
Assignees

Comments

@delanym
Copy link

delanym commented Dec 9, 2022

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
Copy link
Author

delanym commented Dec 9, 2022

image

@Ekryd
Copy link
Owner

Ekryd commented Dec 9, 2022

Have you tried the indentSchemaLocation parameter?

@delanym
Copy link
Author

delanym commented Dec 9, 2022

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
Copy link
Owner

Ekryd commented Dec 11, 2022

Let me have a look

@Ekryd Ekryd self-assigned this Dec 11, 2022
@create-issue-branch
Copy link

Branch issue-267-Xml_formatting created!

@Ekryd
Copy link
Owner

Ekryd commented Dec 11, 2022

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
Copy link
Author

delanym commented Dec 13, 2022

@Ekryd how do I configure it though?

@Ekryd
Copy link
Owner

Ekryd commented Dec 18, 2022

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

@delanym
Copy link
Author

delanym commented Dec 18, 2022

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
Copy link
Owner

Ekryd commented Dec 26, 2022

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.

@Ekryd Ekryd closed this as completed Feb 4, 2023
@delanym
Copy link
Author

delanym commented Feb 4, 2023

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
Copy link
Owner

Ekryd commented Feb 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants