View Javadoc
1   package org.apache.maven.plugins.antrun;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Write the Ant target Plexus configuration to an XML file.
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       * @param configuration {@link PlexusConfiguration}
47       * @param file File to write the Plexus configuration to.
48       * @param customTaskPrefix Prefix to use for the custom Ant tasks. Empty if no prefix should be used.
49       * @param antTargetName Name of the default Ant target.
50       * @throws IOException In case of problems.
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