com.sun.star.io.IOException Java Examples

The following examples show how to use com.sun.star.io.IOException. 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: 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 #3
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 #4
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected File createTempFile(byte[] bytes) {
    try {
        String tempFileName = String.format("document%d", counter.incrementAndGet());
        String tempFileExt = ".tmp";
        if (StringUtils.isNotBlank(officeIntegration.getTemporaryDirPath())) {
            Path tempDir = Paths.get(officeIntegration.getTemporaryDirPath());
            tempDir.toFile().mkdirs();

            temporaryFile = Files.createTempFile(
                    tempDir,
                    tempFileName,
                    tempFileExt)
                    .toFile();
        } else {
            temporaryFile = File.createTempFile(tempFileName, tempFileExt);
        }
        FileUtils.writeByteArrayToFile(temporaryFile, bytes);
        return temporaryFile;
    } catch (java.io.IOException e) {
        throw new ReportFormattingException("Could not create temporary file for pdf conversion", e);
    }
}
 
Example #5
Source File: AbstractConversionTask.java    From kkFileViewOfficeEdit with Apache License 2.0 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 #6
Source File: AbstractConversionTask.java    From kkFileView with Apache License 2.0 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 #7
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 #8
Source File: DocFormatter.java    From yarg with Apache License 2.0 5 votes vote down vote up
protected void saveAndClose(OfficeResourceProvider ooResourceProvider, XComponent xComponent, ReportOutputType outputType, OutputStream outputStream)
        throws IOException {
    OfficeOutputStream ooos = new OfficeOutputStream(outputStream);
    String filterName;
    if (ReportOutputType.pdf.equals(outputType)) {
        filterName = PDF_OUTPUT_FILE;
    } else {
        filterName = MS_WORD_OUTPUT_FILE;
    }
    ooResourceProvider.saveXComponent(xComponent, ooos, filterName);
    ooResourceProvider.closeXComponent(xComponent);
}
 
Example #9
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public XComponent loadXComponent(InputStream inputStream) throws com.sun.star.lang.IllegalArgumentException, IOException {
    try {
        return loadXComponent(IOUtils.toByteArray(inputStream));
    } catch (java.io.IOException e) {
        throw new ReportFormattingException("An error occurred while reading bytes", e);
    }
}
 
Example #10
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 #11
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 #12
Source File: OfficeResourceProvider.java    From yarg with Apache License 2.0 5 votes vote down vote up
public XInputStream getXInputStream(ReportTemplate reportTemplate) {
    try {
        return new OfficeInputStream(IOUtils.toByteArray(reportTemplate.getDocumentContent()));
    } catch (java.io.IOException e) {
        throw new OpenOfficeException("An error occurred while converting template to XInputStream", e);
    }
}
 
Example #13
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 #14
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 #15
Source File: DocFormatter.java    From yarg with Apache License 2.0 4 votes vote down vote up
protected void loadDocument(OfficeResourceProvider ooResourceProvider) throws com.sun.star.lang.IllegalArgumentException, IOException {
    xComponent = ooResourceProvider.loadXComponent(reportTemplate.getDocumentContent());
    officeComponent = new OfficeComponent(ooResourceProvider, xComponent);
}