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