Fixed ALFCOM-2285: swf2pdf command is hard-coded

- Added toMap() method to TransformationOptions
 - toMap() of SWFTransformationOptions adds 'flashVersion' property
 - RuntimeExecutableContentTransformer uses toMap() to override default properties
 - Changed 'transformer.Pdf2swf' bean to use RuntimeExecutableContentTransformer
 - Removed PDFToSWFContentTransformer


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@12623 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-01-08 12:55:20 +00:00
parent 857aff414c
commit c16bdad03f
10 changed files with 99 additions and 338 deletions

View File

@@ -222,15 +222,12 @@ public class RuntimeExecutableContentTransformer extends AbstractContentTransfor
Map<String, String> properties = new HashMap<String, String>(5);
// copy options over
if (options instanceof RuntimeExecutableContentTransformerOptions)
Map<String, Object> optionsMap = options.toMap();
for (Map.Entry<String, Object> entry : optionsMap.entrySet())
{
RuntimeExecutableContentTransformerOptions runtimeOptions = (RuntimeExecutableContentTransformerOptions)options;
for (Map.Entry<String, Object> entry : runtimeOptions.getPropertyValues().entrySet())
{
String key = entry.getKey();
Object value = entry.getValue();
properties.put(key, (value == null ? null : value.toString()));
}
String key = entry.getKey();
Object value = entry.getValue();
properties.put(key, (value == null ? null : value.toString()));
}
// add the source and target properties
properties.put(VAR_SOURCE, sourceFile.getAbsolutePath());

View File

@@ -40,25 +40,27 @@ import org.alfresco.service.cmr.repository.TransformationOptions;
public class RuntimeExecutableContentTransformerOptions extends TransformationOptions
{
/** Map of property values */
private Map<String, Object> propertyValues = new HashMap<String, Object>(1);
private Map<String, String> propertyValues = new HashMap<String, String>(11);
/**
* Sets the map of property values that are used when executing the transformer
*
* @param propertyValues property value
*/
public void setPropertyValues(Map<String, Object> propertyValues)
public void setPropertyValues(Map<String, String> propertyValues)
{
this.propertyValues = propertyValues;
}
/**
* Gets the map of property values that are used when executing the transformer
*
* @return Map<String, Object> property values
* Overrides the base class implementation to add all values set in {@link #setPropertyValues(Map)}
*/
public Map<String, Object> getPropertyValues()
@Override
public Map<String, Object> toMap()
{
return propertyValues;
Map<String, Object> baseProps = super.toMap();
Map<String, Object> props = new HashMap<String, Object>(baseProps);
props.putAll(propertyValues);
return props;
}
}

View File

@@ -70,9 +70,11 @@ public class ImageMagickContentTransformerTest extends AbstractContentTransforme
{
return;
}
boolean reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_TEXT_PLAIN, new TransformationOptions());
boolean reliability = transformer.isTransformable(
MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_TEXT_PLAIN, new TransformationOptions());
assertEquals("Mimetype should not be supported", false, reliability);
reliability = transformer.isTransformable(MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_IMAGE_JPEG, new TransformationOptions());
reliability = transformer.isTransformable(
MimetypeMap.MIMETYPE_IMAGE_GIF, MimetypeMap.MIMETYPE_IMAGE_JPEG, new TransformationOptions());
assertEquals("Mimetype should be supported", true, reliability);
}
}

View File

