1*cd519653SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*cd519653SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*cd519653SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*cd519653SAndrew Rist * distributed with this work for additional information 6*cd519653SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*cd519653SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*cd519653SAndrew Rist * "License"); you may not use this file except in compliance 9*cd519653SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*cd519653SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*cd519653SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*cd519653SAndrew Rist * software distributed under the License is distributed on an 15*cd519653SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*cd519653SAndrew Rist * KIND, either express or implied. See the License for the 17*cd519653SAndrew Rist * specific language governing permissions and limitations 18*cd519653SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*cd519653SAndrew Rist *************************************************************/ 21*cd519653SAndrew Rist 22*cd519653SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.script.framework; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.File; 27cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptStorageManager; 28cdf0e10cSrcweir 29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 31cdf0e10cSrcweir import com.sun.star.uno.XInterface; 32cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 33cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 34cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 35cdf0e10cSrcweir 36cdf0e10cSrcweir public class ScriptingUtils { 37cdf0e10cSrcweir private XScriptStorageManager storageManager; 38cdf0e10cSrcweir private static ScriptingUtils utils; 39cdf0e10cSrcweir ScriptingUtils()40cdf0e10cSrcweir private ScriptingUtils() { 41cdf0e10cSrcweir } 42cdf0e10cSrcweir getDefault()43cdf0e10cSrcweir public static ScriptingUtils getDefault() { 44cdf0e10cSrcweir if (utils == null) { 45cdf0e10cSrcweir synchronized (ScriptingUtils.class) { 46cdf0e10cSrcweir if (utils == null) 47cdf0e10cSrcweir utils = new ScriptingUtils(); 48cdf0e10cSrcweir } 49cdf0e10cSrcweir } 50cdf0e10cSrcweir return utils; 51cdf0e10cSrcweir } 52cdf0e10cSrcweir cleanUserDir()53cdf0e10cSrcweir public static void cleanUserDir() { 54cdf0e10cSrcweir } 55cdf0e10cSrcweir cleanShareDir()56cdf0e10cSrcweir public static void cleanShareDir() { 57cdf0e10cSrcweir } 58cdf0e10cSrcweir getScriptStorage(XMultiServiceFactory xMSF, String location)59cdf0e10cSrcweir public Object getScriptStorage(XMultiServiceFactory xMSF, String location) { 60cdf0e10cSrcweir int id = getStorageId(xMSF, location); 61cdf0e10cSrcweir return storageManager.getScriptStorage(id); 62cdf0e10cSrcweir } 63cdf0e10cSrcweir getStorageId(XMultiServiceFactory xMSF, String location)64cdf0e10cSrcweir public int getStorageId(XMultiServiceFactory xMSF, String location) { 65cdf0e10cSrcweir 66cdf0e10cSrcweir if (location.equals("share")) 67cdf0e10cSrcweir return 0; 68cdf0e10cSrcweir 69cdf0e10cSrcweir if (location.equals("user")) 70cdf0e10cSrcweir return 1; 71cdf0e10cSrcweir 72cdf0e10cSrcweir XSimpleFileAccess access = null; 73cdf0e10cSrcweir String uri = util.utils.getFullTestURL(location); 74cdf0e10cSrcweir 75cdf0e10cSrcweir if (storageManager == null) { 76cdf0e10cSrcweir try { 77cdf0e10cSrcweir XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface( 78cdf0e10cSrcweir XPropertySet.class, xMSF); 79cdf0e10cSrcweir 80cdf0e10cSrcweir XComponentContext xContext = (XComponentContext) 81cdf0e10cSrcweir UnoRuntime.queryInterface(XComponentContext.class, 82cdf0e10cSrcweir xProp.getPropertyValue("DefaultContext")); 83cdf0e10cSrcweir 84cdf0e10cSrcweir XInterface ifc = (XInterface) 85cdf0e10cSrcweir xContext.getValueByName("/singletons/drafts.com.sun.star." + 86cdf0e10cSrcweir "script.framework.storage.theScriptStorageManager"); 87cdf0e10cSrcweir 88cdf0e10cSrcweir storageManager = (XScriptStorageManager) 89cdf0e10cSrcweir UnoRuntime.queryInterface(XScriptStorageManager.class, ifc); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir catch( Exception e ) { 92cdf0e10cSrcweir return -1; 93cdf0e10cSrcweir } 94cdf0e10cSrcweir } 95cdf0e10cSrcweir 96cdf0e10cSrcweir access = getXSimpleFileAccess(xMSF); 97cdf0e10cSrcweir if (access == null) 98cdf0e10cSrcweir return -1; 99cdf0e10cSrcweir 100cdf0e10cSrcweir int id = storageManager.createScriptStorageWithURI(access, uri); 101cdf0e10cSrcweir 102cdf0e10cSrcweir return id; 103cdf0e10cSrcweir } 104cdf0e10cSrcweir getXSimpleFileAccess(XMultiServiceFactory xMSF)105cdf0e10cSrcweir public XSimpleFileAccess getXSimpleFileAccess(XMultiServiceFactory xMSF) { 106cdf0e10cSrcweir XSimpleFileAccess access = null; 107cdf0e10cSrcweir 108cdf0e10cSrcweir try { 109cdf0e10cSrcweir Object fa = 110cdf0e10cSrcweir xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess"); 111cdf0e10cSrcweir 112cdf0e10cSrcweir access = (XSimpleFileAccess) 113cdf0e10cSrcweir UnoRuntime.queryInterface(XSimpleFileAccess.class, fa); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir catch (com.sun.star.uno.Exception e) { 116cdf0e10cSrcweir return null; 117cdf0e10cSrcweir } 118cdf0e10cSrcweir return access; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir } 121