Java Code Examples for com.sun.star.frame.XComponentLoader#loadComponentFromURL()

The following examples show how to use com.sun.star.frame.XComponentLoader#loadComponentFromURL() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: StreamOpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 5 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 8
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public XComponent loadXComponent(byte[] bytes) throws com.sun.star.lang.IllegalArgumentException, IOException {
    XComponentLoader xComponentLoader = getXComponentLoader();

    PropertyValue[] props = new PropertyValue[1];
    props[0] = new PropertyValue();
    props[0].Name = "Hidden";
    props[0].Value = Boolean.TRUE;

    File tempFile = createTempFile(bytes);

    return xComponentLoader.loadComponentFromURL(toURL(tempFile), "_blank", 0, props);
}
 
Example 9
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);
}
 
Example 10
Source File: JodConverterMetadataExtracterWorker.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void execute(OfficeContext context)
{
    if (logger.isDebugEnabled())
    {
        logger.debug("Extracting metadata from file " + inputFile);
    }

    XComponent document = null;
    try
    {
        if (!inputFile.exists())
        {
            throw new OfficeException("input document not found");
        }
        XComponentLoader loader = cast(XComponentLoader.class, context
                .getService(SERVICE_DESKTOP));
        
        // Need to set the Hidden property to ensure that OOo GUI does not appear.
        PropertyValue hiddenOOo = new PropertyValue();
        hiddenOOo.Name = "Hidden";
        hiddenOOo.Value = Boolean.TRUE;
        PropertyValue readOnly = new PropertyValue();
        readOnly.Name = "ReadOnly";
        readOnly.Value = Boolean.TRUE;

        try
        {
            document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0,
                    new PropertyValue[]{hiddenOOo, readOnly});
        } 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());
        }
        XRefreshable refreshable = cast(XRefreshable.class, document);
        if (refreshable != null)
        {
            refreshable.refresh();
        }

        XDocumentInfoSupplier docInfoSupplier = cast(XDocumentInfoSupplier.class, document);
        XPropertySet propSet = cast(XPropertySet.class, docInfoSupplier.getDocumentInfo());

        // The strings below are property names as used by OOo. They need upper-case
        // initial letters.
        Object author = getPropertyValueIfAvailable(propSet, "Author");
        Object description = getPropertyValueIfAvailable(propSet, "Subject");
        Object title = getPropertyValueIfAvailable(propSet, "Title");
        
        Map<String, Serializable> results = new HashMap<String, Serializable>(3);
        results.put(KEY_AUTHOR, author == null ? null : author.toString());
        results.put(KEY_DESCRIPTION, description == null ? null : description.toString());
        results.put(KEY_TITLE, title == null ? null : title.toString());
        callback.setResults(results);
    } 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 11
Source File: OpenOfficeWorker.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public static void convertOODocToFile(XMultiComponentFactory xmulticomponentfactory, String fileInPath, String fileOutPath, String outputMimeType) throws FileNotFoundException, IOException, MalformedURLException, 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);

    /* A desktop environment contains tasks with one or more
       frames in which components can be loaded. Desktop is the
       environment for components which can instanciate within
       frames. */

    Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);
    //XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj);
    XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj);


    // Preparing properties for loading the document
    PropertyValue propertyvalue[] = new PropertyValue[ 2 ];
    // Setting the flag for hidding the open document
    propertyvalue[ 0 ] = new PropertyValue();
    propertyvalue[ 0 ].Name = "Hidden";
    propertyvalue[ 0 ].Value = Boolean.valueOf(false);

    propertyvalue[ 1 ] = new PropertyValue();
    propertyvalue[ 1 ].Name = "UpdateDocMode";
    propertyvalue[ 1 ].Value = "1";

    // Loading the wanted document
    String stringUrl = convertToUrl(fileInPath, xcomponentcontext);
    Debug.logInfo("stringUrl:" + stringUrl, module);
    Object objectDocumentToStore = xcomponentloader.loadComponentFromURL(stringUrl, "_blank", 0, propertyvalue);

    // Getting an object that will offer a simple way to store a document to a URL.
    XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);

    // Preparing properties for converting the document
    propertyvalue = new PropertyValue[ 3 ];
    // Setting the flag for overwriting
    propertyvalue[ 0 ] = new PropertyValue();
    propertyvalue[ 0 ].Name = "Overwrite";
    propertyvalue[ 0 ].Value = Boolean.valueOf(true);
    // Setting the filter name
    // Preparing properties for converting the document
    String filterName = getFilterNameFromMimeType(outputMimeType);

    propertyvalue[ 1 ] = new PropertyValue();
    propertyvalue[ 1 ].Name = "FilterName";
    propertyvalue[ 1 ].Value = filterName;

    propertyvalue[2] = new PropertyValue();
    propertyvalue[2].Name = "CompressionMode";
    propertyvalue[2].Value = "1";

    // Storing and converting the document
    //File newFile = new File(stringConvertedFile);
    //newFile.createNewFile();

    String stringConvertedFile = convertToUrl(fileOutPath, xcomponentcontext);
    Debug.logInfo("stringConvertedFile: "+stringConvertedFile, module);
    xstorable.storeToURL(stringConvertedFile, propertyvalue);

    // Getting the method dispose() for closing the document
    XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable);

    // Closing the converted document
    xcomponent.dispose();
    return;
}
 
