xref: /trunk/main/scripting/workben/ifc/scripting/ScriptingUtils.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.script.framework;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import java.io.File;
31*cdf0e10cSrcweir import drafts.com.sun.star.script.framework.storage.XScriptStorageManager;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
36*cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess;
37*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
38*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir public class ScriptingUtils {
41*cdf0e10cSrcweir     private XScriptStorageManager storageManager;
42*cdf0e10cSrcweir     private static ScriptingUtils utils;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     private ScriptingUtils() {
45*cdf0e10cSrcweir     }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     public static ScriptingUtils getDefault() {
48*cdf0e10cSrcweir         if (utils == null) {
49*cdf0e10cSrcweir             synchronized (ScriptingUtils.class) {
50*cdf0e10cSrcweir                 if (utils == null)
51*cdf0e10cSrcweir                     utils = new ScriptingUtils();
52*cdf0e10cSrcweir             }
53*cdf0e10cSrcweir         }
54*cdf0e10cSrcweir         return utils;
55*cdf0e10cSrcweir     }
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     public static void cleanUserDir() {
58*cdf0e10cSrcweir     }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     public static void cleanShareDir() {
61*cdf0e10cSrcweir     }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     public Object getScriptStorage(XMultiServiceFactory xMSF, String location) {
64*cdf0e10cSrcweir         int id = getStorageId(xMSF, location);
65*cdf0e10cSrcweir         return storageManager.getScriptStorage(id);
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     public int getStorageId(XMultiServiceFactory xMSF, String location) {
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir         if (location.equals("share"))
71*cdf0e10cSrcweir             return 0;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         if (location.equals("user"))
74*cdf0e10cSrcweir             return 1;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir         XSimpleFileAccess access = null;
77*cdf0e10cSrcweir         String uri = util.utils.getFullTestURL(location);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir         if (storageManager == null) {
80*cdf0e10cSrcweir             try {
81*cdf0e10cSrcweir                 XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(
82*cdf0e10cSrcweir                     XPropertySet.class, xMSF);
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir                 XComponentContext xContext = (XComponentContext)
85*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XComponentContext.class,
86*cdf0e10cSrcweir                     xProp.getPropertyValue("DefaultContext"));
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir                 XInterface ifc = (XInterface)
89*cdf0e10cSrcweir                     xContext.getValueByName("/singletons/drafts.com.sun.star." +
90*cdf0e10cSrcweir                     "script.framework.storage.theScriptStorageManager");
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir                 storageManager = (XScriptStorageManager)
93*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XScriptStorageManager.class, ifc);
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir             catch( Exception e ) {
96*cdf0e10cSrcweir                 return -1;
97*cdf0e10cSrcweir             }
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         access = getXSimpleFileAccess(xMSF);
101*cdf0e10cSrcweir         if (access == null)
102*cdf0e10cSrcweir             return -1;
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         int id = storageManager.createScriptStorageWithURI(access, uri);
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir         return id;
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     public XSimpleFileAccess getXSimpleFileAccess(XMultiServiceFactory xMSF) {
110*cdf0e10cSrcweir         XSimpleFileAccess access = null;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         try {
113*cdf0e10cSrcweir             Object fa =
114*cdf0e10cSrcweir                 xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir             access = (XSimpleFileAccess)
117*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
118*cdf0e10cSrcweir         }
119*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e) {
120*cdf0e10cSrcweir             return null;
121*cdf0e10cSrcweir         }
122*cdf0e10cSrcweir         return access;
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir }
125