DocumentConverter.java.orig 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import java.awt.Event;
  2. //import sun.text.Normalizer;
  3. import com.enterprisedt.net.ftp.FTPClient;
  4. import com.enterprisedt.net.ftp.FTPConnectMode;
  5. import com.enterprisedt.net.ftp.FTPTransferType;
  6. import com.sun.star.beans.PropertyValue;
  7. import com.sun.star.beans.XPropertySet;
  8. import com.sun.star.bridge.XBridge;
  9. import com.sun.star.bridge.XBridgeFactory;
  10. import com.sun.star.connection.NoConnectException;
  11. import com.sun.star.connection.XConnection;
  12. import com.sun.star.connection.XConnector;
  13. import com.sun.star.container.XNamed;
  14. import com.sun.star.document.XExporter;
  15. import com.sun.star.document.XFilter;
  16. import com.sun.star.drawing.XDrawPage;
  17. import com.sun.star.drawing.XDrawPages;
  18. import com.sun.star.drawing.XDrawPagesSupplier;
  19. import com.sun.star.frame.XComponentLoader;
  20. import com.sun.star.lang.XComponent;
  21. import com.sun.star.lang.XMultiComponentFactory;
  22. import com.sun.star.uno.UnoRuntime;
  23. import com.sun.star.uno.XComponentContext;
  24. /**
  25. * The class <CODE>DocumentConverter</CODE> allows you to convert all
  26. * documents in a given directory and in its subdirectories to a given type. A
  27. * converted document will be created in the same directory as the origin
  28. * document.
  29. *
  30. */
  31. public class DocumentConverter {
  32. /**
  33. * Containing the loaded documents
  34. */
  35. static XComponentLoader xcomponentloader = null;
  36. /**
  37. * Connecting to the office with the component UnoUrlResolver and calling
  38. * the static method traverse
  39. *
  40. * @param args
  41. * The array of the type String contains the directory, in which
  42. * all files should be converted, the favoured converting type
  43. * and the wanted extension
  44. */
  45. public static void main(String args[]) {
  46. String cnx, ftpuser, host, port, url, ftpPasswd, destinationFolder, remoteFolderFullPath, remoteFolder;
  47. int width, height;
  48. try {
  49. host = args[0];
  50. port = args[1];
  51. url = args[2];
  52. destinationFolder = args[3];
  53. width = Integer.parseInt(args[4]);
  54. height = Integer.parseInt(args[5]);
  55. if(args.length == 8){
  56. ftpuser = args[6];
  57. ftpPasswd = args[7];
  58. }
  59. else{
  60. ftpuser = "";
  61. ftpPasswd = "";
  62. }
  63. if(host.equals("localhost")){
  64. String prefix = "file://";
  65. if(url.charAt(0)!='/')
  66. prefix += '/';
  67. url = prefix+url;
  68. remoteFolder = destinationFolder;
  69. remoteFolderFullPath = prefix;
  70. }
  71. else {
  72. remoteFolderFullPath = "file:///home/"+ftpuser+"/";
  73. remoteFolder = url.replace('/','_');
  74. remoteFolder = removeAccents(remoteFolder);
  75. }
  76. cnx = "socket,host="+host+",port="+port;
  77. XComponentContext xComponentContext = com.sun.star.comp.helper.Bootstrap
  78. .createInitialComponentContext(null);
  79. XComponentContext xRemoteContext = xComponentContext;
  80. Object x = xRemoteContext
  81. .getServiceManager()
  82. .createInstanceWithContext(
  83. "com.sun.star.connection.Connector", xRemoteContext);
  84. XConnector xConnector = (XConnector) UnoRuntime.queryInterface(
  85. XConnector.class, x);
  86. XConnection connection = xConnector.connect(cnx);
  87. //if (connection == null)
  88. //System.out.println("Connection is null");
  89. x = xRemoteContext.getServiceManager().createInstanceWithContext(
  90. "com.sun.star.bridge.BridgeFactory", xRemoteContext);
  91. XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime
  92. .queryInterface(XBridgeFactory.class, x);
  93. // this is the bridge that you will dispose
  94. XBridge bridge = xBridgeFactory.createBridge("", "urp", connection,null);
  95. /*XComponent xComponent = (XComponent) UnoRuntime.queryInterface(
  96. XComponent.class, bridge);*/
  97. // get the remote instance
  98. x = bridge.getInstance("StarOffice.ServiceManager");
  99. // Query the initial object for its main factory interface
  100. XMultiComponentFactory xMultiComponentFactory = (XMultiComponentFactory) UnoRuntime
  101. .queryInterface(XMultiComponentFactory.class, x);
  102. XPropertySet xProperySet = (XPropertySet) UnoRuntime
  103. .queryInterface(XPropertySet.class, xMultiComponentFactory);
  104. // Get the default context from the office server.
  105. Object oDefaultContext = xProperySet
  106. .getPropertyValue("DefaultContext");
  107. // Query for the interface XComponentContext.
  108. xComponentContext = (XComponentContext) UnoRuntime.queryInterface(
  109. XComponentContext.class, oDefaultContext);
  110. while (xcomponentloader == null) {
  111. try {
  112. xcomponentloader = (XComponentLoader) UnoRuntime
  113. .queryInterface(
  114. XComponentLoader.class,
  115. xMultiComponentFactory
  116. .createInstanceWithContext(
  117. "com.sun.star.frame.Desktop",
  118. xComponentContext));
  119. //System.out.println("Loading document "+url);
  120. FTPClient ftp = new FTPClient();
  121. if(!host.equals("localhost")){
  122. //ftp connexion
  123. ftp.setRemoteHost(host);
  124. ftp.connect();
  125. ftp.login(ftpuser, ftpPasswd);
  126. ftp.setConnectMode(FTPConnectMode.PASV);
  127. ftp.setType(FTPTransferType.BINARY);
  128. try{
  129. ftp.mkdir(remoteFolder);
  130. }catch(Exception e){}
  131. ftp.chdir(remoteFolder);
  132. ftp.put(url,"presentation.ppt");
  133. url = remoteFolderFullPath+"/"+remoteFolder+"/presentation.ppt";
  134. }
  135. PropertyValue[] loadProps = new PropertyValue[2];
  136. loadProps[0] = new PropertyValue();
  137. loadProps[0].Name = "Hidden";
  138. loadProps[0].Value = new Boolean(true);
  139. // open the document
  140. XComponent component = xcomponentloader
  141. .loadComponentFromURL(url,
  142. "_blank", 0, loadProps);
  143. //System.out.println("Document Opened");
  144. // filter
  145. loadProps = new PropertyValue[4];
  146. // type of image
  147. loadProps[0] = new PropertyValue();
  148. loadProps[0].Name = "MediaType";
  149. loadProps[0].Value = "image/png";
  150. // Height and width
  151. PropertyValue[] filterDatas = new PropertyValue[4];
  152. for(int i = 0; i<4 ; i++){
  153. filterDatas[i] = new PropertyValue();
  154. }
  155. filterDatas[0].Name = "PixelWidth";
  156. filterDatas[0].Value = new Integer(width);
  157. filterDatas[1].Name = "PixelHeight";
  158. filterDatas[1].Value = new Integer(height);
  159. filterDatas[2].Name = "LogicalWidth";
  160. filterDatas[2].Value = new Integer(2000);
  161. filterDatas[3].Name = "LogicalHeight";
  162. filterDatas[3].Value = new Integer(2000);
  163. XDrawPagesSupplier pagesSupplier = (XDrawPagesSupplier) UnoRuntime
  164. .queryInterface(XDrawPagesSupplier.class, component);
  165. //System.out.println(pagesSupplier.toString());
  166. XDrawPages pages = pagesSupplier.getDrawPages();
  167. int nbPages = pages.getCount();
  168. for (int i = 0; i < nbPages; i++) {
  169. XDrawPage page = (XDrawPage) UnoRuntime.queryInterface(
  170. com.sun.star.drawing.XDrawPage.class, pages
  171. .getByIndex(i));
  172. XNamed xPageName = (XNamed)UnoRuntime.queryInterface(XNamed.class,page);
  173. xPageName.setName("slide"+(i+1));
  174. //if(!xPageName.getName().equals("slide"+(i+1)) && !xPageName.getName().equals("page"+(i+1)))
  175. //xPageName.setName((i+1)+"-"+xPageName.getName());
  176. Object GraphicExportFilter = xMultiComponentFactory
  177. .createInstanceWithContext(
  178. "com.sun.star.drawing.GraphicExportFilter",
  179. xComponentContext);
  180. XExporter xExporter = (XExporter) UnoRuntime
  181. .queryInterface(XExporter.class,
  182. GraphicExportFilter);
  183. XComponent xComp = (XComponent) UnoRuntime
  184. .queryInterface(XComponent.class, page);
  185. xExporter.setSourceDocument(xComp);
  186. loadProps[1] = new PropertyValue();
  187. loadProps[1].Name = "URL";
  188. loadProps[1].Value = remoteFolderFullPath+remoteFolder+"/"+xPageName.getName()+".png";
  189. loadProps[2] = new PropertyValue();
  190. loadProps[2].Name = "FilterData";
  191. loadProps[2].Value = filterDatas;
  192. loadProps[3] = new PropertyValue();
  193. loadProps[3].Name = "Quality";
  194. loadProps[3].Value = new Integer(100);
  195. XFilter xFilter = (XFilter) UnoRuntime.queryInterface(XFilter.class, GraphicExportFilter);
  196. xFilter.filter(loadProps);
  197. System.out.println(xPageName.getName()+".png");
  198. //System.out.println("Page saved to url "+loadProps[1].Value);
  199. }
  200. if(!host.equals("localhost")){
  201. String[] files = ftp.dir();
  202. for (int i = 0; i < files.length; i++){
  203. //System.out.println("Transfer of "+files[i]+ "to "+destinationFolder+"/"+files[i]);
  204. if(!files[i].equals("presentation.ppt"))
  205. ftp.get(destinationFolder+"/"+files[i],files[i]);
  206. ftp.delete(files[i]);
  207. }
  208. ftp.chdir("..");
  209. ftp.rmdir(remoteFolder);
  210. ftp.quit();
  211. }
  212. //System.out.println("Closing Document");
  213. component.dispose();
  214. //System.out.println("Document close");
  215. System.exit(0);
  216. }
  217. catch (NoConnectException e) {
  218. System.out.println(e.toString());
  219. e.printStackTrace();
  220. System.exit(255);
  221. }
  222. catch (Exception e) {
  223. System.out.println(e.toString());
  224. e.printStackTrace();
  225. System.exit(255);
  226. }
  227. }
  228. }
  229. catch (Exception e) {
  230. System.out.println(e.toString());
  231. e.printStackTrace();
  232. System.exit(255);
  233. }
  234. }
  235. public static String removeAccents(String text) {
  236. /*
  237. String newText = Normalizer.decompose(text, false, 0)
  238. .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");*/
  239. /*
  240. newText = newText.replace('\u00B4','_');
  241. newText = newText.replace('\u02CA','_');
  242. newText = newText.replace('\u02B9','_');
  243. newText = newText.replace('\u02BC','_');
  244. newText = newText.replace('\u02B9','_');
  245. newText = newText.replace('\u03D8','_');
  246. newText = newText.replace('\u0374','_');
  247. newText = newText.replace('\u0384','_');
  248. newText = newText.replace('\u055A','_');
  249. */
  250. /*
  251. newText = newText.replace('\u2019','_');
  252. newText = newText.replace('\u00B4','_');
  253. newText = newText.replace('\u055A','_');
  254. newText = newText.replace('?','_');
  255. newText = newText.replace('\'','_');
  256. newText = newText.replace(' ','_');
  257. return newText;*/
  258. return java.text.Normalizer.normalize(text, java.text.Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
  259. }
  260. public boolean handleEvent(Event evt) {
  261. // Traitement de l'evenement de fin de programme
  262. if ( evt.id == evt.WINDOW_DESTROY ) {
  263. System.exit(0) ;
  264. return true ;
  265. }
  266. return false ;
  267. }
  268. }