parent
d6397309a9
commit
28561ec46e
@ -0,0 +1,154 @@ |
||||
//
|
||||
// JODConverter - Java OpenDocument Converter
|
||||
// Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// 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.
|
||||
// http://www.gnu.org/copyleft/lesser.html
|
||||
//
|
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.commons.io.FilenameUtils; |
||||
import org.apache.commons.io.IOUtils; |
||||
|
||||
import com.artofsolving.jodconverter.DocumentConverter; |
||||
import com.artofsolving.jodconverter.DocumentFormat; |
||||
import com.artofsolving.jodconverter.DocumentFormatRegistry; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException; |
||||
import com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter; |
||||
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; |
||||
import com.sun.star.beans.PropertyValue; |
||||
import com.sun.star.container.XNamed; |
||||
import com.sun.star.document.XExporter; |
||||
import com.sun.star.document.XFilter; |
||||
import com.sun.star.drawing.XDrawPage; |
||||
import com.sun.star.drawing.XDrawPages; |
||||
import com.sun.star.drawing.XDrawPagesSupplier; |
||||
import com.sun.star.frame.XComponentLoader; |
||||
import com.sun.star.lang.XComponent; |
||||
import com.sun.star.lang.XMultiComponentFactory; |
||||
import com.sun.star.ucb.XFileIdentifierConverter; |
||||
import com.sun.star.uno.UnoRuntime; |
||||
|
||||
/** |
||||
* Default file-based {@link DocumentConverter} implementation. |
||||
* <p> |
||||
* This implementation passes document data to and from the OpenOffice.org |
||||
* service as file URLs. |
||||
* <p> |
||||
* File-based conversions are faster than stream-based ones (provided by |
||||
* {@link StreamOpenOfficeDocumentConverter}) but they require the |
||||
* OpenOffice.org service to be running locally and have the correct |
||||
* permissions to the files. |
||||
* |
||||
* @see StreamOpenOfficeDocumentConverter |
||||
*/ |
||||
public abstract class AbstractDokeosDocumentConverter extends AbstractOpenOfficeDocumentConverter { |
||||
|
||||
|
||||
int width; |
||||
int height; |
||||
|
||||
|
||||
public AbstractDokeosDocumentConverter(OpenOfficeConnection connection, int width, int height) { |
||||
super(connection); |
||||
this.width = 800; |
||||
this.height = 600; |
||||
} |
||||
|
||||
public AbstractDokeosDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) { |
||||
super(connection, formatRegistry); |
||||
this.width = 800; |
||||
this.height = 600; |
||||
} |
||||
|
||||
/** |
||||
* In this file-based implementation, streams are emulated using temporary files. |
||||
*/ |
||||
protected void convertInternal(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat) { |
||||
File inputFile = null; |
||||
File outputFile = null; |
||||
try { |
||||
inputFile = File.createTempFile("document", "." + inputFormat.getFileExtension()); |
||||
OutputStream inputFileStream = null; |
||||
try { |
||||
inputFileStream = new FileOutputStream(inputFile); |
||||
IOUtils.copy(inputStream, inputFileStream); |
||||
} finally { |
||||
IOUtils.closeQuietly(inputFileStream); |
||||
} |
||||
|
||||
outputFile = File.createTempFile("document", "." + outputFormat.getFileExtension()); |
||||
convert(inputFile, inputFormat, outputFile, outputFormat); |
||||
InputStream outputFileStream = null; |
||||
try { |
||||
outputFileStream = new FileInputStream(outputFile); |
||||
IOUtils.copy(outputFileStream, outputStream); |
||||
} finally { |
||||
IOUtils.closeQuietly(outputFileStream); |
||||
} |
||||
} catch (IOException ioException) { |
||||
throw new OpenOfficeException("conversion failed", ioException); |
||||
} finally { |
||||
if (inputFile != null) { |
||||
inputFile.delete(); |
||||
} |
||||
if (outputFile != null) { |
||||
outputFile.delete(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
protected void convertInternal(File inputFile, DocumentFormat inputFormat, File outputFile, DocumentFormat outputFormat) { |
||||
Map/*<String,Object>*/ loadProperties = new HashMap(); |
||||
loadProperties.putAll(getDefaultLoadProperties()); |
||||
loadProperties.putAll(inputFormat.getImportOptions()); |
||||
|
||||
Map/*<String,Object>*/ storeProperties = outputFormat.getExportOptions(inputFormat.getFamily()); |
||||
|
||||
synchronized (openOfficeConnection) { |
||||
XFileIdentifierConverter fileContentProvider = openOfficeConnection.getFileContentProvider(); |
||||
String inputUrl = fileContentProvider.getFileURLFromSystemPath("", inputFile.getAbsolutePath()); |
||||
String outputUrl = fileContentProvider.getFileURLFromSystemPath("", outputFile.getAbsolutePath()); |
||||
|
||||
try { |
||||
loadAndExport(inputUrl, loadProperties, outputUrl, storeProperties); |
||||
} catch (OpenOfficeException openOfficeException) { |
||||
throw openOfficeException; |
||||
} catch (Throwable throwable) { |
||||
// difficult to provide finer grained error reporting here;
|
||||
// OOo seems to throw ErrorCodeIOException most of the time
|
||||
throw new OpenOfficeException("conversion failed", throwable); |
||||
} |
||||
} |
||||
} |
||||
|
||||
abstract protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception; |
||||
|
||||
|
||||
|
||||
protected DocumentFormat guessDocumentFormat(File file) { |
||||
String extension = FilenameUtils.getExtension(file.getName()); |
||||
DocumentFormat format = getDocumentFormatRegistry().getFormatByFileExtension(extension); |
||||
if (format == null) { |
||||
//throw new IllegalArgumentException("unknown document format for file: " + file);
|
||||
} |
||||
return format; |
||||
} |
||||
} |
@ -0,0 +1,129 @@ |
||||
//
|
||||
// JODConverter - Java OpenDocument Converter
|
||||
// Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// 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.
|
||||
// http://www.gnu.org/copyleft/lesser.html
|
||||
//
|
||||
|
||||
import java.net.ConnectException; |
||||
|
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
|
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException; |
||||
import com.sun.star.beans.XPropertySet; |
||||
import com.sun.star.bridge.XBridge; |
||||
import com.sun.star.bridge.XBridgeFactory; |
||||
import com.sun.star.comp.helper.Bootstrap; |
||||
import com.sun.star.connection.NoConnectException; |
||||
import com.sun.star.connection.XConnection; |
||||
import com.sun.star.connection.XConnector; |
||||
import com.sun.star.frame.XComponentLoader; |
||||
import com.sun.star.lang.EventObject; |
||||
import com.sun.star.lang.XComponent; |
||||
import com.sun.star.lang.XEventListener; |
||||
import com.sun.star.lang.XMultiComponentFactory; |
||||
import com.sun.star.ucb.XFileIdentifierConverter; |
||||
import com.sun.star.uno.UnoRuntime; |
||||
import com.sun.star.uno.XComponentContext; |
||||
|
||||
public abstract class AbstractDokeosOpenOfficeConnection implements OpenOfficeConnection, XEventListener { |
||||
|
||||
protected final Logger logger = LoggerFactory.getLogger(getClass()); |
||||
|
||||
private String connectionString; |
||||
private XComponent bridgeComponent; |
||||
protected XMultiComponentFactory serviceManager; |
||||
protected XComponentContext componentContext; |
||||
private boolean connected = false; |
||||
private boolean expectingDisconnection = false; |
||||
|
||||
protected AbstractDokeosOpenOfficeConnection(String connectionString) { |
||||
this.connectionString = connectionString; |
||||
} |
||||
|
||||
public synchronized void connect() throws ConnectException { |
||||
logger.debug("connecting"); |
||||
try { |
||||
XComponentContext localContext = Bootstrap.createInitialComponentContext(null); |
||||
XMultiComponentFactory localServiceManager = localContext.getServiceManager(); |
||||
XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, |
||||
localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector", localContext)); |
||||
XConnection connection = connector.connect(connectionString); |
||||
XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class, |
||||
localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext)); |
||||
XBridge bridge = bridgeFactory.createBridge("", "urp", connection, null); |
||||
bridgeComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge); |
||||
bridgeComponent.addEventListener(this); |
||||
serviceManager = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, |
||||
bridge.getInstance("StarOffice.ServiceManager")); |
||||
XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceManager); |
||||
componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, |
||||
properties.getPropertyValue("DefaultContext")); |
||||
connected = true; |
||||
logger.info("connected"); |
||||
} catch (NoConnectException connectException) { |
||||
throw new ConnectException("connection failed: "+ connectionString +": " + connectException.getMessage()); |
||||
} catch (Exception exception) { |
||||
throw new OpenOfficeException("connection failed: "+ connectionString, exception); |
||||
} |
||||
} |
||||
|
||||
public synchronized void disconnect() { |
||||
logger.debug("disconnecting"); |
||||
expectingDisconnection = true; |
||||
bridgeComponent.dispose(); |
||||
} |
||||
|
||||
public boolean isConnected() { |
||||
return connected; |
||||
} |
||||
|
||||
public void disposing(EventObject event) { |
||||
connected = false; |
||||
if (expectingDisconnection) { |
||||
logger.info("disconnected"); |
||||
} else { |
||||
logger.error("disconnected unexpectedly"); |
||||
} |
||||
expectingDisconnection = false; |
||||
} |
||||
|
||||
// for unit tests only
|
||||
void simulateUnexpectedDisconnection() { |
||||
disposing(null); |
||||
bridgeComponent.dispose(); |
||||
} |
||||
|
||||
private Object getService(String className) { |
||||
try { |
||||
if (!connected) { |
||||
logger.info("trying to (re)connect"); |
||||
connect(); |
||||
} |
||||
return serviceManager.createInstanceWithContext(className, componentContext); |
||||
} catch (Exception exception) { |
||||
throw new OpenOfficeException("could not obtain service: " + className, exception); |
||||
} |
||||
} |
||||
|
||||
public XComponentLoader getDesktop() { |
||||
return (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, |
||||
getService("com.sun.star.frame.Desktop")); |
||||
} |
||||
|
||||
public XFileIdentifierConverter getFileContentProvider() { |
||||
return (XFileIdentifierConverter) UnoRuntime.queryInterface(XFileIdentifierConverter.class, |
||||
getService("com.sun.star.ucb.FileContentProvider")); |
||||
} |
||||
} |
@ -0,0 +1,169 @@ |
||||
//
|
||||
// JODConverter - Java OpenDocument Converter
|
||||
// Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// 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.
|
||||
// http://www.gnu.org/copyleft/lesser.html
|
||||
//
|
||||
|
||||
import java.io.File; |
||||
import java.net.ConnectException; |
||||
|
||||
import org.apache.commons.cli.CommandLine; |
||||
import org.apache.commons.cli.CommandLineParser; |
||||
import org.apache.commons.cli.HelpFormatter; |
||||
import org.apache.commons.cli.Option; |
||||
import org.apache.commons.cli.Options; |
||||
import org.apache.commons.cli.PosixParser; |
||||
import org.apache.commons.io.FilenameUtils; |
||||
|
||||
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; |
||||
|
||||
/** |
||||
* Command line tool to convert a document into a different format. |
||||
* <p> |
||||
* Usage: can convert a single file |
||||
* |
||||
* <pre> |
||||
* ConvertDocument test.odt test.pdf |
||||
* </pre> |
||||
* |
||||
* or multiple files at once by specifying the output format |
||||
* |
||||
* <pre> |
||||
* ConvertDocument -f pdf test1.odt test2.odt |
||||
* ConvertDocument -f pdf *.odt |
||||
* </pre> |
||||
*/ |
||||
public class DokeosConverter { |
||||
|
||||
private static final Option OPTION_OUTPUT_FORMAT = new Option("f", "output-format", true, "output format (e.g. pdf)"); |
||||
private static final Option OPTION_PORT = new Option("p", "port", true, "OpenOffice.org port"); |
||||
private static final Option OPTION_VERBOSE = new Option("v", "verbose", false, "verbose"); |
||||
private static final Option OPTION_DOKEOS_MODE = new Option("d", "dokeos-mode", true, "use oogie or woogie"); |
||||
private static final Option OPTION_WIDTH = new Option("w", "width", true, "width"); |
||||
private static final Option OPTION_HEIGHT = new Option("h", "height", true, "height"); |
||||
private static final Options OPTIONS = initOptions(); |
||||
|
||||
private static final int EXIT_CODE_CONNECTION_FAILED = 1; |
||||
private static final int EXIT_CODE_TOO_FEW_ARGS = 255; |
||||
|
||||
private static Options initOptions() { |
||||
Options options = new Options(); |
||||
options.addOption(OPTION_OUTPUT_FORMAT); |
||||
options.addOption(OPTION_PORT); |
||||
options.addOption(OPTION_VERBOSE); |
||||
options.addOption(OPTION_DOKEOS_MODE); |
||||
options.addOption(OPTION_WIDTH); |
||||
options.addOption(OPTION_HEIGHT); |
||||
return options; |
||||
} |
||||
|
||||
public static void main(String[] arguments) throws Exception { |
||||
CommandLineParser commandLineParser = new PosixParser(); |
||||
CommandLine commandLine = commandLineParser.parse(OPTIONS, arguments); |
||||
|
||||
int port = SocketOpenOfficeConnection.DEFAULT_PORT; |
||||
if (commandLine.hasOption(OPTION_PORT.getOpt())) { |
||||
port = Integer.parseInt(commandLine.getOptionValue(OPTION_PORT.getOpt())); |
||||
} |
||||
|
||||
String outputFormat = null; |
||||
if (commandLine.hasOption(OPTION_OUTPUT_FORMAT.getOpt())) { |
||||
outputFormat = commandLine.getOptionValue(OPTION_OUTPUT_FORMAT.getOpt()); |
||||
} |
||||
|
||||
boolean verbose = false; |
||||
if (commandLine.hasOption(OPTION_VERBOSE.getOpt())) { |
||||
verbose = true; |
||||
} |
||||
|
||||
String dokeosMode = "woogie"; |
||||
if (commandLine.hasOption(OPTION_DOKEOS_MODE.getOpt())) { |
||||
dokeosMode = commandLine.getOptionValue(OPTION_DOKEOS_MODE.getOpt()); |
||||
} |
||||
int width = 800; |
||||
if (commandLine.hasOption(OPTION_WIDTH.getOpt())) { |
||||
width = Integer.parseInt(commandLine.getOptionValue(OPTION_WIDTH.getOpt())); |
||||
} |
||||
|
||||
int height = 600; |
||||
if (commandLine.hasOption(OPTION_HEIGHT.getOpt())) { |
||||
height = Integer.parseInt(commandLine.getOptionValue(OPTION_HEIGHT.getOpt())); |
||||
} |
||||
|
||||
String[] fileNames = commandLine.getArgs(); |
||||
if ((outputFormat == null && fileNames.length != 2 && dokeosMode!=null) || fileNames.length < 1) { |
||||
String syntax = "convert [options] input-file output-file; or\n" |
||||
+ "[options] -f output-format input-file [input-file...]"; |
||||
HelpFormatter helpFormatter = new HelpFormatter(); |
||||
helpFormatter.printHelp(syntax, OPTIONS); |
||||
System.exit(EXIT_CODE_TOO_FEW_ARGS); |
||||
} |
||||
|
||||
OpenOfficeConnection connection = new DokeosSocketOfficeConnection(port); |
||||
try { |
||||
if (verbose) { |
||||
System.out.println("-- connecting to OpenOffice.org on port " + port); |
||||
} |
||||
connection.connect(); |
||||
} catch (ConnectException officeNotRunning) { |
||||
System.err |
||||
.println("ERROR: connection failed. Please make sure OpenOffice.org is running and listening on port " |
||||
+ port + "."); |
||||
System.exit(EXIT_CODE_CONNECTION_FAILED); |
||||
} |
||||
try { |
||||
|
||||
|
||||
// choose the good constructor to deal with the conversion
|
||||
DocumentConverter converter; |
||||
if(dokeosMode.equals("oogie")){ |
||||
converter = new OogieDocumentConverter(connection, width, height); |
||||
} |
||||
else if(dokeosMode.equals("woogie")){ |
||||
converter = new WoogieDocumentConverter(connection, width, height); |
||||
} |
||||
else { |
||||
converter = new OpenOfficeDocumentConverter(connection); |
||||
} |
||||
|
||||
|
||||
if (outputFormat == null) { |
||||
File inputFile = new File(fileNames[0]); |
||||
File outputFile = new File(fileNames[1]); |
||||
convertOne(converter, inputFile, outputFile, verbose); |
||||
} else { |
||||
for (int i = 0; i < fileNames.length; i++) { |
||||
File inputFile = new File(fileNames[i]); |
||||
File outputFile = new File(FilenameUtils.getFullPath(fileNames[i]) |
||||
+ FilenameUtils.getBaseName(fileNames[i]) + "." + outputFormat); |
||||
convertOne(converter, inputFile, outputFile, verbose); |
||||
} |
||||
} |
||||
} finally { |
||||
if (verbose) { |
||||
System.out.println("-- disconnecting"); |
||||
} |
||||
connection.disconnect(); |
||||
} |
||||
} |
||||
|
||||
private static void convertOne(DocumentConverter converter, File inputFile, File outputFile, boolean verbose) { |
||||
if (verbose) { |
||||
System.out.println("-- converting " + inputFile + " to " + outputFile); |
||||
} |
||||
converter.convert(inputFile, outputFile); |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
import com.sun.star.lang.XMultiComponentFactory; |
||||
import com.sun.star.uno.XComponentContext; |
||||
|
||||
|
||||
|
||||
public class DokeosSocketOfficeConnection extends AbstractDokeosOpenOfficeConnection { |
||||
|
||||
public static final String DEFAULT_HOST = "localhost"; |
||||
public static final int DEFAULT_PORT = 8100; |
||||
|
||||
public DokeosSocketOfficeConnection() { |
||||
this(DEFAULT_HOST, DEFAULT_PORT); |
||||
} |
||||
|
||||
public DokeosSocketOfficeConnection(int port) { |
||||
this(DEFAULT_HOST, port); |
||||
} |
||||
|
||||
public DokeosSocketOfficeConnection(String host, int port) { |
||||
super("socket,host=" + host + ",port=" + port + ",tcpNoDelay=1"); |
||||
} |
||||
|
||||
public XMultiComponentFactory getServiceManager(){ |
||||
|
||||
return serviceManager; |
||||
|
||||
} |
||||
|
||||
public XComponentContext getComponentContext(){ |
||||
|
||||
return componentContext; |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,166 @@ |
||||
//
|
||||
// JODConverter - Java OpenDocument Converter
|
||||
// Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// 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.
|
||||
// http://www.gnu.org/copyleft/lesser.html
|
||||
//
|
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.commons.io.FilenameUtils; |
||||
import org.apache.commons.io.IOUtils; |
||||
|
||||
import com.artofsolving.jodconverter.DocumentConverter; |
||||
import com.artofsolving.jodconverter.DocumentFormat; |
||||
import com.artofsolving.jodconverter.DocumentFormatRegistry; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException; |
||||
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; |
||||
import com.sun.star.beans.PropertyValue; |
||||
import com.sun.star.container.XNamed; |
||||
import com.sun.star.document.XExporter; |
||||
import com.sun.star.document.XFilter; |
||||
import com.sun.star.drawing.XDrawPage; |
||||
import com.sun.star.drawing.XDrawPages; |
||||
import com.sun.star.drawing.XDrawPagesSupplier; |
||||
import com.sun.star.frame.XComponentLoader; |
||||
import com.sun.star.lang.XComponent; |
||||
import com.sun.star.lang.XMultiComponentFactory; |
||||
import com.sun.star.ucb.XFileIdentifierConverter; |
||||
import com.sun.star.uno.UnoRuntime; |
||||
|
||||
/** |
||||
* Default file-based {@link DocumentConverter} implementation. |
||||
* <p> |
||||
* This implementation passes document data to and from the OpenOffice.org |
||||
* service as file URLs. |
||||
* <p> |
||||
* File-based conversions are faster than stream-based ones (provided by |
||||
* {@link StreamOpenOfficeDocumentConverter}) but they require the |
||||
* OpenOffice.org service to be running locally and have the correct |
||||
* permissions to the files. |
||||
* |
||||
* @see StreamOpenOfficeDocumentConverter |
||||
*/ |
||||
public class OogieDocumentConverter extends AbstractDokeosDocumentConverter { |
||||
|
||||
public OogieDocumentConverter(OpenOfficeConnection connection, int width, int height) { |
||||
super(connection, width, height); |
||||
} |
||||
|
||||
public OogieDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) { |
||||
super(connection, formatRegistry, width, height); |
||||
} |
||||
|
||||
protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception { |
||||
XComponentLoader desktop = openOfficeConnection.getDesktop(); |
||||
XComponent document = desktop.loadComponentFromURL(inputUrl, "_blank", 0, toPropertyValues(loadProperties)); |
||||
if (document == null) { |
||||
throw new OpenOfficeException("conversion failed: input document is null after loading"); |
||||
} |
||||
|
||||
refreshDocument(document); |
||||
|
||||
try { |
||||
|
||||
outputUrl = FilenameUtils.getFullPath(outputUrl)+FilenameUtils.getBaseName(outputUrl); |
||||
|
||||
// filter
|
||||
PropertyValue[] loadProps = new PropertyValue[4]; |
||||
|
||||
// type of image
|
||||
loadProps[0] = new PropertyValue(); |
||||
loadProps[0].Name = "MediaType"; |
||||
loadProps[0].Value = "image/png"; |
||||
|
||||
// Height and width
|
||||
PropertyValue[] filterDatas = new PropertyValue[4]; |
||||
for(int i = 0; i<4 ; i++){ |
||||
filterDatas[i] = new PropertyValue(); |
||||
} |
||||
|
||||
filterDatas[0].Name = "PixelWidth"; |
||||
filterDatas[0].Value = new Integer(this.width); |
||||
filterDatas[1].Name = "PixelHeight"; |
||||
filterDatas[1].Value = new Integer(this.height); |
||||
filterDatas[2].Name = "LogicalWidth"; |
||||
filterDatas[2].Value = new Integer(2000); |
||||
filterDatas[3].Name = "LogicalHeight"; |
||||
filterDatas[3].Value = new Integer(2000); |
||||
|
||||
|
||||
XDrawPagesSupplier pagesSupplier = (XDrawPagesSupplier) UnoRuntime |
||||
.queryInterface(XDrawPagesSupplier.class, document); |
||||
//System.out.println(pagesSupplier.toString());
|
||||
XDrawPages pages = pagesSupplier.getDrawPages(); |
||||
int nbPages = pages.getCount(); |
||||
|
||||
|
||||
for (int i = 0; i < nbPages; i++) { |
||||
|
||||
XDrawPage page = (XDrawPage) UnoRuntime.queryInterface( |
||||
com.sun.star.drawing.XDrawPage.class, pages |
||||
.getByIndex(i)); |
||||
|
||||
XNamed xPageName = (XNamed)UnoRuntime.queryInterface(XNamed.class,page); |
||||
|
||||
xPageName.setName("slide"+(i+1)); |
||||
if(!xPageName.getName().equals("slide"+(i+1)) && !xPageName.getName().equals("page"+(i+1))) |
||||
xPageName.setName((i+1)+"-"+xPageName.getName()); |
||||
|
||||
|
||||
XMultiComponentFactory localServiceManager = ((DokeosSocketOfficeConnection)this.openOfficeConnection).getServiceManager(); |
||||
Object GraphicExportFilter = localServiceManager |
||||
.createInstanceWithContext( |
||||
"com.sun.star.drawing.GraphicExportFilter", |
||||
((DokeosSocketOfficeConnection)this.openOfficeConnection).getComponentContext()); |
||||
|
||||
XExporter xExporter = (XExporter) UnoRuntime |
||||
.queryInterface(XExporter.class, |
||||
GraphicExportFilter); |
||||
|
||||
XComponent xComp = (XComponent) UnoRuntime |
||||
.queryInterface(XComponent.class, page); |
||||
|
||||
xExporter.setSourceDocument(xComp); |
||||
loadProps[1] = new PropertyValue(); |
||||
loadProps[1].Name = "URL"; |
||||
|
||||
|
||||
|
||||
loadProps[1].Value = outputUrl+"/"+xPageName.getName()+".png"; |
||||
loadProps[2] = new PropertyValue(); |
||||
loadProps[2].Name = "FilterData"; |
||||
loadProps[2].Value = filterDatas; |
||||
loadProps[3] = new PropertyValue(); |
||||
loadProps[3].Name = "Quality"; |
||||
loadProps[3].Value = new Integer(100); |
||||
|
||||
XFilter xFilter = (XFilter) UnoRuntime.queryInterface(XFilter.class, GraphicExportFilter); |
||||
|
||||
xFilter.filter(loadProps); |
||||
System.out.println(xPageName.getName()+".png"); |
||||
|
||||
} |
||||
|
||||
} finally { |
||||
document.dispose(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,149 @@ |
||||
//
|
||||
// JODConverter - Java OpenDocument Converter
|
||||
// Copyright (C) 2004-2007 - Mirko Nasato <mirko@artofsolving.com>
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
//
|
||||
// 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.
|
||||
// http://www.gnu.org/copyleft/lesser.html
|
||||
//
|
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.apache.commons.io.FilenameUtils; |
||||
|
||||
import com.artofsolving.jodconverter.DocumentConverter; |
||||
import com.artofsolving.jodconverter.DocumentFormatRegistry; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; |
||||
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException; |
||||
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; |
||||
import com.sun.star.beans.PropertyValue; |
||||
import com.sun.star.frame.XComponentLoader; |
||||
import com.sun.star.frame.XController; |
||||
import com.sun.star.frame.XDesktop; |
||||
import com.sun.star.frame.XModel; |
||||
import com.sun.star.frame.XStorable; |
||||
import com.sun.star.lang.XComponent; |
||||
import com.sun.star.text.XPageCursor; |
||||
import com.sun.star.text.XText; |
||||
import com.sun.star.text.XTextCursor; |
||||
import com.sun.star.text.XTextViewCursor; |
||||
import com.sun.star.text.XTextViewCursorSupplier; |
||||
import com.sun.star.uno.UnoRuntime; |
||||
|
||||
/** |
||||
* Default file-based {@link DocumentConverter} implementation. |
||||
* <p> |
||||
* This implementation passes document data to and from the OpenOffice.org |
||||
* service as file URLs. |
||||
* <p> |
||||
* File-based conversions are faster than stream-based ones (provided by |
||||
* {@link StreamOpenOfficeDocumentConverter}) but they require the |
||||
* OpenOffice.org service to be running locally and have the correct |
||||
* permissions to the files. |
||||
* |
||||
* @see StreamOpenOfficeDocumentConverter |
||||
*/ |
||||
public class WoogieDocumentConverter extends AbstractDokeosDocumentConverter { |
||||
|
||||
public WoogieDocumentConverter(OpenOfficeConnection connection, int width, int height) { |
||||
super(connection, width, height); |
||||
} |
||||
|
||||
public WoogieDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) { |
||||
super(connection, formatRegistry, width, height); |
||||
} |
||||
|
||||
protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception { |
||||
XComponentLoader desktop = openOfficeConnection.getDesktop(); |
||||
XComponent document = desktop.loadComponentFromURL(inputUrl, "_blank", 0, null); |
||||
|
||||
|
||||
if (document == null) { |
||||
throw new OpenOfficeException("conversion failed: input document is null after loading"); |
||||
} |
||||
|
||||
refreshDocument(document); |
||||
|
||||
try { |
||||
|
||||
// filter
|
||||
PropertyValue[] loadProps = new PropertyValue[4]; |
||||
|
||||
// type of image
|
||||
loadProps[0] = new PropertyValue(); |
||||
loadProps[0].Name = "MediaType"; |
||||
loadProps[0].Value = "image/png"; |
||||
|
||||
// Height and width
|
||||
PropertyValue[] filterDatas = new PropertyValue[4]; |
||||
for(int i = 0; i<4 ; i++){ |
||||
filterDatas[i] = new PropertyValue(); |
||||
} |
||||
|
||||
filterDatas[0].Name = "PixelWidth"; |
||||
filterDatas[0].Value = new Integer(this.width); |
||||
filterDatas[1].Name = "PixelHeight"; |
||||
filterDatas[1].Value = new Integer(this.height); |
||||
filterDatas[2].Name = "LogicalWidth"; |
||||
filterDatas[2].Value = new Integer(2000); |
||||
filterDatas[3].Name = "LogicalHeight"; |
||||
filterDatas[3].Value = new Integer(2000); |
||||
// query its XDesktop interface, we need the current component
|
||||
|
||||
XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface( |
||||
|
||||
XDesktop.class, desktop); |
||||
|
||||
|
||||
XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, document); |
||||
|
||||
// the model knows its controller
|
||||
|
||||
XController xController = xModel.getCurrentController(); |
||||
|
||||
XTextViewCursorSupplier xViewCursorSupplier = (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController); |
||||
|
||||
// get the cursor
|
||||
XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor(); |
||||
|
||||
|
||||
XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface( |
||||
|
||||
XPageCursor.class, xViewCursor); |
||||
|
||||
|
||||
XText xDocumentText = xViewCursor.getText(); |
||||
|
||||
|
||||
|
||||
XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor); |
||||
|
||||
|
||||
do{ // swith to the next page
|
||||
|
||||
// select the current page of document with the cursor
|
||||
xPageCursor.jumpToEndOfPage(); |
||||
xModelCursor.gotoRange(xViewCursor,false); |
||||
xModelCursor.setString("||page_break||"); |
||||
|
||||
} while(xPageCursor.jumpToNextPage()); |
||||
|
||||
|
||||
|
||||
} finally { |
||||
|
||||
// store the document
|
||||
XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); |
||||
|
||||
storable.storeToURL(outputUrl+"/fichier.html", toPropertyValues(storeProperties)); |
||||
document.dispose(); |
||||
} |
||||
} |
||||
} |
Binary file not shown.
Loading…
Reference in new issue