1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package com.sun.star.report; 24 25 import com.sun.star.beans.PropertyVetoException; 26 import com.sun.star.beans.UnknownPropertyException; 27 import com.sun.star.beans.XPropertySet; 28 import com.sun.star.container.NoSuchElementException; 29 import com.sun.star.embed.ElementModes; 30 import com.sun.star.embed.InvalidStorageException; 31 import com.sun.star.embed.XStorage; 32 import com.sun.star.embed.XTransactedObject; 33 import com.sun.star.io.XStream; 34 import com.sun.star.lang.IllegalArgumentException; 35 import com.sun.star.lang.WrappedTargetException; 36 import com.sun.star.lib.uno.adapter.XInputStreamToInputStreamAdapter; 37 import com.sun.star.lib.uno.adapter.XOutputStreamToOutputStreamAdapter; 38 import com.sun.star.uno.UnoRuntime; 39 40 import java.io.BufferedInputStream; 41 import java.io.BufferedOutputStream; 42 import java.io.IOException; 43 import java.io.InputStream; 44 import java.io.OutputStream; 45 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 49 /** 50 * A directory holds all the contents here. 51 * 52 * 53 * @author Ocke Janssen 54 */ 55 public class StorageRepository implements InputRepository, OutputRepository 56 { 57 58 private static final Log LOGGER = LogFactory.getLog(SDBCReportDataFactory.class); 59 private static final String REPORT_PROCESSING_FAILED = "ReportProcessing failed"; 60 private XStorage input; 61 private XStorage output; 62 private final String rootURL; 63 64 /** 65 * 66 * @param input 67 * @param output 68 * @param rootURL 69 * @throws java.io.IOException 70 */ StorageRepository(final XStorage input, final XStorage output, final String rootURL)71 public StorageRepository(final XStorage input, final XStorage output, final String rootURL) 72 { 73 this.input = input; 74 this.output = output; 75 this.rootURL = rootURL; 76 77 } 78 StorageRepository(final XStorage storage, final boolean isOutput, final String rootURL)79 public StorageRepository(final XStorage storage, final boolean isOutput, final String rootURL) 80 { 81 this.rootURL = rootURL; 82 if (isOutput) 83 { 84 this.output = storage; 85 } 86 else 87 { 88 this.input = storage; 89 } 90 } 91 createInputStream(final String name)92 public InputStream createInputStream(final String name) throws IOException 93 { 94 if (input == null) 95 { 96 throw new IOException("input is NULL"); 97 } 98 try 99 { 100 final XStream xStream = (XStream) UnoRuntime.queryInterface(XStream.class, input.openStreamElement(name, ElementModes.READ)); 101 return new BufferedInputStream(new XInputStreamToInputStreamAdapter(xStream.getInputStream()), 102400); 102 } 103 catch (com.sun.star.uno.Exception e) 104 { 105 throw new IOException("createInputStream"); 106 } 107 } 108 109 /** 110 * Creates an output stream for writing the data. If there is an entry with 111 * that name already contained in the repository, try to overwrite it. 112 * 113 * @param name 114 * @param mimeType 115 * @return the outputstream 116 * @throws IOException if opening the stream fails 117 */ createOutputStream(final String name, final String mimeType)118 public OutputStream createOutputStream(final String name, final String mimeType) throws IOException 119 { 120 if (output == null) 121 { 122 throw new IOException("output is NULL"); 123 } 124 try 125 { 126 final XStream stream = output.openStreamElement(name, ElementModes.WRITE | ElementModes.TRUNCATE); 127 stream.getInputStream().closeInput(); 128 if (mimeType != null) 129 { 130 final XPropertySet prop = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, stream); 131 prop.setPropertyValue("MediaType", mimeType); 132 } 133 return new BufferedOutputStream(new XOutputStreamToOutputStreamAdapter(stream.getOutputStream()), 204800); 134 } 135 catch (com.sun.star.uno.Exception e) 136 { 137 throw new IOException("createOutputStream"); 138 } 139 } 140 exists(final String name)141 public boolean exists(final String name) 142 { 143 try 144 { 145 return output.isStreamElement(name); 146 } 147 catch (InvalidStorageException ex) 148 { 149 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 150 } 151 catch (com.sun.star.lang.IllegalArgumentException ex) 152 { 153 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 154 } 155 catch (NoSuchElementException e) 156 { 157 // We expect this exception, no need to log it. 158 } 159 return false; 160 } 161 isWritable(final String name)162 public boolean isWritable(final String name) 163 { 164 return true; 165 } 166 getId()167 public Object getId() 168 { 169 return "1"; 170 } 171 getVersion(final String name)172 public long getVersion(final String name) 173 { 174 return 1; 175 } 176 isReadable(final String name)177 public boolean isReadable(final String name) 178 { 179 try 180 { 181 if (input != null) 182 { 183 return input.isStreamElement(name); 184 } 185 } 186 catch (InvalidStorageException ex) 187 { 188 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 189 } 190 catch (com.sun.star.lang.IllegalArgumentException ex) 191 { 192 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 193 } 194 catch (NoSuchElementException ex) 195 { 196 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 197 } 198 return false; 199 } 200 openInputRepository(final String name)201 public InputRepository openInputRepository(final String name) throws IOException 202 { 203 try 204 { 205 final String temp = shortenName(name); 206 if (!input.isStorageElement(temp)) 207 { 208 throw new IOException(); 209 } 210 final XStorage storage = (XStorage) UnoRuntime.queryInterface(XStorage.class, input.openStorageElement(temp, ElementModes.READ)); 211 return new StorageRepository(storage, false, rootURL); 212 } 213 catch (NoSuchElementException ex) 214 { 215 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 216 } 217 catch (WrappedTargetException ex) 218 { 219 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 220 } 221 catch (InvalidStorageException ex) 222 { 223 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 224 } 225 catch (IllegalArgumentException ex) 226 { 227 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 228 } 229 catch (com.sun.star.io.IOException ex) 230 { 231 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 232 } 233 throw new IOException(); 234 } 235 shortenName(final String name)236 final String shortenName(final String name) 237 { 238 final String temp; 239 if (name.startsWith("./")) 240 { 241 temp = name.substring(2); 242 } 243 else 244 { 245 temp = name; 246 } 247 return temp; 248 } 249 openOutputRepository(final String name, final String mimeType)250 public OutputRepository openOutputRepository(final String name, final String mimeType) throws IOException 251 { 252 try 253 { 254 final String temp = shortenName(name); 255 final XStorage storage = (XStorage) UnoRuntime.queryInterface(XStorage.class, output.openStorageElement(temp, ElementModes.WRITE)); 256 if (mimeType != null) 257 { 258 final XPropertySet prop = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, storage); 259 prop.setPropertyValue("MediaType", mimeType); 260 } 261 return new StorageRepository(storage, true, rootURL); 262 } 263 catch (UnknownPropertyException ex) 264 { 265 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 266 } 267 catch (PropertyVetoException ex) 268 { 269 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 270 } 271 catch (IllegalArgumentException ex) 272 { 273 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 274 } 275 catch (WrappedTargetException ex) 276 { 277 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 278 } 279 catch (InvalidStorageException ex) 280 { 281 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 282 } 283 catch (com.sun.star.io.IOException ex) 284 { 285 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 286 } 287 288 throw new IOException(); 289 } 290 closeInputRepository()291 public void closeInputRepository() 292 { 293 if (input != null) 294 { 295 input.dispose(); 296 } 297 } 298 closeOutputRepository()299 public void closeOutputRepository() 300 { 301 if (output != null) 302 { 303 try 304 { 305 final XTransactedObject obj = (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, output); 306 if (obj != null) 307 { 308 obj.commit(); 309 } 310 } 311 catch (com.sun.star.io.IOException ex) 312 { 313 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 314 } 315 catch (WrappedTargetException ex) 316 { 317 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 318 } 319 output.dispose(); 320 } 321 322 } 323 existsStorage(final String name)324 public boolean existsStorage(final String name) 325 { 326 try 327 { 328 return output.isStorageElement(name); 329 } 330 catch (InvalidStorageException ex) 331 { 332 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 333 } 334 catch (com.sun.star.lang.IllegalArgumentException ex) 335 { 336 LOGGER.error(REPORT_PROCESSING_FAILED, ex); 337 } 338 catch (NoSuchElementException ex) 339 { 340 // We expect this exception, no need to log it. 341 } 342 return false; 343 } 344 getRootURL()345 public String getRootURL() 346 { 347 return rootURL; 348 } 349 } 350