1010import static java .util .Objects .requireNonNull ;
1111
1212import java .io .IOException ;
13- import java .io .OutputStream ;
14- import java .io .StringReader ;
15- import java .nio .charset .StandardCharsets ;
16- import java .nio .file .Files ;
1713import java .nio .file .Path ;
1814import java .util .ArrayList ;
19- import java .util .Iterator ;
2015import java .util .List ;
2116import java .util .Objects ;
2217import java .util .function .Consumer ;
2318import java .util .function .Function ;
2419import org .eclipse .aether .artifact .Artifact ;
25- import org .jdom2 .CDATA ;
26- import org .jdom2 .Comment ;
2720import org .jdom2 .Document ;
2821import org .jdom2 .Element ;
29- import org .jdom2 .JDOMException ;
30- import org .jdom2 .filter .ContentFilter ;
31- import org .jdom2 .input .SAXBuilder ;
32- import org .jdom2 .located .LocatedJDOMFactory ;
33- import org .jdom2 .output .Format ;
34- import org .jdom2 .output .XMLOutputter ;
3522
3623/**
3724 * Construction to accept collection of artifacts, and applies it to some POM based on provided transformations.
@@ -52,19 +39,7 @@ public interface TransformationContext {
5239 * Removes empty remnant tags, like {@code <plugins />}.
5340 */
5441 private static final Consumer <TransformationContext > removeEmptyElements = ctx -> {
55- // Remove empty elements
56- for (String cleanUpEmptyElement : List .of (
57- JDomCfg .POM_ELEMENT_MODULES ,
58- JDomCfg .POM_ELEMENT_PROPERTIES ,
59- JDomCfg .POM_ELEMENT_PLUGINS ,
60- JDomCfg .POM_ELEMENT_PLUGIN_MANAGEMENT ,
61- JDomCfg .POM_ELEMENT_DEPENDENCIES ,
62- JDomCfg .POM_ELEMENT_DEPENDENCY_MANAGEMENT )) {
63- JDomCleanupHelper .cleanupEmptyElements (ctx .getDocument ().getRootElement (), cleanUpEmptyElement );
64- }
65- // Remove empty (i.e. with no elements) profile and profiles tag
66- JDomCleanupHelper .cleanupEmptyProfiles (
67- ctx .getDocument ().getRootElement (), List .of (JDomCfg .POM_ELEMENT_PROJECT ));
42+ JDomCleanupHelper .cleanup (ctx .getDocument ().getRootElement ());
6843 };
6944
7045 /**
@@ -443,92 +418,40 @@ public void apply(Path pom, List<Consumer<TransformationContext>> transformation
443418 requireNonNull (pom , "pom" );
444419 requireNonNull (transformations , "transformations" );
445420 if (!transformations .isEmpty ()) {
446- String head = null ;
447- String body ;
448- String tail = null ;
449-
450- body = Files .readString (pom , StandardCharsets .UTF_8 );
451- body = normalizeLineEndings (body , System .lineSeparator ());
452-
453- SAXBuilder builder = new SAXBuilder ();
454- builder .setJDOMFactory (new LocatedJDOMFactory ());
455- Document document ;
456- try {
457- document = builder .build (new StringReader (body ));
458- } catch (JDOMException e ) {
459- throw new IOException (e );
460- }
461- normaliseLineEndings (document , System .lineSeparator ());
462-
463- int headIndex = body .indexOf ("<" + document .getRootElement ().getName ());
464- if (headIndex >= 0 ) {
465- head = body .substring (0 , headIndex );
466- }
467- String lastTag = "</" + document .getRootElement ().getName () + ">" ;
468- int tailIndex = body .lastIndexOf (lastTag );
469- if (tailIndex >= 0 ) {
470- tail = body .substring (tailIndex + lastTag .length ());
471- }
472-
473- ArrayList <Consumer <TransformationContext >> postProcessors = new ArrayList <>();
474- TransformationContext context = new TransformationContext () {
475- @ Override
476- public boolean pomHasParent () {
477- return document .getRootElement ()
478- .getChild (
479- "parent" , document .getRootElement ().getNamespace ())
480- != null ;
481- }
482-
483- @ Override
484- public Document getDocument () {
485- return document ;
486- }
421+ try (JDomDocumentIO domDocumentIO = new JDomDocumentIO (pom )) {
422+ ArrayList <Consumer <TransformationContext >> postProcessors = new ArrayList <>();
423+ TransformationContext context = new TransformationContext () {
424+ @ Override
425+ public boolean pomHasParent () {
426+ return domDocumentIO
427+ .getDocument ()
428+ .getRootElement ()
429+ .getChild (
430+ "parent" ,
431+ domDocumentIO
432+ .getDocument ()
433+ .getRootElement ()
434+ .getNamespace ())
435+ != null ;
436+ }
487437
488- @ Override
489- public void registerPostTransformation (Consumer <TransformationContext > transformation ) {
490- postProcessors .add (transformation );
491- }
492- };
493- for (Consumer <TransformationContext > transformation : transformations ) {
494- transformation .accept (context );
495- }
496- for (Consumer <TransformationContext > transformation : postProcessors ) {
497- transformation .accept (context );
498- }
438+ @ Override
439+ public Document getDocument () {
440+ return domDocumentIO .getDocument ();
441+ }
499442
500- Format format = Format .getRawFormat ();
501- format .setLineSeparator (System .lineSeparator ());
502- XMLOutputter out = new XMLOutputter (format );
503- try (OutputStream outputStream = Files .newOutputStream (pom )) {
504- if (head != null ) {
505- outputStream .write (head .getBytes (StandardCharsets .UTF_8 ));
443+ @ Override
444+ public void registerPostTransformation (Consumer <TransformationContext > transformation ) {
445+ postProcessors .add (transformation );
446+ }
447+ };
448+ for (Consumer <TransformationContext > transformation : transformations ) {
449+ transformation .accept (context );
506450 }
507- out .output (document .getRootElement (), outputStream );
508- if (tail != null ) {
509- outputStream .write (tail .getBytes (StandardCharsets .UTF_8 ));
451+ for (Consumer <TransformationContext > transformation : postProcessors ) {
452+ transformation .accept (context );
510453 }
511- outputStream .flush ();
512454 }
513455 }
514456 }
515-
516- private static void normaliseLineEndings (Document document , String lineSeparator ) {
517- for (Iterator <?> i = document .getDescendants (new ContentFilter (ContentFilter .COMMENT )); i .hasNext (); ) {
518- Comment c = (Comment ) i .next ();
519- c .setText (normalizeLineEndings (c .getText (), lineSeparator ));
520- }
521- for (Iterator <?> i = document .getDescendants (new ContentFilter (ContentFilter .CDATA )); i .hasNext (); ) {
522- CDATA c = (CDATA ) i .next ();
523- c .setText (normalizeLineEndings (c .getText (), lineSeparator ));
524- }
525- }
526-
527- private static String normalizeLineEndings (String text , String separator ) {
528- String norm = text ;
529- if (text != null ) {
530- norm = text .replaceAll ("(\r \n )|(\n )|(\r )" , separator );
531- }
532- return norm ;
533- }
534457}
0 commit comments