| 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.IOException; |
| import java.io.StringReader; |
| import java.io.StringWriter; |
| import java.io.Writer; |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.LinkedHashSet; |
| import java.util.List; |
| import java.util.Properties; |
| import java.util.Set; |
| |
| import org.apache.commons.lang3.time.DateFormatUtils; |
| import org.codehaus.plexus.util.IOUtil; |
| import org.codehaus.plexus.util.WriterFactory; |
| import org.codehaus.plexus.util.xml.XMLWriter; |
| import org.codehaus.plexus.util.xml.XmlWriterUtil; |
| import org.jdom.Document; |
| import org.jdom.JDOMException; |
| import org.jdom.input.SAXBuilder; |
| import org.jdom.output.Format; |
| import org.jdom.output.XMLOutputter; |
| |
| /** |
| * Base class with common utilities to write effective Pom/settings. |
| * |
| * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> |
| * @version $Id$ |
| * @since 2.1 |
| */ |
| public abstract class AbstractEffectiveMojo |
| extends AbstractHelpMojo |
| { |
| /** |
| * Utility method to write an XML content in a given file. |
| * |
| * @param output is the wanted output file. |
| * @param content contains the XML content to be written to the file. |
| * @param encoding is the wanted encoding to use when writing file. |
| * @throws IOException if any |
| * @see AbstractHelpMojo#writeFile(File, String) if encoding is null. |
| */ |
| protected static void writeXmlFile( File output, String content ) |
| throws IOException |
| { |
| if ( output == null ) |
| { |
| return; |
| } |
| |
| Writer out = null; |
| try |
| { |
| output.getParentFile().mkdirs(); |
| |
| out = WriterFactory.newXmlWriter( output ); |
| |
| out.write( content ); |
| |
| out.close(); |
| out = null; |
| } |
| finally |
| { |
| IOUtil.close( out ); |
| } |
| } |
| |
| /** |
| * Write comments in the Effective POM/settings header. |
| * |
| * @param writer not null |
| */ |
| protected static void writeHeader( XMLWriter writer ) |
| { |
| XmlWriterUtil.writeCommentLineBreak( writer ); |
| XmlWriterUtil.writeComment( writer, " " ); |
| // Use ISO 8601 format for date and time |
| String formattedDateTime = DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT |
| .format( System.currentTimeMillis() ); |
| XmlWriterUtil.writeComment( writer, "Generated by Maven Help Plugin on " + formattedDateTime ); |
| XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-help-plugin/" ); |
| XmlWriterUtil.writeComment( writer, " " ); |
| XmlWriterUtil.writeCommentLineBreak( writer ); |
| } |
| |
| /** |
| * Write comments in a normalize way. |
| * |
| * @param writer not null |
| * @param comment not null |
| */ |
| protected static void writeComment( XMLWriter writer, String comment ) |
| { |
| XmlWriterUtil.writeCommentLineBreak( writer ); |
| XmlWriterUtil.writeComment( writer, " " ); |
| XmlWriterUtil.writeComment( writer, comment ); |
| XmlWriterUtil.writeComment( writer, " " ); |
| XmlWriterUtil.writeCommentLineBreak( writer ); |
| } |
| |
| /** |
| * @param effectiveModel not null |
| * @param encoding not null |
| * @param omitDeclaration whether the XML declaration should be omitted from the effective pom |
| * @return pretty format of the xml or the original {@code effectiveModel} if an error occurred. |
| */ |
| protected static String prettyFormat( String effectiveModel, String encoding, boolean omitDeclaration ) |
| { |
| SAXBuilder builder = new SAXBuilder(); |
| |
| try |
| { |
| Document effectiveDocument = builder.build( new StringReader( effectiveModel ) ); |
| |
| StringWriter w = new StringWriter(); |
| Format format = Format.getPrettyFormat(); |
| if ( encoding != null ) |
| { |
| // This is a design flaw in JDOM, no NPE on null arguments, but null is not prohibited |
| format.setEncoding( encoding ); |
| } |
| format.setLineSeparator( System.lineSeparator() ); |
| format.setOmitDeclaration( omitDeclaration ); |
| XMLOutputter out = new XMLOutputter( format ); |
| out.output( effectiveDocument, w ); |
| |
| return w.toString(); |
| } |
| catch ( JDOMException e ) |
| { |
| return effectiveModel; |
| } |
| catch ( IOException e ) |
| { |
| return effectiveModel; |
| } |
| } |
| |
| /** |
| * Properties which provides a sorted keySet(). |
| */ |
| protected static class SortedProperties |
| extends Properties |
| { |
| /** serialVersionUID */ |
| static final long serialVersionUID = -8985316072702233744L; |
| |
| /** {@inheritDoc} */ |
| @SuppressWarnings( { "rawtypes", "unchecked" } ) |
| public Set<Object> keySet() |
| { |
| Set<Object> keynames = super.keySet(); |
| List list = new ArrayList( keynames ); |
| Collections.sort( list ); |
| |
| return new LinkedHashSet<Object>( list ); |
| } |
| } |
| } |