@@ -1,273 +0,0 @@
/*
* Copyright (C) 2005-2008 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have received a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.content.transform.swf;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.content.transform.AbstractContentTransformer2;
import org.alfresco.service.cmr.repository.ContentIOException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.alfresco.util.TempFileProvider;
import org.alfresco.util.exec.RuntimeExec;
import org.alfresco.util.exec.RuntimeExec.ExecutionResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* PDF to SWF content transformer
*
* @author Roy Wetherall
*/
public class PDFToSWFContentTransformer extends AbstractContentTransformer2
{
/** Logger */
private static final Log logger = LogFactory.getLog(PDFToSWFContentTransformer.class);
/** Used to indicate whether the transformaer in available or not */
private boolean available = false;
/** Stores the output from the check command */
private String versionString;
/** Check and transform command */
private RuntimeExec checkCommand;
private RuntimeExec transformCommand;
/** Default flash version to convert to */
private static final String DEFAULT_FLASH_VERSION = "9";
/** Check command string */
private static final String PDF2SWF_CHECK = "pdf2swf -V";
/** Transformation command string */
private static final String PDF2SWF_COMMAND = "pdf2swf -t ${flashVersion} ${source} -o ${target}";
private static final String VAR_SOURCE = "source";
private static final String VAR_TARGET = "target";
private static final String VAR_FLASH_VERSION = "flashVersion";
/**
* Get the check command for the PDF2SWF tool
*
* @return RuntimeExec check command
*/
private RuntimeExec getCheckCommand()
{
if (this.checkCommand == null)
{
this.checkCommand = createCommand(PDF2SWF_CHECK);
}
return this.checkCommand;
}
/**
* Get the transform command for the PDF2SWF tool
*
* @return RuntimeExec transform command
*/
private RuntimeExec getTransformCommand()
{
if (this.transformCommand == null)
{
this.transformCommand = createCommand(PDF2SWF_COMMAND);
}
return this.transformCommand;
}
/**
* Helper to create a runtime exec object for a given command string
*
* @param commandString command string
* @return RuntimeExec runtime exec command
*/
private RuntimeExec createCommand(String commandString)
{
// Create command
RuntimeExec result = new RuntimeExec();
// Set the command string
Map<String, String> commandsByOS = new HashMap<String, String>(1);
commandsByOS.put(".*", commandString);
result.setCommandMap(commandsByOS);
// Set the error code
result.setErrorCodes("1");
return result;
}
/**
* @see org.alfresco.repo.content.transform.AbstractContentTransformer2#register()
*/
@Override
public void register()
{
ExecutionResult result = getCheckCommand().execute();
// check the return code
if (this.available = result.getSuccess())
{
this.versionString = result.getStdOut().trim();
}
else
{
logger.error("Failed to start SWF2PDF transformer: \n" + result);
}
// call the base class to make sure that it gets registered
super.register();
}
/**
* @see org.alfresco.repo.content.transform.AbstractContentTransformer2#transformInternal(org.alfresco.service.cmr.repository.ContentReader, org.alfresco.service.cmr.repository.ContentWriter, org.alfresco.service.cmr.repository.TransformationOptions)
*/
@Override
protected void transformInternal(ContentReader reader, ContentWriter writer, TransformationOptions options)
throws Exception
{
// get mimetypes
String sourceMimetype = getMimetype(reader);
String targetMimetype = getMimetype(writer);
// get the extensions to use
String sourceExtension = getMimetypeService().getExtension(sourceMimetype);
String targetExtension = getMimetypeService().getExtension(targetMimetype);
if (sourceExtension == null || targetExtension == null)
{
throw new AlfrescoRuntimeException("Unknown extensions for mimetypes: \n" +
" source mimetype: " + sourceMimetype + "\n" +
" source extension: " + sourceExtension + "\n" +
" target mimetype: " + targetMimetype + "\n" +
" target extension: " + targetExtension);
}
// create required temp files
File sourceFile = TempFileProvider.createTempFile(
getClass().getSimpleName() + "_source_",
"." + sourceExtension);
File targetFile = TempFileProvider.createTempFile(
getClass().getSimpleName() + "_target_",
"." + targetExtension);
String flashVersion = DEFAULT_FLASH_VERSION;
Map<String, String> properties = new HashMap<String, String>(5);
if (options instanceof SWFTransformationOptions)
{
SWFTransformationOptions swfOptions = (SWFTransformationOptions)options;
if (swfOptions.getFlashVersion() != null)
{
flashVersion = swfOptions.getFlashVersion();
}
}
properties.put(VAR_FLASH_VERSION, "-T " + flashVersion);
// add the source and target properties
properties.put(VAR_SOURCE, sourceFile.getAbsolutePath());
properties.put(VAR_TARGET, targetFile.getAbsolutePath());
// pull reader file into source temp file
reader.getContent(sourceFile);
// execute the transformation command
ExecutionResult result = null;
try
{
result = getTransformCommand().execute(properties);
}
catch (Throwable e)
{
throw new ContentIOException("Transformation failed during command execution: \n" + transformCommand, e);
}
// check
if (!result.getSuccess())
{
throw new ContentIOException("Transformation failed - status indicates an error: \n" + result);
}
// check that the file was created
if (!targetFile.exists())
{
throw new ContentIOException("Transformation failed - target file doesn't exist: \n" + result);
}
// copy the target file back into the repo
writer.putContent(targetFile);
// done
if (logger.isDebugEnabled())
{
logger.debug("PDF2SWF transformation completed: \n" +
" source: " + reader + "\n" +
" target: " + writer + "\n" +
" options: " + options + "\n" +
" result: \n" + result);
}
}
/**
* @see org.alfresco.repo.content.transform.ContentTransformer#isTransformable(java.lang.String, java.lang.String, org.alfresco.service.cmr.repository.TransformationOptions)
*/
public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
{
boolean result = false;
if (this.available == true)
{
if (MimetypeMap.MIMETYPE_PDF.equals(sourceMimetype) == true &&
MimetypeMap.MIMETYPE_FLASH.equals(targetMimetype) == true)
{
result = true;
}
}
return result;
}
/**
* Signals whether this transformer is available.
*
* @return true, if is available
*/
public boolean isAvailable()
{
return this.available;
}
/**
* Gets the version string captured from the check command.
*
* @return the version string
*/
public String getVersionString()
{
return this.versionString;
}
}

View File

@@ -24,7 +24,11 @@
*/
package org.alfresco.repo.content.transform.swf;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.alfresco.util.ParameterCheck;
/**
* SFW transformation options
@@ -33,11 +37,14 @@ import org.alfresco.service.cmr.repository.TransformationOptions;
*/
public class SWFTransformationOptions extends TransformationOptions
{
private static final String OPT_FLASH_VERSION = "flashVersion";
/** The version of the flash to convert to */
private String flashVersion = "9";
public void setFlashVersion(String flashVersion)
{
ParameterCheck.mandatory("flashVersion", flashVersion);
this.flashVersion = flashVersion;
}
@@ -45,4 +52,13 @@ public class SWFTransformationOptions extends TransformationOptions
{
return flashVersion;
}
@Override
public Map<String, Object> toMap()
{
Map<String, Object> baseProps = super.toMap();
Map<String, Object> props = new HashMap<String, Object>(baseProps);
props.put(OPT_FLASH_VERSION, flashVersion);
return props;
}
}