maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject [maven-studies] branch maven-xml updated: Respect linebreaks when adding new elements
Date Sun, 13 May 2018 21:10:37 GMT
This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch maven-xml
in repository https://gitbox.apache.org/repos/asf/maven-studies.git


The following commit(s) were added to refs/heads/maven-xml by this push:
     new 79cf041  Respect linebreaks when adding new elements
79cf041 is described below

commit 79cf041c5d367694ccebc49c052b4936c0ce026c
Author: rfscholte <rfscholte@apache.org>
AuthorDate: Sun May 13 23:10:29 2018 +0200

    Respect linebreaks when adding new elements
---
 .../apache/maven/xml/filters/ParentXMLFilter.java  | 23 ++++++++++++++++++++++
 .../maven/xml/filters/ParentXMLFilterTest.java     | 20 +++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java b/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java
index a5bdff8..1c6bb4b 100644
--- a/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java
+++ b/src/main/java/org/apache/maven/xml/filters/ParentXMLFilter.java
@@ -59,6 +59,8 @@ public class ParentXMLFilter
     
     private String resolvedVersion;
 
+    private char[] linebreak;
+    
     private List<SAXEvent> saxEvents = new ArrayList<>();
 
     private SAXEventFactory eventFactory;
@@ -155,6 +157,20 @@ public class ParentXMLFilter
             
             switch ( eventState )
             {
+                case "parent":
+                    int l;
+                    for ( l = length ; l >= 0; l-- )
+                    {
+                        int i = start + l - 1; 
+                        if ( ch[i] == '\n' || ch[i] == '\r' )
+                        {
+                            break;
+                        }
+                    }
+                    
+                    linebreak = new char[l];
+                    System.arraycopy( ch, start, linebreak, 0, l );
+                    break;
                 case "relativePath":
                     String relativePath = new String( ch, start, length );
                     resolvedVersion = relativePathToVersion( relativePath );
@@ -233,12 +249,19 @@ public class ParentXMLFilter
                             {
                                 String versionQName = SAXEventUtils.renameQName( qName, "version"
);
                                 
+//                                getEventFactory().characters( indent, 0, indent.length
).execute();
+                                
                                 getEventFactory().startElement( uri, "version", versionQName,
null ).execute();
                                 
                                 getEventFactory().characters( resolvedVersion.toCharArray(),
0,
                                                               resolvedVersion.length() ).execute();
                                 
                                 getEventFactory().endElement( uri, "version", versionQName
).execute();
+                                
+                                if ( linebreak != null )
+                                {
+                                    getEventFactory().characters( linebreak, 0, linebreak.length
).execute();
+                                }
                             } );
                         }
                     }
diff --git a/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java b/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java
index b50fda7..060b320 100644
--- a/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java
+++ b/src/test/java/org/apache/maven/xml/filters/ParentXMLFilterTest.java
@@ -152,4 +152,24 @@ public class ParentXMLFilterTest extends AbstractXMLFilterTests
 
         assertEquals( expected, actual );
     }
+
+    
+    @Test
+    public void testIndent() throws Exception
+    {
+        String input = "<parent>\n"
+                    + "  <groupId>GROUPID</groupId>\n"
+                    + "  <artifactId>ARTIFACTID</artifactId>\n"
+                    + "  </parent>";
+        // transformer is responsible for line separator and indents
+        String expected = "<parent>" + System.lineSeparator()
+                    + "  <groupId>GROUPID</groupId>" + System.lineSeparator()
+                    + "  <artifactId>ARTIFACTID</artifactId>" + System.lineSeparator()
+                    + "  <version>1.0.0</version>" + System.lineSeparator()
+                    + "</parent>";
+
+        String actual = transform( input, filter );
+
+        assertEquals( expected, actual );
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
rfscholte@apache.org.

Mime
View raw message