KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > openoffice > OpenOfficeServices


1 /*
2  * $Id: $
3  *
4  * Copyright 2001-2006 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7  * use this file except in compliance with the License. You may obtain a copy of
8  * the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */

18 package org.ofbiz.content.openoffice;
19
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Random JavaDoc;
29 import java.sql.Timestamp JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.UtilValidate;
33 import org.ofbiz.base.util.UtilDateTime;
34 import org.ofbiz.base.util.UtilProperties;
35 import org.ofbiz.entity.util.ByteWrapper;
36 import org.ofbiz.entity.GenericDelegator;
37 import org.ofbiz.service.DispatchContext;
38 import org.ofbiz.service.ServiceUtil;
39
40 import com.sun.star.beans.PropertyValue;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.frame.XComponentLoader;
43 import com.sun.star.frame.XDesktop;
44 import com.sun.star.frame.XDispatchHelper;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XFrame;
47 import com.sun.star.frame.XStorable;
48 import com.sun.star.lang.XComponent;
49 import com.sun.star.lang.XMultiComponentFactory;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XComponentContext;
52
53 /**
54  * OpenOfficeServices Class
55  *
56  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
57  */

58 public class OpenOfficeServices {
59     public static final String JavaDoc module = OpenOfficeServices.class.getName();
60
61     /**
62      * Use OpenOffice to convert documents between types
63      */

64     public static Map JavaDoc convertDocumentByteWrapper(DispatchContext dctx, Map JavaDoc context) {
65         
66         Map JavaDoc results = ServiceUtil.returnSuccess();
67         GenericDelegator delegator = dctx.getDelegator();
68         XMultiComponentFactory xmulticomponentfactory = null;
69         //String uniqueSeqNum = delegator.getNextSeqId("OOTempDir");
70
Timestamp JavaDoc ts = UtilDateTime.nowTimestamp();
71         Random JavaDoc random = new Random JavaDoc(ts.getTime());
72         String JavaDoc uniqueSeqNum = Long.toString(random.nextLong());
73         String JavaDoc fileInName = "OOIN_" + uniqueSeqNum;
74         String JavaDoc fileOutName = "OOOUT_" + uniqueSeqNum;
75         File JavaDoc fileIn = null;
76         File JavaDoc fileOut = null;
77         
78         ByteWrapper inByteWrapper = (ByteWrapper) context.get("inByteWrapper");
79         String JavaDoc inputMimeType = (String JavaDoc) context.get("inputMimeType");
80         String JavaDoc outputMimeType = (String JavaDoc) context.get("outputMimeType");
81         String JavaDoc extName = OpenOfficeWorker.getExtensionFromMimeType(outputMimeType);
82         fileOutName += "." + extName;
83
84         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
85
String JavaDoc oooHost = (String JavaDoc) context.get("oooHost");
86         String JavaDoc oooPort = (String JavaDoc) context.get("oooPort");
87         
88         try {
89             xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
90             byte[] inByteArray = inByteWrapper.getBytes();
91             
92             // The following line work in linux, but not Windows or Mac environment. It is preferred because it does not use temporary files
93
//OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(inByteArray);
94
//Debug.logInfo("Doing convertDocumentByteWrapper, inBytes size is [" + inByteArray.length + "]", module);
95
//OpenOfficeByteArrayOutputStream baos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
96

97             
98             String JavaDoc tempDir = UtilProperties.getPropertyValue("content", "content.temp.dir");
99             fileIn = new File JavaDoc(tempDir + fileInName);
100             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileIn);
101             fos.write(inByteArray);
102             fos.close();
103             OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, "file://" + tempDir + fileInName, "file://" + tempDir + fileOutName, outputMimeType);
104             fileOut = new File JavaDoc(tempDir + fileOutName);
105             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fileOut);
106             int c;
107             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
108             while ((c=fis.read()) > -1) {
109                 baos.write(c);
110             }
111             fis.close();
112             
113             results.put("outByteWrapper", new ByteWrapper(baos.toByteArray()));
114             baos.close();
115
116         } catch (FileNotFoundException JavaDoc e) {
117             Debug.logError(e, "Error in OpenOffice operation: ", module);
118             return ServiceUtil.returnError(e.toString());
119         } catch (IOException JavaDoc e) {
120             Debug.logError(e, "Error in OpenOffice operation: ", module);
121             return ServiceUtil.returnError(e.toString());
122         } catch(Exception JavaDoc e) {
123             Debug.logError(e, "Error in OpenOffice operation: ", module);
124             return ServiceUtil.returnError(e.toString());
125         } finally {
126             if (fileIn != null) fileIn.delete();
127             if (fileOut != null) fileOut.delete();
128         }
129         return results;
130     }
131
132     /**
133      * Use OpenOffice to convert documents between types
134      */

135     public static Map JavaDoc convertDocument(DispatchContext dctx, Map JavaDoc context) {
136         XMultiComponentFactory xmulticomponentfactory = null;
137         
138         String JavaDoc stringUrl = "file:///" + context.get("filenameFrom");
139         String JavaDoc stringConvertedFile = "file:///" + context.get("filenameTo");
140         String JavaDoc filterName = "file:///" + context.get("filterName");
141
142         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
143
String JavaDoc oooHost = (String JavaDoc) context.get("oooHost");
144         String JavaDoc oooPort = (String JavaDoc) context.get("oooPort");
145         
146         try {
147             xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
148             OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, stringUrl, stringConvertedFile, filterName);
149             
150             Map JavaDoc results = ServiceUtil.returnSuccess();
151             return results;
152         } catch (IOException JavaDoc e) {
153             Debug.logError(e, "Error in OpenOffice operation: ", module);
154             return ServiceUtil.returnError(e.toString());
155         } catch(Exception JavaDoc e) {
156             Debug.logError(e, "Error in OpenOffice operation: ", module);
157             return ServiceUtil.returnError(e.toString());
158         }
159     }
160
161     /**
162      * Use OpenOffice to convert documents between types
163      */

164     public static Map JavaDoc convertDocumentFileToFile(DispatchContext dctx, Map JavaDoc context) {
165         XMultiComponentFactory xmulticomponentfactory = null;
166         
167         String JavaDoc stringUrl = (String JavaDoc) context.get("filenameFrom");
168         String JavaDoc stringConvertedFile = (String JavaDoc) context.get("filenameTo");
169         String JavaDoc inputMimeType = (String JavaDoc) context.get("inputMimeType");
170         String JavaDoc outputMimeType = (String JavaDoc) context.get("outputMimeType");
171
172         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
173
String JavaDoc oooHost = (String JavaDoc) context.get("oooHost");
174         String JavaDoc oooPort = (String JavaDoc) context.get("oooPort");
175         
176         try {
177             xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
178             File JavaDoc inputFile = new File JavaDoc(stringUrl);
179             long fileSize = inputFile.length();
180             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(inputFile);
181             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc((int)fileSize);
182             int c;
183             while ((c = fis.read()) != -1) {
184                 baos.write(c);
185             }
186             OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray());
187             OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
188             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(stringConvertedFile);
189             fos.write(oobaos.toByteArray());
190             fos.close();
191             fis.close();
192             oobais.close();
193             oobaos.close();
194             
195             Map JavaDoc results = ServiceUtil.returnSuccess();
196             return results;
197         } catch (IOException JavaDoc e) {
198             Debug.logError(e, "Error in OpenOffice operation: ", module);
199             return ServiceUtil.returnError(e.toString());
200         } catch(Exception JavaDoc e) {
201             Debug.logError(e, "Error in OpenOffice operation: ", module);
202             return ServiceUtil.returnError(e.toString());
203         }
204     }
205
206     /**
207      * Use OpenOffice to convert documents between types
208      */

209     public static Map JavaDoc convertDocumentStreamToStream(DispatchContext dctx, Map JavaDoc context) {
210         XMultiComponentFactory xmulticomponentfactory = null;
211         
212         String JavaDoc stringUrl = "file:///" + context.get("filenameFrom");
213         String JavaDoc stringConvertedFile = "file:///" + context.get("filenameTo");
214         String JavaDoc inputMimeType = (String JavaDoc) context.get("inputMimeType");
215         String JavaDoc outputMimeType = (String JavaDoc) context.get("outputMimeType");
216
217         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
218
String JavaDoc oooHost = (String JavaDoc) context.get("oooHost");
219         String JavaDoc oooPort = (String JavaDoc) context.get("oooPort");
220         
221         try {
222             xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
223             File JavaDoc inputFile = new File JavaDoc(stringUrl);
224             long fileSize = inputFile.length();
225             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(inputFile);
226             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc((int)fileSize);
227             int c;
228             while ((c = fis.read()) != -1) {
229                 baos.write(c);
230             }
231             OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(baos.toByteArray());
232             OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
233             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(stringConvertedFile);
234             fos.write(oobaos.toByteArray());
235             fos.close();
236             fis.close();
237             oobais.close();
238             oobaos.close();
239             
240             Map JavaDoc results = ServiceUtil.returnSuccess();
241             return results;
242         } catch (IOException JavaDoc e) {
243             Debug.logError(e, "Error in OpenOffice operation: ", module);
244             return ServiceUtil.returnError(e.toString());
245         } catch(Exception JavaDoc e) {
246             Debug.logError(e, "Error in OpenOffice operation: ", module);
247             return ServiceUtil.returnError(e.toString());
248         }
249     }
250
251     /**
252      * Use OpenOffice to compare documents
253      */

254     public static Map JavaDoc compareDocuments(DispatchContext dctx, Map JavaDoc context) {
255         XMultiComponentFactory xmulticomponentfactory = null;
256         
257         String JavaDoc stringUrl = "file:///" + context.get("filenameFrom");
258         String JavaDoc stringOriginalFile = "file:///" + context.get("filenameOriginal");
259         String JavaDoc stringOutFile = "file:///" + context.get("filenameOut");
260
261         // if these are empty don't worry, the OpenOfficeWorker down below will take care of it
262
String JavaDoc oooHost = (String JavaDoc)context.get("oooHost");
263         String JavaDoc oooPort = (String JavaDoc)context.get("oooPort");
264         
265         try {
266             xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
267         } catch (IOException JavaDoc e) {
268             Debug.logError(e, "Error in OpenOffice operation: ", module);
269             return ServiceUtil.returnError(e.toString());
270         } catch(Exception JavaDoc e) {
271             Debug.logError(e, "Error in OpenOffice operation: ", module);
272             return ServiceUtil.returnError(e.toString());
273         }
274         //System.out.println("xmulticomponentfactory: " + xmulticomponentfactory);
275

276         // Converting the document to the favoured type
277
try {
278             // Composing the URL
279

280             
281             // Query for the XPropertySet interface.
282
XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory);
283             
284             // Get the default context from the office server.
285
Object JavaDoc objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext");
286             
287             // Query for the interface XComponentContext.
288
XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext);
289             
290             /* A desktop environment contains tasks with one or more
291                frames in which components can be loaded. Desktop is the
292                environment for components which can instanciate within
293                frames. */

294             
295             Object JavaDoc desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);
296             XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj);
297             XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj);
298            
299             
300             // Preparing properties for loading the document
301
PropertyValue propertyvalue[] = new PropertyValue[ 1 ];
302             // Setting the flag for hidding the open document
303
propertyvalue[ 0 ] = new PropertyValue();
304             propertyvalue[ 0 ].Name = "Hidden";
305             propertyvalue[ 0 ].Value = new Boolean JavaDoc(true);
306             //TODO: Hardcoding opening word documents -- this will need to change.
307
//propertyvalue[ 1 ] = new PropertyValue();
308
//propertyvalue[ 1 ].Name = "FilterName";
309
//propertyvalue[ 1 ].Value = "HTML (StarWriter)";
310

311             // Loading the wanted document
312
Object JavaDoc objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue);
313             
314             // Getting an object that will offer a simple way to store a document to a URL.
315
XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);
316             
317             // Preparing properties for comparing the document
318
propertyvalue = new PropertyValue[ 1 ];
319             // Setting the flag for overwriting
320
propertyvalue[ 0 ] = new PropertyValue();
321             propertyvalue[ 0 ].Name = "URL";
322             propertyvalue[ 0 ].Value = stringOriginalFile;
323             // Setting the filter name
324
//propertyvalue[ 1 ] = new PropertyValue();
325
//propertyvalue[ 1 ].Name = "FilterName";
326
//propertyvalue[ 1 ].Value = context.get("convertFilterName");
327
XFrame frame = desktop.getCurrentFrame();
328             //XFrame frame = (XFrame) UnoRuntime.queryInterface(XFrame.class, desktop);
329
Object JavaDoc dispatchHelperObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.DispatchHelper", xcomponentcontext);
330             XDispatchHelper dispatchHelper = (XDispatchHelper) UnoRuntime.queryInterface(XDispatchHelper.class, dispatchHelperObj);
331             XDispatchProvider dispatchProvider = (XDispatchProvider) UnoRuntime.queryInterface(XDispatchProvider.class, frame);
332             dispatchHelper.executeDispatch(dispatchProvider, ".uno:CompareDocuments", "", 0, propertyvalue);
333             
334             // Preparing properties for storing the document
335
propertyvalue = new PropertyValue[ 1 ];
336             // Setting the flag for overwriting
337
propertyvalue[ 0 ] = new PropertyValue();
338             propertyvalue[ 0 ].Name = "Overwrite";
339             propertyvalue[ 0 ].Value = new Boolean JavaDoc(true);
340             // Setting the filter name
341
//propertyvalue[ 1 ] = new PropertyValue();
342
//propertyvalue[ 1 ].Name = "FilterName";
343
//propertyvalue[ 1 ].Value = context.get("convertFilterName");
344

345             Debug.logInfo("stringOutFile: "+stringOutFile, module);
346             // Storing and converting the document
347
xstorable.storeToURL(stringOutFile, propertyvalue);
348             
349             // Getting the method dispose() for closing the document
350
XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class,
351             xstorable);
352             
353             // Closing the converted document
354
xcomponent.dispose();
355             
356             Map JavaDoc results = ServiceUtil.returnSuccess();
357             return results;
358         } catch (Exception JavaDoc e) {
359             Debug.logError(e, "Error in OpenOffice operation: ", module);
360             return ServiceUtil.returnError("Error converting document: " + e.toString());
361         }
362     }
363 }
364
Popular Tags