options) throws Exception
+ {
+ String sourceMimetype = getMimetype(reader);
+ String targetMimetype = getMimetype(writer);
+
+ MimetypeService mimetypeService = getMimetypeService();
+ String sourceExtension = mimetypeService.getExtension(sourceMimetype);
+ String targetExtension = mimetypeService.getExtension(targetMimetype);
+ // query the registry for the source format
+ DocumentFormat sourceFormat = formatRegistry.getFormatByFileExtension(sourceExtension);
+ if (sourceFormat == null)
+ {
+ // source format is not recognised
+ throw new ContentIOException("No OpenOffice document format for source extension: " + sourceExtension);
+ }
+ // query the registry for the target format
+ DocumentFormat targetFormat = formatRegistry.getFormatByFileExtension(targetExtension);
+ if (targetFormat == null)
+ {
+ // target format is not recognised
+ throw new ContentIOException("No OpenOffice document format for target extension: " + sourceExtension);
+ }
+ // get the family of the target document
+ DocumentFamily sourceFamily = sourceFormat.getFamily();
+ // does the format support the conversion
+ if (!targetFormat.isExportableFrom(sourceFamily))
+ {
+ throw new ContentIOException(
+ "OpenOffice conversion not supported: \n" +
+ " reader: " + reader + "\n" +
+ " writer: " + writer);
+ }
+
+ // create temporary files to convert from and to
+ File tempFromFile = TempFileProvider.createTempFile(
+ "OpenOfficeContentTransformer-source-",
+ "." + sourceExtension);
+ File tempToFile = TempFileProvider.createTempFile(
+ "OpenOfficeContentTransformer-target-",
+ "." + targetExtension);
+ // download the content from the source reader
+ reader.getContent(tempFromFile);
+
+ try
+ {
+ converter.convert(tempFromFile, sourceFormat, tempToFile, targetFormat);
+ // conversion success
+ }
+ catch (OpenOfficeException e)
+ {
+ throw new ContentIOException("OpenOffice server conversion failed: \n" +
+ " reader: " + reader + "\n" +
+ " writer: " + writer + "\n" +
+ " from file: " + tempFromFile + "\n" +
+ " to file: " + tempToFile,
+ e);
+ }
+
+ // upload the temp output to the writer given us
+ writer.putContent(tempToFile);
+ }
+}
diff --git a/source/java/org/alfresco/repo/content/transform/UnoContentTransformerTest.java b/source/java/org/alfresco/repo/content/transform/OpenOfficeContentTransformerTest.java
similarity index 70%
rename from source/java/org/alfresco/repo/content/transform/UnoContentTransformerTest.java
rename to source/java/org/alfresco/repo/content/transform/OpenOfficeContentTransformerTest.java
index 76133b224a..12d016db23 100644
--- a/source/java/org/alfresco/repo/content/transform/UnoContentTransformerTest.java
+++ b/source/java/org/alfresco/repo/content/transform/OpenOfficeContentTransformerTest.java
@@ -16,24 +16,30 @@
*/
package org.alfresco.repo.content.transform;
+import net.sf.jooreports.openoffice.connection.OpenOfficeConnection;
+
import org.alfresco.repo.content.MimetypeMap;
/**
- * @see org.alfresco.repo.content.transform.UnoContentTransformer
+ * @see org.alfresco.repo.content.transform.OpenOfficeContentTransformer
*
* @author Derek Hulley
*/
-public class UnoContentTransformerTest extends AbstractContentTransformerTest
+public class OpenOfficeContentTransformerTest extends AbstractContentTransformerTest
{
private static String MIMETYPE_RUBBISH = "text/rubbish";
- private UnoContentTransformer transformer;
+ private OpenOfficeContentTransformer transformer;
public void onSetUpInTransaction() throws Exception
{
- transformer = new UnoContentTransformer();
+ OpenOfficeConnection connection = (OpenOfficeConnection) applicationContext.getBean("openOfficeConnection");
+
+ transformer = new OpenOfficeContentTransformer();
transformer.setMimetypeService(mimetypeMap);
- transformer.init();
+ transformer.setConnection(connection);
+ transformer.setDocumentFormatsConfiguration("classpath:alfresco/mimetype/openoffice-document-formats.xml");
+ transformer.register();
}
/**
@@ -62,6 +68,8 @@ public class UnoContentTransformerTest extends AbstractContentTransformerTest
assertEquals("Mimetype should not be supported", 0.0, reliability);
reliability = transformer.getReliability(MimetypeMap.MIMETYPE_TEXT_PLAIN, MIMETYPE_RUBBISH);
assertEquals("Mimetype should not be supported", 0.0, reliability);
+ reliability = transformer.getReliability(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_XHTML);
+ assertEquals("Mimetype should not be supported", 0.0, reliability);
reliability = transformer.getReliability(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_WORD);
assertEquals("Mimetype should be supported", 1.0, reliability);
reliability = transformer.getReliability(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_TEXT_PLAIN);
diff --git a/source/java/org/alfresco/repo/content/transform/UnoContentTransformer.java b/source/java/org/alfresco/repo/content/transform/UnoContentTransformer.java
deleted file mode 100644
index 643336d718..0000000000
--- a/source/java/org/alfresco/repo/content/transform/UnoContentTransformer.java
+++ /dev/null
@@ -1,337 +0,0 @@
-/*
- * Copyright (C) 2005 Alfresco, Inc.
- *
- * Licensed under the Mozilla Public License version 1.1
- * with a permitted attribution clause. You may obtain a
- * copy of the License at
- *
- * http://www.alfresco.org/legal/license.txt
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- * either express or implied. See the License for the specific
- * language governing permissions and limitations under the
- * License.
- */
-package org.alfresco.repo.content.transform;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.ConnectException;
-import java.util.HashMap;
-import java.util.Map;
-
-import net.sf.joott.uno.DocumentConverter;
-import net.sf.joott.uno.DocumentFormat;
-import net.sf.joott.uno.UnoConnection;
-
-import org.alfresco.repo.content.MimetypeMap;
-import org.alfresco.service.cmr.repository.ContentIOException;
-import org.alfresco.service.cmr.repository.ContentReader;
-import org.alfresco.service.cmr.repository.ContentWriter;
-import org.alfresco.util.TempFileProvider;
-
-/**
- * Makes use of the OpenOffice Uno interfaces to convert the content.
- *
- * The conversions are slow but reliable. Not all possible combinations of transformations
- * have been enabled because they don't necessarily work and need to be specifically tested before
- * being made available generally. As the conversion process is mostly automated, the introduction
- * of faulty transformations can lead to unnecessary bugs. Feel free to experiment and, assuming
- * that the unit test works, report any interesting conversions that can be enabled.
- *
- * @author Derek Hulley
- */
-public class UnoContentTransformer extends AbstractContentTransformer
-{
- /** map of DocumentFormat instances keyed by mimetype conversion */
- private static Map formatsByConversion;
-
- static
- {
- // Build the map of known Uno document formats and store by conversion key
- formatsByConversion = new HashMap(17);
-
- // Open Office 2.0 / Open Document
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT, MimetypeMap.MIMETYPE_TEXT_PLAIN),
- new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_SPREADSHEET, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENDOCUMENT_PRESENTATION, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- // Open Office
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER, MimetypeMap.MIMETYPE_TEXT_PLAIN),
- new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_CALC, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_DRAW, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_OPENOFFICE1_IMPRESS, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0));
- // Star Office 5.x
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_DRAW, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_CALC, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_CHART, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_IMPRESS_PACKED, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_WRITER, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_STAROFFICE5_WRITER_GLOBAL, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- // MS Office
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_TEXT_PLAIN),
- new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_EXCEL, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_CALC, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_WORD, MimetypeMap.MIMETYPE_HTML),
- new DocumentFormatWrapper(DocumentFormat.HTML_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_PPT, MimetypeMap.MIMETYPE_FLASH),
- new DocumentFormatWrapper(DocumentFormat.FLASH_IMPRESS, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_PPT, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_IMPRESS, 1.0));
- // Other
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_HTML),
- new DocumentFormatWrapper(DocumentFormat.HTML_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_WORD),
- new DocumentFormatWrapper(DocumentFormat.TEXT, 1.0));
- formatsByConversion.put(
- new ContentTransformerRegistry.TransformationKey(MimetypeMap.MIMETYPE_HTML, MimetypeMap.MIMETYPE_PDF),
- new DocumentFormatWrapper(DocumentFormat.PDF_WRITER_WEB, 1.0));
-
- // there are many more formats available and therefore many more transformation combinations possible
-// DocumentFormat.FLASH_IMPRESS
-// DocumentFormat.HTML_CALC
-// DocumentFormat.HTML_WRITER
-// DocumentFormat.MS_EXCEL_97
-// DocumentFormat.MS_POWERPOINT_97
-// DocumentFormat.MS_WORD_97
-// DocumentFormat.PDF_CALC
-// DocumentFormat.PDF_IMPRESS
-// DocumentFormat.PDF_WRITER
-// DocumentFormat.PDF_WRITER_WEB
-// DocumentFormat.RTF
-// DocumentFormat.TEXT
-// DocumentFormat.TEXT_CALC
-// DocumentFormat.XML_CALC
-// DocumentFormat.XML_IMPRESS
-// DocumentFormat.XML_WRITER
-// DocumentFormat.XML_WRITER_WEB
- }
-
- private String connectionUrl = UnoConnection.DEFAULT_CONNECTION_STRING;
- private UnoConnection connection;
- private boolean isConnected;
-
- /**
- * Constructs the default transformer that will attempt to connect to the
- * Uno server using the default connect string.
- *
- * @see UnoConnection#DEFAULT_CONNECTION_STRING
- */
- public UnoContentTransformer()
- {
- isConnected = false;
- }
-
- /**
- * Override the default connection URL with a new one.
- *
- * @param connectionUrl the connection string
- *
- * @see UnoConnection#DEFAULT_CONNECTION_STRING
- */
- public void setConnectionUrl(String connectionUrl)
- {
- this.connectionUrl = connectionUrl;
- }
-
- /**
- * Connects to the OpenOffice server. If successful, then
- * {@link AbstractContentTransformer#register() auto-registers}.
- */
- public synchronized void init()
- {
- connection = new UnoConnection(connectionUrl);
- // attempt to make an connection
- try
- {
- connection.connect();
- isConnected = true;
- // register
- super.register();
- }
- catch (ConnectException e)
- {
- isConnected = false;
- }
- }
-
- /**
- * @return Returns true if a connection to the Uno server could be established
- */
- public boolean isConnected()
- {
- return isConnected;
- }
-
- /**
- * @param sourceMimetype
- * @param targetMimetype
- * @return Returns a document format wrapper that is valid for the given source and target mimetypes
- */
- private static DocumentFormatWrapper getDocumentFormatWrapper(String sourceMimetype, String targetMimetype)
- {
- // get the well-known document format for the specific conversion
- ContentTransformerRegistry.TransformationKey key =
- new ContentTransformerRegistry.TransformationKey(sourceMimetype, targetMimetype);
- DocumentFormatWrapper wrapper = UnoContentTransformer.formatsByConversion.get(key);
- return wrapper;
- }
-
- /**
- * Checks how reliable the conversion will be when performed by the Uno server.
- *
- * The connection for the Uno server is checked in order to have any chance of
- * being reliable.
- *
- * The conversions' reliabilities are set up statically based on prior tests that
- * included checking performance as well as accuracy.
- */
- public double getReliability(String sourceMimetype, String targetMimetype)
- {
- // check if a connection to the Uno server can be established
- if (!isConnected())
- {
- // no connection means that conversion is not possible
- return 0.0;
- }
- // check if the source and target mimetypes are supported
- DocumentFormatWrapper docFormatWrapper = getDocumentFormatWrapper(sourceMimetype, targetMimetype);
- if (docFormatWrapper == null)
- {
- return 0.0;
- }
- else
- {
- return docFormatWrapper.getReliability();
- }
- }
-
- public void transformInternal(ContentReader reader, ContentWriter writer, Map options)
- throws Exception
- {
- String sourceMimetype = getMimetype(reader);
- String targetMimetype = getMimetype(writer);
-
- // create temporary files to convert from and to
- File tempFromFile = TempFileProvider.createTempFile(
- "UnoContentTransformer",
- "." + getMimetypeService().getExtension(sourceMimetype));
- File tempToFile = TempFileProvider.createTempFile(
- "UnoContentTransformer",
- "." + getMimetypeService().getExtension(targetMimetype));
- // download the content from the source reader
- reader.getContent(tempFromFile);
-
- // get the document format that should be used
- DocumentFormatWrapper docFormatWrapper = getDocumentFormatWrapper(sourceMimetype, targetMimetype);
- try
- {
- docFormatWrapper.execute(tempFromFile, tempToFile, connection);
- // conversion success
- }
- catch (ConnectException e)
- {
- throw new ContentIOException("Connection to Uno server failed: \n" +
- " reader: " + reader + "\n" +
- " writer: " + writer,
- e);
- }
- catch (IOException e)
- {
- throw new ContentIOException("Uno server conversion failed: \n" +
- " reader: " + reader + "\n" +
- " writer: " + writer + "\n" +
- " from file: " + tempFromFile + "\n" +
- " to file: " + tempToFile,
- e);
- }
-
- // upload the temp output to the writer given us
- writer.putContent(tempToFile);
- }
-
- /**
- * Wraps a document format as well the reliability. The source and target mimetypes
- * are not kept, but will probably be closely associated with the reliability.
- */
- private static class DocumentFormatWrapper
- {
- /*
- * Source and target mimetypes not kept -> class is private as it doesn't keep
- * enough info to be used safely externally
- */
-
- private DocumentFormat documentFormat;
- private double reliability;
-
- public DocumentFormatWrapper(DocumentFormat documentFormat, double reliability)
- {
- this.documentFormat = documentFormat;
- this.reliability = reliability;
- }
-
- public double getReliability()
- {
- return reliability;
- }
-
- /**
- * Executs the transformation
- */
- public void execute(File fromFile, File toFile, UnoConnection connection) throws ConnectException, IOException
- {
- DocumentConverter converter = new DocumentConverter(connection);
- converter.convert(fromFile, toFile, documentFormat);
- }
- }
-}