OogieDocumentConverter.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.Arrays;
  17. import java.util.Map;
  18. import org.apache.commons.io.FilenameUtils;
  19. import com.artofsolving.jodconverter.DocumentConverter;
  20. import com.artofsolving.jodconverter.DocumentFormatRegistry;
  21. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  22. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException;
  23. import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
  24. import com.sun.star.awt.Point;
  25. import com.sun.star.beans.PropertyValue;
  26. import com.sun.star.container.XNamed;
  27. import com.sun.star.document.XExporter;
  28. import com.sun.star.document.XFilter;
  29. import com.sun.star.drawing.XDrawPage;
  30. import com.sun.star.drawing.XDrawPages;
  31. import com.sun.star.drawing.XDrawPagesSupplier;
  32. import com.sun.star.drawing.XShape;
  33. import com.sun.star.drawing.XShapes;
  34. import com.sun.star.frame.XComponentLoader;
  35. import com.sun.star.lang.XComponent;
  36. import com.sun.star.lang.XMultiComponentFactory;
  37. import com.sun.star.text.XText;
  38. import com.sun.star.uno.UnoRuntime;
  39. /**
  40. * Default file-based {@link DocumentConverter} implementation.
  41. * <p>
  42. * This implementation passes document data to and from the OpenOffice.org
  43. * service as file URLs.
  44. * <p>
  45. * File-based conversions are faster than stream-based ones (provided by
  46. * {@link StreamOpenOfficeDocumentConverter}) but they require the
  47. * OpenOffice.org service to be running locally and have the correct
  48. * permissions to the files.
  49. *
  50. * @see StreamOpenOfficeDocumentConverter
  51. */
  52. public class OogieDocumentConverter extends AbstractDokeosDocumentConverter {
  53. public OogieDocumentConverter(OpenOfficeConnection connection, int width, int height) {
  54. super(connection, width, height);
  55. }
  56. public OogieDocumentConverter(OpenOfficeConnection connection, DocumentFormatRegistry formatRegistry, int width, int height) {
  57. super(connection, formatRegistry, width, height);
  58. }
  59. protected void loadAndExport(String inputUrl, Map/*<String,Object>*/ loadProperties, String outputUrl, Map/*<String,Object>*/ storeProperties) throws Exception {
  60. XComponentLoader desktop = openOfficeConnection.getDesktop();
  61. XComponent document = desktop.loadComponentFromURL(inputUrl, "_blank", 0, toPropertyValues(loadProperties));
  62. if (document == null) {
  63. throw new OpenOfficeException("conversion failed: input document is null after loading");
  64. }
  65. refreshDocument(document);
  66. try {
  67. outputUrl = FilenameUtils.getFullPath(outputUrl)+FilenameUtils.getBaseName(outputUrl);
  68. // filter
  69. PropertyValue[] loadProps = new PropertyValue[4];
  70. // type of image
  71. loadProps[0] = new PropertyValue();
  72. loadProps[0].Name = "MediaType";
  73. loadProps[0].Value = "image/png";
  74. // Height and width
  75. PropertyValue[] filterDatas = new PropertyValue[4];
  76. for(int i = 0; i<4 ; i++){
  77. filterDatas[i] = new PropertyValue();
  78. }
  79. filterDatas[0].Name = "PixelWidth";
  80. filterDatas[0].Value = new Integer(this.width);
  81. filterDatas[1].Name = "PixelHeight";
  82. filterDatas[1].Value = new Integer(this.height);
  83. filterDatas[2].Name = "LogicalWidth";
  84. filterDatas[2].Value = new Integer(2000);
  85. filterDatas[3].Name = "LogicalHeight";
  86. filterDatas[3].Value = new Integer(2000);
  87. XDrawPagesSupplier pagesSupplier = (XDrawPagesSupplier) UnoRuntime
  88. .queryInterface(XDrawPagesSupplier.class, document);
  89. //System.out.println(pagesSupplier.toString());
  90. XDrawPages pages = pagesSupplier.getDrawPages();
  91. int nbPages = pages.getCount();
  92. String[] slidenames = new String[nbPages];
  93. Arrays.fill(slidenames,"");
  94. for (int i = 0; i < nbPages; i++) {
  95. XDrawPage page = (XDrawPage) UnoRuntime.queryInterface(
  96. com.sun.star.drawing.XDrawPage.class, pages
  97. .getByIndex(i));
  98. // get all the page shapes
  99. XShapes xShapes = (XShapes)UnoRuntime.queryInterface(XShapes.class, page);
  100. int top = 0;
  101. String slidename = "";
  102. String slidebody = "";
  103. String shapetext = "";
  104. for (int j = 0; j < xShapes.getCount(); j++) {
  105. XShape firstXshape = (XShape)UnoRuntime.queryInterface(XShape.class, xShapes.getByIndex(j));
  106. Point pos = firstXshape.getPosition();
  107. XText xText = (XText)UnoRuntime.queryInterface( XText.class, firstXshape );
  108. if(xText!=null && xText.getString().length()>0)
  109. {
  110. shapetext = xText.getString();
  111. // concatening all shape texts to later use
  112. slidebody += " " + shapetext;
  113. // get the top shape
  114. if(pos.Y < top || top==0)
  115. {
  116. top = pos.Y;
  117. slidename = shapetext;
  118. }
  119. }
  120. }
  121. // remove unwanted chars
  122. slidebody = slidebody.replaceAll("\n", " ");
  123. String slidenameDisplayed = "";
  124. if(slidename.trim().length()==0)
  125. {
  126. slidename = "slide"+(i+1);
  127. }
  128. else
  129. {
  130. int nbSpaces = 0;
  131. String formatedSlidename = "";
  132. slidename = slidename.replaceAll(" ", "_");
  133. slidename = slidename.replaceAll("\n", "_");
  134. slidename = slidename.replaceAll("__", "_");
  135. for(int j=0 ; j<slidename.length() ; j++)
  136. {
  137. char currentChar = slidename.charAt(j);
  138. if(currentChar=='_')
  139. {
  140. nbSpaces++;
  141. }
  142. if(nbSpaces == 5)
  143. {
  144. break;
  145. }
  146. formatedSlidename += slidename.charAt(j);
  147. }
  148. slidenameDisplayed = formatedSlidename;
  149. slidename = formatedSlidename.toLowerCase();
  150. slidename = slidename.replaceAll("\\W", "_");
  151. slidename = slidename.replaceAll("__", "_");
  152. slidename = StringOperation.sansAccent(slidename);
  153. }
  154. int j=1;
  155. String slidenamebackup = slidename;
  156. Arrays.sort(slidenames);
  157. while(Arrays.binarySearch(slidenames, slidename)>=0)
  158. {
  159. j++;
  160. slidename = slidenamebackup+j;
  161. }
  162. slidenames[nbPages-(i+1)] = slidename;
  163. XNamed xPageName = (XNamed)UnoRuntime.queryInterface(XNamed.class,page);
  164. xPageName.setName(slidename);
  165. XMultiComponentFactory localServiceManager = ((DokeosSocketOfficeConnection)this.openOfficeConnection).getServiceManager();
  166. Object GraphicExportFilter = localServiceManager
  167. .createInstanceWithContext(
  168. "com.sun.star.drawing.GraphicExportFilter",
  169. ((DokeosSocketOfficeConnection)this.openOfficeConnection).getComponentContext());
  170. XExporter xExporter = (XExporter) UnoRuntime
  171. .queryInterface(XExporter.class,
  172. GraphicExportFilter);
  173. XComponent xComp = (XComponent) UnoRuntime
  174. .queryInterface(XComponent.class, page);
  175. xExporter.setSourceDocument(xComp);
  176. loadProps[1] = new PropertyValue();
  177. loadProps[1].Name = "URL";
  178. loadProps[1].Value = outputUrl+"/"+xPageName.getName()+".png";
  179. loadProps[2] = new PropertyValue();
  180. loadProps[2].Name = "FilterData";
  181. loadProps[2].Value = filterDatas;
  182. loadProps[3] = new PropertyValue();
  183. loadProps[3].Name = "Quality";
  184. loadProps[3].Value = new Integer(100);
  185. XFilter xFilter = (XFilter) UnoRuntime.queryInterface(XFilter.class, GraphicExportFilter);
  186. xFilter.filter(loadProps);
  187. if(slidenameDisplayed=="")
  188. slidenameDisplayed = xPageName.getName();
  189. System.out.println(slidenameDisplayed+"||"+xPageName.getName()+".png"+"||"+slidebody);
  190. }
  191. } finally {
  192. document.dispose();
  193. }
  194. }
  195. }