Skip to content

Commit 9d9308e

Browse files
committed
Remove extra indentation text nodes
When removing an element, extra text nodes may be still left over, this ensures these extra text nodes are removed
1 parent b21fb24 commit 9d9308e

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

src/main/java/io/fabric8/maven/MavenJDOMWriter.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.util.Arrays;
1313
import java.util.Collection;
1414
import java.util.Iterator;
15+
import java.util.List;
1516
import java.util.ListIterator;
1617
import java.util.Properties;
1718

@@ -224,6 +225,7 @@ protected void findAndReplaceSimpleLists(Counter counter, Element parent, Collec
224225
elIt.next();
225226
elIt.remove();
226227
}
228+
removeExtraTextNodes(element.getContent());
227229
}
228230
}
229231
}
@@ -321,6 +323,7 @@ protected void iterateContributor(Counter counter, Element parent, Collection<Co
321323
elIt.next();
322324
elIt.remove();
323325
}
326+
removeExtraTextNodes(element.getContent());
324327
}
325328
}
326329
}
@@ -360,9 +363,28 @@ protected void iterateDependency(Counter counter, Element parent, Collection<Dep
360363
}
361364
if (elIt != null) {
362365
while (elIt.hasNext()) {
363-
elIt.next();
366+
Element toRemove = elIt.next();
364367
elIt.remove();
365368
}
369+
removeExtraTextNodes(element.getContent());
370+
}
371+
}
372+
}
373+
374+
/**
375+
* Remove the extra text nodes
376+
*
377+
* @param content
378+
*/
379+
private void removeExtraTextNodes(List<Content> content) {
380+
// We don't want to remove the first or last text node
381+
for (int i = content.size() - 2; i > 0; i--) {
382+
Content current = content.get(i);
383+
if (current instanceof Text) {
384+
content.remove(i);
385+
} else {
386+
// If the current element is not a text node, we can stop
387+
break;
366388
}
367389
}
368390
}
@@ -405,6 +427,7 @@ protected void iterateDeveloper(Counter counter, Element parent, Collection<Deve
405427
elIt.next();
406428
elIt.remove();
407429
}
430+
removeExtraTextNodes(element.getContent());
408431
}
409432
}
410433
}
@@ -447,6 +470,7 @@ protected void iterateExclusion(Counter counter, Element parent, Collection<Excl
447470
elIt.next();
448471
elIt.remove();
449472
}
473+
removeExtraTextNodes(element.getContent());
450474
}
451475
}
452476
}
@@ -489,6 +513,7 @@ protected void iterateExtension(Counter counter, Element parent, Collection<Exte
489513
elIt.next();
490514
elIt.remove();
491515
}
516+
removeExtraTextNodes(element.getContent());
492517
}
493518
}
494519
}
@@ -531,6 +556,7 @@ protected void iterateLicense(Counter counter, Element parent, Collection<Licens
531556
elIt.next();
532557
elIt.remove();
533558
}
559+
removeExtraTextNodes(element.getContent());
534560
}
535561
}
536562
}
@@ -573,6 +599,7 @@ protected void iterateMailingList(Counter counter, Element parent, Collection<Ma
573599
elIt.next();
574600
elIt.remove();
575601
}
602+
removeExtraTextNodes(element.getContent());
576603
}
577604
}
578605
}
@@ -615,6 +642,7 @@ protected void iterateNotifier(Counter counter, Element parent, Collection<Notif
615642
elIt.next();
616643
elIt.remove();
617644
}
645+
removeExtraTextNodes(element.getContent());
618646
}
619647
}
620648
}
@@ -657,6 +685,7 @@ protected void iteratePlugin(Counter counter, Element parent, Collection<Plugin>
657685
elIt.next();
658686
elIt.remove();
659687
}
688+
removeExtraTextNodes(element.getContent());
660689
}
661690
}
662691
}
@@ -699,6 +728,7 @@ protected void iteratePluginExecution(Counter counter, Element parent, Collectio
699728
elIt.next();
700729
elIt.remove();
701730
}
731+
removeExtraTextNodes(element.getContent());
702732
}
703733
}
704734
}
@@ -741,6 +771,7 @@ protected void iterateProfile(Counter counter, Element parent, Collection<Profil
741771
elIt.next();
742772
elIt.remove();
743773
}
774+
removeExtraTextNodes(element.getContent());
744775
}
745776
}
746777
}
@@ -783,6 +814,7 @@ protected void iterateReportPlugin(Counter counter, Element parent, Collection<R
783814
elIt.next();
784815
elIt.remove();
785816
}
817+
removeExtraTextNodes(element.getContent());
786818
}
787819
}
788820
}
@@ -825,6 +857,7 @@ protected void iterateReportSet(Counter counter, Element parent, Collection<Repo
825857
elIt.next();
826858
elIt.remove();
827859
}
860+
removeExtraTextNodes(element.getContent());
828861
}
829862
}
830863
}
@@ -870,6 +903,7 @@ protected void iterateRepository(Counter counter, Element parent, Collection<Rep
870903
elIt.next();
871904
elIt.remove();
872905
}
906+
removeExtraTextNodes(element.getContent());
873907
}
874908
}
875909
}
@@ -915,6 +949,7 @@ protected void iterateResource(Counter counter, Element parent, Collection<Resou
915949
elIt.next();
916950
elIt.remove();
917951
}
952+
removeExtraTextNodes(element.getContent());
918953
}
919954
}
920955
}

