2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package org
.libreoffice
.report
;
20 import com
.sun
.star
.beans
.PropertyVetoException
;
21 import com
.sun
.star
.beans
.UnknownPropertyException
;
22 import com
.sun
.star
.beans
.XPropertySet
;
23 import com
.sun
.star
.container
.NoSuchElementException
;
24 import com
.sun
.star
.embed
.ElementModes
;
25 import com
.sun
.star
.embed
.InvalidStorageException
;
26 import com
.sun
.star
.embed
.XStorage
;
27 import com
.sun
.star
.embed
.XTransactedObject
;
28 import com
.sun
.star
.io
.XStream
;
29 import com
.sun
.star
.lang
.IllegalArgumentException
;
30 import com
.sun
.star
.lang
.WrappedTargetException
;
31 import com
.sun
.star
.lib
.uno
.adapter
.XInputStreamToInputStreamAdapter
;
32 import com
.sun
.star
.lib
.uno
.adapter
.XOutputStreamToOutputStreamAdapter
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
35 import java
.io
.BufferedInputStream
;
36 import java
.io
.BufferedOutputStream
;
37 import java
.io
.IOException
;
38 import java
.io
.InputStream
;
39 import java
.io
.OutputStream
;
40 import java
.util
.logging
.Logger
;
43 * A directory holds all the contents here.
47 public class StorageRepository
implements InputRepository
, OutputRepository
50 private static final Logger LOGGER
= Logger
.getLogger(StorageRepository
.class.getName());
51 private static final String REPORT_PROCESSING_FAILED
= "ReportProcessing failed: ";
52 private XStorage input
;
53 private XStorage output
;
54 private final String rootURL
;
56 public StorageRepository(final XStorage input
, final XStorage output
, final String rootURL
)
60 this.rootURL
= rootURL
;
64 private StorageRepository(final XStorage storage
, final boolean isOutput
, final String rootURL
)
66 this.rootURL
= rootURL
;
69 this.output
= storage
;
77 public InputStream
createInputStream(final String name
) throws IOException
81 throw new IOException("input is NULL");
85 final XStream xStream
= UnoRuntime
.queryInterface(XStream
.class, input
.openStreamElement(name
, ElementModes
.READ
));
86 return new BufferedInputStream(new XInputStreamToInputStreamAdapter(xStream
.getInputStream()), 102400);
88 catch (com
.sun
.star
.uno
.Exception ex1
)
90 java
.io
.IOException ex2
= new java
.io
.IOException();
97 * Creates an output stream for writing the data. If there is an entry with
98 * that name already contained in the repository, try to overwrite it.
100 * @throws IOException if opening the stream fails
102 public OutputStream
createOutputStream(final String name
, final String mimeType
) throws IOException
106 throw new IOException("output is NULL");
110 final XStream stream
= output
.openStreamElement(name
, ElementModes
.WRITE
| ElementModes
.TRUNCATE
);
111 stream
.getInputStream().closeInput();
112 if (mimeType
!= null)
114 final XPropertySet prop
= UnoRuntime
.queryInterface(XPropertySet
.class, stream
);
115 prop
.setPropertyValue("MediaType", mimeType
);
117 return new BufferedOutputStream(new XOutputStreamToOutputStreamAdapter(stream
.getOutputStream()), 204800);
119 catch (com
.sun
.star
.uno
.Exception ex1
)
121 java
.io
.IOException ex2
= new java
.io
.IOException();
127 public boolean exists(final String name
)
131 return output
.isStreamElement(name
);
133 catch (InvalidStorageException ex
)
135 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
137 catch (com
.sun
.star
.lang
.IllegalArgumentException ex
)
139 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
141 catch (NoSuchElementException e
)
143 // We expect this exception, no need to log it.
148 public Object
getId()
153 public long getVersion(final String name
)
158 public boolean isReadable(final String name
)
164 return input
.isStreamElement(name
);
167 catch (InvalidStorageException ex
)
169 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
171 catch (com
.sun
.star
.lang
.IllegalArgumentException ex
)
173 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
175 catch (NoSuchElementException ex
)
177 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
182 public InputRepository
openInputRepository(final String name
) throws IOException
186 final String temp
= shortenName(name
);
187 if (!input
.isStorageElement(temp
))
189 throw new IOException();
191 final XStorage storage
= UnoRuntime
.queryInterface(XStorage
.class, input
.openStorageElement(temp
, ElementModes
.READ
));
192 return new StorageRepository(storage
, false, rootURL
);
194 catch (NoSuchElementException ex
)
196 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
198 catch (WrappedTargetException ex
)
200 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
202 catch (InvalidStorageException ex
)
204 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
206 catch (IllegalArgumentException ex
)
208 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
210 catch (com
.sun
.star
.io
.IOException ex
)
212 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
214 throw new IOException();
217 private final String
shortenName(final String name
)
220 if (name
.startsWith("./"))
222 temp
= name
.substring(2);
231 public OutputRepository
openOutputRepository(final String name
, final String mimeType
) throws IOException
235 final String temp
= shortenName(name
);
236 final XStorage storage
= UnoRuntime
.queryInterface(XStorage
.class, output
.openStorageElement(temp
, ElementModes
.WRITE
));
237 if (mimeType
!= null)
239 final XPropertySet prop
= UnoRuntime
.queryInterface(XPropertySet
.class, storage
);
240 prop
.setPropertyValue("MediaType", mimeType
);
242 return new StorageRepository(storage
, true, rootURL
);
244 catch (UnknownPropertyException ex
)
246 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
248 catch (PropertyVetoException ex
)
250 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
252 catch (IllegalArgumentException ex
)
254 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
256 catch (WrappedTargetException ex
)
258 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
260 catch (InvalidStorageException ex
)
262 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
264 catch (com
.sun
.star
.io
.IOException ex
)
266 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
269 throw new IOException();
272 public void closeInputRepository()
280 public void closeOutputRepository()
286 final XTransactedObject obj
= UnoRuntime
.queryInterface(XTransactedObject
.class, output
);
292 catch (com
.sun
.star
.io
.IOException ex
)
294 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
296 catch (WrappedTargetException ex
)
298 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
305 public boolean existsStorage(final String name
)
309 return output
.isStorageElement(name
);
311 catch (InvalidStorageException ex
)
313 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
315 catch (com
.sun
.star
.lang
.IllegalArgumentException ex
)
317 LOGGER
.severe(REPORT_PROCESSING_FAILED
+ ex
);
319 catch (NoSuchElementException ex
)
321 // We expect this exception, no need to log it.
326 public String
getRootURL()