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.util.Collections;
 29  
 import java.util.Date;
 30  
 import java.util.Iterator;
 31  
 import java.util.LinkedHashSet;
 32  
 import java.util.Locale;
 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 689770 2008-08-28 09:47:18Z vsiveton $
 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  0
         DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.US );
 117  0
         XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on "
 118  
             + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
 119  0
         XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" );
 120  0
         XmlWriterUtil.writeComment( writer, " " );
 121  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 122  
 
 123  0
         XmlWriterUtil.writeLineBreak( writer );
 124  0
     }
 125  
 
 126  
     /**
 127  
      * Write comments in a normalize way.
 128  
      *
 129  
      * @param writer not null
 130  
      * @param comment not null
 131  
      */
 132  
     protected static void writeComment( XMLWriter writer, String comment )
 133  
     {
 134  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 135  0
         XmlWriterUtil.writeComment( writer, " " );
 136  0
         XmlWriterUtil.writeComment( writer, comment );
 137  0
         XmlWriterUtil.writeComment( writer, " " );
 138  0
         XmlWriterUtil.writeCommentLineBreak( writer );
 139  
 
 140  0
         XmlWriterUtil.writeLineBreak( writer );
 141  0
     }
 142  
 
 143  
     /**
 144  
      * Add a Pom/Settings namespaces to the effective XML content.
 145  
      *
 146  
      * @param effectiveXml not null the effective POM or Settings
 147  
      * @param isPom if <code>true</code> add the Pom xsd url, otherwise add the settings xsd url.
 148  
      * @return the content of the root element, i.e. &lt;project/&gt; or &lt;settings/&gt; with the Maven namespace
 149  
      * or the original <code>effective</code> if an error occurred.
 150  
      * @see #POM_XSD_URL
 151  
      * @see #SETTINGS_XSD_URL
 152  
      */
 153  
     protected static String addMavenNamespace( String effectiveXml, boolean isPom )
 154  
     {
 155  0
         SAXBuilder builder = new SAXBuilder();
 156  
 
 157  
         try
 158  
         {
 159  0
             Document document = builder.build( new StringReader( effectiveXml ) );
 160  0
             Element rootElement = document.getRootElement();
 161  
 
 162  
             // added namespaces
 163  0
             Namespace pomNamespace = Namespace.getNamespace( "", "http://maven.apache.org/POM/4.0.0" );
 164  0
             rootElement.setNamespace( pomNamespace );
 165  
 
 166  0
             Namespace xsiNamespace = Namespace.getNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
 167  0
             rootElement.addNamespaceDeclaration( xsiNamespace );
 168  0
             if ( rootElement.getAttribute( "schemaLocation", xsiNamespace ) == null )
 169  
             {
 170  0
                 rootElement.setAttribute( "schemaLocation", "http://maven.apache.org/POM/4.0.0 "
 171  
                     + ( isPom ? POM_XSD_URL : SETTINGS_XSD_URL ), xsiNamespace );
 172  
             }
 173  
 
 174  0
             ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace( "" ) );
 175  0
             for ( Iterator i = rootElement.getDescendants( elementFilter ); i.hasNext(); )
 176  
             {
 177  0
                 Element e = (Element) i.next();
 178  0
                 e.setNamespace( pomNamespace );
 179  0
             }
 180  
 
 181  0
             StringWriter w = new StringWriter();
 182  0
             Format format = Format.getPrettyFormat();
 183  0
             XMLOutputter out = new XMLOutputter( format );
 184  0
             out.output( document.getRootElement(), w );
 185  
 
 186  0
             return w.toString();
 187  
         }
 188  0
         catch ( JDOMException e )
 189  
         {
 190  0
             return effectiveXml;
 191  
         }
 192  0
         catch ( IOException e )
 193  
         {
 194  0
             return effectiveXml;
 195  
         }
 196  
     }
 197  
 
 198  
     /**
 199  
      * Properties which provides a sorted keySet().
 200  
      */
 201  0
     protected static class SortedProperties
 202  
         extends Properties
 203  
     {
 204  
         /** serialVersionUID */
 205  
         static final long serialVersionUID = -8985316072702233744L;
 206  
 
 207  
         /** {@inheritDoc} */
 208  
         public Set keySet()
 209  
         {
 210  0
             Set keynames = super.keySet();
 211  0
             Vector list = new Vector( keynames );
 212  0
             Collections.sort( list );
 213  
 
 214  0
             return new LinkedHashSet( list );
 215  
         }
 216  
     }
 217  
 }