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.text.DateFormat;
28  import java.text.SimpleDateFormat;
29  import java.util.Collections;
30  import java.util.Date;
31  import java.util.Iterator;
32  import java.util.LinkedHashSet;
33  import java.util.Properties;
34  import java.util.Set;
35  import java.util.Vector;
36  
37  import org.codehaus.plexus.util.IOUtil;
38  import org.codehaus.plexus.util.StringUtils;
39  import org.codehaus.plexus.util.WriterFactory;
40  import org.codehaus.plexus.util.xml.XMLWriter;
41  import org.codehaus.plexus.util.xml.XmlWriterUtil;
42  import org.jdom.Document;
43  import org.jdom.Element;
44  import org.jdom.JDOMException;
45  import org.jdom.Namespace;
46  import org.jdom.filter.ElementFilter;
47  import org.jdom.input.SAXBuilder;
48  import org.jdom.output.Format;
49  import org.jdom.output.XMLOutputter;
50  
51  /**
52   * Base class with common utilities to write effective Pom/settings.
53   *
54   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
55   * @version $Id: AbstractEffectiveMojo.java 901859 2010-01-21 21:22:59Z dennisl $
56   * @since 2.1
57   */
58  public abstract class AbstractEffectiveMojo
59      extends AbstractHelpMojo
60  {
61      /** The POM XSD URL */
62      private static final String POM_XSD_URL = "http://maven.apache.org/maven-v4_0_0.xsd";
63  
64      /** The Settings XSD URL */
65      private static final String SETTINGS_XSD_URL = "http://maven.apache.org/xsd/settings-1.0.0.xsd";
66  
67      /**
68       * Utility method to write an XML content in a given file.
69       *
70       * @param output is the wanted output file.
71       * @param content contains the XML content to be written to the file.
72       * @param encoding is the wanted encoding to use when writing file.
73       * @throws IOException if any
74       * @see AbstractHelpMojo#writeFile(File, String) if encoding is null.
75       */
76      protected static void writeXmlFile( File output, String content, String encoding )
77          throws IOException
78      {
79          if ( output == null )
80          {
81              return;
82          }
83  
84          if ( StringUtils.isEmpty( encoding ) )
85          {
86              writeFile( output, content );
87              return;
88          }
89  
90          Writer out = null;
91          try
92          {
93              output.getParentFile().mkdirs();
94  
95              out = WriterFactory.newXmlWriter( output );
96  
97              out.write( content );
98  
99              out.flush();
100         }
101         finally
102         {
103             IOUtil.close( out );
104         }
105     }
106 
107     /**
108      * Write comments in the Effective POM/settings header.
109      *
110      * @param writer not null
111      */
112     protected static void writeHeader( XMLWriter writer )
113     {
114         XmlWriterUtil.writeCommentLineBreak( writer );
115         XmlWriterUtil.writeComment( writer, " " );
116       // Use ISO8601-format for date and time
117       DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
118         XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on "
119             + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
120         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
121         XmlWriterUtil.writeComment( writer, " " );
122         XmlWriterUtil.writeCommentLineBreak( writer );
123 
124         XmlWriterUtil.writeLineBreak( writer );
125     }
126 
127     /**
128      * Write comments in a normalize way.
129      *
130      * @param writer not null
131      * @param comment not null
132      */
133     protected static void writeComment( XMLWriter writer, String comment )
134     {
135         XmlWriterUtil.writeCommentLineBreak( writer );
136         XmlWriterUtil.writeComment( writer, " " );
137         XmlWriterUtil.writeComment( writer, comment );
138         XmlWriterUtil.writeComment( writer, " " );
139         XmlWriterUtil.writeCommentLineBreak( writer );
140 
141         XmlWriterUtil.writeLineBreak( writer );
142     }
143 
144     /**
145      * Add a Pom/Settings namespaces to the effective XML content.
146      *
147      * @param effectiveXml not null the effective POM or Settings
148      * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
149      * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace
150      * or the original <code>effective</code> if an error occurred.
151      * @see #POM_XSD_URL
152      * @see #SETTINGS_XSD_URL
153      */
154     protected static String addMavenNamespace( String effectiveXml, boolean isPom )
155     {
156         SAXBuilder builder = new SAXBuilder();
157 
158         try
159         {
160             Document document = builder.build( new StringReader( effectiveXml ) );
161             Element rootElement = document.getRootElement();
162 
163             // added namespaces
164             Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/4.0.0" );
165             rootElement.setNamespace( pomNamespace );
166 
167             Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
168             rootElement.addNamespaceDeclaration( xsiNamespace );
169             if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null )
170             {
171                 rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/4.0.0 "
172                     + ( isPom ? POM_XSD_URL : SETTINGS_XSD_URL ), xsiNamespace );
173             }
174 
175             ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace( "" ) );
176             for ( Iterator i = rootElement.getDescendants( elementFilter ); i.hasNext(); )
177             {
178                 Element e = (Element) i.next();
179                 e.setNamespace( pomNamespace );
180             }
181 
182             StringWriter w = new StringWriter();
183             Format format = Format.getPrettyFormat();
184             XMLOutputter out = new XMLOutputter( format );
185             out.output( document.getRootElement(), w );
186 
187             return w.toString();
188         }
189         catch ( JDOMException e )
190         {
191             return effectiveXml;
192         }
193         catch ( IOException e )
194         {
195             return effectiveXml;
196         }
197     }
198 
199     /**
200      * Properties which provides a sorted keySet().
201      */
202     protected static class SortedProperties
203         extends Properties
204     {
205         /** serialVersionUID */
206         static final long serialVersionUID = -8985316072702233744L;
207 
208         /** {@inheritDoc} */
209         public Set keySet()
210         {
211             Set keynames = super.keySet();
212             Vector list = new Vector( keynames );
213             Collections.sort( list );
214 
215             return new LinkedHashSet( list );
216         }
217     }
218 }