update dev300-m58
[ooovba.git] / qadevOOo / runner / util / BasicMacroTools.java
blob9de71e5e8112f7c12ffaeb35ed512f71061c7c89
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BasicMacroTools.java,v $
10 * $Revision: 1.7 $
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 ************************************************************************/
30 package util;
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 {
62 try {
63 mMSF = msf;
64 mDispProv = makeDispatchProvider(mMSF, xModel);
65 mParser = makeParser(mMSF);
67 Object DocLibCont = null;
69 try {
70 XPropertySet xDocProps = (XPropertySet) UnoRuntime.queryInterface(
71 XPropertySet.class, xDoc);
72 DocLibCont = xDocProps.getPropertyValue("BasicLibraries");
73 } catch (com.sun.star.uno.Exception e) {
74 throw new Exception(
75 "Couldn't get BasicLibraries-Container from document: " + e.toString());
78 mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
79 DocLibCont);
81 mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
82 XLibraryContainer.class, DocLibCont);
84 } catch (Exception e) {
85 throw new Exception("could not initialize BasicMacros " +
86 e.toString());
91 * While initializing the Basic Libraries will be appendend to the Office
93 public BasicMacroTools(XMultiServiceFactory msf, XModel xModel)
94 throws java.lang.Exception {
95 try {
96 mMSF = msf;
97 mDispProv = makeDispatchProvider(mMSF, xModel);
98 mParser = makeParser(mMSF);
100 Object ASLC = null;
102 try {
103 ASLC = mMSF.createInstance(
104 "com.sun.star.script.ApplicationScriptLibraryContainer");
105 } catch (com.sun.star.uno.Exception e) {
106 throw new Exception(
107 "Couldn't create ApplicationScriptLibraryContainer" + e.toString());
110 mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
111 ASLC);
113 mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
114 XLibraryContainer.class, ASLC);
116 } catch (Exception e) {
117 throw new Exception("could not initialize BasicMacros " +
118 e.toString());
122 private static XDispatchProvider makeDispatchProvider(XMultiServiceFactory mMSF,
123 XModel aModel)
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 {
138 try {
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 " +
144 e.toString());
148 public void loadLibrary(String LibraryName, String LibraryURL)
149 throws java.lang.Exception {
150 try {
151 appendLibrary(LibraryName, LibraryURL);
152 } catch (java.lang.Exception e) {
153 e.printStackTrace();
154 throw new Exception("ERROR: Could not append Library " +
155 LibraryName + e.toString());
158 try {
159 mLCxLC.loadLibrary(LibraryName);
160 } catch (com.sun.star.container.NoSuchElementException e) {
161 e.printStackTrace();
162 throw new Exception("ERROR: Could not load Library " +
163 LibraryName + e.toString());
164 } catch (com.sun.star.lang.WrappedTargetException e) {
165 e.printStackTrace();
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 {
173 try {
174 removeLibrary(LibraryName);
175 } catch (java.lang.Exception e) {
178 try {
179 mLCxLC.createLibraryLink(LibraryName, LibraryURL, false);
180 } catch (com.sun.star.container.ElementExistException e) {
181 e.printStackTrace();
182 throw new Exception("ERROR: Library " + LibraryName +
183 "already exist." + e.toString());
184 } catch (com.sun.star.uno.Exception e) {
185 e.printStackTrace();
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)) {
193 try {
194 mLCxLC.removeLibrary(LibraryName);
195 } catch (com.sun.star.container.NoSuchElementException e) {
196 e.printStackTrace();
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) {
200 e.printStackTrace();
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);
218 } else {
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
226 * automatically.
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)
232 throws Exception {
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";
241 ProvArgs[0] = Arg;
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",
251 ProvArgs);
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();