|
| 1 | +// |
| 2 | +// JODConverter - Java OpenDocument Converter |
| 3 | +// Copyright 2004-2012 Mirko Nasato and contributors |
| 4 | +// |
| 5 | +// JODConverter is Open Source software, you can redistribute it and/or |
| 6 | +// modify it under either (at your option) of the following licenses |
| 7 | +// |
| 8 | +// 1. The GNU Lesser General Public License v3 (or later) |
| 9 | +// -> http://www.gnu.org/licenses/lgpl-3.0.txt |
| 10 | +// 2. The Apache License, Version 2.0 |
| 11 | +// -> http://www.apache.org/licenses/LICENSE-2.0.txt |
| 12 | +// |
| 13 | +package org.artofsolving.jodconverter; |
| 14 | + |
| 15 | +import static org.artofsolving.jodconverter.office.OfficeUtils.SERVICE_DESKTOP; |
| 16 | +import static org.artofsolving.jodconverter.office.OfficeUtils.cast; |
| 17 | +import static org.artofsolving.jodconverter.office.OfficeUtils.toUnoProperties; |
| 18 | +import static org.artofsolving.jodconverter.office.OfficeUtils.toUrl; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.artofsolving.jodconverter.office.OfficeContext; |
| 24 | +import org.artofsolving.jodconverter.office.OfficeException; |
| 25 | +import org.artofsolving.jodconverter.office.OfficeTask; |
| 26 | + |
| 27 | +import com.sun.star.frame.XComponentLoader; |
| 28 | +import com.sun.star.frame.XStorable; |
| 29 | +import com.sun.star.io.IOException; |
| 30 | +import com.sun.star.lang.IllegalArgumentException; |
| 31 | +import com.sun.star.lang.XComponent; |
| 32 | +import com.sun.star.task.ErrorCodeIOException; |
| 33 | +import com.sun.star.util.CloseVetoException; |
| 34 | +import com.sun.star.util.XCloseable; |
| 35 | + |
| 36 | +public abstract class AbstractConversionTask implements OfficeTask { |
| 37 | + |
| 38 | + private final File inputFile; |
| 39 | + private final File outputFile; |
| 40 | + |
| 41 | + public AbstractConversionTask(File inputFile, File outputFile) { |
| 42 | + this.inputFile = inputFile; |
| 43 | + this.outputFile = outputFile; |
| 44 | + } |
| 45 | + |
| 46 | + protected abstract Map<String,?> getLoadProperties(File inputFile); |
| 47 | + |
| 48 | + protected abstract Map<String,?> getStoreProperties(File outputFile, XComponent document); |
| 49 | + |
| 50 | + public void execute(OfficeContext context) throws OfficeException { |
| 51 | + XComponent document = null; |
| 52 | + try { |
| 53 | + document = loadDocument(context, inputFile); |
| 54 | + modifyDocument(document); |
| 55 | + storeDocument(document, outputFile); |
| 56 | + } catch (OfficeException officeException) { |
| 57 | + throw officeException; |
| 58 | + } catch (Exception exception) { |
| 59 | + throw new OfficeException("conversion failed", exception); |
| 60 | + } finally { |
| 61 | + if (document != null) { |
| 62 | + XCloseable closeable = cast(XCloseable.class, document); |
| 63 | + if (closeable != null) { |
| 64 | + try { |
| 65 | + closeable.close(true); |
| 66 | + } catch (CloseVetoException closeVetoException) { |
| 67 | + // whoever raised the veto should close the document |
| 68 | + } |
| 69 | + } else { |
| 70 | + document.dispose(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private XComponent loadDocument(OfficeContext context, File inputFile) throws OfficeException { |
| 77 | + if (!inputFile.exists()) { |
| 78 | + throw new OfficeException("input document not found"); |
| 79 | + } |
| 80 | + XComponentLoader loader = cast(XComponentLoader.class, context.getService(SERVICE_DESKTOP)); |
| 81 | + Map<String,?> loadProperties = getLoadProperties(inputFile); |
| 82 | + XComponent document = null; |
| 83 | + try { |
| 84 | + document = loader.loadComponentFromURL(toUrl(inputFile), "_blank", 0, toUnoProperties(loadProperties)); |
| 85 | + } catch (IllegalArgumentException illegalArgumentException) { |
| 86 | + throw new OfficeException("could not load document: " + inputFile.getName(), illegalArgumentException); |
| 87 | + } catch (ErrorCodeIOException errorCodeIOException) { |
| 88 | + throw new OfficeException("could not load document: " + inputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException); |
| 89 | + } catch (IOException ioException) { |
| 90 | + throw new OfficeException("could not load document: " + inputFile.getName(), ioException); |
| 91 | + } |
| 92 | + if (document == null) { |
| 93 | + throw new OfficeException("could not load document: " + inputFile.getName()); |
| 94 | + } |
| 95 | + return document; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Override to modify the document after it has been loaded and before it gets |
| 100 | + * saved in the new format. |
| 101 | + * <p> |
| 102 | + * Does nothing by default. |
| 103 | + * |
| 104 | + * @param document |
| 105 | + * @throws OfficeException |
| 106 | + */ |
| 107 | + protected void modifyDocument(XComponent document) throws OfficeException { |
| 108 | + // noop |
| 109 | + } |
| 110 | + |
| 111 | + private void storeDocument(XComponent document, File outputFile) throws OfficeException { |
| 112 | + Map<String,?> storeProperties = getStoreProperties(outputFile, document); |
| 113 | + if (storeProperties == null) { |
| 114 | + throw new OfficeException("unsupported conversion"); |
| 115 | + } |
| 116 | + try { |
| 117 | + cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties)); |
| 118 | + } catch (ErrorCodeIOException errorCodeIOException) { |
| 119 | + throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException); |
| 120 | + } catch (IOException ioException) { |
| 121 | + throw new OfficeException("could not store document: " + outputFile.getName(), ioException); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments