1 /* 2 * This file is part of the LibreOffice project. 3 * 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/. 7 * 8 * This file incorporates work covered by the following license notice: 9 * 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 . 17 */ 18 package org.libreoffice.report; 19 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; 34 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; 41 42 /** 43 * A directory holds all the contents here. 44 * 45 * 46 */ 47 public class StorageRepository implements InputRepository, OutputRepository 48 { 49 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; 55 StorageRepository(final XStorage input, final XStorage output, final String rootURL)56 public StorageRepository(final XStorage input, final XStorage output, final String rootURL) 57 { 58 this.input = input; 59 this.output = output; 60 this.rootURL = rootURL; 61 62 } 63 StorageRepository(final XStorage storage, final boolean isOutput, final String rootURL)64 private StorageRepository(final XStorage storage, final boolean isOutput, final String rootURL) 65 { 66 this.rootURL = rootURL; 67 if (isOutput) 68 { 69 this.output = storage; 70 } 71 else 72 { 73 this.input = storage; 74 } 75 } 76 createInputStream(final String name)77 public InputStream createInputStream(final String name) throws IOException 78 { 79 if (input == null) 80 { 81 throw new IOException("input is NULL"); 82 } 83 try 84 { 85 final XStream xStream = UnoRuntime.queryInterface(XStream.class, input.openStreamElement(name, ElementModes.READ)); 86 return new BufferedInputStream(new XInputStreamToInputStreamAdapter(xStream.getInputStream()), 102400); 87 } 88 catch (com.sun.star.uno.Exception ex1) 89 { 90 java.io.IOException ex2 = new java.io.IOException(); 91 ex2.initCause(ex1); 92 throw ex2; 93 } 94 } 95 96 /** 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. 99 * 100 * @throws IOException if opening the stream fails 101 */ createOutputStream(final String name, final String mimeType)102 public OutputStream createOutputStream(final String name, final String mimeType) throws IOException 103 { 104 if (output == null) 105 { 106 throw new IOException("output is NULL"); 107 } 108 try 109 { 110 final XStream stream = output.openStreamElement(name, ElementModes.WRITE | ElementModes.TRUNCATE); 111 stream.getInputStream().closeInput(); 112 if (mimeType != null) 113 { 114 final XPropertySet prop = UnoRuntime.queryInterface(XPropertySet.class, stream); 115 prop.setPropertyValue("MediaType", mimeType); 116 } 117 return new BufferedOutputStream(new XOutputStreamToOutputStreamAdapter(stream.getOutputStream()), 204800); 118 } 119 catch (com.sun.star.uno.Exception ex1) 120 { 121 java.io.IOException ex2 = new java.io.IOException(); 122 ex2.initCause(ex1); 123 throw ex2; 124 } 125 } 126 exists(final String name)127 public boolean exists(final String name) 128 { 129 try 130 { 131 return output.isStreamElement(name); 132 } 133 catch (InvalidStorageException ex) 134 { 135 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 136 } 137 catch (com.sun.star.lang.IllegalArgumentException ex) 138 { 139 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 140 } 141 catch (NoSuchElementException e) 142 { 143 // We expect this exception, no need to log it. 144 } 145 return false; 146 } 147 getId()148 public Object getId() 149 { 150 return "1"; 151 } 152 getVersion(final String name)153 public long getVersion(final String name) 154 { 155 return 1; 156 } 157 isReadable(final String name)158 public boolean isReadable(final String name) 159 { 160 try 161 { 162 if (input != null) 163 { 164 return input.isStreamElement(name); 165 } 166 } 167 catch (InvalidStorageException ex) 168 { 169 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 170 } 171 catch (com.sun.star.lang.IllegalArgumentException ex) 172 { 173 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 174 } 175 catch (NoSuchElementException ex) 176 { 177 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 178 } 179 return false; 180 } 181 openInputRepository(final String name)182 public InputRepository openInputRepository(final String name) throws IOException 183 { 184 try 185 { 186 final String temp = shortenName(name); 187 if (!input.isStorageElement(temp)) 188 { 189 throw new IOException(); 190 } 191 final XStorage storage = UnoRuntime.queryInterface(XStorage.class, input.openStorageElement(temp, ElementModes.READ)); 192 return new StorageRepository(storage, false, rootURL); 193 } 194 catch (NoSuchElementException ex) 195 { 196 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 197 } 198 catch (WrappedTargetException ex) 199 { 200 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 201 } 202 catch (InvalidStorageException ex) 203 { 204 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 205 } 206 catch (IllegalArgumentException ex) 207 { 208 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 209 } 210 catch (com.sun.star.io.IOException ex) 211 { 212 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 213 } 214 throw new IOException(); 215 } 216 shortenName(final String name)217 private final String shortenName(final String name) 218 { 219 final String temp; 220 if (name.startsWith("./")) 221 { 222 temp = name.substring(2); 223 } 224 else 225 { 226 temp = name; 227 } 228 return temp; 229 } 230 openOutputRepository(final String name, final String mimeType)231 public OutputRepository openOutputRepository(final String name, final String mimeType) throws IOException 232 { 233 try 234 { 235 final String temp = shortenName(name); 236 final XStorage storage = UnoRuntime.queryInterface(XStorage.class, output.openStorageElement(temp, ElementModes.WRITE)); 237 if (mimeType != null) 238 { 239 final XPropertySet prop = UnoRuntime.queryInterface(XPropertySet.class, storage); 240 prop.setPropertyValue("MediaType", mimeType); 241 } 242 return new StorageRepository(storage, true, rootURL); 243 } 244 catch (UnknownPropertyException ex) 245 { 246 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 247 } 248 catch (PropertyVetoException ex) 249 { 250 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 251 } 252 catch (IllegalArgumentException ex) 253 { 254 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 255 } 256 catch (WrappedTargetException ex) 257 { 258 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 259 } 260 catch (InvalidStorageException ex) 261 { 262 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 263 } 264 catch (com.sun.star.io.IOException ex) 265 { 266 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 267 } 268 269 throw new IOException(); 270 } 271 closeInputRepository()272 public void closeInputRepository() 273 { 274 if (input != null) 275 { 276 input.dispose(); 277 } 278 } 279 closeOutputRepository()280 public void closeOutputRepository() 281 { 282 if (output != null) 283 { 284 try 285 { 286 final XTransactedObject obj = UnoRuntime.queryInterface(XTransactedObject.class, output); 287 if (obj != null) 288 { 289 obj.commit(); 290 } 291 } 292 catch (com.sun.star.io.IOException ex) 293 { 294 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 295 } 296 catch (WrappedTargetException ex) 297 { 298 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 299 } 300 output.dispose(); 301 } 302 303 } 304 existsStorage(final String name)305 public boolean existsStorage(final String name) 306 { 307 try 308 { 309 return output.isStorageElement(name); 310 } 311 catch (InvalidStorageException ex) 312 { 313 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 314 } 315 catch (com.sun.star.lang.IllegalArgumentException ex) 316 { 317 LOGGER.severe(REPORT_PROCESSING_FAILED + ex); 318 } 319 catch (NoSuchElementException ex) 320 { 321 // We expect this exception, no need to log it. 322 } 323 return false; 324 } 325 getRootURL()326 public String getRootURL() 327 { 328 return rootURL; 329 } 330 } 331
