Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.apache.maven.plugins.help;
  
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
  * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 import java.io.File;
 import java.io.Writer;
 import java.util.Date;
 import java.util.Set;
 
Base class with common utilities to write effective Pom/settings.

Author(s):
Vincent Siveton
Version:
$Id: AbstractEffectiveMojo.java 901859 2010-01-21 21:22:59Z dennisl $
Since:
2.1
 
 public abstract class AbstractEffectiveMojo
     extends AbstractHelpMojo
 {
    
The POM XSD URL
 
     private static final String POM_XSD_URL = "http://maven.apache.org/maven-v4_0_0.xsd";

    
The Settings XSD URL
 
     private static final String SETTINGS_XSD_URL = "http://maven.apache.org/xsd/settings-1.0.0.xsd";

    
Utility method to write an XML content in a given file.

Parameters:
output is the wanted output file.
content contains the XML content to be written to the file.
encoding is the wanted encoding to use when writing file.
Throws:
java.io.IOException if any
See also:
AbstractHelpMojo.writeFile(java.io.File,java.lang.String) if encoding is null.
 
     protected static void writeXmlFileFile outputString contentString encoding )
         throws IOException
     {
         if ( output == null )
         {
             return;
         }
 
         if ( StringUtils.isEmptyencoding ) )
         {
             writeFileoutputcontent );
             return;
         }
 
         Writer out = null;
         try
         {
             output.getParentFile().mkdirs();
 
             out = WriterFactory.newXmlWriteroutput );
 
             out.writecontent );
 
             out.flush();
        }
        finally
        {
            IOUtil.closeout );
        }
    }

    
Write comments in the Effective POM/settings header.

Parameters:
writer not null
    protected static void writeHeaderXMLWriter writer )
    {
        XmlWriterUtil.writeCommentLineBreakwriter );
        XmlWriterUtil.writeCommentwriter" " );
      // Use ISO8601-format for date and time
      DateFormat dateFormat = new SimpleDateFormat"yyyy-MM-dd'T'hh:mm:ss" );
        XmlWriterUtil.writeCommentwriter"Generated by Maven Help Plugin on "
            + dateFormat.formatnew Date( System.currentTimeMillis() ) ) );
        XmlWriterUtil.writeCommentwriter"See: http://maven.apache.org/plugins/maven-help-plugin/" );
        XmlWriterUtil.writeCommentwriter" " );
        XmlWriterUtil.writeCommentLineBreakwriter );
        XmlWriterUtil.writeLineBreakwriter );
    }

    
Write comments in a normalize way.

Parameters:
writer not null
comment not null
    protected static void writeCommentXMLWriter writerString comment )
    {
        XmlWriterUtil.writeCommentLineBreakwriter );
        XmlWriterUtil.writeCommentwriter" " );
        XmlWriterUtil.writeCommentwritercomment );
        XmlWriterUtil.writeCommentwriter" " );
        XmlWriterUtil.writeCommentLineBreakwriter );
        XmlWriterUtil.writeLineBreakwriter );
    }

    
Add a Pom/Settings namespaces to the effective XML content.

Parameters:
effectiveXml not null the effective POM or Settings
isPom if true add the Pom xsd url, otherwise add the settings xsd url.
Returns:
the content of the root element, i.e. <project/> or <settings/> with the Maven namespace or the original effective if an error occurred.
See also:
POM_XSD_URL
SETTINGS_XSD_URL
    protected static String addMavenNamespaceString effectiveXmlboolean isPom )
    {
        SAXBuilder builder = new SAXBuilder();
        try
        {
            Document document = builder.buildnew StringReadereffectiveXml ) );
            Element rootElement = document.getRootElement();
            // added namespaces
            Namespace pomNamespace = Namespace.getNamespace"""http://maven.apache.org/POM/4.0.0" );
            rootElement.setNamespacepomNamespace );
            Namespace xsiNamespace = Namespace.getNamespace"xsi""http://www.w3.org/2001/XMLSchema-instance" );
            rootElement.addNamespaceDeclarationxsiNamespace );
            if ( rootElement.getAttribute"schemaLocation"xsiNamespace ) == null )
            {
                rootElement.setAttribute"schemaLocation""http://maven.apache.org/POM/4.0.0 "
                    + ( isPom ?  :  ), xsiNamespace );
            }
            ElementFilter elementFilter = new ElementFilter( Namespace.getNamespace"" ) );
            for ( Iterator i = rootElement.getDescendantselementFilter ); i.hasNext(); )
            {
                Element e = (Elementi.next();
                e.setNamespacepomNamespace );
            }
            StringWriter w = new StringWriter();
            Format format = Format.getPrettyFormat();
            XMLOutputter out = new XMLOutputterformat );
            out.outputdocument.getRootElement(), w );
            return w.toString();
        }
        catch ( JDOMException e )
        {
            return effectiveXml;
        }
        catch ( IOException e )
        {
            return effectiveXml;
        }
    }

    
Properties which provides a sorted keySet().
    protected static class SortedProperties
        extends Properties
    {
        
serialVersionUID
        static final long serialVersionUID = -8985316072702233744L;

        
        public Set keySet()
        {
            Set keynames = super.keySet();
            Vector list = new Vectorkeynames );
            Collections.sortlist );
            return new LinkedHashSetlist );
        }
    }
New to GrepCode? Check out our FAQ X