📄 openofficeworker.java
字号:
/* * $Id: $ * * Copyright 2005-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.IOException;import java.io.FileNotFoundException;import java.io.File;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;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilProperties;import org.ofbiz.base.util.UtilValidate;import com.sun.star.beans.PropertyValue;import com.sun.star.beans.XPropertySet;import com.sun.star.bridge.XUnoUrlResolver;import com.sun.star.container.XNameAccess;import com.sun.star.frame.XComponentLoader;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;/** * OpenOfficeWorker Class * * Note that for this to work you must start OpenOffice with a command such as the following: * <code>soffice -accept=socket,host=localhost,port=8100;urp;</code> * * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a> */public class OpenOfficeWorker{ public static final String module = OpenOfficeWorker.class.getName(); /** * Use OpenOffice to convert documents between types */ public static XMultiComponentFactory getRemoteServer(String host, String port) throws IOException, Exception { if (UtilValidate.isEmpty(host)) host = UtilProperties.getPropertyValue("openoffice-uno", "oo.host", "localhost"); if (UtilValidate.isEmpty(port)) port = UtilProperties.getPropertyValue("openoffice-uno", "oo.port", "8100"); XMultiComponentFactory xmulticomponentfactory = null; XComponentContext xcomponentcontext = null; Object objectUrlResolver = null; XUnoUrlResolver xurlresolver = null; Object objectInitial = null; // Converting the document to the favoured type try { /* Bootstraps a component context with the jurt base components registered. Component context to be granted to a component for running. Arbitrary values can be retrieved from the context. */ xcomponentcontext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null); /* Gets the service manager instance to be used (or null). This method has been added for convenience, because the service manager is a often used object. */ xmulticomponentfactory = xcomponentcontext.getServiceManager(); /* Creates an instance of the component UnoUrlResolver which supports the services specified by the factory. */ objectUrlResolver = xmulticomponentfactory.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xcomponentcontext); // Create a new url resolver xurlresolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, objectUrlResolver); // Resolves an object that is specified as follow: // uno:<connection description>;<protocol description>;<initial object name> String url = "uno:socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager"; objectInitial = xurlresolver.resolve(url); // Create a service manager from the initial object xmulticomponentfactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial); } catch(Exception e) { // 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"); //Process procXvfb = Runtime.getRuntime().exec(ooxvfb); //Process procExport = Runtime.getRuntime().exec(ooexport); /* Process procSoffice = Runtime.getRuntime().exec(oosoffice); Thread.sleep(3000); objectInitial = xurlresolver.resolve("uno:socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager"); xmulticomponentfactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial); Debug.logInfo("soffice started. " + procSoffice, module); */ String errMsg = "Error connecting to OpenOffice with host [" + host + "] and port [" + port + "]: " + e.toString(); Debug.logError(e, errMsg, module); throw new IllegalArgumentException(errMsg); } return xmulticomponentfactory; } public static String listFilterNamesEvent(HttpServletRequest request, HttpServletResponse response) { XMultiComponentFactory factory = null; try { factory = getRemoteServer("localhost", "8100"); List filterList = getFilterNames(factory); request.setAttribute("filterList", filterList); } catch(IOException e) { request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); return "error"; } catch(Exception e) { request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); return "error"; } return "success"; } public static List getFilterNames(XMultiComponentFactory xmulticomponentfactory) throws Exception { XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); Object oDefaultContext = xPropertySet.getPropertyValue("DefaultContext"); XComponentContext xComponentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, oDefaultContext); Object filterFactory = xmulticomponentfactory.createInstanceWithContext("com.sun.star.document.FilterFactory", xComponentContext); XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, filterFactory); String [] filterNames = xNameAccess.getElementNames(); //String [] serviceNames = filterFactory.getAvailableServiceNames(); for (int i=0; i < filterNames.length; i++) { String s = filterNames[i]; Debug.logInfo(s, module); /* if (s.toLowerCase().indexOf("filter") >= 0) { Debug.logInfo("FILTER: " + s, module); } if (s.toLowerCase().indexOf("desktop") >= 0) { Debug.logInfo("DESKTOP: " + s, module); } */ } List filterNameList = UtilMisc.toListArray(filterNames); return filterNameList; } 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); // Get the default context from the office server. Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext"); // Query for the interface XComponentContext. XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -