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