com.sun.star.lang.XComponent Java Examples

The following examples show how to use com.sun.star.lang.XComponent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocumentService.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns current documents of an application.
 * 
 * @return documents of an application
 * 
 * @throws DocumentException if the documents cannot be provided
 * 
 * @author Markus Krüger
 * @date 11.11.2008
 */
public static IDocument[] getCurrentDocuments(IServiceProvider serviceProvider)
    throws DocumentException {
  try {
    if (serviceProvider == null)
      return new IDocument[0];
    Object desktop = serviceProvider.createService("com.sun.star.frame.Desktop"); //$NON-NLS-1$
    XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);
    XEnumeration aktComponents = xDesktop.getComponents().createEnumeration();
    List arrayList = new ArrayList();
    while (aktComponents.hasMoreElements()) {
      Any a = (Any) aktComponents.nextElement();
      arrayList.add(DocumentLoader.getDocument((XComponent) a.getObject(), serviceProvider, null));
    }
    IDocument[] documents = new IDocument[arrayList.size()];
    documents = (IDocument[]) arrayList.toArray(documents);
    return documents;
  }
  catch (Exception exception) {
    throw new DocumentException(exception);
  }
}
 
Example #2
Source File: OfficeDocumentUtils.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
public static DocumentFamily getDocumentFamily(XComponent document) throws OfficeException {
    XServiceInfo serviceInfo = cast(XServiceInfo.class, document);
    if (serviceInfo.supportsService("com.sun.star.text.GenericTextDocument")) {
        // NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
        // but this further distinction doesn't seem to matter for conversions
        return DocumentFamily.TEXT;
    } else if (serviceInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")) {
        return DocumentFamily.SPREADSHEET;
    } else if (serviceInfo.supportsService("com.sun.star.presentation.PresentationDocument")) {
        return DocumentFamily.PRESENTATION;
    } else if (serviceInfo.supportsService("com.sun.star.drawing.DrawingDocument")) {
        return DocumentFamily.DRAWING;
    } else {
        throw new OfficeException("document of unknown family: " + serviceInfo.getImplementationName());
    }
}
 
Example #3
Source File: OOPresentation.java    From Quelea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Connect to an office, if no office is running a new instance is
 * started. A new connection is established and the service manger from
 * the running office is returned.
 *
 * @param path the path to the openoffice install.
 */
private static XComponentContext connect(String path) throws BootstrapException, Exception {
    File progPath = new File(path, "program");
    xOfficeContext = BootstrapSocketConnector.bootstrap(progPath.getAbsolutePath());
    XComponentContext localContext = Bootstrap.createInitialComponentContext(null);
    XMultiComponentFactory localServiceManager = localContext.getServiceManager();
    XConnector connector = UnoRuntime.queryInterface(XConnector.class,
            localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector",
                    localContext));
    connection = connector.connect(RUN_ARGS);
    XBridgeFactory bridgeFactory = UnoRuntime.queryInterface(XBridgeFactory.class,
            localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext));
    bridge = bridgeFactory.createBridge("", "urp", connection, null);
    bridgeComponent = UnoRuntime.queryInterface(XComponent.class, bridge);
    bridgeComponent.addEventListener(new com.sun.star.lang.XEventListener() {
        @Override
        public void disposing(EventObject eo) {
        }
    });
    return xOfficeContext;

}
 
Example #4
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #5
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #6
Source File: MemoryUsage.java    From kkFileViewOfficeEdit with Apache License 2.0 6 votes vote down vote up
private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
    throws Exception
{
    XComponentLoader loader = (XComponentLoader)
        UnoRuntime.queryInterface(
            XComponentLoader.class, ctxt.getDesktop());

    XComponent comp = loader.loadComponentFromURL(
        "private:factory/scalc", "_blank", 4, new PropertyValue[0]);

    XSpreadsheetDocument doc = (XSpreadsheetDocument)
        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);

    XIndexAccess index = (XIndexAccess)
        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());

    XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));

    return sheet;
}
 
