| 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 com.sun.star.script.framework.container; |
| 19 |
|
| 20 |
import com.sun.star.container.ElementExistException; |
| 21 |
import com.sun.star.container.XNameContainer; |
| 22 |
|
| 23 |
import com.sun.star.script.framework.io.XInputStreamImpl; |
| 24 |
import com.sun.star.script.framework.log.LogUtils; |
| 25 |
import com.sun.star.script.framework.provider.PathUtils; |
| 26 |
|
| 27 |
import com.sun.star.ucb.XSimpleFileAccess; |
| 28 |
import com.sun.star.ucb.XSimpleFileAccess2; |
| 29 |
|
| 30 |
import com.sun.star.uno.Type; |
| 31 |
import com.sun.star.uno.UnoRuntime; |
| 32 |
|
| 33 |
import java.io.ByteArrayInputStream; |
| 34 |
import java.io.ByteArrayOutputStream; |
| 35 |
|
| 36 |
public class Parcel implements XNameContainer { |
| 37 |
|
| 38 |
private ParcelDescriptor m_descriptor; |
| 39 |
private String name; |
| 40 |
protected ParcelContainer parent; |
| 41 |
protected XSimpleFileAccess m_xSFA; |
| 42 |
|
| 43 |
public Parcel(XSimpleFileAccess xSFA, ParcelContainer parent, |
| 44 |
ParcelDescriptor desc, String parcelName) { |
| 45 |
|
| 46 |
this(parent, desc, parcelName); |
| 47 |
this.m_xSFA = xSFA; |
| 48 |
} |
| 49 |
|
| 50 |
private Parcel(ParcelContainer parent, ParcelDescriptor desc, |
| 51 |
String parcelName) { |
| 52 |
|
| 53 |
this.parent = parent; |
| 54 |
this.m_descriptor = desc; |
| 55 |
this.name = parcelName; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Tests if this <tt>Parcel</tt> is in a UNO package |
| 60 |
* or within a sub package within a UNO package |
| 61 |
* |
| 62 |
* @return <tt>true</tt> if has parent <tt>false</tt> otherwise |
| 63 |
*/ |
| 64 |
public boolean isUnoPkg() { |
| 65 |
return parent.isUnoPkg(); |
| 66 |
} |
| 67 |
|
| 68 |
public String getName() { |
| 69 |
return name; |
| 70 |
} |
| 71 |
|
| 72 |
public ScriptMetaData getByName(String aName) throws |
| 73 |
com.sun.star.container.NoSuchElementException, |
| 74 |
com.sun.star.lang.WrappedTargetException { |
| 75 |
|
| 76 |
LogUtils.DEBUG("** Parcel.getByName for " + aName); |
| 77 |
ScriptEntry thescript = null; |
| 78 |
|
| 79 |
try { |
| 80 |
if (m_descriptor != null && hasElements()) { |
| 81 |
ScriptEntry[] scripts = m_descriptor.getScriptEntries(); |
| 82 |
|
| 83 |
if (scripts.length != 0) { |
| 84 |
for (ScriptEntry script : scripts) { |
| 85 |
if (script.getLanguageName().equals(aName)) { |
| 86 |
thescript = script; |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
// catch unknown or un-checked exceptions |
| 94 |
catch (Exception e) { |
| 95 |
throw new com.sun.star.lang.WrappedTargetException(e); |
| 96 |
} |
| 97 |
|
| 98 |
if (thescript == null) { |
| 99 |
LogUtils.DEBUG("No script for " + aName); |
| 100 |
throw new com.sun.star.container.NoSuchElementException("No script named " + |
| 101 |
aName); |
| 102 |
} |
| 103 |
|
| 104 |
ScriptMetaData data = new ScriptMetaData(this, thescript, null); |
| 105 |
|
| 106 |
LogUtils.DEBUG("returning date for " + aName); |
| 107 |
return data; |
| 108 |
} |
| 109 |
|
| 110 |
public String[] getElementNames() { |
| 111 |
|
| 112 |
String[] results = new String[0]; |
| 113 |
|
| 114 |
if (m_descriptor != null) { |
| 115 |
ScriptEntry[] scripts = m_descriptor.getScriptEntries(); |
| 116 |
results = new String[ scripts.length ]; |
| 117 |
|
| 118 |
for (int index = 0; index < scripts.length; index++) { |
| 119 |
results[ index ] = scripts[ index ].getLanguageName(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return results; |
| 124 |
} |
| 125 |
|
| 126 |
public boolean hasByName(String aName) { |
| 127 |
|
| 128 |
boolean result = true; |
| 129 |
ScriptMetaData containee = null; |
| 130 |
|
| 131 |
try { |
| 132 |
containee = getByName(aName); |
| 133 |
|
| 134 |
if (containee != null) { |
| 135 |
result = true; |
| 136 |
} |
| 137 |
} catch (Exception e) { |
| 138 |
result = false; |
| 139 |
} |
| 140 |
|
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
public com.sun.star.uno.Type getElementType() { |
| 145 |
// TODO at the moment this returns void indicating |
| 146 |
// type is unknown ( from UNO point of view this is correct ) |
| 147 |
// but, maybe we want to have a private UNO interface |
| 148 |
|
| 149 |
return new Type(); |
| 150 |
} |
| 151 |
|
| 152 |
public boolean hasElements() { |
| 153 |
return m_descriptor != null && m_descriptor.getScriptEntries().length > 0; |
| 154 |
} |
| 155 |
|
| 156 |
public void replaceByName(String aName, java.lang.Object aElement) throws |
| 157 |
com.sun.star.lang.IllegalArgumentException, |
| 158 |
com.sun.star.container.NoSuchElementException, |
| 159 |
com.sun.star.lang.WrappedTargetException { |
| 160 |
|
| 161 |
// TODO check type of aElement |
| 162 |
// if not ok, throw IllegalArgument |
| 163 |
if (m_descriptor != null) { |
| 164 |
try { |
| 165 |
ScriptMetaData script = getByName(aName); |
| 166 |
|
| 167 |
if (script != null) { |
| 168 |
//m_descriptor.removeScriptEntry( script ); |
| 169 |
// TODO needs to create source file ( if there is one ) |
| 170 |
//m_descriptor.write(); |
| 171 |
} else { |
| 172 |
throw new com.sun.star.container.NoSuchElementException( |
| 173 |
"No script named " + aName); |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
// TO DO should catch specified exceptions |
| 178 |
catch (Exception ex) { |
| 179 |
throw new com.sun.star.lang.WrappedTargetException(ex); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// create |
| 186 |
public void insertByName(String aName, java.lang.Object aElement) throws |
| 187 |
com.sun.star.lang.IllegalArgumentException, ElementExistException, |
| 188 |
com.sun.star.lang.WrappedTargetException { |
| 189 |
|
| 190 |
// TODO check the type of aElement and throw# |
| 191 |
// if not appropriate |
| 192 |
try { |
| 193 |
if (hasByName(aName)) { |
| 194 |
throw new ElementExistException(aName); |
| 195 |
} |
| 196 |
|
| 197 |
ScriptMetaData script = (ScriptMetaData)aElement; |
| 198 |
|
| 199 |
if (script.hasSource()) { |
| 200 |
LogUtils.DEBUG("Inserting source: " + script.getSource()); |
| 201 |
|
| 202 |
if (!script.writeSourceFile()) { |
| 203 |
throw new com.sun.star.lang.WrappedTargetException( |
| 204 |
"Failed to create source file " + script.getLanguageName()); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
m_descriptor.addScriptEntry(script); |
| 209 |
writeParcelDescriptor(); |
| 210 |
} catch (Exception e) { |
| 211 |
LogUtils.DEBUG("Failed to insert entry " + aName + ": " + e.getMessage()); |
| 212 |
throw new com.sun.star.lang.WrappedTargetException(e); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private void writeParcelDescriptor() |
| 217 |
throws com.sun.star.ucb.CommandAbortedException, |
| 218 |
com.sun.star.io.IOException, |
| 219 |
com.sun.star.uno.Exception, java.io.IOException { |
| 220 |
|
| 221 |
String pathToDescriptor = |
| 222 |
PathUtils.make_url(getPathToParcel(), ParcelDescriptor.PARCEL_DESCRIPTOR_NAME); |
| 223 |
|
| 224 |
XSimpleFileAccess2 xSFA2 = |
| 225 |
UnoRuntime.queryInterface(XSimpleFileAccess2.class, m_xSFA); |
| 226 |
|
| 227 |
if (xSFA2 != null) { |
| 228 |
|
| 229 |
ByteArrayOutputStream bos = null; |
| 230 |
ByteArrayInputStream bis = null; |
| 231 |
XInputStreamImpl xis = null; |
| 232 |
|
| 233 |
try { |
| 234 |
bos = new ByteArrayOutputStream(1024); |
| 235 |
m_descriptor.write(bos); |
| 236 |
bis = new ByteArrayInputStream(bos.toByteArray()); |
| 237 |
|
| 238 |
xis = new XInputStreamImpl(bis); |
| 239 |
xSFA2.writeFile(pathToDescriptor, xis); |
| 240 |
} finally { |
| 241 |
if (bos != null) bos.close(); |
| 242 |
|
| 243 |
if (bis != null) bis.close(); |
| 244 |
|
| 245 |
if (xis != null) xis.closeInput(); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
// delete |
| 251 |
public void removeByName(String Name) throws |
| 252 |
com.sun.star.container.NoSuchElementException, |
| 253 |
com.sun.star.lang.WrappedTargetException { |
| 254 |
|
| 255 |
try { |
| 256 |
ScriptMetaData script = getByName(Name); |
| 257 |
|
| 258 |
if (script != null) { |
| 259 |
if (!script.removeSourceFile()) { |
| 260 |
LogUtils.DEBUG("** Parcel.removeByName Failed to remove script " |
| 261 |
+ Name); |
| 262 |
throw new com.sun.star.lang.WrappedTargetException( |
| 263 |
"Failed to remove script " + Name); |
| 264 |
} |
| 265 |
|
| 266 |
LogUtils.DEBUG("** Parcel.removeByName have removed script source file " |
| 267 |
+ Name); |
| 268 |
|
| 269 |
m_descriptor.removeScriptEntry(script); |
| 270 |
writeParcelDescriptor(); |
| 271 |
} else { |
| 272 |
throw new com.sun.star.container.NoSuchElementException( |
| 273 |
"No script named " + Name); |
| 274 |
} |
| 275 |
|
| 276 |
} catch (Exception e) { |
| 277 |
LogUtils.DEBUG("** Parcel.removeByName Exception: " + e); |
| 278 |
throw new com.sun.star.lang.WrappedTargetException(e); |
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
// rename parcel |
| 284 |
public void rename(String name) { |
| 285 |
this.name = name; |
| 286 |
} |
| 287 |
|
| 288 |
public ParcelContainer getParent() { |
| 289 |
return parent; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Returns the path of this <tt>Parcel</tt> |
| 294 |
* |
| 295 |
* @return <tt>String</tt> path to parcel |
| 296 |
*/ |
| 297 |
public String getPathToParcel() { |
| 298 |
String path = parent.getParcelContainerDir() + "/" + name; |
| 299 |
return path; |
| 300 |
} |
| 301 |
|
| 302 |
} |