/* * (C) Copyright 2002-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser General Public License * (LGPL) version 2.1 which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * This library 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 GNU * Lesser General Public License for more details. * * Contributors: * Nuxeo - initial API and implementation * */ package org.nuxeo.ecm.platform.convert.plugins; import java.io.File; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.nuxeo.common.utils.Path; import org.nuxeo.ecm.core.api.Blob; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.blobholder.BlobHolder; import org.nuxeo.ecm.core.api.impl.blob.FileBlob; import org.nuxeo.ecm.core.convert.api.ConversionException; import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder; import org.nuxeo.ecm.core.convert.extension.Converter; import org.nuxeo.ecm.platform.commandline.executor.api.CmdParameters; /** * Pdf2Html converter based on the pdftohtml command-line executable. * * @author tiry */ public class PDF2HtmlConverter extends CommandLineBasedConverter { @Override protected BlobHolder buildResult(List cmdOutput, CmdParameters cmdParams) { String outputPath = cmdParams.getParameters().get("outDirPath"); File outputDir = new File(outputPath); File[] files = outputDir.listFiles(); List blobs = new ArrayList(); for (File file : files) { Blob blob = new FileBlob(file); blob.setFilename(file.getName()); if (file.getName().equalsIgnoreCase("index.html")) { blobs.add(0, blob); } else { blobs.add(blob); } } return new SimpleCachableBlobHolder(blobs); } @Override protected Map getCmdBlobParameters(BlobHolder blobHolder, Map parameters) throws ConversionException { Map cmdBlobParams = new HashMap(); try { cmdBlobParams.put("inFilePath", blobHolder.getBlob()); } catch (ClientException e) { throw new ConversionException("Unable to get Blob for holder", e); } return cmdBlobParams; } @Override protected Map getCmdStringParameters(BlobHolder blobHolder, Map parameters) throws ConversionException { Map cmdStringParams = new HashMap(); String baseDir = getTmpDirectory(parameters); Path tmpPath = new Path(baseDir).append("pdf2html_" + System.currentTimeMillis()); File outDir = new File(tmpPath.toString()); boolean dirCreated = outDir.mkdir(); if (!dirCreated) { throw new ConversionException("Unable to create tmp dir for transformer output"); } if (System.getProperty("os.name").toLowerCase().contains("windows")) { cmdStringParams.put("outDirPath", outDir.getAbsolutePath().replace("\\", "\\\\")); } else { cmdStringParams.put("outDirPath", outDir.getAbsolutePath()); } return cmdStringParams; } }