Skip to content

Commit 54eb5fc

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 54eb5fc

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

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

Lines changed: 37 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+
removeExtraIndents(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+
removeExtraIndents(element.getContent());
324327
}
325328
}
326329
}
@@ -360,9 +363,29 @@ 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+
removeExtraIndents(element.getContent());
370+
}
371+
}
372+
}
373+
374+
/**
375+
* When elements are removed from the JDOM tree, there may be Text nodes (used for indentation) that are left behind.
376+
* This method removes these nodes.
377+
*
378+
* @param content The list of content to remove text nodes from
379+
*/
380+
private void removeExtraIndents(List<Content> content) {
381+
// We don't want to remove the first or last text node
382+
for (int i = content.size() - 2; i > 0; i--) {
383+
Content current = content.get(i);
384+
if (current instanceof Text) {
385+
content.remove(i);
386+
} else {
387+
// If the current element is not a text node, we can stop
388+
break;
366389
}
367390
}
368391
}
@@ -405,6 +428,7 @@ protected void iterateDeveloper(Counter counter, Element parent, Collection<Deve
405428
elIt.next();
406429
elIt.remove();
407430
}
431+
removeExtraIndents(element.getContent());
408432
}
409433
}
410434
}
@@ -447,6 +471,7 @@ protected void iterateExclusion(Counter counter, Element parent, Collection<Excl
447471
elIt.next();
448472
elIt.remove();
449473
}
474+
removeExtraIndents(element.getContent());
450475
}
451476
}
452477
}
@@ -489,6 +514,7 @@ protected void iterateExtension(Counter counter, Element parent, Collection<Exte
489514
elIt.next();
490515
elIt.remove();
491516
}
517+
removeExtraIndents(element.getContent());
492518
}
493519
}
494520
}
@@ -531,6 +557,7 @@ protected void iterateLicense(Counter counter, Element parent, Collection<Licens
531557
elIt.next();
532558
elIt.remove();
533559
}
560+
removeExtraIndents(element.getContent());
534561
}
535562
}
536563
}
@@ -573,6 +600,7 @@ protected void iterateMailingList(Counter counter, Element parent, Collection<Ma
573600
elIt.next();
574601
elIt.remove();
575602
}
603+
removeExtraIndents(element.getContent());
576604
}
577605
}
578606
}
@@ -615,6 +643,7 @@ protected void iterateNotifier(Counter counter, Element parent, Collection<Notif
615643
elIt.next();
616644
elIt.remove();
617645
}
646+
removeExtraIndents(element.getContent());
618647
}
619648
}
620649
}
@@ -657,6 +686,7 @@ protected void iteratePlugin(Counter counter, Element parent, Collection<Plugin>
657686
elIt.next();
658687
elIt.remove();
659688
}
689+
removeExtraIndents(element.getContent());
660690
}
661691
}
662692
}
@@ -699,6 +729,7 @@ protected void iteratePluginExecution(Counter counter, Element parent, Collectio
699729
elIt.next();
700730
elIt.remove();
701731
}
732+
removeExtraIndents(element.getContent());
702733
}
703734
}
704735
}
@@ -741,6 +772,7 @@ protected void iterateProfile(Counter counter, Element parent, Collection<Profil
741772
elIt.next();
742773
elIt.remove();
743774
}
775+
removeExtraIndents(element.getContent());
744776
}
745777
}
746778
}
@@ -783,6 +815,7 @@ protected void iterateReportPlugin(Counter counter, Element parent, Collection<R
783815
elIt.next();
784816
elIt.remove();
785817
}
818+
removeExtraIndents(element.getContent());
786819
}
787820
}
788821
}
@@ -825,6 +858,7 @@ protected void iterateReportSet(Counter counter, Element parent, Collection<Repo
825858
elIt.next();
826859
elIt.remove();
827860
}
861+
removeExtraIndents(element.getContent());
828862
}
829863
}
830864
}
@@ -870,6 +904,7 @@ protected void iterateRepository(Counter counter, Element parent, Collection<Rep
870904
elIt.next();
871905
elIt.remove();
872906
}
907+
removeExtraIndents(element.getContent());
873908
}
874909
}
875910
}
@@ -915,6 +950,7 @@ protected void iterateResource(Counter counter, Element parent, Collection<Resou
915950
elIt.next();
916951
elIt.remove();
917952
}
953+
removeExtraIndents(element.getContent());
918954
}
919955
}
920956
}

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)