001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016
017package org.forgerock.doc.maven.pre;
018
019import org.apache.commons.io.FileUtils;
020import org.apache.commons.io.filefilter.TrueFileFilter;
021import org.apache.commons.io.filefilter.WildcardFileFilter;
022import org.apache.maven.plugin.MojoExecutionException;
023import org.forgerock.doc.maven.AbstractDocbkxMojo;
024import org.forgerock.doc.maven.utils.PngUtils;
025
026import java.io.File;
027import java.io.IOException;
028
029/**
030 * Create thumbnails of .png images in the modifiable copy of the sources.
031 *
032 * <p>
033 *
034 * This class creates resized thumbnail files prefixed with 'thumb_' in the
035 * same directory as the original image.
036 */
037public class CreateThumbs {
038
039    /**
040     * The Mojo that holds configuration and related methods.
041     */
042    private AbstractDocbkxMojo m;
043
044    /**
045     * Constructor setting the Mojo that holds the configuration.
046     *
047     * @param mojo The Mojo that holds the configuration.
048     */
049    public CreateThumbs(final AbstractDocbkxMojo mojo) {
050        m = mojo;
051    }
052
053    /**
054     * Create thumbnails of .png images in the modifiable copy of
055     * the sources.
056     *
057     * @throws org.apache.maven.plugin.MojoExecutionException Failed to edit image file.
058     */
059    public void execute() throws MojoExecutionException {
060
061        try {
062
063            for (File image : FileUtils.listFiles(
064                    m.getDocbkxModifiableSourcesDirectory(),
065                    new WildcardFileFilter("*.png"),
066                    TrueFileFilter.INSTANCE)) {
067                PngUtils.resizePng(image);
068            }
069
070        } catch (IOException e) {
071            throw new MojoExecutionException(e.getMessage(), e);
072        }
073    }
074}