Example 12
Source File: OpenOfficeWorker.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public static OpenOfficeByteArrayOutputStream convertOODocByteStreamToByteStream(XMultiComponentFactory xmulticomponentfactory,
        OpenOfficeByteArrayInputStream is, String inputMimeType, String outputMimeType) throws Exception {

    // 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);

    /* A desktop environment contains tasks with one or more
       frames in which components can be loaded. Desktop is the
       environment for components which can instanciate within
       frames. */

    Object desktopObj = xmulticomponentfactory.createInstanceWithContext("com.sun.star.frame.Desktop", xcomponentcontext);
    //XDesktop desktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktopObj);
    XComponentLoader xcomponentloader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktopObj);

    // Preparing properties for loading the document
    PropertyValue propertyvalue[] = new PropertyValue[2];
    // Setting the flag for hidding the open document
    propertyvalue[0] = new PropertyValue();
    propertyvalue[0].Name = "Hidden";
    propertyvalue[0].Value = Boolean.TRUE;
    //
    propertyvalue[1] = new PropertyValue();
    propertyvalue[1].Name = "InputStream";
    propertyvalue[1].Value = is;

    // Loading the wanted document
    Object objectDocumentToStore = xcomponentloader.loadComponentFromURL("private:stream", "_blank", 0, propertyvalue);
    if (objectDocumentToStore == null) {
        Debug.logError("Could not get objectDocumentToStore object from xcomponentloader.loadComponentFromURL", module);
    }

    // Getting an object that will offer a simple way to store a document to a URL.
    XStorable xstorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, objectDocumentToStore);
    if (xstorable == null) {
        Debug.logError("Could not get XStorable object from UnoRuntime.queryInterface", module);
    }

    // Preparing properties for converting the document
    String filterName = getFilterNameFromMimeType(outputMimeType);
    propertyvalue = new PropertyValue[4];

    propertyvalue[0] = new PropertyValue();
    propertyvalue[0].Name = "OutputStream";
    OpenOfficeByteArrayOutputStream os = new OpenOfficeByteArrayOutputStream();
    propertyvalue[0].Value = os;
    // Setting the filter name
    propertyvalue[1] = new PropertyValue();
    propertyvalue[1].Name = "FilterName";
    propertyvalue[1].Value = filterName;
    // Setting the flag for overwriting
    propertyvalue[3] = new PropertyValue();
    propertyvalue[3].Name = "Overwrite";
    propertyvalue[3].Value = Boolean.TRUE;
    // For PDFs
    propertyvalue[2] = new PropertyValue();
    propertyvalue[2].Name = "CompressionMode";
    propertyvalue[2].Value = "1";

    xstorable.storeToURL("private:stream", propertyvalue);
    //xstorable.storeToURL("file:///home/byersa/testdoc1_file.pdf", propertyvalue);

    // Getting the method dispose() for closing the document
    XComponent xcomponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xstorable);

    // Closing the converted document
    xcomponent.dispose();

    return os;
}
 
Example 13
Source File: DocumentLoader.java    From noa-libre with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Loads document into OpenOffice.org
 * 
 * @param serviceProvider the service provider to be used
 * @param xComponentLoader OpenOffice.org component loader
 * @param URL URL of the document
 * @param targetFrameName name of the OpenOffice.org target frame
 * @param searchFlags search flags for the target frame
 * @param properties properties for OpenOffice.org
 * 
 * @return loaded document
 * 
 * @throws Exception if an OpenOffice.org communication error occurs
 * @throws IOException if document can not be found
 */
private static IDocument loadDocument(IServiceProvider serviceProvider,
    XComponentLoader xComponentLoader, String URL, String targetFrameName, int searchFlags,
    PropertyValue[] properties) throws Exception, IOException {
  DocumentService.checkMaxOpenDocuments(serviceProvider);
  XComponent xComponent = xComponentLoader.loadComponentFromURL(URL,
      targetFrameName,
      searchFlags,
      properties);
  if (xComponent != null) {
    return getDocument(xComponent, serviceProvider, properties);
  }
  throw new IOException("Document not found.");
}
 
Example 14
Source File: OpenOfficeDocumentConverter.java    From translationstudio8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Load document.
 * @param inputUrl
 *            the input url
 * @param loadProperties
 *            the load properties
 * @return the x component
 * @throws IllegalArgumentException
 *             the illegal argument exception
 */
@SuppressWarnings("unchecked")
private XComponent loadDocument(String inputUrl, Map loadProperties) throws com.sun.star.io.IOException,
		IllegalArgumentException {
	XComponentLoader desktop = openOfficeConnection.getDesktopObject();
	return desktop.loadComponentFromURL(inputUrl, "_blank", 0, toPropertyValues(loadProperties)); //$NON-NLS-1$
}
 
Example 15
Source File: OpenOfficeDocumentConverter.java    From tmxeditor8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Load document.
 * @param inputUrl
 *            the input url
 * @param loadProperties
 *            the load properties
 * @return the x component
 * @throws IllegalArgumentException
 *             the illegal argument exception
 */
@SuppressWarnings("unchecked")
private XComponent loadDocument(String inputUrl, Map loadProperties) throws com.sun.star.io.IOException,
		IllegalArgumentException {
	XComponentLoader desktop = openOfficeConnection.getDesktopObject();
	return desktop.loadComponentFromURL(inputUrl, "_blank", 0, toPropertyValues(loadProperties)); //$NON-NLS-1$
}