123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- import java.awt.Event;
- import com.enterprisedt.net.ftp.FTPClient;
- import com.enterprisedt.net.ftp.FTPConnectMode;
- import com.enterprisedt.net.ftp.FTPTransferType;
- import com.sun.star.beans.PropertyValue;
- import com.sun.star.beans.XPropertySet;
- import com.sun.star.bridge.XBridge;
- import com.sun.star.bridge.XBridgeFactory;
- import com.sun.star.connection.NoConnectException;
- import com.sun.star.connection.XConnection;
- import com.sun.star.connection.XConnector;
- 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.uno.UnoRuntime;
- import com.sun.star.uno.XComponentContext;
- public class DocumentConverter {
-
- static XComponentLoader xcomponentloader = null;
-
-
- public static void main(String args[]) {
-
- String cnx, ftpuser, host, port, url, ftpPasswd, destinationFolder, remoteFolderFullPath, remoteFolder;
- int width, height;
-
- try {
- host = args[0];
- port = args[1];
- url = args[2];
- destinationFolder = args[3];
- width = Integer.parseInt(args[4]);
- height = Integer.parseInt(args[5]);
- if(args.length == 8){
- ftpuser = args[6];
- ftpPasswd = args[7];
- }
- else{
- ftpuser = "";
- ftpPasswd = "";
- }
-
-
- if(host.equals("localhost")){
- String prefix = "file://";
- if(url.charAt(0)!='/')
- prefix += '/';
- url = prefix+url;
- remoteFolder = destinationFolder;
- remoteFolderFullPath = prefix;
- }
- else {
- remoteFolderFullPath = "file:///home/"+ftpuser+"/";
- remoteFolder = url.replace('/','_');
- remoteFolder = removeAccents(remoteFolder);
- }
-
- cnx = "socket,host="+host+",port="+port;
-
- XComponentContext xComponentContext = com.sun.star.comp.helper.Bootstrap
- .createInitialComponentContext(null);
-
-
- XComponentContext xRemoteContext = xComponentContext;
- Object x = xRemoteContext
- .getServiceManager()
- .createInstanceWithContext(
- "com.sun.star.connection.Connector", xRemoteContext);
- XConnector xConnector = (XConnector) UnoRuntime.queryInterface(
- XConnector.class, x);
-
- XConnection connection = xConnector.connect(cnx);
-
-
- x = xRemoteContext.getServiceManager().createInstanceWithContext(
- "com.sun.star.bridge.BridgeFactory", xRemoteContext);
-
- XBridgeFactory xBridgeFactory = (XBridgeFactory) UnoRuntime
- .queryInterface(XBridgeFactory.class, x);
-
-
- XBridge bridge = xBridgeFactory.createBridge("", "urp", connection,null);
-
-
-
- x = bridge.getInstance("StarOffice.ServiceManager");
-
- XMultiComponentFactory xMultiComponentFactory = (XMultiComponentFactory) UnoRuntime
- .queryInterface(XMultiComponentFactory.class, x);
- XPropertySet xProperySet = (XPropertySet) UnoRuntime
- .queryInterface(XPropertySet.class, xMultiComponentFactory);
-
-
- Object oDefaultContext = xProperySet
- .getPropertyValue("DefaultContext");
-
- xComponentContext = (XComponentContext) UnoRuntime.queryInterface(
- XComponentContext.class, oDefaultContext);
-
-
- while (xcomponentloader == null) {
- try {
-
- xcomponentloader = (XComponentLoader) UnoRuntime
- .queryInterface(
- XComponentLoader.class,
- xMultiComponentFactory
- .createInstanceWithContext(
- "com.sun.star.frame.Desktop",
- xComponentContext));
-
-
-
- FTPClient ftp = new FTPClient();
- if(!host.equals("localhost")){
-
- ftp.setRemoteHost(host);
- ftp.connect();
- ftp.login(ftpuser, ftpPasswd);
- ftp.setConnectMode(FTPConnectMode.PASV);
- ftp.setType(FTPTransferType.BINARY);
- try{
- ftp.mkdir(remoteFolder);
- }catch(Exception e){}
- ftp.chdir(remoteFolder);
- ftp.put(url,"presentation.ppt");
- url = remoteFolderFullPath+"/"+remoteFolder+"/presentation.ppt";
-
-
- }
-
- PropertyValue[] loadProps = new PropertyValue[2];
- loadProps[0] = new PropertyValue();
- loadProps[0].Name = "Hidden";
- loadProps[0].Value = new Boolean(true);
-
-
- XComponent component = xcomponentloader
- .loadComponentFromURL(url,
- "_blank", 0, loadProps);
-
-
-
-
-
- loadProps = new PropertyValue[4];
-
-
- loadProps[0] = new PropertyValue();
- loadProps[0].Name = "MediaType";
- loadProps[0].Value = "image/png";
-
-
- 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(width);
- filterDatas[1].Name = "PixelHeight";
- filterDatas[1].Value = new Integer(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, component);
-
- 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));
-
-
- Object GraphicExportFilter = xMultiComponentFactory
- .createInstanceWithContext(
- "com.sun.star.drawing.GraphicExportFilter",
- xComponentContext);
- 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 = remoteFolderFullPath+remoteFolder+"/"+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");
-
-
- }
-
- if(!host.equals("localhost")){
- String[] files = ftp.dir();
- for (int i = 0; i < files.length; i++){
-
- if(!files[i].equals("presentation.ppt"))
- ftp.get(destinationFolder+"/"+files[i],files[i]);
- ftp.delete(files[i]);
- }
- ftp.chdir("..");
- ftp.rmdir(remoteFolder);
- ftp.quit();
- }
-
-
- component.dispose();
-
- System.exit(0);
- }
- catch (NoConnectException e) {
- System.out.println(e.toString());
- e.printStackTrace();
- System.exit(255);
- }
- catch (Exception e) {
- System.out.println(e.toString());
- e.printStackTrace();
- System.exit(255);
- }
- }
- }
- catch (Exception e) {
- System.out.println(e.toString());
- e.printStackTrace();
- System.exit(255);
- }
- }
-
- public static String removeAccents(String text) {
-
-
-
-
- return java.text.Normalizer.normalize(text, java.text.Normalizer.Form.NFD).replaceAll("[\u0300-\u036F]", "");
- }
-
- public boolean handleEvent(Event evt) {
-
- if ( evt.id == evt.WINDOW_DESTROY ) {
- System.exit(0) ;
- return true ;
- }
- return false ;
- }
- }
|