WoogieDocumentConverter.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.util.Map;
  17. import org.apache.commons.io.FilenameUtils;
  18. import com.artofsolving.jodconverter.DocumentConverter;
  19. import com.artofsolving.jodconverter.DocumentFormatRegistry;
  20. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  21. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
  22. import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
  23. import com.sun.star.beans.PropertyValue;
  24. import com.sun.star.frame.XComponentLoader;
  25. import com.sun.star.frame.XController;
  26. import com.sun.star.frame.XDesktop;
  27. import com.sun.star.frame.XModel;
  28. import com.sun.star.frame.XStorable;
  29. import com.sun.star.lang.XComponent;
  30. import com.sun.star.text.XPageCursor;
  31. import com.sun.star.text.XText;
  32. import com.sun.star.text.XTextCursor;
  33. import com.sun.star.text.XTextViewCursor;
  34. import com.sun.star.text.XTextViewCursorSupplier;
  35. import com.sun.star.uno.UnoRuntime;
  36. /**
  37. * Default file-based {@link DocumentConverter} implementation.
  38. * <p>
  39. * This implementation passes document data to and from the OpenOffice.org
  40. * service as file URLs.
  41. * <p>
  42. * File-based conversions are faster than stream-based ones (provided by
  43. * {@link StreamOpenOfficeDocumentConverter}) but they require the
  44. * OpenOffice.org service to be running locally and have the correct
  45. * permissions to the files.
  46. *
  47. * @see StreamOpenOfficeDocumentConverter
  48. */
  49. public class WoogieDocumentConverter extends AbstractDokeosDocumentConverter {
  50. public WoogieDocumentConverter(OpenOfficeConnection connection, int width, int height) {
  51. super(connection, width, height);
  52. }
  53. public WoogieDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) {
  54. super(connection, formatRegistry, width, height);
  55. }
  56. protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception {
  57. XComponentLoader desktop = openOfficeConnection.getDesktop();
  58. XComponent document = desktop.loadComponentFromURL(inputUrl, "_blank", 0, null);
  59. if (document == null) {
  60. throw new OpenOfficeException("conversion failed: input document is null after loading");
  61. }
  62. refreshDocument(document);
  63. try {
  64. // filter
  65. PropertyValue[] loadProps = new PropertyValue[4];
  66. // type of image
  67. loadProps[0] = new PropertyValue();
  68. loadProps[0].Name = "MediaType";
  69. loadProps[0].Value = "image/png";
  70. // Height and width
  71. PropertyValue[] filterDatas = new PropertyValue[4];
  72. for(int i = 0; i<4 ; i++){
  73. filterDatas[i] = new PropertyValue();
  74. }
  75. filterDatas[0].Name = "PixelWidth";
  76. filterDatas[0].Value = new Integer(this.width);
  77. filterDatas[1].Name = "PixelHeight";
  78. filterDatas[1].Value = new Integer(this.height);
  79. filterDatas[2].Name = "LogicalWidth";
  80. filterDatas[2].Value = new Integer(2000);
  81. filterDatas[3].Name = "LogicalHeight";
  82. filterDatas[3].Value = new Integer(2000);
  83. filterDatas[3].Name = "CharacterSet";
  84. filterDatas[3].Value = "iso-8859-15";
  85. // query its XDesktop interface, we need the current component
  86. XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
  87. XDesktop.class, desktop);
  88. XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, document);
  89. // the model knows its controller
  90. XController xController = xModel.getCurrentController();
  91. XTextViewCursorSupplier xViewCursorSupplier = (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
  92. // get the cursor
  93. XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
  94. XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
  95. XPageCursor.class, xViewCursor);
  96. XText xDocumentText = xViewCursor.getText();
  97. XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor);
  98. do{ // swith to the next page
  99. // select the current page of document with the cursor
  100. xPageCursor.jumpToEndOfPage();
  101. xModelCursor.gotoRange(xViewCursor,false);
  102. xModelCursor.setString("||page_break||");
  103. } while(xPageCursor.jumpToNextPage());
  104. } finally {
  105. // store the document
  106. XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
  107. storeProperties.put("CharacterSet", "UTF-8");
  108. storable.storeToURL(outputUrl, toPropertyValues(storeProperties));
  109. document.dispose();
  110. }
  111. }
  112. }