📄 openofficeservices.java
字号:
/* * $Id: $ * * Copyright 2001-2006 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */package org.ofbiz.content.openoffice;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.FileNotFoundException;import java.util.Map;import java.util.Random;import java.sql.Timestamp;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilDateTime;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;import com.sun.star.beans.PropertyValue;import com.sun.star.beans.XPropertySet;import com.sun.star.frame.XComponentLoader;import com.sun.star.frame.XDesktop;import com.sun.star.frame.XDispatchHelper;import com.sun.star.frame.XDispatchProvider;import com.sun.star.frame.XFrame;import com.sun.star.frame.XStorable;import com.sun.star.lang.XComponent;import com.sun.star.lang.XMultiComponentFactory;import com.sun.star.uno.UnoRuntime;import com.sun.star.uno.XComponentContext;/** * OpenOfficeServices Class * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> */public class OpenOfficeServices { public static final String module = OpenOfficeServices.class.getName(); /** * 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"); Timestamp ts = UtilDateTime.nowTimestamp(); Random random = new Random(ts.getTime()); String uniqueSeqNum = Long.toString(random.nextLong()); 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"); String outputMimeType = (String) context.get("outputMimeType"); String extName = OpenOfficeWorker.getExtensionFromMimeType(outputMimeType); fileOutName += "." + extName; // if these are empty don't worry, the OpenOfficeWorker down below will take care of it String oooHost = (String) context.get("oooHost"); String oooPort = (String) context.get("oooPort"); try { xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); byte[] inByteArray = inByteWrapper.getBytes(); // 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, "file://" + tempDir + fileInName, "file://" + 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(); } 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 { if (fileIn != null) fileIn.delete(); if (fileOut != null) fileOut.delete(); } return results; } /** * Use OpenOffice to convert documents between types */ public static Map convertDocument(DispatchContext dctx, Map context) { XMultiComponentFactory xmulticomponentfactory = null; String stringUrl = "file:///" + context.get("filenameFrom"); String stringConvertedFile = "file:///" + context.get("filenameTo"); String filterName = "file:///" + context.get("filterName"); // if these are empty don't worry, the OpenOfficeWorker down below will take care of it String oooHost = (String) context.get("oooHost"); String oooPort = (String) context.get("oooPort"); try { xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); OpenOfficeWorker.convertOODocToFile(xmulticomponentfactory, stringUrl, stringConvertedFile, filterName); Map results = ServiceUtil.returnSuccess(); return results; } 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()); } } /** * Use OpenOffice to convert documents between types */ public static Map convertDocumentFileToFile(DispatchContext dctx, Map context) { XMultiComponentFactory xmulticomponentfactory = null; String stringUrl = (String) context.get("filenameFrom"); String stringConvertedFile = (String) context.get("filenameTo"); String inputMimeType = (String) context.get("inputMimeType"); String outputMimeType = (String) context.get("outputMimeType"); // if these are empty don't worry, the OpenOfficeWorker down below will take care of it String oooHost = (String) context.get("oooHost"); String oooPort = (String) context.get("oooPort"); try { xmulticomponentfactory = OpenOfficeWorker.getRemoteServer(oooHost, oooPort); File inputFile = new File(stringUrl); long fileSize = inputFile.length(); FileInputStream fis = new FileInputStream(inputFile); ByteArrayOutputStream baos = new ByteArrayOutputStream((int)fileSize); int c;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -