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