merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / util / BasicMacroTools.java
blob0d64779137f7a5d89a7829264b7b89d10e7b180a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package util;
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XNameAccess;
32 import com.sun.star.frame.XController;
33 import com.sun.star.frame.XDispatch;
34 import com.sun.star.frame.XDispatchProvider;
35 import com.sun.star.frame.XFrame;
36 import com.sun.star.frame.XModel;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.script.XLibraryContainer;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.util.*;
42 import com.sun.star.util.URL;
43 import com.sun.star.util.XURLTransformer;
47 public class BasicMacroTools {
48 private final XDispatchProvider mDispProv;
49 private final XMultiServiceFactory mMSF;
50 private final XURLTransformer mParser;
51 private final XNameAccess mLCxNA; //LibraryContainer::XNameAccess
52 private final XLibraryContainer mLCxLC; //LibraryContainer::XLibraryContainer
55 *While initializing the Basic Libraries will be appendend to the Document
57 public BasicMacroTools(XMultiServiceFactory msf, XModel xModel,
58 XComponent xDoc) throws java.lang.Exception {
59 try {
60 mMSF = msf;
61 mDispProv = makeDispatchProvider(mMSF, xModel);
62 mParser = makeParser(mMSF);
64 Object DocLibCont = null;
66 try {
67 XPropertySet xDocProps = (XPropertySet) UnoRuntime.queryInterface(
68 XPropertySet.class, xDoc);
69 DocLibCont = xDocProps.getPropertyValue("BasicLibraries");
70 } catch (com.sun.star.uno.Exception e) {
71 throw new Exception(
72 "Couldn't get BasicLibraries-Container from document: " + e.toString());
75 mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
76 DocLibCont);
78 mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
79 XLibraryContainer.class, DocLibCont);
81 } catch (Exception e) {
82 throw new Exception("could not initialize BasicMacros " +
83 e.toString());
88 * While initializing the Basic Libraries will be appendend to the Office
90 public BasicMacroTools(XMultiServiceFactory msf, XModel xModel)
91 throws java.lang.Exception {
92 try {
93 mMSF = msf;
94 mDispProv = makeDispatchProvider(mMSF, xModel);
95 mParser = makeParser(mMSF);
97 Object ASLC = null;
99 try {
100 ASLC = mMSF.createInstance(
101 "com.sun.star.script.ApplicationScriptLibraryContainer");
102 } catch (com.sun.star.uno.Exception e) {
103 throw new Exception(
104 "Couldn't create ApplicationScriptLibraryContainer" + e.toString());
107 mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
108 ASLC);
110 mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
111 XLibraryContainer.class, ASLC);
113 } catch (Exception e) {
114 throw new Exception("could not initialize BasicMacros " +
115 e.toString());
119 private static XDispatchProvider makeDispatchProvider(XMultiServiceFactory mMSF,
120 XModel aModel)
121 throws java.lang.Exception {
122 XController xController = aModel.getCurrentController();
123 XFrame xFrame = xController.getFrame();
125 if (xFrame == null) {
126 throw new Exception("Could not create DispatchProvider");
129 return (XDispatchProvider) UnoRuntime.queryInterface(
130 XDispatchProvider.class, xFrame);
133 private static XURLTransformer makeParser(XMultiServiceFactory mMSF)
134 throws java.lang.Exception {
135 try {
136 return (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
137 XURLTransformer.class, mMSF.createInstance(
138 "com.sun.star.util.URLTransformer"));
139 } catch (Exception e) {
140 throw new Exception("could not create UTL-Transformer " +
141 e.toString());
145 public void loadLibrary(String LibraryName, String LibraryURL)
146 throws java.lang.Exception {
147 try {
148 appendLibrary(LibraryName, LibraryURL);
149 } catch (java.lang.Exception e) {
150 e.printStackTrace();
151 throw new Exception("ERROR: Could not append Library " +
152 LibraryName + e.toString());
155 try {
156 mLCxLC.loadLibrary(LibraryName);
157 } catch (com.sun.star.container.NoSuchElementException e) {
158 e.printStackTrace();
159 throw new Exception("ERROR: Could not load Library " +
160 LibraryName + e.toString());
161 } catch (com.sun.star.lang.WrappedTargetException e) {
162 e.printStackTrace();
163 throw new Exception("ERROR: Could not load Library " +
164 LibraryName + e.toString());
168 private void appendLibrary(String LibraryName, String LibraryURL)
169 throws java.lang.Exception {
170 try {
171 removeLibrary(LibraryName);
172 } catch (java.lang.Exception e) {
175 try {
176 mLCxLC.createLibraryLink(LibraryName, LibraryURL, false);
177 } catch (com.sun.star.container.ElementExistException e) {
178 e.printStackTrace();
179 throw new Exception("ERROR: Library " + LibraryName +
180 "already exist." + e.toString());
181 } catch (com.sun.star.uno.Exception e) {
182 e.printStackTrace();
183 throw new Exception("Could not link Basic library:" +
184 LibraryName + e.toString());
188 public void removeLibrary(String LibraryName) throws java.lang.Exception {
189 if (mLCxNA.hasByName(LibraryName)) {
190 try {
191 mLCxLC.removeLibrary(LibraryName);
192 } catch (com.sun.star.container.NoSuchElementException e) {
193 e.printStackTrace();
194 throw new Exception("Could not remove Basic library:" +
195 LibraryName + ": Library does not exist" + e.toString());
196 } catch (com.sun.star.lang.WrappedTargetException e) {
197 e.printStackTrace();
198 throw new Exception("Could not remove Basic library:" +
199 LibraryName + e.toString());
204 public void runMarco(String MacroName) throws java.lang.Exception {
205 URL[] aParseURL = new URL[1];
206 aParseURL[0] = new URL();
207 aParseURL[0].Complete = "macro://./" + MacroName; //Standard.Stock.GetSymbol('micro','')";
208 mParser.parseStrict(aParseURL);
210 URL aURL = aParseURL[0];
211 XDispatch xDispatcher = mDispProv.queryDispatch(aURL, "", 0);
213 if (xDispatcher != null) {
214 xDispatcher.dispatch(aURL, null);
215 } else {
216 throw new Exception("Could not run Macro " + MacroName);
221 * Set the given <CODE>secureURL</CODE> as secure URL for marco execution.
222 * The macros of documents located in <CODE>secureURL</CODE> will be executed
223 * automatically.
224 * @param xMSF the XMultiServiceFactory
225 * @param secureURL the URL the documet is located
226 * @throws java.lang.Exception throws this exception on any error
228 public static void addSecureBasicMarcosURL(XMultiServiceFactory xMSF, String secureURL)
229 throws Exception {
231 secureURL = utils.getFullURL(secureURL);
233 // configure Office to allow to execute macos
234 PropertyValue[] ProvArgs = new PropertyValue [1];
235 PropertyValue Arg = new PropertyValue();
236 Arg.Name = "nodepath";
237 Arg.Value = "/org.openoffice.Office.Common/Security";
238 ProvArgs[0] = Arg;
240 Object oProvider = xMSF.createInstance("com.sun.star.configuration.ConfigurationProvider");
243 XMultiServiceFactory oProviderMSF = (XMultiServiceFactory)
244 UnoRuntime.queryInterface(XMultiServiceFactory.class, oProvider);
246 Object oSecure = oProviderMSF.createInstanceWithArguments(
247 "com.sun.star.configuration.ConfigurationUpdateAccess",
248 ProvArgs);
250 XPropertySet oSecureProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oSecure);
252 Object oScripting = oSecureProps.getPropertyValue("Scripting");
253 XPropertySet oScriptingSettings = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oScripting);
255 oScriptingSettings.setPropertyValue("SecureURL", new String[]{secureURL});
256 oScriptingSettings.setPropertyValue("OfficeBasic", new Integer(2));
258 XChangesBatch oSecureChange = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, oSecure);
259 oSecureChange.commitChanges();