src/test/approvals/io/fabric8/maven/MavenTest.should_not_add_extra_line_break.approved.txt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,11 @@
1515

1616
<dependencyManagement>
1717
<dependencies>
18-
<dependency>
19-
<groupId>org.springdoc</groupId>
20-
<artifactId>springdoc-openapi-ui</artifactId>
21-
<version>${springdoc-openapi.version}</version>
22-
</dependency>
2318
<dependency>
2419
<groupId>org.springframework.boot</groupId>
2520
<artifactId>spring-boot-dependencies</artifactId>
2621
<version>${spring-boot.version}</version>
27-
<scope>import</scope>
2822
<type>pom</type>
29-
</dependency>
30-
<dependency>
31-
<groupId>org.springframework.boot</groupId>
32-
<artifactId>spring-boot-dependencies</artifactId>
33-
<version>${spring-boot.version}</version>
34-
<type>pom</type>
35-
<scope>import</scope>
36-
</dependency>
37-
<dependency>
38-
<groupId>org.springframework.boot</groupId>
39-
<artifactId>spring-boot-dependencies</artifactId>
40-
<version>${spring-boot.version}</version>
4123
<scope>import</scope>
4224
</dependency>
4325
</dependencies>

src/test/java/io/fabric8/maven/MavenTest.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.stream.IntStream;
1818

1919
import org.apache.maven.model.Dependency;
20-
import org.apache.maven.model.DependencyManagement;
2120
import org.apache.maven.model.Model;
2221
import org.apache.maven.model.Scm;
2322
import org.approvaltests.Approvals;
@@ -338,25 +337,9 @@ void should_write_attributes() throws Exception {
338337
void should_not_add_extra_line_break() throws Exception {
339338
Path pom = Paths.get(getClass().getResource("extra-line-pom.xml").toURI());
340339
Model model = Maven.readModel(pom);
341-
DependencyManagement dependencyManagement = model.getDependencyManagement();
342-
Dependency dep = new Dependency();
343-
dep.setGroupId("org.springframework.boot");
344-
dep.setArtifactId("spring-boot-dependencies");
345-
dep.setVersion("${spring-boot.version}");
346-
dep.setScope("import");
347-
dep.setType("pom");
348-
dependencyManagement.addDependency(dep);
349-
350-
Dependency dep2 = new Dependency();
351-
dep2.setGroupId("org.springframework.boot");
352-
dep2.setArtifactId("spring-boot-dependencies");
353-
dep2.setVersion("${spring-boot.version}");
354-
dep2.setScope("import");
355-
dependencyManagement.addDependency(dep2);
356-
340+
model.getDependencyManagement().getDependencies().remove(0);
357341
StringWriter sw = new StringWriter();
358342
Maven.writeModel(model, sw);
359343
Approvals.verify(sw.toString());
360344
}
361-
362345
}

0 commit comments

Comments
 (0)