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.help;
20  
21  import javax.xml.XMLConstants;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.io.Writer;
28  import java.util.ArrayList;
29  import java.util.Collections;
30  import java.util.LinkedHashSet;
31  import java.util.List;
32  import java.util.Properties;
33  import java.util.Set;
34  
35  import org.codehaus.plexus.util.WriterFactory;
36  import org.codehaus.plexus.util.xml.XMLWriter;
37  import org.codehaus.plexus.util.xml.XmlWriterUtil;
38  import org.jdom2.Document;
39  import org.jdom2.JDOMException;
40  import org.jdom2.input.SAXBuilder;
41  import org.jdom2.output.Format;
42  import org.jdom2.output.XMLOutputter;
43  
44  /**
45   * Base class with common utilities to write effective Pom/settings.
46   *
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @since 2.1
49   */
50  public abstract class AbstractEffectiveMojo extends AbstractHelpMojo {
51      /**
52       * Utility method to write an XML content in a given file.
53       *
54       * @param output is the wanted output file.
55       * @param content contains the XML content to be written to the file.
56       * @throws IOException if any
57       * @see AbstractHelpMojo#writeFile(File, String) if encoding is null.
58       */
59      protected static void writeXmlFile(File output, String content) throws IOException {
60          if (output == null) {
61              return;
62          }
63  
64          output.getParentFile().mkdirs();
65          try (Writer out = WriterFactory.newXmlWriter(output)) {
66              out.write(content);
67          }
68      }
69  
70      /**
71       * Write comments in the Effective POM/settings header.
72       *
73       * @param writer not null
74       */
75      protected static void writeHeader(XMLWriter writer) {
76          XmlWriterUtil.writeCommentLineBreak(writer);
77          XmlWriterUtil.writeComment(writer, " ");
78          XmlWriterUtil.writeComment(writer, "Generated by Maven Help Plugin");
79          XmlWriterUtil.writeComment(writer, "See: https://maven.apache.org/plugins/maven-help-plugin/");
80          XmlWriterUtil.writeComment(writer, " ");
81          XmlWriterUtil.writeCommentLineBreak(writer);
82      }
83  
84      /**
85       * Write comments in a normalize way.
86       *
87       * @param writer not null
88       * @param comment not null
89       */
90      protected static void writeComment(XMLWriter writer, String comment) {
91          XmlWriterUtil.writeCommentLineBreak(writer);
92          XmlWriterUtil.writeComment(writer, " ");
93          XmlWriterUtil.writeComment(writer, comment);
94          XmlWriterUtil.writeComment(writer, " ");
95          XmlWriterUtil.writeCommentLineBreak(writer);
96      }
97  
98      /**
99       * @param effectiveModel not null
100      * @param encoding not null
101      * @param omitDeclaration whether the XML declaration should be omitted from the effective pom
102      * @return pretty format of the xml or the original {@code effectiveModel} if an error occurred.
103      */
104     protected static String prettyFormat(String effectiveModel, String encoding, boolean omitDeclaration) {
105         SAXBuilder builder = new SAXBuilder();
106         builder.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
107         builder.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
108         try {
109             Document effectiveDocument = builder.build(new StringReader(effectiveModel));
110 
111             StringWriter w = new StringWriter();
112             Format format = Format.getPrettyFormat();
113             if (encoding != null) {
114                 // This is a design flaw in JDOM, no NPE on null arguments, but null is not prohibited
115                 format.setEncoding(encoding);
116             }
117             format.setLineSeparator(System.lineSeparator());
118             format.setOmitDeclaration(omitDeclaration);
119             XMLOutputter out = new XMLOutputter(format);
120             out.output(effectiveDocument, w);
121 
122             return w.toString();
123         } catch (JDOMException | IOException e) {
124             return effectiveModel;
125         }
126     }
127 
128     /**
129      * Properties which provides a sorted keySet().
130      */
131     protected static class SortedProperties extends Properties {
132         /** serialVersionUID */
133         static final long serialVersionUID = -8985316072702233744L;
134 
135         /** {@inheritDoc} */
136         @SuppressWarnings({"rawtypes", "unchecked"})
137         @Override
138         public Set<Object> keySet() {
139             Set<Object> keynames = super.keySet();
140             List list = new ArrayList(keynames);
141             Collections.sort(list);
142 
143             return new LinkedHashSet<>(list);
144         }
145     }
146 }