1 package org.apache.maven.plugins.antrun;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.codehaus.plexus.configuration.PlexusConfiguration;
23 import org.codehaus.plexus.util.xml.Xpp3DomUtils;
24 import org.codehaus.plexus.util.xml.pull.MXSerializer;
25 import org.codehaus.plexus.util.xml.pull.XmlSerializer;
26
27 import java.io.BufferedOutputStream;
28 import java.io.File;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.Set;
34
35
36
37
38 class AntrunXmlPlexusConfigurationWriter
39 {
40
41 private static final Set<String> EXCLUDED_ATTRIBUTES =
42 new HashSet<>( Arrays.asList( Xpp3DomUtils.CHILDREN_COMBINATION_MODE_ATTRIBUTE,
43 Xpp3DomUtils.SELF_COMBINATION_MODE_ATTRIBUTE ) );
44
45
46
47
48
49
50
51
52 public void write( PlexusConfiguration configuration, File file, String customTaskPrefix, String antTargetName )
53 throws IOException
54 {
55 MXSerializer serializer = new MXSerializer();
56 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator",
57 System.getProperty( "line.separator" ) );
58 serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " );
59 try ( BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( file ) ) )
60 {
61 serializer.setOutput( bos, AntRunMojo.UTF_8 );
62 serializer.startDocument( AntRunMojo.UTF_8, null );
63 if ( !customTaskPrefix.isEmpty() )
64 {
65 serializer.setPrefix( customTaskPrefix, AntRunMojo.TASK_URI );
66 }
67 serializer.startTag( null, "project" );
68 serializer.attribute( null, "name", "maven-antrun-" );
69 serializer.attribute( null, "default", antTargetName );
70 write( configuration, serializer );
71 serializer.endTag( null, "project" );
72 serializer.endDocument();
73 }
74 }
75
76 private void write( PlexusConfiguration c, XmlSerializer serializer )
77 throws IOException
78 {
79 serializer.startTag( null, c.getName() );
80 writeAttributes( c, serializer );
81
82 int count = c.getChildCount();
83 if ( count == 0 )
84 {
85 String value = c.getValue();
86 if ( value != null )
87 {
88 serializer.text( value );
89 }
90 }
91 else
92 {
93 for ( int i = 0; i < count; i++ )
94 {
95 PlexusConfiguration child = c.getChild( i );
96 write( child, serializer );
97 }
98 }
99
100 serializer.endTag( null, c.getName() );
101 }
102
103 private void writeAttributes( PlexusConfiguration c, XmlSerializer serializer )
104 throws IOException
105 {
106 String[] names = c.getAttributeNames();
107
108 for ( String name : names )
109 {
110 if ( !EXCLUDED_ATTRIBUTES.contains( name ) )
111 {
112 serializer.attribute( null, name, c.getAttribute( name ) );
113 }
114 }
115 }
116
117 }
118