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