Example #7
Source File: OfficeDocumentUtils.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public static DocumentFamily getDocumentFamily(XComponent document) throws OfficeException {
    XServiceInfo serviceInfo = cast(XServiceInfo.class, document);
    if (serviceInfo.supportsService("com.sun.star.text.GenericTextDocument")) {
        // NOTE: a GenericTextDocument is either a TextDocument, a WebDocument, or a GlobalDocument
        // but this further distinction doesn't seem to matter for conversions
        return DocumentFamily.TEXT;
    } else if (serviceInfo.supportsService("com.sun.star.sheet.SpreadsheetDocument")) {
        return DocumentFamily.SPREADSHEET;
    } else if (serviceInfo.supportsService("com.sun.star.presentation.PresentationDocument")) {
        return DocumentFamily.PRESENTATION;
    } else if (serviceInfo.supportsService("com.sun.star.drawing.DrawingDocument")) {
        return DocumentFamily.DRAWING;
    } else {
        throw new OfficeException("document of unknown family: " + serviceInfo.getImplementationName());
    }
}
 
Example #8
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #9
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #10
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
@Override
public void inlineToDoc(OfficeComponent officeComponent, XTextRange textRange, XText destination, Object paramValue,
                        Matcher paramsMatcher) throws Exception {
    try {
        if (paramValue != null) {
            Image image = new Image(paramValue, paramsMatcher);

            if (image.isValid()) {
                XComponent xComponent = officeComponent.getOfficeComponent();
                insertImage(xComponent, officeComponent.getOfficeResourceProvider(), destination, textRange, image);
            }
        }
    } catch (Exception e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to doc file", e);
    }
}
 
Example #11
Source File: AbstractInliner.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected void insertImage(XComponent document, OfficeResourceProvider officeResourceProvider, XText destination, XTextRange textRange,
                           Image image) throws Exception {
    XMultiServiceFactory xFactory = as(XMultiServiceFactory.class, document);
    XComponentContext xComponentContext = officeResourceProvider.getXComponentContext();
    XMultiComponentFactory serviceManager = xComponentContext.getServiceManager();

    Object oImage = xFactory.createInstance(TEXT_GRAPHIC_OBJECT);
    Object oGraphicProvider = serviceManager.createInstanceWithContext(GRAPHIC_PROVIDER_OBJECT, xComponentContext);

    XGraphicProvider xGraphicProvider = as(XGraphicProvider.class, oGraphicProvider);

    XPropertySet imageProperties = buildImageProperties(xGraphicProvider, oImage, image.imageContent);
    XTextContent xTextContent = as(XTextContent.class, oImage);
    destination.insertTextContent(textRange, xTextContent, true);
    setImageSize(image.width, image.height, oImage, imageProperties);
}
 
Example #12
Source File: StreamOpenOfficeDocumentConverter.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Load and export.
 * @param inputStream
 *            the input stream
 * @param importOptions
 *            the import options
 * @param outputStream
 *            the output stream
 * @param exportOptions
 *            the export options
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
private void loadAndExport(InputStream inputStream, Map/* <String,Object> */importOptions,
		OutputStream outputStream, Map/* <String,Object> */exportOptions) throws Exception {
	XComponentLoader desktop = openOfficeConnection.getDesktopObject();

	Map/* <String,Object> */loadProperties = new HashMap();
	loadProperties.putAll(getDefaultLoadProperties());
	loadProperties.putAll(importOptions);
	// doesn't work using InputStreamToXInputStreamAdapter; probably because
	// it's not XSeekable
	// property("InputStream", new
	// InputStreamToXInputStreamAdapter(inputStream))
	loadProperties.put("InputStream", new ByteArrayToXInputStreamAdapter(IOUtils.toByteArray(inputStream))); //$NON-NLS-1$

	XComponent document = desktop.loadComponentFromURL(
			"private:stream", "_blank", 0, toPropertyValues(loadProperties)); //$NON-NLS-1$ //$NON-NLS-2$
	if (document == null) {
		throw new OPException(Messages.getString("ooconverter.StreamOpenOfficeDocumentConverter.6")); //$NON-NLS-1$
	}

	refreshDocument(document);

	Map/* <String,Object> */storeProperties = new HashMap();
	storeProperties.putAll(exportOptions);
	storeProperties.put("OutputStream", new OutputStreamToXOutputStreamAdapter(outputStream)); //$NON-NLS-1$

	try {
		XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
		storable.storeToURL("private:stream", toPropertyValues(storeProperties)); //$NON-NLS-1$
	} finally {
		document.dispose();
	}
}
 
