[ 
https://issues.apache.org/jira/browse/ARCHETYPE-584?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17008264#comment-17008264
 ] 

Ruwen Reddig edited comment on ARCHETYPE-584 at 1/5/20 7:34 AM:
----------------------------------------------------------------

Stumpled over the same problem yesterday. Please have a look at my PR. I added 
some explanation in the github comment.

Here is a minimal, reproducible example of the problem + fix
{code:java}
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URI;

public class ReadWriteXmlTest {

    public static void main(String[] args) throws Exception {
        String dummyPom =
            "<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/xsd/maven-4.0.0.xsd\";>\n" +
                "  <modelVersion>4.0.0</modelVersion>\n" +
                "  <packaging>pom</packaging>\n" +
            "</project>";

        InputSource inputSource = new InputSource();
        inputSource.setCharacterStream( new StringReader(dummyPom) );

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        // comment any of the following 3 lines to see the problem
        //dbf.setIgnoringElementContentWhitespace(true);
        dbf.setNamespaceAware(true);
        dbf.setSchema( getSchema() );

        Document document = dbf.newDocumentBuilder().parse( inputSource );

        Transformer tr = TransformerFactory.newInstance().newTransformer();
        tr.setOutputProperty( OutputKeys.INDENT, "yes" );

        // comment this line and output still looks okay?!!
        tr.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount";, "2" 
);

        StringWriter fileWriter = new StringWriter();
        tr.transform( new DOMSource( document ), new StreamResult( fileWriter ) 
);

        System.out.println(fileWriter.toString());
    }

    private static Schema getSchema() throws SAXException, 
MalformedURLException {
        URI uri = URI.create("https://maven.apache.org/xsd/maven-4.0.0.xsd";);
        return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
            .newSchema(uri.toURL());
    }

}
{code}


was (Author: newur):
Stumpled over the same problem yesterday. Please have a look at my PR. I added 
some explanation in the github comment.

Here is a minimal, reproducible example of the problem + fix
{code:java}
public static void main(String[] args) throws Exception {
    String dummyPom =
        "<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/xsd/maven-4.0.0.xsd\";>\n" +
            "  <modelVersion>4.0.0</modelVersion>\n" +
            "  <packaging>pom</packaging>\n" +
        "</project>";

    InputSource inputSource = new InputSource();
    inputSource.setCharacterStream( new StringReader(dummyPom) );

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    // uncomment the following lines to see the fix(?)
    //dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    dbf.setSchema( getSchema() );

    Document document = dbf.newDocumentBuilder().parse( inputSource );

    Transformer tr = TransformerFactory.newInstance().newTransformer();
    tr.setOutputProperty( OutputKeys.INDENT, "yes" );

    // comment this line and output still looks okay?!!
    tr.setOutputProperty( "{http://xml.apache.org/xslt}indent-amount";, "2" );

    StringWriter fileWriter = new StringWriter();
    tr.transform( new DOMSource( document ), new StreamResult( fileWriter ) );

    System.out.println(fileWriter.toString());
}

private static Schema getSchema() throws SAXException, MalformedURLException {
    URI uri = URI.create("https://maven.apache.org/xsd/maven-4.0.0.xsd";);
    return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
        .newSchema(uri.toURL());
}

{code}

> Resulting root pom.xml from archetype generation has bad indentation
> --------------------------------------------------------------------
>
>                 Key: ARCHETYPE-584
>                 URL: https://issues.apache.org/jira/browse/ARCHETYPE-584
>             Project: Maven Archetype
>          Issue Type: Bug
>          Components: Archetypes
>    Affects Versions: 3.1.1, 3.1.2
>            Reporter: Andre Prata
>            Priority: Major
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> This issue does not apply to version 3.1.0, it was introduced in version 
> 3.1.0.
>  
> In our project, we have the default configuration of the root pom.xml that 
> contains lines such as this:
> {code:java}
> <properties>
>     <java.version>11</java.version>
>     <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
> </properties>
> {code}
> These lines are still in good shape when added to the jar artifact. However, 
> upon generating the project from the archetype, we get the following (note 
> also the whitespace on the blank lines hinting at some indentation attempts, 
> not just the unexpected line breaks)
> {code:java}
> <properties>
>               
>   
>   <java.version>11</java.version>
>               
>   
>   <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
>           
> </properties>
>       
> {code}
> This issue does *not* occur in in child module pom.xml files.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to