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.apache.commons.lang3.time.DateFormatUtils;
35  import org.codehaus.plexus.util.IOUtil;
36  import org.codehaus.plexus.util.WriterFactory;
37  import org.codehaus.plexus.util.xml.XMLWriter;
38  import org.codehaus.plexus.util.xml.XmlWriterUtil;
39  import org.jdom2.Document;
40  import org.jdom2.JDOMException;
41  import org.jdom2.input.SAXBuilder;
42  import org.jdom2.output.Format;
43  import org.jdom2.output.XMLOutputter;
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          Writer out = null;
71          try
72          {
73              output.getParentFile().mkdirs();
74  
75              out = WriterFactory.newXmlWriter( output );
76  
77              out.write( content );
78  
79              out.close();
80              out = null;
81          }
82          finally
83          {
84              IOUtil.close( out );
85          }
86      }
87  
88      /**
89       * Write comments in the Effective POM/settings header.
90       *
91       * @param writer not null
92       */
93      protected static void writeHeader( XMLWriter writer )
94      {
95          XmlWriterUtil.writeCommentLineBreak( writer );
96          XmlWriterUtil.writeComment( writer, " " );
97          // Use ISO 8601 format for date and time
98          String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
99              .format( System.currentTimeMillis() );
100         XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on " + formattedDateTime );
101         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
102         XmlWriterUtil.writeComment( writer, " " );
103         XmlWriterUtil.writeCommentLineBreak( writer );
104     }
105 
106     /**
107      * Write comments in a normalize way.
108      *
109      * @param writer not null
110      * @param comment not null
111      */
112     protected static void writeComment( XMLWriter writer, String comment )
113     {
114         XmlWriterUtil.writeCommentLineBreak( writer );
115         XmlWriterUtil.writeComment( writer, " " );
116         XmlWriterUtil.writeComment( writer, comment );
117         XmlWriterUtil.writeComment( writer, " " );
118         XmlWriterUtil.writeCommentLineBreak( writer );
119     }
120 
121     /**
122      * @param effectiveModel not null
123      * @param encoding not null
124      * @param omitDeclaration whether the XML declaration should be omitted from the effective pom
125      * @return pretty format of the xml or the original {@code effectiveModel} if an error occurred.
126      */
127     protected static String prettyFormat( String effectiveModel, String encoding, boolean omitDeclaration )
128     {
129         SAXBuilder builder = new SAXBuilder();
130 
131         try
132         {
133             Document effectiveDocument = builder.build( new StringReader( effectiveModel ) );
134 
135             StringWriter w = new StringWriter();
136             Format format = Format.getPrettyFormat();
137             if ( encoding != null )
138             {
139                 // This is a design flaw in JDOM, no NPE on null arguments, but null is not prohibited
140                 format.setEncoding( encoding );
141             }
142             format.setLineSeparator( System.lineSeparator() );
143             format.setOmitDeclaration( omitDeclaration );
144             XMLOutputter out = new XMLOutputter( format );
145             out.output( effectiveDocument, w );
146 
147             return w.toString();
148         }
149         catch ( JDOMException e )
150         {
151             return effectiveModel;
152         }
153         catch ( IOException e )
154         {
155             return effectiveModel;
156         }
157     }
158 
159     /**
160      * Properties which provides a sorted keySet().
161      */
162     protected static class SortedProperties
163         extends Properties
164     {
165         /** serialVersionUID */
166         static final long serialVersionUID = -8985316072702233744L;
167 
168         /** {@inheritDoc} */
169         @SuppressWarnings( { "rawtypes", "unchecked" } )
170         public Set<Object> keySet()
171         {
172             Set<Object> keynames = super.keySet();
173             List list = new ArrayList( keynames );
174             Collections.sort( list );
175 
176             return new LinkedHashSet<Object>( list );
177         }
178     }
179 }