Example #13
Source File: OpenOfficeDocumentConverter.java    From translationstudio8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Store document.
 * @param document
 *            the document
 * @param outputUrl
 *            the output url
 * @param storeProperties
 *            the store properties
 */
@SuppressWarnings("unchecked")
private void storeDocument(XComponent document, String outputUrl, Map storeProperties)
		throws com.sun.star.io.IOException {
	try {
		XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
		storable.storeToURL(outputUrl, toPropertyValues(storeProperties));
	} finally {
		XCloseable closeable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, document);
		if (closeable != null) {
			try {
				closeable.close(true);
			} catch (CloseVetoException closeVetoException) {
				if (Converter.DEBUG_MODE) {
					closeVetoException.printStackTrace();
				}
			}
		} else {
			document.dispose();
		}
	}
}
 
Example #14
Source File: RemoteOfficeConnection.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Closes connection to OpenOffice.org.
 */
public void closeConnection() {
  xMultiComponentFactory  = null;
  xMultiServiceFactory    = null;
  xRemoteContext          = null;
  XComponent xComponent = (XComponent)UnoRuntime.queryInterface(XComponent.class, xBridge);
  if(xComponent != null) {    
    try {
      xComponent.dispose();
      isConnectionEstablished = false;
    }
    catch(Exception exception) {
      //do nothing
    }
  }    
  xBridge = null; 
}
 
Example #15
Source File: OpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Store document.
 * @param document
 *            the document
 * @param outputUrl
 *            the output url
 * @param storeProperties
 *            the store properties
 */
@SuppressWarnings("unchecked")
private void storeDocument(XComponent document, String outputUrl, Map storeProperties)
		throws com.sun.star.io.IOException {
	try {
		XStorable storable = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
		storable.storeToURL(outputUrl, toPropertyValues(storeProperties));
	} finally {
		XCloseable closeable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, document);
		if (closeable != null) {
			try {
				closeable.close(true);
			} catch (CloseVetoException closeVetoException) {
				if (Converter.DEBUG_MODE) {
					closeVetoException.printStackTrace();
				}
			}
		} else {
			document.dispose();
		}
	}
}
 
Example #16
Source File: AbstractConversionTask.java    From wenku with MIT License 6 votes vote down vote up
private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException {
    if (!inputFile.exists()) {
        throw new OfficeException("input document not found");
    }
    XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP));
    Map<String,?> loadProperties = getLoadProperties(inputFile);
    XComponent document = null;
    try {
        document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties));
    } catch (IllegalArgumentException illegalArgumentException) {
        throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException);
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not load document: "  + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not load document: "  + inputFile.getName(), ioException);
    }
    if (document == null) {
        throw new OfficeException("could not load document: "  + inputFile.getName());
    }
    return document;
}
 
Example #17
Source File: AbstractConversionTask.java    From wenku with MIT License 6 votes vote down vote up
public void execute(OfficeContext context) throws OfficeException {
    XComponent document = null;
    try {
        document = loadDocument(context, inputFile);
        modifyDocument(document);
        storeDocument(document, outputFile);
    } catch (OfficeException officeException) {
        throw officeException;
    } catch (Exception exception) {
        throw new OfficeException("conversion failed", exception);
    } finally {
        if (document != null) {
            XCloseable closeable = cast(XCloseable.class, document);
            if (closeable != null) {
                try {
                    closeable.close(true);
                } catch (CloseVetoException closeVetoException) {
                    // whoever raised the veto should close the document
                }
            } else {
                document.dispose();
            }
        }
    }
}
 
