View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.antrun;
20  
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.util.Arrays;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import org.codehaus.plexus.configuration.PlexusConfiguration;
30  import org.codehaus.plexus.util.xml.Xpp3DomUtils;
31  import org.codehaus.plexus.util.xml.pull.MXSerializer;
32  import org.codehaus.plexus.util.xml.pull.XmlSerializer;
33  
34  /**
35   * Write the Ant target Plexus configuration to an XML file.
36   */
37  class AntrunXmlPlexusConfigurationWriter {
38  
39      private static final Set<String> EXCLUDED_ATTRIBUTES = new HashSet<>(Arrays.asList(
40              Xpp3DomUtils.CHILDREN_COMBINATION_MODE_ATTRIBUTE, Xpp3DomUtils.SELF_COMBINATION_MODE_ATTRIBUTE));
41  
42      /**
43       * @param configuration {@link PlexusConfiguration}
44       * @param file File to write the Plexus configuration to.
45       * @param customTaskPrefix Prefix to use for the custom Ant tasks. Empty if no prefix should be used.
46       * @param antTargetName Name of the default Ant target.
47       * @throws IOException In case of problems.
48       */
49      public void write(PlexusConfiguration configuration, File file, String customTaskPrefix, String antTargetName)
50              throws IOException {
51          MXSerializer serializer = new MXSerializer();
52          serializer.setProperty(
53                  "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator",
54                  System.getProperty("line.separator"));
55          serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", "  ");
56          try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
57              serializer.setOutput(bos, AntRunMojo.UTF_8);
58              serializer.startDocument(AntRunMojo.UTF_8, null);
59              if (!customTaskPrefix.isEmpty()) {
60                  serializer.setPrefix(customTaskPrefix, AntRunMojo.TASK_URI);
61              }
62              serializer.startTag(null, "project");
63              serializer.attribute(null, "name", "maven-antrun-");
64              serializer.attribute(null, "default", antTargetName);
65              write(configuration, serializer);
66              serializer.endTag(null, "project");
67              serializer.endDocument();
68          }
69      }
70  
71      private void write(PlexusConfiguration c, XmlSerializer serializer) throws IOException {
72          serializer.startTag(null, c.getName());
73          writeAttributes(c, serializer);
74  
75          int count = c.getChildCount();
76          if (count == 0) {
77              String value = c.getValue();
78              if (value != null) {
79                  serializer.text(value);
80              }
81          } else {
82              for (int i = 0; i < count; i++) {
83                  PlexusConfiguration child = c.getChild(i);
84                  write(child, serializer);
85              }
86          }
87  
88          serializer.endTag(null, c.getName());
89      }
90  
91      private void writeAttributes(PlexusConfiguration c, XmlSerializer serializer) throws IOException {
92          String[] names = c.getAttributeNames();
93  
94          for (String name : names) {
95              if (!EXCLUDED_ATTRIBUTES.contains(name)) {
96                  serializer.attribute(null, name, c.getAttribute(name));
97              }
98          }
99      }
100 }