AbstractDokeosDocumentConverter.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // DokeosConverter using JODConverter - Java OpenDocument Converter
  3. // Eric Marguin <e.marguin@elixir-interactive.com>
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. // http://www.gnu.org/copyleft/lesser.html
  15. //
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.OutputStream;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.apache.commons.io.FilenameUtils;
  25. import org.apache.commons.io.IOUtils;
  26. import com.artofsolving.jodconverter.DocumentConverter;
  27. import com.artofsolving.jodconverter.DocumentFormat;
  28. import com.artofsolving.jodconverter.DocumentFormatRegistry;
  29. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  30. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
  31. import com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter;
  32. import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
  33. import com.sun.star.beans.PropertyValue;
  34. import com.sun.star.container.XNamed;
  35. import com.sun.star.document.XExporter;
  36. import com.sun.star.document.XFilter;
  37. import com.sun.star.drawing.XDrawPage;
  38. import com.sun.star.drawing.XDrawPages;
  39. import com.sun.star.drawing.XDrawPagesSupplier;
  40. import com.sun.star.frame.XComponentLoader;
  41. import com.sun.star.lang.XComponent;
  42. import com.sun.star.lang.XMultiComponentFactory;
  43. import com.sun.star.ucb.XFileIdentifierConverter;
  44. import com.sun.star.uno.UnoRuntime;
  45. /**
  46. * Default file-based {@link DocumentConverter} implementation.
  47. * <p>
  48. * This implementation passes document data to and from the OpenOffice.org
  49. * service as file URLs.
  50. * <p>
  51. * File-based conversions are faster than stream-based ones (provided by
  52. * {@link StreamOpenOfficeDocumentConverter}) but they require the
  53. * OpenOffice.org service to be running locally and have the correct
  54. * permissions to the files.
  55. *
  56. * @see StreamOpenOfficeDocumentConverter
  57. */
  58. public abstract class AbstractDokeosDocumentConverter extends AbstractOpenOfficeDocumentConverter {
  59. int width;
  60. int height;
  61. public AbstractDokeosDocumentConverter(OpenOfficeConnection connection, int width, int height) {
  62. super(connection);
  63. this.width = width;
  64. this.height = height;
  65. }
  66. public AbstractDokeosDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) {
  67. super(connection, formatRegistry);
  68. this.width = width;
  69. this.height = height;
  70. }
  71. /**
  72. * In this file-based implementation, streams are emulated using temporary files.
  73. */
  74. protected void convertInternal(InputStream inputStream, DocumentFormat inputFormat, OutputStream outputStream, DocumentFormat outputFormat) {
  75. File inputFile = null;
  76. File outputFile = null;
  77. try {
  78. inputFile = File.createTempFile("document", "." + inputFormat.getFileExtension());
  79. OutputStream inputFileStream = null;
  80. try {
  81. inputFileStream = new FileOutputStream(inputFile);
  82. IOUtils.copy(inputStream, inputFileStream);
  83. } finally {
  84. IOUtils.closeQuietly(inputFileStream);
  85. }
  86. outputFile = File.createTempFile("document", "." + outputFormat.getFileExtension());
  87. convert(inputFile, inputFormat, outputFile, outputFormat);
  88. InputStream outputFileStream = null;
  89. try {
  90. outputFileStream = new FileInputStream(outputFile);
  91. IOUtils.copy(outputFileStream, outputStream);
  92. } finally {
  93. IOUtils.closeQuietly(outputFileStream);
  94. }
  95. } catch (IOException ioException) {
  96. throw new OpenOfficeException("conversion failed", ioException);
  97. } finally {
  98. if (inputFile != null) {
  99. inputFile.delete();
  100. }
  101. if (outputFile != null) {
  102. outputFile.delete();
  103. }
  104. }
  105. }
  106. protected void convertInternal(File inputFile, DocumentFormat inputFormat, File outputFile, DocumentFormat outputFormat) {
  107. Map/*<String,Object>*/ loadProperties = new HashMap();
  108. loadProperties.putAll(getDefaultLoadProperties());
  109. loadProperties.putAll(inputFormat.getImportOptions());
  110. Map/*<String,Object>*/ storeProperties = outputFormat.getExportOptions(inputFormat.getFamily());
  111. synchronized (openOfficeConnection) {
  112. XFileIdentifierConverter fileContentProvider = openOfficeConnection.getFileContentProvider();
  113. String inputUrl = fileContentProvider.getFileURLFromSystemPath("", inputFile.getAbsolutePath());
  114. String outputUrl = fileContentProvider.getFileURLFromSystemPath("", outputFile.getAbsolutePath());
  115. try {
  116. loadAndExport(inputUrl, loadProperties, outputUrl, storeProperties);
  117. } catch (OpenOfficeException openOfficeException) {
  118. throw openOfficeException;
  119. } catch (Throwable throwable) {
  120. // difficult to provide finer grained error reporting here;
  121. // OOo seems to throw ErrorCodeIOException most of the time
  122. throw new OpenOfficeException("conversion failed", throwable);
  123. }
  124. }
  125. }
  126. abstract protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception;
  127. protected DocumentFormat guessDocumentFormat(File file) {
  128. String extension = FilenameUtils.getExtension(file.getName());
  129. DocumentFormat format = getDocumentFormatRegistry().getFormatByFileExtension(extension);
  130. if (format == null) {
  131. //throw new IllegalArgumentException("unknown document format for file: " + file);
  132. }
  133. return format;
  134. }
  135. }