Example #18
Source File: MemoryUsage.java    From kkFileView with Apache License 2.0 6 votes vote down vote up
private XSpreadsheet createSpreadsheet(XScriptContext ctxt)
    throws Exception
{
    XComponentLoader loader = (XComponentLoader)
        UnoRuntime.queryInterface(
            XComponentLoader.class, ctxt.getDesktop());

    XComponent comp = loader.loadComponentFromURL(
        "private:factory/scalc", "_blank", 4, new PropertyValue[0]);

    XSpreadsheetDocument doc = (XSpreadsheetDocument)
        UnoRuntime.queryInterface(XSpreadsheetDocument.class, comp);

    XIndexAccess index = (XIndexAccess)
        UnoRuntime.queryInterface(XIndexAccess.class, doc.getSheets());

    XSpreadsheet sheet = (XSpreadsheet) AnyConverter.toObject(
        new Type(com.sun.star.sheet.XSpreadsheet.class), index.getByIndex(0));

    return sheet;
}
 
Example #19
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public void closeXComponent(XComponent xComponent) {
    XCloseable xCloseable = as(XCloseable.class, xComponent);
    try {
        xCloseable.close(false);
    } catch (com.sun.star.util.CloseVetoException e) {
        xComponent.dispose();
    }
    FileUtils.deleteQuietly(temporaryFile);
}
 
Example #20
Source File: StandardConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
@Override
protected void modifyDocument(XComponent document) throws OfficeException {
    XRefreshable refreshable = cast(XRefreshable.class, document);
    if (refreshable != null) {
        refreshable.refresh();
    }
}
 
Example #21
Source File: OPConnection.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.converter.ooconnect.OPconnect#connect()
 * @throws ConnectException
 */
public void connect() throws ConnectException {
	try {
		XComponentContext localContext;

		localContext = Bootstrap.createInitialComponentContext(null);

		XMultiComponentFactory localServiceManager = localContext.getServiceManager();
		XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, localServiceManager
				.createInstanceWithContext("com.sun.star.connection.Connector", localContext)); //$NON-NLS-1$
		XConnection connection = connector.connect(strConnection);
		XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class,
				localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext)); //$NON-NLS-1$
		bridge = bridgeFactory.createBridge("ms2ooBridge", "urp", connection, null); //$NON-NLS-1$ //$NON-NLS-2$
		bgComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
		// bgComponent.addEventListener(this);
		serviceMg = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridge
				.getInstance("StarOffice.ServiceManager")); //$NON-NLS-1$
		XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceMg);
		componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, properties
				.getPropertyValue("DefaultContext")); //$NON-NLS-1$
		connected = true;
		if (connected) {
			System.out.println("has already connected"); //$NON-NLS-1$
		} else {
			System.out.println("connect to Openoffice fail,please check OpenOffice service that have to open"); //$NON-NLS-1$
		}

	} catch (NoConnectException connectException) {
		throw new ConnectException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection + ": " + connectException.getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
	} catch (Exception exception) {
		throw new OPException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection), exception); //$NON-NLS-1$
	} catch (java.lang.Exception e) {
		if (Converter.DEBUG_MODE) {
			e.printStackTrace();
		}
	}
}
 
Example #22
Source File: AbstractConversionTask.java    From wenku with MIT License 5 votes vote down vote up
private void storeDocument(XComponent document, File outputFile) throws OfficeException {
    Map<String,?> storeProperties = getStoreProperties(outputFile, document);
    if (storeProperties == null) {
        throw new OfficeException("unsupported conversion");
    }
    try {
        cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties));
    } catch (ErrorCodeIOException errorCodeIOException) {
        throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
    } catch (IOException ioException) {
        throw new OfficeException("could not store document: " + outputFile.getName(), ioException);
    }
}
 
