1 18 package org.ofbiz.content.openoffice; 19 20 import java.io.IOException ; 21 import java.io.FileNotFoundException ; 22 import java.io.File ; 23 import java.util.List ; 24 import java.util.Hashtable ; 25 import java.util.Properties ; 26 import java.util.Enumeration ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.ofbiz.base.util.Debug; 32 import org.ofbiz.base.util.UtilMisc; 33 import org.ofbiz.base.util.UtilProperties; 34 import org.ofbiz.base.util.UtilValidate; 35 36 import com.sun.star.beans.PropertyValue; 37 import com.sun.star.beans.XPropertySet; 38 import com.sun.star.bridge.XUnoUrlResolver; 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.frame.XComponentLoader; 41 import com.sun.star.frame.XStorable; 42 import com.sun.star.lang.XComponent; 43 import com.sun.star.lang.XMultiComponentFactory; 44 import com.sun.star.uno.UnoRuntime; 45 import com.sun.star.uno.XComponentContext; 46 47 55 public class OpenOfficeWorker{ 56 57 public static final String module = OpenOfficeWorker.class.getName(); 58 59 62 public static XMultiComponentFactory getRemoteServer(String host, String port) throws IOException , Exception { 63 64 if (UtilValidate.isEmpty(host)) host = UtilProperties.getPropertyValue("openoffice-uno", "oo.host", "localhost"); 65 if (UtilValidate.isEmpty(port)) port = UtilProperties.getPropertyValue("openoffice-uno", "oo.port", "8100"); 66 67 XMultiComponentFactory xmulticomponentfactory = null; 68 XComponentContext xcomponentcontext = null; 69 Object objectUrlResolver = null; 70 XUnoUrlResolver xurlresolver = null; 71 Object objectInitial = null; 72 try { 74 77 xcomponentcontext = com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null); 78 79 82 xmulticomponentfactory = xcomponentcontext.getServiceManager(); 83 84 86 objectUrlResolver = xmulticomponentfactory.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xcomponentcontext); 87 88 xurlresolver = (XUnoUrlResolver) UnoRuntime.queryInterface(XUnoUrlResolver.class, objectUrlResolver); 90 91 String url = "uno:socket,host=" + host + ",port=" + port + ";urp;StarOffice.ServiceManager"; 94 objectInitial = xurlresolver.resolve(url); 95 96 xmulticomponentfactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, objectInitial); 98 } catch(Exception e) { 99 112 String errMsg = "Error connecting to OpenOffice with host [" + host + "] and port [" + port + "]: " + e.toString(); 113 Debug.logError(e, errMsg, module); 114 throw new IllegalArgumentException (errMsg); 115 } 116 117 return xmulticomponentfactory; 118 } 119 120 public static String listFilterNamesEvent(HttpServletRequest request, HttpServletResponse response) { 121 XMultiComponentFactory factory = null; 122 123 try { 124 factory = getRemoteServer("localhost", "8100"); 125 List filterList = getFilterNames(factory); 126 request.setAttribute("filterList", filterList); 127 } catch(IOException e) { 128 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 129 return "error"; 130 } catch(Exception e) { 131 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 132 return "error"; 133 } 134 return "success"; 135 } 136 137 public static List getFilterNames(XMultiComponentFactory xmulticomponentfactory) throws Exception { 138 XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); 139 Object oDefaultContext = xPropertySet.getPropertyValue("DefaultContext"); 140 XComponentContext xComponentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, oDefaultContext); 141 142 143 Object filterFactory = xmulticomponentfactory.createInstanceWithContext("com.sun.star.document.FilterFactory", xComponentContext); 144 XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface(XNameAccess.class, filterFactory); 145 String [] filterNames = xNameAccess.getElementNames(); 146 147 for (int i=0; i < filterNames.length; i++) { 149 String s = filterNames[i]; 150 Debug.logInfo(s, module); 151 159 } 160 161 List filterNameList = UtilMisc.toListArray(filterNames); 162 return filterNameList; 163 } 164 165 public static void convertOODocToFile(XMultiComponentFactory xmulticomponentfactory, String stringUrl, String stringConvertedFile, String outputMimeType) throws FileNotFoundException , IOException , Exception { 166 XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); 169 170 Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext"); 172 173 XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext); 175 176 180 181 Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext); 182 XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj); 184 185 186 PropertyValue propertyvalue[] = new PropertyValue[ 2 ]; 188 propertyvalue[ 0 ] = new PropertyValue(); 190 propertyvalue[ 0 ].Name = "Hidden"; 191 propertyvalue[ 0 ].Value = new Boolean (true); 192 193 propertyvalue[ 1 ] = new PropertyValue(); 194 propertyvalue[ 1 ].Name = "UpdateDocMode"; 195 propertyvalue[ 1 ].Value = "1"; 196 197 Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue); 199 200 XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore); 202 203 propertyvalue = new PropertyValue[ 3 ]; 205 propertyvalue[ 0 ] = new PropertyValue(); 207 propertyvalue[ 0 ].Name = "Overwrite"; 208 propertyvalue[ 0 ].Value = new Boolean (true); 209 String filterName = getFilterNameFromMimeType(outputMimeType); 212 213 propertyvalue[ 1 ] = new PropertyValue(); 214 propertyvalue[ 1 ].Name = "FilterName"; 215 propertyvalue[ 1 ].Value = filterName; 216 217 propertyvalue[2] = new PropertyValue(); 218 propertyvalue[2].Name = "CompressionMode"; 219 propertyvalue[2].Value = "1"; 220 221 Debug.logInfo("stringConvertedFile: "+stringConvertedFile, module); 222 226 xstorable.storeToURL(stringConvertedFile, propertyvalue); 227 228 XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable); 230 231 xcomponent.dispose(); 233 return; 234 } 235 236 public static OpenOfficeByteArrayOutputStream convertOODocByteStreamToByteStream(XMultiComponentFactory xmulticomponentfactory, 237 OpenOfficeByteArrayInputStream is, String inputMimeType, String outputMimeType) throws Exception { 238 239 XPropertySet xpropertysetMultiComponentFactory = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xmulticomponentfactory); 241 242 Object objectDefaultContext = xpropertysetMultiComponentFactory.getPropertyValue("DefaultContext"); 244 245 XComponentContext xcomponentcontext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, objectDefaultContext); 247 248 252 253 Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext); 254 XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj); 256 257 PropertyValue propertyvalue[] = new PropertyValue[2]; 259 propertyvalue[0] = new PropertyValue(); 261 propertyvalue[0].Name = "Hidden"; 262 propertyvalue[0].Value = Boolean.TRUE; 263 propertyvalue[1] = new PropertyValue(); 265 propertyvalue[1].Name = "InputStream"; 266 propertyvalue[1].Value = is; 267 268 Object objectDocumentToStore = xcomponentloader.loadComponentFromURL("private:stream", "_blank", 0, propertyvalue); 270 if (objectDocumentToStore == null) { 271 Debug.logError("Could not get objectDocumentToStore object from xcomponentloader.loadComponentFromURL", module); 272 } 273 274 XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore); 276 if (xstorable == null) { 277 Debug.logError("Could not get XStorable object from UnoRuntime.queryInterface", module); 278 } 279 280 String filterName = getFilterNameFromMimeType(outputMimeType); 282 propertyvalue = new PropertyValue[4]; 283 284 propertyvalue[0] = new PropertyValue(); 285 propertyvalue[0].Name = "OutputStream"; 286 OpenOfficeByteArrayOutputStream os = new OpenOfficeByteArrayOutputStream(); 287 propertyvalue[0].Value = os; 288 propertyvalue[1] = new PropertyValue(); 290 propertyvalue[1].Name = "FilterName"; 291 propertyvalue[1].Value = filterName; 292 propertyvalue[3] = new PropertyValue(); 294 propertyvalue[3].Name = "Overwrite"; 295 propertyvalue[3].Value = Boolean.TRUE; 296 propertyvalue[2] = new PropertyValue(); 298 propertyvalue[2].Name = "CompressionMode"; 299 propertyvalue[2].Value = "1"; 300 301 xstorable.storeToURL("private:stream", propertyvalue); 302 304 XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable); 306 307 xcomponent.dispose(); 309 310 return os; 311 } 312 313 public static String getFilterNameFromMimeType(String mimeType) { 314 String filterName = ""; 315 if (UtilValidate.isEmpty(mimeType)) { 316 filterName = "HTML"; 317 } else if (mimeType.equalsIgnoreCase("application/pdf")) { 318 filterName = "writer_pdf_Export"; 319 } else if (mimeType.equalsIgnoreCase("application/msword")) { 320 filterName = "MS Word 97"; 321 } else if (mimeType.equalsIgnoreCase("text/html")) { 322 filterName = "HTML (StarWriter)"; 323 } else { 324 filterName = "HTML"; 325 } 326 return filterName; 327 328 } 329 330 public static String getExtensionFromMimeType(String mimeType) { 331 String extension = ""; 332 if (UtilValidate.isEmpty(mimeType)) { 333 extension = "html"; 334 } else if (mimeType.equalsIgnoreCase("application/pdf")) { 335 extension = "pdf"; 336 } else if (mimeType.equalsIgnoreCase("application/msword")) { 337 extension = "doc"; 338 } else if (mimeType.equalsIgnoreCase("text/html")) { 339 extension = "html"; 340 } else { 341 extension = "html"; 342 } 343 return extension; 344 345 } 346 } 347 | Popular Tags |