1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BasicMacroTools.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 import com
.sun
.star
.beans
.PropertyValue
;
33 import com
.sun
.star
.beans
.XPropertySet
;
34 import com
.sun
.star
.container
.XNameAccess
;
35 import com
.sun
.star
.frame
.XController
;
36 import com
.sun
.star
.frame
.XDispatch
;
37 import com
.sun
.star
.frame
.XDispatchProvider
;
38 import com
.sun
.star
.frame
.XFrame
;
39 import com
.sun
.star
.frame
.XModel
;
40 import com
.sun
.star
.lang
.XComponent
;
41 import com
.sun
.star
.lang
.XMultiServiceFactory
;
42 import com
.sun
.star
.script
.XLibraryContainer
;
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.util
.*;
45 import com
.sun
.star
.util
.URL
;
46 import com
.sun
.star
.util
.XURLTransformer
;
50 public class BasicMacroTools
{
51 private final XDispatchProvider mDispProv
;
52 private final XMultiServiceFactory mMSF
;
53 private final XURLTransformer mParser
;
54 private final XNameAccess mLCxNA
; //LibraryContainer::XNameAccess
55 private final XLibraryContainer mLCxLC
; //LibraryContainer::XLibraryContainer
58 *While initializing the Basic Libraries will be appendend to the Document
60 public BasicMacroTools(XMultiServiceFactory msf
, XModel xModel
,
61 XComponent xDoc
) throws java
.lang
.Exception
{
64 mDispProv
= makeDispatchProvider(mMSF
, xModel
);
65 mParser
= makeParser(mMSF
);
67 Object DocLibCont
= null;
70 XPropertySet xDocProps
= (XPropertySet
) UnoRuntime
.queryInterface(
71 XPropertySet
.class, xDoc
);
72 DocLibCont
= xDocProps
.getPropertyValue("BasicLibraries");
73 } catch (com
.sun
.star
.uno
.Exception e
) {
75 "Couldn't get BasicLibraries-Container from document: " + e
.toString());
78 mLCxNA
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class,
81 mLCxLC
= (XLibraryContainer
) UnoRuntime
.queryInterface(
82 XLibraryContainer
.class, DocLibCont
);
84 } catch (Exception e
) {
85 throw new Exception("could not initialize BasicMacros " +
91 * While initializing the Basic Libraries will be appendend to the Office
93 public BasicMacroTools(XMultiServiceFactory msf
, XModel xModel
)
94 throws java
.lang
.Exception
{
97 mDispProv
= makeDispatchProvider(mMSF
, xModel
);
98 mParser
= makeParser(mMSF
);
103 ASLC
= mMSF
.createInstance(
104 "com.sun.star.script.ApplicationScriptLibraryContainer");
105 } catch (com
.sun
.star
.uno
.Exception e
) {
107 "Couldn't create ApplicationScriptLibraryContainer" + e
.toString());
110 mLCxNA
= (XNameAccess
) UnoRuntime
.queryInterface(XNameAccess
.class,
113 mLCxLC
= (XLibraryContainer
) UnoRuntime
.queryInterface(
114 XLibraryContainer
.class, ASLC
);
116 } catch (Exception e
) {
117 throw new Exception("could not initialize BasicMacros " +
122 private static XDispatchProvider
makeDispatchProvider(XMultiServiceFactory mMSF
,
124 throws java
.lang
.Exception
{
125 XController xController
= aModel
.getCurrentController();
126 XFrame xFrame
= xController
.getFrame();
128 if (xFrame
== null) {
129 throw new Exception("Could not create DispatchProvider");
132 return (XDispatchProvider
) UnoRuntime
.queryInterface(
133 XDispatchProvider
.class, xFrame
);
136 private static XURLTransformer
makeParser(XMultiServiceFactory mMSF
)
137 throws java
.lang
.Exception
{
139 return (com
.sun
.star
.util
.XURLTransformer
) UnoRuntime
.queryInterface(
140 XURLTransformer
.class, mMSF
.createInstance(
141 "com.sun.star.util.URLTransformer"));
142 } catch (Exception e
) {
143 throw new Exception("could not create UTL-Transformer " +
148 public void loadLibrary(String LibraryName
, String LibraryURL
)
149 throws java
.lang
.Exception
{
151 appendLibrary(LibraryName
, LibraryURL
);
152 } catch (java
.lang
.Exception e
) {
154 throw new Exception("ERROR: Could not append Library " +
155 LibraryName
+ e
.toString());
159 mLCxLC
.loadLibrary(LibraryName
);
160 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
162 throw new Exception("ERROR: Could not load Library " +
163 LibraryName
+ e
.toString());
164 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
166 throw new Exception("ERROR: Could not load Library " +
167 LibraryName
+ e
.toString());
171 private void appendLibrary(String LibraryName
, String LibraryURL
)
172 throws java
.lang
.Exception
{
174 removeLibrary(LibraryName
);
175 } catch (java
.lang
.Exception e
) {
179 mLCxLC
.createLibraryLink(LibraryName
, LibraryURL
, false);
180 } catch (com
.sun
.star
.container
.ElementExistException e
) {
182 throw new Exception("ERROR: Library " + LibraryName
+
183 "already exist." + e
.toString());
184 } catch (com
.sun
.star
.uno
.Exception e
) {
186 throw new Exception("Could not link Basic library:" +
187 LibraryName
+ e
.toString());
191 public void removeLibrary(String LibraryName
) throws java
.lang
.Exception
{
192 if (mLCxNA
.hasByName(LibraryName
)) {
194 mLCxLC
.removeLibrary(LibraryName
);
195 } catch (com
.sun
.star
.container
.NoSuchElementException e
) {
197 throw new Exception("Could not remove Basic library:" +
198 LibraryName
+ ": Library does not exist" + e
.toString());
199 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
201 throw new Exception("Could not remove Basic library:" +
202 LibraryName
+ e
.toString());
207 public void runMarco(String MacroName
) throws java
.lang
.Exception
{
208 URL
[] aParseURL
= new URL
[1];
209 aParseURL
[0] = new URL();
210 aParseURL
[0].Complete
= "macro://./" + MacroName
; //Standard.Stock.GetSymbol('micro','')";
211 mParser
.parseStrict(aParseURL
);
213 URL aURL
= aParseURL
[0];
214 XDispatch xDispatcher
= mDispProv
.queryDispatch(aURL
, "", 0);
216 if (xDispatcher
!= null) {
217 xDispatcher
.dispatch(aURL
, null);
219 throw new Exception("Could not run Macro " + MacroName
);
224 * Set the given <CODE>secureURL</CODE> as secure URL for marco execution.
225 * The macros of documents located in <CODE>secureURL</CODE> will be executed
227 * @param xMSF the XMultiServiceFactory
228 * @param secureURL the URL the documet is located
229 * @throws java.lang.Exception throws this exception on any error
231 public static void addSecureBasicMarcosURL(XMultiServiceFactory xMSF
, String secureURL
)
234 secureURL
= utils
.getFullURL(secureURL
);
236 // configure Office to allow to execute macos
237 PropertyValue
[] ProvArgs
= new PropertyValue
[1];
238 PropertyValue Arg
= new PropertyValue();
239 Arg
.Name
= "nodepath";
240 Arg
.Value
= "/org.openoffice.Office.Common/Security";
243 Object oProvider
= xMSF
.createInstance("com.sun.star.configuration.ConfigurationProvider");
246 XMultiServiceFactory oProviderMSF
= (XMultiServiceFactory
)
247 UnoRuntime
.queryInterface(XMultiServiceFactory
.class, oProvider
);
249 Object oSecure
= oProviderMSF
.createInstanceWithArguments(
250 "com.sun.star.configuration.ConfigurationUpdateAccess",
253 XPropertySet oSecureProps
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, oSecure
);
255 Object oScripting
= oSecureProps
.getPropertyValue("Scripting");
256 XPropertySet oScriptingSettings
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, oScripting
);
258 oScriptingSettings
.setPropertyValue("SecureURL", new String
[]{secureURL
});
259 oScriptingSettings
.setPropertyValue("OfficeBasic", new Integer(2));
261 XChangesBatch oSecureChange
= (XChangesBatch
) UnoRuntime
.queryInterface(XChangesBatch
.class, oSecure
);
262 oSecureChange
.commitChanges();