xref: /AOO41X/main/qadevOOo/runner/util/BasicMacroTools.java (revision ef39d40d3f5e66cf3f035b3e93783012b340500d)
1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package util;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
26cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
27cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
28cdf0e10cSrcweir import com.sun.star.frame.XController;
29cdf0e10cSrcweir import com.sun.star.frame.XDispatch;
30cdf0e10cSrcweir import com.sun.star.frame.XDispatchProvider;
31cdf0e10cSrcweir import com.sun.star.frame.XFrame;
32cdf0e10cSrcweir import com.sun.star.frame.XModel;
33cdf0e10cSrcweir import com.sun.star.lang.XComponent;
34cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35cdf0e10cSrcweir import com.sun.star.script.XLibraryContainer;
36cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37cdf0e10cSrcweir import com.sun.star.util.*;
38cdf0e10cSrcweir import com.sun.star.util.URL;
39cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir 
43cdf0e10cSrcweir public class BasicMacroTools {
44cdf0e10cSrcweir     private final XDispatchProvider mDispProv;
45cdf0e10cSrcweir     private final XMultiServiceFactory mMSF;
46cdf0e10cSrcweir     private final XURLTransformer mParser;
47cdf0e10cSrcweir     private final XNameAccess mLCxNA; //LibraryContainer::XNameAccess
48cdf0e10cSrcweir     private final XLibraryContainer mLCxLC; //LibraryContainer::XLibraryContainer
49cdf0e10cSrcweir 
50cdf0e10cSrcweir     /*
51cdf0e10cSrcweir      *While initializing the Basic Libraries will be appendend to the Document
52cdf0e10cSrcweir      */
BasicMacroTools(XMultiServiceFactory msf, XModel xModel, XComponent xDoc)53cdf0e10cSrcweir     public BasicMacroTools(XMultiServiceFactory msf, XModel xModel,
54cdf0e10cSrcweir                            XComponent xDoc) throws java.lang.Exception {
55cdf0e10cSrcweir         try {
56cdf0e10cSrcweir             mMSF = msf;
57cdf0e10cSrcweir             mDispProv = makeDispatchProvider(mMSF, xModel);
58cdf0e10cSrcweir             mParser = makeParser(mMSF);
59cdf0e10cSrcweir 
60cdf0e10cSrcweir             Object DocLibCont = null;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir             try {
63cdf0e10cSrcweir                 XPropertySet xDocProps = (XPropertySet) UnoRuntime.queryInterface(
64cdf0e10cSrcweir                                                  XPropertySet.class, xDoc);
65cdf0e10cSrcweir                 DocLibCont = xDocProps.getPropertyValue("BasicLibraries");
66cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
67cdf0e10cSrcweir                 throw new Exception(
68cdf0e10cSrcweir                         "Couldn't get BasicLibraries-Container from document: " + e.toString());
69cdf0e10cSrcweir             }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
72cdf0e10cSrcweir                                                              DocLibCont);
73cdf0e10cSrcweir 
74cdf0e10cSrcweir             mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
75cdf0e10cSrcweir                              XLibraryContainer.class, DocLibCont);
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         } catch (Exception e) {
78cdf0e10cSrcweir             throw new Exception("could not initialize BasicMacros " +
79cdf0e10cSrcweir                                 e.toString());
80cdf0e10cSrcweir         }
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     /*
84cdf0e10cSrcweir      * While initializing the Basic Libraries will be appendend to the Office
85cdf0e10cSrcweir     */
BasicMacroTools(XMultiServiceFactory msf, XModel xModel)86cdf0e10cSrcweir     public BasicMacroTools(XMultiServiceFactory msf, XModel xModel)
87cdf0e10cSrcweir                     throws java.lang.Exception {
88cdf0e10cSrcweir         try {
89cdf0e10cSrcweir             mMSF = msf;
90cdf0e10cSrcweir             mDispProv = makeDispatchProvider(mMSF, xModel);
91cdf0e10cSrcweir             mParser = makeParser(mMSF);
92cdf0e10cSrcweir 
93cdf0e10cSrcweir             Object ASLC = null;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir             try {
96cdf0e10cSrcweir                 ASLC = mMSF.createInstance(
97cdf0e10cSrcweir                                "com.sun.star.script.ApplicationScriptLibraryContainer");
98cdf0e10cSrcweir             } catch (com.sun.star.uno.Exception e) {
99cdf0e10cSrcweir                 throw new Exception(
100cdf0e10cSrcweir                         "Couldn't create ApplicationScriptLibraryContainer" + e.toString());
101cdf0e10cSrcweir             }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir             mLCxNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class,
104cdf0e10cSrcweir                                                              ASLC);
105cdf0e10cSrcweir 
106cdf0e10cSrcweir             mLCxLC = (XLibraryContainer) UnoRuntime.queryInterface(
107cdf0e10cSrcweir                              XLibraryContainer.class, ASLC);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         } catch (Exception e) {
110cdf0e10cSrcweir             throw new Exception("could not initialize BasicMacros " +
111cdf0e10cSrcweir                                 e.toString());
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
makeDispatchProvider(XMultiServiceFactory mMSF, XModel aModel)115cdf0e10cSrcweir     private static XDispatchProvider makeDispatchProvider(XMultiServiceFactory mMSF,
116cdf0e10cSrcweir                                                           XModel aModel)
117cdf0e10cSrcweir                                                    throws java.lang.Exception {
118cdf0e10cSrcweir         XController xController = aModel.getCurrentController();
119cdf0e10cSrcweir         XFrame xFrame = xController.getFrame();
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         if (xFrame == null) {
122cdf0e10cSrcweir             throw new Exception("Could not create DispatchProvider");
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         return (XDispatchProvider) UnoRuntime.queryInterface(
126cdf0e10cSrcweir                        XDispatchProvider.class, xFrame);
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
makeParser(XMultiServiceFactory mMSF)129cdf0e10cSrcweir     private static XURLTransformer makeParser(XMultiServiceFactory mMSF)
130cdf0e10cSrcweir                                        throws java.lang.Exception {
131cdf0e10cSrcweir         try {
132cdf0e10cSrcweir             return (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
133cdf0e10cSrcweir                            XURLTransformer.class, mMSF.createInstance(
134cdf0e10cSrcweir                                    "com.sun.star.util.URLTransformer"));
135cdf0e10cSrcweir         } catch (Exception e) {
136cdf0e10cSrcweir             throw new Exception("could not create UTL-Transformer " +
137cdf0e10cSrcweir                                 e.toString());
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
loadLibrary(String LibraryName, String LibraryURL)141cdf0e10cSrcweir     public void loadLibrary(String LibraryName, String LibraryURL)
142cdf0e10cSrcweir                      throws java.lang.Exception {
143cdf0e10cSrcweir         try {
144cdf0e10cSrcweir             appendLibrary(LibraryName, LibraryURL);
145cdf0e10cSrcweir         } catch (java.lang.Exception e) {
146cdf0e10cSrcweir             e.printStackTrace();
147cdf0e10cSrcweir             throw new Exception("ERROR: Could not append Library " +
148cdf0e10cSrcweir                                 LibraryName + e.toString());
149cdf0e10cSrcweir         }
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         try {
152cdf0e10cSrcweir             mLCxLC.loadLibrary(LibraryName);
153cdf0e10cSrcweir         } catch (com.sun.star.container.NoSuchElementException e) {
154cdf0e10cSrcweir             e.printStackTrace();
155cdf0e10cSrcweir             throw new Exception("ERROR: Could not load Library " +
156cdf0e10cSrcweir                                 LibraryName + e.toString());
157cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
158cdf0e10cSrcweir             e.printStackTrace();
159cdf0e10cSrcweir             throw new Exception("ERROR: Could not load Library " +
160cdf0e10cSrcweir                                 LibraryName + e.toString());
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir     }
163cdf0e10cSrcweir 
appendLibrary(String LibraryName, String LibraryURL)164cdf0e10cSrcweir     private void appendLibrary(String LibraryName, String LibraryURL)
165cdf0e10cSrcweir                         throws java.lang.Exception {
166cdf0e10cSrcweir         try {
167cdf0e10cSrcweir             removeLibrary(LibraryName);
168cdf0e10cSrcweir         } catch (java.lang.Exception e) {
169cdf0e10cSrcweir         }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         try {
172cdf0e10cSrcweir             mLCxLC.createLibraryLink(LibraryName, LibraryURL, false);
173cdf0e10cSrcweir         } catch (com.sun.star.container.ElementExistException e) {
174cdf0e10cSrcweir             e.printStackTrace();
175cdf0e10cSrcweir             throw new Exception("ERROR: Library " + LibraryName +
176cdf0e10cSrcweir                                 "already exist." + e.toString());
177cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
178cdf0e10cSrcweir             e.printStackTrace();
179cdf0e10cSrcweir             throw new Exception("Could not link Basic library:" +
180cdf0e10cSrcweir                                 LibraryName + e.toString());
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir 
removeLibrary(String LibraryName)184cdf0e10cSrcweir     public void removeLibrary(String LibraryName) throws java.lang.Exception {
185cdf0e10cSrcweir         if (mLCxNA.hasByName(LibraryName)) {
186cdf0e10cSrcweir             try {
187cdf0e10cSrcweir                 mLCxLC.removeLibrary(LibraryName);
188cdf0e10cSrcweir             } catch (com.sun.star.container.NoSuchElementException e) {
189cdf0e10cSrcweir                 e.printStackTrace();
190cdf0e10cSrcweir                 throw new Exception("Could not remove Basic library:" +
191cdf0e10cSrcweir                                     LibraryName + ": Library does not exist" + e.toString());
192cdf0e10cSrcweir             } catch (com.sun.star.lang.WrappedTargetException e) {
193cdf0e10cSrcweir                 e.printStackTrace();
194cdf0e10cSrcweir                 throw new Exception("Could not remove Basic library:" +
195cdf0e10cSrcweir                                     LibraryName + e.toString());
196cdf0e10cSrcweir             }
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
runMarco(String MacroName)200cdf0e10cSrcweir     public void runMarco(String MacroName) throws java.lang.Exception {
201cdf0e10cSrcweir         URL[] aParseURL = new URL[1];
202cdf0e10cSrcweir         aParseURL[0] = new URL();
203cdf0e10cSrcweir         aParseURL[0].Complete = "macro://./" + MacroName; //Standard.Stock.GetSymbol('micro','')";
204cdf0e10cSrcweir         mParser.parseStrict(aParseURL);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         URL aURL = aParseURL[0];
207cdf0e10cSrcweir         XDispatch xDispatcher = mDispProv.queryDispatch(aURL, "", 0);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir         if (xDispatcher != null) {
210cdf0e10cSrcweir             xDispatcher.dispatch(aURL, null);
211cdf0e10cSrcweir         } else {
212cdf0e10cSrcweir             throw new Exception("Could not run Macro " + MacroName);
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     /**
217cdf0e10cSrcweir      * Set the given <CODE>secureURL</CODE> as secure URL for marco execution.
218cdf0e10cSrcweir      * The macros of documents located in <CODE>secureURL</CODE> will be executed
219cdf0e10cSrcweir      * automatically.
220cdf0e10cSrcweir      * @param xMSF the XMultiServiceFactory
221cdf0e10cSrcweir      * @param secureURL the URL the documet is located
222cdf0e10cSrcweir      * @throws java.lang.Exception throws this exception on any error
223cdf0e10cSrcweir      */
addSecureBasicMarcosURL(XMultiServiceFactory xMSF, String secureURL)224cdf0e10cSrcweir     public static void addSecureBasicMarcosURL(XMultiServiceFactory xMSF, String secureURL)
225cdf0e10cSrcweir         throws Exception {
226cdf0e10cSrcweir 
227cdf0e10cSrcweir         secureURL = utils.getFullURL(secureURL);
228cdf0e10cSrcweir 
229cdf0e10cSrcweir         // configure Office to allow to execute macos
230cdf0e10cSrcweir         PropertyValue[] ProvArgs = new PropertyValue [1];
231cdf0e10cSrcweir         PropertyValue Arg = new PropertyValue();
232cdf0e10cSrcweir         Arg.Name = "nodepath";
233cdf0e10cSrcweir         Arg.Value = "/org.openoffice.Office.Common/Security";
234cdf0e10cSrcweir         ProvArgs[0] = Arg;
235cdf0e10cSrcweir 
236cdf0e10cSrcweir         Object oProvider = xMSF.createInstance("com.sun.star.configuration.ConfigurationProvider");
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 
239cdf0e10cSrcweir         XMultiServiceFactory oProviderMSF = (XMultiServiceFactory)
240cdf0e10cSrcweir                         UnoRuntime.queryInterface(XMultiServiceFactory.class, oProvider);
241cdf0e10cSrcweir 
242cdf0e10cSrcweir         Object oSecure = oProviderMSF.createInstanceWithArguments(
243cdf0e10cSrcweir             "com.sun.star.configuration.ConfigurationUpdateAccess",
244cdf0e10cSrcweir             ProvArgs);
245cdf0e10cSrcweir 
246cdf0e10cSrcweir         XPropertySet oSecureProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oSecure);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         Object oScripting = oSecureProps.getPropertyValue("Scripting");
249cdf0e10cSrcweir         XPropertySet oScriptingSettings = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oScripting);
250cdf0e10cSrcweir 
251cdf0e10cSrcweir         oScriptingSettings.setPropertyValue("SecureURL", new String[]{secureURL});
252cdf0e10cSrcweir         oScriptingSettings.setPropertyValue("OfficeBasic", new Integer(2));
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         XChangesBatch oSecureChange = (XChangesBatch) UnoRuntime.queryInterface(XChangesBatch.class, oSecure);
255cdf0e10cSrcweir         oSecureChange.commitChanges();
256cdf0e10cSrcweir     }
257cdf0e10cSrcweir }
258