Package com.artofsolving.jodconverter.maven

Source Code of com.artofsolving.jodconverter.maven.ConvertMojo

/*
* ConvertDocumentMojo.java
*
* This file is part of jodconverter-maven-plugin.
*
* It is free software; you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* It is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.artofsolving.jodconverter.maven;

import java.io.File;
import java.net.ConnectException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

/**
* Convert documents using JODConverter and OpenOffice.org.
* <p>
* Sample configuration:
* <pre>
*   &lt;plugin&gt;
*     &lt;groupId&gt;com.artofsolving&lt;/groupId&gt;
*     &lt;artifactId&gt;jodconverter-maven-plugin&lt;/artifactId&gt;
*     &lt;configuration&gt;
*       &lt;sourceDirectory&gt;${basedir}/src/site/resources&lt;/sourceDirectory&gt;
*       &lt;include&gt;**\*.odt&lt;/include&gt;
*       &lt;outputDirectory&gt;${basedir}/target/site&lt;/outputDirectory&gt;
*       &lt;outputFormat&gt;pdf&lt;/outputFormat&gt;
*     &lt;/configuration&gt;
*   &lt;/plugin&gt;
* </pre>
*
* @goal convert
*/
public class ConvertMojo extends AbstractMojo {

    private static final String COMMA_AND_OR_WHITESPACE = "[,\\s]\\s*";

    /**
     * Source directory
     *
     * @parameter expression="${sourceDirectory}"
     * @required
     */
    private File sourceDirectory;

    /**
     * Output directory
     *
     * @parameter expression="${outputDirectory}"
     * @required
     */
    private File outputDirectory;

    /**
     * Include
     *
     * @parameter expression="${include}"
     * @required
     */
    private String include;

    /**
     * Output format
     *
     * @parameter expression="${outputFormat}"
     * @required
     */
    private String outputFormat;
   
    /**
     * OpenOffice.org port number
     *
     * @parameter expression="${port}" default-value=8100
     */
    private int port = 8100;

    public void execute() throws MojoExecutionException, MojoFailureException {
        String[] sourceNames = getIncludedFiles();
        if (sourceNames.length == 0) {
            getLog().info("No documents to convert.");
            return;
        }
       
        String[] outputFormats = outputFormat.split(COMMA_AND_OR_WHITESPACE);
        if (outputFormats.length == 0) {
            getLog().warn("No outputFormat specified.");
            return;
        }
       
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(port);
        try {
            connection.connect();
        } catch (ConnectException connectException) {
            throw new MojoExecutionException(
                "Could not connect to OpenOffice.org on port " + port + ". " +
                "Start with: soffice -accept=\"socket,host=127.0.0.1,port=" + port + ";urp;\"", connectException);
        }
       
        getLog().info("Converting documents from " + sourceDirectory + " to " + outputDirectory + " ...");
        try {
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            for (int inputIndex = 0; inputIndex < sourceNames.length; inputIndex++) {
                String inputName = sourceNames[inputIndex];
                File inputFile = FileUtils.resolveFile(sourceDirectory, inputName);
                for (int formatIndex = 0; formatIndex < outputFormats.length; formatIndex++) {
                    String format = outputFormats[formatIndex];
                    String outputName = FileUtils.removeExtension(inputName) + "." + format;
                    File outputFile = FileUtils.resolveFile(outputDirectory, outputName);
                    getLog().info("converting " + inputName + " to " + outputName);
                    converter.convert(inputFile, outputFile);
                }
            }
        } finally {
            connection.disconnect();
        }
    }

    private String[] getIncludedFiles() {
        String[] includes = include.split(COMMA_AND_OR_WHITESPACE);
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(sourceDirectory);
        scanner.setIncludes(includes);
        scanner.scan();
        return scanner.getIncludedFiles();
    }

}
TOP

Related Classes of com.artofsolving.jodconverter.maven.ConvertMojo

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.