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 2013-2014 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 * Set DPI on .png images in the modifiable copy of the sources.
031 *
032 * <p>
033 *
034 * This class transforms the .png images in place.
035 */
036public class Dpi {
037
038    /**
039     * The Mojo that holds configuration and related methods.
040     */
041    private AbstractDocbkxMojo m;
042
043    /**
044     * Constructor setting the Mojo that holds the configuration.
045     *
046     * @param mojo The Mojo that holds the configuration.
047     */
048    public Dpi(final AbstractDocbkxMojo mojo) {
049        m = mojo;
050    }
051
052    /**
053     * Set DPI on .png images in the modifiable copy of the sources.
054     * Default: 160 DPI.
055     *
056     * @throws MojoExecutionException Failed to edit image file.
057     */
058    public void execute() throws MojoExecutionException {
059
060        try {
061
062            for (File image : FileUtils.listFiles(
063                    m.getDocbkxModifiableSourcesDirectory(),
064                    new WildcardFileFilter("*.png"),
065                    TrueFileFilter.INSTANCE)) {
066                PngUtils.setSafeDpi(image, m.getMaxImageHeightInInches());
067            }
068
069        } catch (IOException e) {
070            throw new MojoExecutionException(e.getMessage(), e);
071        }
072    }
073}