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