| 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.browse; |
| 19 |
|
| 20 |
import com.sun.star.beans.XIntrospectionAccess; |
| 21 |
|
| 22 |
import com.sun.star.container.ElementExistException; |
| 23 |
import com.sun.star.container.NoSuchElementException; |
| 24 |
|
| 25 |
import com.sun.star.lang.NoSupportException; |
| 26 |
import com.sun.star.lang.WrappedTargetException; |
| 27 |
import com.sun.star.lang.XMultiComponentFactory; |
| 28 |
|
| 29 |
import com.sun.star.lib.uno.helper.PropertySet; |
| 30 |
|
| 31 |
import com.sun.star.reflection.InvocationTargetException; |
| 32 |
|
| 33 |
import com.sun.star.script.XInvocation; |
| 34 |
import com.sun.star.script.browse.BrowseNodeTypes; |
| 35 |
import com.sun.star.script.browse.XBrowseNode; |
| 36 |
import com.sun.star.script.framework.container.Parcel; |
| 37 |
import com.sun.star.script.framework.container.ScriptEntry; |
| 38 |
import com.sun.star.script.framework.container.ScriptMetaData; |
| 39 |
import com.sun.star.script.framework.log.LogUtils; |
| 40 |
import com.sun.star.script.framework.provider.ScriptProvider; |
| 41 |
import com.sun.star.script.provider.XScriptContext; |
| 42 |
|
| 43 |
import com.sun.star.ucb.XSimpleFileAccess; |
| 44 |
|
| 45 |
import com.sun.star.uno.Any; |
| 46 |
import com.sun.star.uno.AnyConverter; |
| 47 |
import com.sun.star.uno.Type; |
| 48 |
import com.sun.star.uno.UnoRuntime; |
| 49 |
import com.sun.star.uno.XComponentContext; |
| 50 |
|
| 51 |
public class ScriptBrowseNode extends PropertySet implements |
| 52 |
XBrowseNode, XInvocation { |
| 53 |
|
| 54 |
private final ScriptProvider provider; |
| 55 |
|
| 56 |
private Parcel parent; |
| 57 |
private String name; |
| 58 |
|
| 59 |
// these are properties, accessed by reflection |
| 60 |
public String uri; |
| 61 |
public String description; |
| 62 |
public boolean editable; |
| 63 |
public boolean deletable = false; |
| 64 |
public boolean renamable = false; |
| 65 |
|
| 66 |
public ScriptBrowseNode(ScriptProvider provider, Parcel parent, String name) { |
| 67 |
|
| 68 |
this.provider = provider; |
| 69 |
this.name = name; |
| 70 |
this.parent = parent; |
| 71 |
ScriptMetaData data = null; |
| 72 |
XComponentContext xCtx = provider.getScriptingContext().getComponentContext(); |
| 73 |
XMultiComponentFactory xFac = xCtx.getServiceManager(); |
| 74 |
|
| 75 |
try { |
| 76 |
data = parent.getByName( name ); |
| 77 |
XSimpleFileAccess xSFA = UnoRuntime.queryInterface( |
| 78 |
XSimpleFileAccess.class, |
| 79 |
xFac.createInstanceWithContext( |
| 80 |
"com.sun.star.ucb.SimpleFileAccess", |
| 81 |
xCtx)); |
| 82 |
|
| 83 |
uri = data.getShortFormScriptURL(); |
| 84 |
description = data.getDescription(); |
| 85 |
if (provider.hasScriptEditor()) { |
| 86 |
this.editable = true; |
| 87 |
|
| 88 |
try { |
| 89 |
if (!parent.isUnoPkg() |
| 90 |
&& !xSFA.isReadOnly(parent.getPathToParcel())) { |
| 91 |
|
| 92 |
this.deletable = true; |
| 93 |
this.renamable = true; |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
// TODO propagate errors |
| 98 |
catch (Exception e) { |
| 99 |
LogUtils.DEBUG("Caught exception in creation of ScriptBrowseNode"); |
| 100 |
LogUtils.DEBUG(LogUtils.getTrace(e)); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
// TODO fix exception types to be caught here, should we rethrow? |
| 107 |
catch (Exception e) { |
| 108 |
|
| 109 |
LogUtils.DEBUG("** caught exception getting script data for " + name + |
| 110 |
" ->" + e.toString()); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
registerProperty("Deletable", new Type(boolean.class), (short)0, "deletable"); |
| 115 |
registerProperty("Editable", new Type(boolean.class), (short)0, "editable"); |
| 116 |
registerProperty("Renamable", new Type(boolean.class), (short)0, "renamable"); |
| 117 |
registerProperty("URI", new Type(String.class), (short)0, "uri"); |
| 118 |
registerProperty("Description", new Type(String.class), (short)0, |
| 119 |
"description"); |
| 120 |
} |
| 121 |
|
| 122 |
public String getName() { |
| 123 |
return name; |
| 124 |
} |
| 125 |
|
| 126 |
public XBrowseNode[] getChildNodes() { |
| 127 |
return new XBrowseNode[0]; |
| 128 |
} |
| 129 |
|
| 130 |
public boolean hasChildNodes() { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
public short getType() { |
| 135 |
return BrowseNodeTypes.SCRIPT; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public String toString() { |
| 140 |
return getName(); |
| 141 |
} |
| 142 |
|
| 143 |
public void updateURI(Parcel p) { |
| 144 |
parent = p; |
| 145 |
|
| 146 |
try { |
| 147 |
ScriptMetaData data = parent.getByName(name); |
| 148 |
uri = data.getShortFormScriptURL(); |
| 149 |
} |
| 150 |
// TODO fix exception types to be caught here, should we rethrow? |
| 151 |
catch (Exception e) { |
| 152 |
LogUtils.DEBUG("** caught exception getting script data for " + name + |
| 153 |
" ->" + e.toString()); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
// implementation of XInvocation interface |
| 158 |
public XIntrospectionAccess getIntrospection() { |
| 159 |
return null; |
| 160 |
} |
| 161 |
|
| 162 |
public Object invoke(String aFunctionName, Object[] aParams, |
| 163 |
short[][] aOutParamIndex, Object[][] aOutParam) throws |
| 164 |
com.sun.star.lang.IllegalArgumentException, |
| 165 |
com.sun.star.script.CannotConvertException, |
| 166 |
com.sun.star.reflection.InvocationTargetException { |
| 167 |
|
| 168 |
// Initialise the out parameters - not used but prevents error in |
| 169 |
// UNO bridge |
| 170 |
aOutParamIndex[0] = new short[0]; |
| 171 |
aOutParam[0] = new Object[0]; |
| 172 |
|
| 173 |
Any result = new Any(new Type(Boolean.class), Boolean.TRUE); |
| 174 |
|
| 175 |
if (aFunctionName.equals("Editable")) { |
| 176 |
if (!editable) { |
| 177 |
|
| 178 |
NoSupportException nse = |
| 179 |
new NoSupportException(aFunctionName + " is not editable "); |
| 180 |
|
| 181 |
throw new InvocationTargetException( |
| 182 |
"Scripting framework error editing script", null, nse); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
XScriptContext ctxt = provider.getScriptingContext(); |
| 187 |
ScriptMetaData data = null; |
| 188 |
|
| 189 |
try { |
| 190 |
data = parent.getByName(name); |
| 191 |
} catch (NoSuchElementException nse) { |
| 192 |
throw new com.sun.star.lang.IllegalArgumentException(nse, |
| 193 |
name + " does not exist or can't be found "); |
| 194 |
} catch (com.sun.star.lang.WrappedTargetException wte) { |
| 195 |
// rethrow |
| 196 |
throw new InvocationTargetException( |
| 197 |
"Scripting framework editing script ", null, wte.TargetException); |
| 198 |
} |
| 199 |
|
| 200 |
provider.getScriptEditor().edit(ctxt, data); |
| 201 |
} else if (aFunctionName.equals("Deletable")) { |
| 202 |
if (!deletable) { |
| 203 |
|
| 204 |
NoSupportException nse = new NoSupportException( |
| 205 |
aFunctionName + " is not supported for this node"); |
| 206 |
|
| 207 |
throw new InvocationTargetException( |
| 208 |
"Scripting framework error deleting script", null, nse); |
| 209 |
} |
| 210 |
|
| 211 |
try { |
| 212 |
parent.removeByName(name); |
| 213 |
result = new Any(new Type(Boolean.class), Boolean.TRUE); |
| 214 |
} catch (NoSuchElementException nse) { |
| 215 |
throw new com.sun.star.lang.IllegalArgumentException(nse, |
| 216 |
name + " does not exist or can't be found "); |
| 217 |
} catch (WrappedTargetException wte) { |
| 218 |
// rethrow |
| 219 |
throw new InvocationTargetException( |
| 220 |
"Scripting framework deleting script ", null, wte.TargetException); |
| 221 |
} |
| 222 |
|
| 223 |
} else if (aFunctionName.equals("Renamable")) { |
| 224 |
result = new Any(new Type(XBrowseNode.class), new XBrowseNode[0]); |
| 225 |
|
| 226 |
if (!renamable) { |
| 227 |
|
| 228 |
NoSupportException nse = new NoSupportException( |
| 229 |
aFunctionName + " is not supported for this node"); |
| 230 |
|
| 231 |
throw new InvocationTargetException( |
| 232 |
"Scripting framework error renaming script", null, nse); |
| 233 |
} |
| 234 |
|
| 235 |
try { |
| 236 |
String newName = AnyConverter.toString(aParams[0]); |
| 237 |
ScriptMetaData oldData = parent.getByName(name); |
| 238 |
oldData.loadSource(); |
| 239 |
String oldSource = oldData.getSource(); |
| 240 |
|
| 241 |
LogUtils.DEBUG("Create renamed script"); |
| 242 |
|
| 243 |
String languageName = |
| 244 |
newName + "." + provider.getScriptEditor().getExtension(); |
| 245 |
|
| 246 |
String language = provider.getName(); |
| 247 |
|
| 248 |
ScriptEntry entry = new ScriptEntry(language, languageName); |
| 249 |
|
| 250 |
ScriptMetaData data = |
| 251 |
new ScriptMetaData(parent, entry, oldSource); |
| 252 |
|
| 253 |
parent.insertByName(languageName, data); |
| 254 |
|
| 255 |
LogUtils.DEBUG("Now remove old script"); |
| 256 |
parent.removeByName(name); |
| 257 |
|
| 258 |
uri = data.getShortFormScriptURL(); |
| 259 |
name = languageName; |
| 260 |
result = new Any(new Type(XBrowseNode.class), this); |
| 261 |
} catch (NoSuchElementException nse) { |
| 262 |
throw new com.sun.star.lang.IllegalArgumentException(nse, |
| 263 |
name + " does not exist or can't be found "); |
| 264 |
} catch (ElementExistException eee) { |
| 265 |
// rethrow |
| 266 |
throw new InvocationTargetException( |
| 267 |
"Scripting framework error renaming script ", null, eee); |
| 268 |
} catch (WrappedTargetException wte) { |
| 269 |
// rethrow |
| 270 |
throw new InvocationTargetException( |
| 271 |
"Scripting framework rename script ", null, wte.TargetException); |
| 272 |
} |
| 273 |
} else { |
| 274 |
throw new com.sun.star.lang.IllegalArgumentException( |
| 275 |
"Function " + aFunctionName + " not supported."); |
| 276 |
} |
| 277 |
|
| 278 |
return result; |
| 279 |
} |
| 280 |
|
| 281 |
public void setValue(String aPropertyName, Object aValue) throws |
| 282 |
com.sun.star.beans.UnknownPropertyException, |
| 283 |
com.sun.star.script.CannotConvertException, |
| 284 |
com.sun.star.reflection.InvocationTargetException { |
| 285 |
} |
| 286 |
|
| 287 |
public Object getValue(String aPropertyName) throws |
| 288 |
com.sun.star.beans.UnknownPropertyException { |
| 289 |
|
| 290 |
return null; |
| 291 |
} |
| 292 |
|
| 293 |
public boolean hasMethod(String aName) { |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
public boolean hasProperty(String aName) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
} |