All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.nicoulaj.maven.plugins.checksum.execution.target.XmlSummaryFileTarget Maven / Gradle / Ivy

/*-
 * ====================================================================
 * checksum-maven-plugin
 * ====================================================================
 * Copyright (C) 2010 - 2016 Julien Nicoulaud 
 * ====================================================================
 * Licensed 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.
 * ====================================================================
 */

package net.nicoulaj.maven.plugins.checksum.execution.target;


import net.nicoulaj.maven.plugins.checksum.artifacts.ArtifactListener;
import net.nicoulaj.maven.plugins.checksum.mojo.ChecksumFile;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.xml.PrettyPrintXMLWriter;

import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * An {@link net.nicoulaj.maven.plugins.checksum.execution.target.ExecutionTarget} that writes digests to an XML file.
 *
 * @author Julien Nicoulaud
 * @since 1.0
 * @version $Id: $Id
 */
public class XmlSummaryFileTarget
    implements ExecutionTarget
{
    /**
     * The number of spaces used to indent the output file.
     */
    public static final int XML_INDENTATION_SIZE = 2;

    /**
     * Encoding to use for generated files.
     */
    protected final String encoding;

    /**
     * The association file => (algorithm,hashcode).
     */
    protected Map> filesHashcodes;

    /**
     * The target file where the summary is written.
     */
    protected final File summaryFile;

    /**
     * List of listeners which are notified every time a XML file is created.
     *
     * @since 1.3
     */
    protected final Iterable artifactListeners;

    /**
     * Build a new instance of {@link net.nicoulaj.maven.plugins.checksum.execution.target.XmlSummaryFileTarget}.
     *
     * @param summaryFile the file to which the summary should be written.
     * @param encoding    the encoding to use for generated files.
     * @param artifactListeners listeners which are notified every time a CSV file is created
     */
    public XmlSummaryFileTarget(File summaryFile, String encoding, Iterable artifactListeners)
    {
        this.summaryFile = summaryFile;
        this.encoding = encoding;
        this.artifactListeners = artifactListeners;
    }

    /**
     * {@inheritDoc}
     */
    public void init()
    {
        filesHashcodes = new HashMap<>();
    }

    /** {@inheritDoc} */
    public void write( String digest, ChecksumFile file, String algorithm )
    {
        // Initialize an entry for the file if needed.
        if ( !filesHashcodes.containsKey( file ) )
        {
            filesHashcodes.put( file, new HashMap() );
        }

        // Store the algorithm => hashcode mapping for this file.
        Map fileHashcodes = filesHashcodes.get( file );
        fileHashcodes.put( algorithm, digest );
    }

    /** {@inheritDoc} */
    public void close(final String subPath)
        throws ExecutionTargetCloseException
    {
        // Make sure the parent directory exists.
        try
        {
            Files.createDirectories( summaryFile.getParentFile().toPath() );
        }
        catch ( IOException e )
        {
            throw new ExecutionTargetCloseException( "Could not create summary file parent directory", e );
        }

        // Open the target file.
        Writer outputStream;
        try
        {
            outputStream = new OutputStreamWriter( new FileOutputStream( summaryFile ), encoding );
        }
        catch ( FileNotFoundException | UnsupportedEncodingException e )
        {
            throw new ExecutionTargetCloseException( "Failed writing to output summary file", e );
        }

        // Write in sorted order (per globing argument)
        @SuppressWarnings("unchecked")
        Map.Entry>[] entries = filesHashcodes.entrySet().toArray((Map.Entry>[]) new Map.Entry[0] );
        Arrays.sort(entries, new Comparator>>() {
            @Override
            public int compare(Map.Entry> o1, Map.Entry> o2) {
                ChecksumFile f1 = o1.getKey();
                ChecksumFile f2 = o2.getKey();
                return f1.getRelativePath(f1, subPath).compareTo(f2.getRelativePath(f2, subPath));
            }
        });

        // Output hashcodes formatted in XML.
        try
        {
            PrettyPrintXMLWriter xmlWriter =
                new PrettyPrintXMLWriter( outputStream, StringUtils.repeat( " ", XML_INDENTATION_SIZE ) );
            xmlWriter.startElement( "files" );
            for ( Map.Entry> entry : entries )
            {
                ChecksumFile file = entry.getKey();
                xmlWriter.startElement( "file" );
                xmlWriter.addAttribute( "name", file.getRelativePath(file, subPath) );
                Map fileHashcodes = entry.getValue();
                for ( String algorithm : fileHashcodes.keySet() )
                {
                    xmlWriter.startElement( "hashcode" );
                    xmlWriter.addAttribute( "algorithm", algorithm );
                    xmlWriter.writeText( fileHashcodes.get( algorithm ) );
                    xmlWriter.endElement();
                }
                xmlWriter.endElement();
            }
            xmlWriter.endElement();
        }
        catch ( IOException e )
        {
            throw new ExecutionTargetCloseException( "Failed writing to output summary file", e );
        }

        // Close the target file.
        try
        {
            outputStream.close();
            for (ArtifactListener artifactListener : artifactListeners) {
                artifactListener.artifactCreated(summaryFile, "xml", null,null);
            }
        }
        catch ( IOException e )
        {
            throw new ExecutionTargetCloseException( "Failed writing to output summary file", e );
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy