[OFBiz] SVN: r7259 - trunk/applications/content/src/org/ofbiz/content/openoffice
byersa@svn.ofbiz.org
byersa at svn.ofbiz.org
Mon Apr 10 14:07:53 CDT 2006
Author: byersa
Date: 2006-04-10 14:07:50 -0500 (Mon, 10 Apr 2006)
New Revision: 7259
Modified:
trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java
trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java
Log:
Uploading these so I can port to a windows test machine.
Modified: trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java
===================================================================
--- trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java 2006-04-10 16:38:48 UTC (rev 7258)
+++ trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeServices.java 2006-04-10 19:07:50 UTC (rev 7259)
@@ -23,11 +23,14 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.Map;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.entity.util.ByteWrapper;
+import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;
@@ -56,7 +59,15 @@
* Use OpenOffice to convert documents between types
*/
public static Map convertDocumentByteWrapper(DispatchContext dctx, Map context) {
+
+ Map results = ServiceUtil.returnSuccess();
+ GenericDelegator delegator = dctx.getDelegator();
XMultiComponentFactory xmulticomponentfactory = null;
+ String uniqueSeqNum = delegator.getNextSeqId("OOTempDir");
+ String fileInName = "OOIN_" + uniqueSeqNum;
+ String fileOutName = "OOOUT_" + uniqueSeqNum;
+ File fileIn = null;
+ File fileOut = null;
ByteWrapper inByteWrapper = (ByteWrapper) context.get("inByteWrapper");
String inputMimeType = (String) context.get("inputMimeType");
@@ -69,23 +80,45 @@
try {
xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort);
byte[] inByteArray = inByteWrapper.getBytes();
- OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(inByteArray);
- Debug.logInfo("Doing convertDocumentByteWrapper, inBytes size is [" + inByteArray.length + "]", module);
- OpenOfficeByteArrayOutputStream oobaos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
- Map results = ServiceUtil.returnSuccess();
- results.put("outByteWrapper", new ByteWrapper(oobaos.toByteArray()));
- oobais.close();
- oobaos.close();
+ // The following line work in linux, but not Windows or Mac environment. It is preferred because it does not use temporary files
+ //OpenOfficeByteArrayInputStream oobais = new OpenOfficeByteArrayInputStream(inByteArray);
+ //Debug.logInfo("Doing convertDocumentByteWrapper, inBytes size is [" + inByteArray.length + "]", module);
+ // OpenOfficeByteArrayOutputStream baos = OpenOfficeWorker.convertOODocByteStreamToByteStream(xmulticomponentfactory, oobais, inputMimeType, outputMimeType);
+
+
+ String tempDir = UtilProperties.getPropertyValue("content", "content.temp.dir");
+ fileIn = new File(tempDir + fileInName);
+ FileOutputStream fos = new FileOutputStream(fileIn);
+ fos.write(inByteArray);
+ fos.close();
+ OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, tempDir + fileInName, tempDir + fileOutName, outputMimeType);
+ fileOut = new File(tempDir + fileOutName);
+ FileInputStream fis = new FileInputStream(fileOut);
+ int c;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ while ((c=fis.read()) > -1) {
+ baos.write(c);
+ }
+ fis.close();
+
+ results.put("outByteWrapper", new ByteWrapper(baos.toByteArray()));
+ baos.close();
- return results;
+ } catch (FileNotFoundException e) {
+ Debug.logError(e, "Error in OpenOffice operation: ", module);
+ return ServiceUtil.returnError(e.toString());
} catch (IOException e) {
Debug.logError(e, "Error in OpenOffice operation: ", module);
return ServiceUtil.returnError(e.toString());
} catch(Exception e) {
Debug.logError(e, "Error in OpenOffice operation: ", module);
return ServiceUtil.returnError(e.toString());
+ } finally {
+ fileIn.delete();
+ fileOut.delete();
}
+ return results;
}
/**
Modified: trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java
===================================================================
--- trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java 2006-04-10 16:38:48 UTC (rev 7258)
+++ trunk/applications/content/src/org/ofbiz/content/openoffice/OpenOfficeWorker.java 2006-04-10 19:07:50 UTC (rev 7259)
@@ -18,7 +18,11 @@
package org.ofbiz.content.openoffice;
import java.io.IOException;
+import java.io.FileNotFoundException;
import java.util.List;
+import java.util.Hashtable;
+import java.util.Properties;
+import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -38,6 +42,7 @@
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
+import com.sun.star.comp.helper.Bootstrap;
/**
* OpenOfficeWorker Class
@@ -51,6 +56,26 @@
public static final String module = OpenOfficeWorker.class.getName();
+ public static XMultiComponentFactory getRemoteServer_test(String host, String port) throws IOException, Exception {
+ XMultiComponentFactory xmulticomponentfactory = null;
+ String sofficepath = "file:///usr/lib/openoffice2/program";
+
+ String unoini = sofficepath + "/unorc"; // To change in uno.ini for Windows
+
+
+
+ Hashtable params = new Hashtable();
+
+ params.put("SYSBINDIR", sofficepath);
+ //XComponentContext xComponentContext = Bootstrap.defaultBootstrap_InitialComponentContext(unoini, params);
+ XComponentContext xComponentContext = Bootstrap.bootstrap();
+
+// XComponentContext xComponentContext = Bootstrap.bootstrap();
+ if (xComponentContext != null) {
+ xmulticomponentfactory = xComponentContext.getServiceManager();
+ }
+ return xmulticomponentfactory;
+ }
/**
* Use OpenOffice to convert documents between types
*/
@@ -93,7 +118,7 @@
// TODO: None of this works. Need a programmable start solution.
//String ooxvfb = UtilProperties.getPropertyValue("openoffice-uno", "oo.start.xvfb");
//String ooexport = UtilProperties.getPropertyValue("openoffice-uno", "oo.start.export");
- String oosoffice = UtilProperties.getPropertyValue("openoffice-uno", "oo.start.soffice");
+ // String oosoffice = UtilProperties.getPropertyValue("openoffice-uno", "oo.start.soffice");
//Process procXvfb = Runtime.getRuntime().exec(ooxvfb);
//Process procExport = Runtime.getRuntime().exec(ooexport);
/*
@@ -156,7 +181,7 @@
return filterNameList;
}
- public static void convertOODocToFile(XMultiComponentFactory xmulticomponentfactory, String stringUrl, String stringConvertedFile, String convertFilterName) throws Exception {
+ public static void convertOODocToFile(XMultiComponentFactory xmulticomponentfactory, String stringUrl, String stringConvertedFile, String outputMimeType) throws FileNotFoundException, IOException, Exception {
// Converting the document to the favoured type
// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory);
@@ -183,6 +208,7 @@
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "Hidden";
propertyvalue[ 0 ].Value = new Boolean(true);
+
//TODO: Hardcoding opening word documents -- this will need to change.
//propertyvalue[ 1 ] = new PropertyValue();
//propertyvalue[ 1 ].Name = "FilterName";
@@ -195,15 +221,17 @@
XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);
// Preparing properties for converting the document
- propertyvalue = new PropertyValue[ 1 ];
+ propertyvalue = new PropertyValue[ 2 ];
// Setting the flag for overwriting
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "Overwrite";
propertyvalue[ 0 ].Value = new Boolean(true);
// Setting the filter name
- //propertyvalue[ 1 ] = new PropertyValue();
- //propertyvalue[ 1 ].Name = "FilterName";
- //propertyvalue[ 1 ].Value = convertFilterName;
+ // Preparing properties for converting the document
+ String filterName = getFilterNameFromMimeType(outputMimeType);
+ propertyvalue[ 1 ] = new PropertyValue();
+ propertyvalue[ 1 ].Name = "FilterName";
+ propertyvalue[ 1 ].Value = filterName;
Debug.logInfo("stringConvertedFile: "+stringConvertedFile, module);
// Storing and converting the document
xstorable.storeToURL(stringConvertedFile, propertyvalue);
@@ -261,18 +289,7 @@
}
// Preparing properties for converting the document
- String filterName = "";
- if (UtilValidate.isEmpty(outputMimeType)) {
- filterName = "HTML";
- } else if (outputMimeType.equalsIgnoreCase("application/pdf")) {
- filterName = "writer_pdf_Export";
- } else if (outputMimeType.equalsIgnoreCase("application/msword")) {
- filterName = "MS Word 97";
- } else if (outputMimeType.equalsIgnoreCase("text/html")) {
- filterName = "HTML (StarWriter)";
- } else {
- filterName = "HTML";
- }
+ String filterName = getFilterNameFromMimeType(outputMimeType);
propertyvalue = new PropertyValue[4];
propertyvalue[0] = new PropertyValue();
@@ -303,4 +320,21 @@
return os;
}
+
+ public static String getFilterNameFromMimeType(String mimeType) {
+ String filterName = "";
+ if (UtilValidate.isEmpty(mimeType)) {
+ filterName = "HTML";
+ } else if (mimeType.equalsIgnoreCase("application/pdf")) {
+ filterName = "writer_pdf_Export";
+ } else if (mimeType.equalsIgnoreCase("application/msword")) {
+ filterName = "MS Word 97";
+ } else if (mimeType.equalsIgnoreCase("text/html")) {
+ filterName = "HTML (StarWriter)";
+ } else {
+ filterName = "HTML";
+ }
+ return filterName;
+
+ }
}
More information about the Svn
mailing list