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 2012-2014 ForgeRock AS
015 */
016
017package org.forgerock.doc.maven.release;
018
019import org.apache.commons.io.FileUtils;
020import org.apache.maven.plugin.MojoExecutionException;
021import org.forgerock.doc.maven.AbstractDocbkxMojo;
022
023import java.io.File;
024
025/**
026 * Rename PDF files in the release layout.
027 */
028public class PdfNames {
029
030    /**
031     * The Mojo that holds configuration and related methods.
032     */
033    private AbstractDocbkxMojo m;
034
035    /**
036     * Constructor setting the Mojo that holds the configuration.
037     *
038     * @param mojo The Mojo that holds the configuration.
039     */
040    public PdfNames(final AbstractDocbkxMojo mojo) {
041        m = mojo;
042    }
043
044    /**
045     * Rename PDF files in the release layout.
046     *
047     * @throws MojoExecutionException Failed to rename files.
048     */
049    public void execute() throws MojoExecutionException {
050        final File dir = new File(m.getReleaseVersionPath());
051        final String[] ext = {"pdf"};
052
053        for (File pdf : FileUtils.listFiles(dir, ext, false)) { // Not recursive
054            String name = pdf.getName().replaceFirst("-", "-" + m.getReleaseVersion() + "-");
055            if (!pdf.renameTo(new File(pdf.getParent() + File.separator + name))) {
056                throw new MojoExecutionException("Failed to rename PDF: " + name);
057            }
058        }
059    }
060}