Coverage Report - org.apache.maven.plugins.help.AbstractEffectiveMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractEffectiveMojo
0%
0/54
0%
0/10
3
AbstractEffectiveMojo$SortedProperties
0%
0/5
N/A
3
 
 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  0
 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  0
         if ( output == null )
 80  
         {
 81  0
             return;
 82  
         }
 83  
 
 84  0
         if ( StringUtils.isEmpty( encoding ) )
 85  
         {
 86  0
             writeFile( output, content );
 87  0
             return;
 88  
         }
 89  
 
 90  0
         Writer out = null;
 91  
         try
 92  
         {
 93  0
             output.getParentFile().mkdirs();
 94  
 
 95  0
             out = WriterFactory.newXmlWriter( output );
 96  
 
 97  0
             out.write( content );
 98  
 
 99  0
             out.flush();
 100  
         }
 101  
         finally
 102  
         {
 103  0
             IOUtil.close( out );
 104  0
         }
 105  0
     }
 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  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 115  0
         XmlWriterUtil.writeComment( writer, " " );
 116  
       // Use ISO8601-format for date and time
 117  0
       DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd'T'hh:mm:ss" );
 118  0
         XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on "
 119  
             + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
 120  0
         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
 121  0
         XmlWriterUtil.writeComment( writer, " " );
 122  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 123  
 
 124  0
         XmlWriterUtil.writeLineBreak( writer );
 125  0
     }
 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  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 136  0
         XmlWriterUtil.writeComment( writer, " " );
 137  0
         XmlWriterUtil.writeComment( writer, comment );
 138  0
         XmlWriterUtil.writeComment( writer, " " );
 139  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 140  
 
 141  0
         XmlWriterUtil.writeLineBreak( writer );
 142  0
     }
 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  0
         SAXBuilder builder = new SAXBuilder();
 157  
 
 158  
         try
 159  
         {
 160  0
             Document document = builder.build( new StringReader( effectiveXml ) );
 161  0
             Element rootElement = document.getRootElement();
 162  
 
 163  
             // added namespaces
 164  0
             Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/4.0.0" );
 165  0
             rootElement.setNamespace( pomNamespace );
 166  
 
 167  0
             Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 168  0
             rootElement.addNamespaceDeclaration( xsiNamespace );
 169  0
             if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null )
 170  
             {
 171  0
                 rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/4.0.0 "
 172  
                     + ( isPom ? POM_XSD_URL : SETTINGS_XSD_URL ), xsiNamespace );
 173  
             }
 174  
 
 175  0
             ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace( "" ) );
 176  0
             for ( Iterator i = rootElement.getDescendants( elementFilter ); i.hasNext(); )
 177  
             {
 178  0
                 Element e = (Element) i.next();
 179  0
                 e.setNamespace( pomNamespace );
 180  0
             }
 181  
 
 182  0
             StringWriter w = new StringWriter();
 183  0
             Format format = Format.getPrettyFormat();
 184  0
             XMLOutputter out = new XMLOutputter( format );
 185  0
             out.output( document.getRootElement(), w );
 186  
 
 187  0
             return w.toString();
 188  
         }
 189  0
         catch ( JDOMException e )
 190  
         {
 191  0
             return effectiveXml;
 192  
         }
 193  0
         catch ( IOException e )
 194  
         {
 195  0
             return effectiveXml;
 196  
         }
 197  
     }
 198  
 
 199  
     /**
 200  
      * Properties which provides a sorted keySet().
 201  
      */
 202  0
     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  0
             Set keynames = super.keySet();
 212  0
             Vector list = new Vector( keynames );
 213  0
             Collections.sort( list );
 214  
 
 215  0
             return new LinkedHashSet( list );
 216  
         }
 217  
     }
 218  
 }