Example #23
Source File: OpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Load and export.
 * @param inputUrl
 *            the input url
 * @param loadProperties
 *            the load properties
 * @param outputUrl
 *            the output url
 * @param storeProperties
 *            the store properties
 * @throws Exception
 *             the exception
 */
@SuppressWarnings("unchecked")
private void loadAndExport(String inputUrl, Map/* <String,Object> */loadProperties, String outputUrl,
		Map/* <String,Object> */storeProperties) throws Exception {
	XComponent document;
	// try {
	document = loadDocument(inputUrl, loadProperties);
	// } catch (ErrorCodeIOException errorCodeIOException) {
	// throw new
	// OPException("conversion failed: could not load input document; OOo errorCode: "
	// + errorCodeIOException.ErrCode, errorCodeIOException);
	// } catch (Exception otherException) {
	// throw new
	// OPException("conversion failed: could not load input document",
	// otherException);
	// }
	if (document == null) {
		throw new OPException(Messages.getString("ooconverter.OpenOfficeDocumentConverter.9")); //$NON-NLS-1$
	}

	refreshDocument(document);

	// try {
	storeDocument(document, outputUrl, storeProperties);
	// } catch (ErrorCodeIOException errorCodeIOException) {
	// throw new
	// OPException("conversion failed: could not save output document; OOo errorCode: "
	// + errorCodeIOException.ErrCode, errorCodeIOException);
	// } catch (Exception otherException) {
	// throw new
	// OPException("conversion failed: could not save output document",
	// otherException);
	// }
}
 
Example #24
Source File: AbstractOfficeConnection.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds new listener for the internal XBridge of OpenOffice.org.
 * 
 * @param eventListener new event listener
 * 
 * @author Andreas Bröker
 */
public void addBridgeEventListener(IEventListener eventListener) {
  if (xBridge != null) {
    if (eventListener != null) {
      EventListenerWrapper eventListenerWrapper = new EventListenerWrapper(eventListener, null);
      XComponent xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xBridge);
      xComponent.addEventListener(eventListenerWrapper);
    }
  }
}
 
Example #25
Source File: AbstractOpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Refresh document.
 * @param document
 *            the document
 */
protected void refreshDocument(XComponent document) {
	XRefreshable refreshable = (XRefreshable) UnoRuntime.queryInterface(XRefreshable.class, document);
	if (refreshable != null) {
		refreshable.refresh();
	}
}
 
Example #26
Source File: OPConnection.java    From tmxeditor8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * (non-Javadoc)
 * @see net.heartsome.cat.converter.ooconnect.OPconnect#connect()
 * @throws ConnectException
 */
public void connect() throws ConnectException {
	try {
		XComponentContext localContext;

		localContext = Bootstrap.createInitialComponentContext(null);

		XMultiComponentFactory localServiceManager = localContext.getServiceManager();
		XConnector connector = (XConnector) UnoRuntime.queryInterface(XConnector.class, localServiceManager
				.createInstanceWithContext("com.sun.star.connection.Connector", localContext)); //$NON-NLS-1$
		XConnection connection = connector.connect(strConnection);
		XBridgeFactory bridgeFactory = (XBridgeFactory) UnoRuntime.queryInterface(XBridgeFactory.class,
				localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext)); //$NON-NLS-1$
		bridge = bridgeFactory.createBridge("ms2ooBridge", "urp", connection, null); //$NON-NLS-1$ //$NON-NLS-2$
		bgComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, bridge);
		// bgComponent.addEventListener(this);
		serviceMg = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, bridge
				.getInstance("StarOffice.ServiceManager")); //$NON-NLS-1$
		XPropertySet properties = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, serviceMg);
		componentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, properties
				.getPropertyValue("DefaultContext")); //$NON-NLS-1$
		connected = true;
		if (connected) {
			System.out.println("has already connected"); //$NON-NLS-1$
		} else {
			System.out.println("connect to Openoffice fail,please check OpenOffice service that have to open"); //$NON-NLS-1$
		}

	} catch (NoConnectException connectException) {
		throw new ConnectException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection + ": " + connectException.getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
	} catch (Exception exception) {
		throw new OPException(MessageFormat.format(Messages.getString("ooconnect.OPConnection.msg"), strConnection), exception); //$NON-NLS-1$
	} catch (java.lang.Exception e) {
		if (Converter.DEBUG_MODE) {
			e.printStackTrace();
		}
	}
}
 
