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