Example #27
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public void saveXComponent(XComponent xComponent, XOutputStream xOutputStream, String filterName) throws IOException {
    PropertyValue[] props = new PropertyValue[2];
    props[0] = new PropertyValue();
    props[1] = new PropertyValue();
    props[0].Name = "OutputStream";
    props[0].Value = xOutputStream;
    props[1].Name = "FilterName";
    props[1].Value = filterName;
    XStorable xStorable = as(XStorable.class, xComponent);
    xStorable.storeToURL("private:stream", props);
}
 
Example #28
Source File: OfficeConnection.java    From kkFileViewOfficeEdit with Apache License 2.0 5 votes vote down vote up
public void connect() throws ConnectException {
    logger.fine(String.format("connecting with connectString '%s'", unoUrl));
    try {
        XComponentContext localContext = Bootstrap.createInitialComponentContext(null);
        XMultiComponentFactory localServiceManager = localContext.getServiceManager();
        XConnector connector = OfficeUtils.cast(XConnector.class, localServiceManager.createInstanceWithContext("com.sun.star.connection.Connector", localContext));
        XConnection connection = connector.connect(unoUrl.getConnectString());
        XBridgeFactory bridgeFactory = OfficeUtils.cast(XBridgeFactory.class, localServiceManager.createInstanceWithContext("com.sun.star.bridge.BridgeFactory", localContext));
        String bridgeName = "jodconverter_" + bridgeIndex.getAndIncrement();
        XBridge bridge = bridgeFactory.createBridge(bridgeName, "urp", connection, null);
        bridgeComponent = OfficeUtils.cast(XComponent.class, bridge);
        bridgeComponent.addEventListener(bridgeListener);
        serviceManager = OfficeUtils.cast(XMultiComponentFactory.class, bridge.getInstance("StarOffice.ServiceManager"));
        XPropertySet properties = OfficeUtils.cast(XPropertySet.class, serviceManager);
        componentContext = OfficeUtils.cast(XComponentContext.class, properties.getPropertyValue("DefaultContext"));
        connected = true;
        logger.info(String.format("connected: '%s'", unoUrl));
        OfficeConnectionEvent connectionEvent = new OfficeConnectionEvent(this);
        for (OfficeConnectionEventListener listener : connectionEventListeners) {
            listener.connected(connectionEvent);
        }
    } catch (NoConnectException connectException) {
        throw new ConnectException(String.format("connection failed: '%s'; %s", unoUrl, connectException.getMessage()));
    } catch (Exception exception) {
        throw new OfficeException("connection failed: "+ unoUrl, exception);
    }
}
 
Example #29
Source File: AbstractOpenOfficeDocumentConverter.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Refresh document.
 * @param document
 *            the document
 */
protected void refreshDocument(XComponent document) {
	XRefreshable refreshable = (XRefreshable) UnoRuntime.queryInterface(XRefreshable.class, document);
	if (refreshable != null) {
		refreshable.refresh();
	}
}
 
Example #30
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public XComponent loadXComponent(XInputStream inputStream) throws com.sun.star.lang.IllegalArgumentException, IOException {
    XComponentLoader xComponentLoader = getXComponentLoader();

    PropertyValue[] props = new PropertyValue[2];
    props[0] = new PropertyValue();
    props[1] = new PropertyValue();
    props[0].Name = "InputStream";
    props[0].Value = inputStream;
    props[1].Name = "Hidden";
    props[1].Value = true;
    return xComponentLoader.loadComponentFromURL("private:stream", "_blank", 0, props);
}