xref: /AOO41X/main/sc/source/ui/vba/testvba/testvba.cxx (revision b3f79822e811ac3493b185030a72c3c5a51f32d8)
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 #include "cppuhelper/bootstrap.hxx"
23 
24 #include <com/sun/star/beans/Property.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/XPropertySetInfo.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <com/sun/star/frame/XComponentLoader.hpp>
30 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
31 #include <com/sun/star/sheet/XSpreadsheet.hpp>
32 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
33 #include <com/sun/star/util/XCloseable.hpp>
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
36 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
37 #include <com/sun/star/document/XTypeDetection.hpp>
38 
39 #include <tools/urlobj.hxx>
40 #include <osl/file.hxx>
41 
42 #include <memory>
43 #include <iostream>
44 
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::sheet;
47 
48 using ::com::sun::star::beans::Property;
49 using ::com::sun::star::beans::PropertyValue;
50 using ::com::sun::star::beans::XPropertySet;
51 using ::com::sun::star::beans::XPropertySetInfo;
52 using ::com::sun::star::container::XNameContainer;
53 using ::com::sun::star::lang::XComponent;
54 using ::com::sun::star::lang::XMultiComponentFactory;
55 using ::com::sun::star::frame::XComponentLoader;
56 using ::com::sun::star::uno::Reference;
57 using ::com::sun::star::uno::Sequence;
58 using ::com::sun::star::uno::UNO_QUERY;
59 using ::com::sun::star::uno::UNO_QUERY_THROW;
60 using ::com::sun::star::uno::XComponentContext;
61 using ::com::sun::star::uno::XInterface;
62 using ::com::sun::star::ucb::XSimpleFileAccess;
63 using ::com::sun::star::document::XTypeDetection;
64 using ::rtl::OUString;
65 
66 using ::std::auto_ptr;
67 
68 const OUString EXTN = rtl::OUString::createFromAscii(".xls");
69 
convertToURL(const OUString & rPath)70 OUString convertToURL( const OUString& rPath )
71 {
72         rtl::OUString aURL;
73         INetURLObject aObj;
74         aObj.SetURL( rPath );
75         bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
76         if ( bIsURL )
77                 aURL = rPath;
78         else
79         {
80                 osl::FileBase::getFileURLFromSystemPath( rPath, aURL );
81                 if ( aURL.equals( rPath ) )
82                     throw uno::RuntimeException( rtl::OUString::createFromAscii( "could'nt convert " ).concat( rPath ).concat( rtl::OUString::createFromAscii( " to a URL, is it a fully qualified path name? " ) ), Reference< uno::XInterface >() );
83         }
84         return aURL;
85 }
86 
ascii(const sal_Char * cstr)87 OUString ascii(const sal_Char* cstr)
88 {
89     return OUString::createFromAscii(cstr);
90 }
91 
getStr(const OUString & ou)92 const sal_Char* getStr(const OUString& ou)
93 {
94     return OUStringToOString(ou, RTL_TEXTENCODING_UTF8).getStr();
95 }
96 
97 
usage(const char * pName)98 int usage( const char* pName )
99 {
100     std::cerr << "usage: " << pName << "<path to testdocument dir> <output_directory>" << std::endl;
101         return 1;
102 
103 }
104 
105 class TestVBA
106 {
107 private:
108     Reference< XComponentContext >  mxContext;
109     Reference< XMultiComponentFactory > mxMCF;
110     Reference< XComponentLoader > mxCompLoader;
111     Reference< XSimpleFileAccess > mxSFA;
112     rtl::OUString msOutDirPath;
113 protected:
114 public:
TestVBA(const Reference<XComponentContext> & _xContext,const Reference<XMultiComponentFactory> & _xMCF,const Reference<XComponentLoader> & _xCompLoader,const rtl::OUString & _outDirPath)115     TestVBA( const Reference< XComponentContext >&  _xContext,
116         const Reference< XMultiComponentFactory >& _xMCF,
117         const Reference< XComponentLoader >& _xCompLoader,
118         const rtl::OUString& _outDirPath ) : mxContext( _xContext ), mxMCF( _xMCF ),
119 mxCompLoader( _xCompLoader ), msOutDirPath( convertToURL( _outDirPath  ) )
120     {
121         mxSFA.set( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ), mxContext), uno::UNO_QUERY_THROW );
122     }
123 
getLogLocation()124     rtl::OUString getLogLocation() throw (  beans::UnknownPropertyException,  lang::IllegalArgumentException, lang::WrappedTargetException,  uno::Exception )
125     {
126         rtl::OUString sLogLocation;
127         Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
128         pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
129         sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( rtl::OUString::createFromAscii( "HelperAPI-test.log" ) );
130         return sLogLocation;
131     }
getLogLocationWithName(OUString fileName)132     rtl::OUString getLogLocationWithName( OUString fileName ) throw (  beans::UnknownPropertyException,  lang::IllegalArgumentException, lang::WrappedTargetException,  uno::Exception )
133     {
134         printf("%s\n", getenv("HOME") );
135         printf("file name %s\n", rtl::OUStringToOString( fileName, RTL_TEXTENCODING_UTF8 ).getStr() );
136         //rtl::OUString sLogLocation( rtl::OUString::createFromAscii( getenv("HOME") ) );
137         rtl::OUString sLogLocation;
138         Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
139         pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
140         sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( fileName.copy ( 0, fileName.lastIndexOf( EXTN )  ) + rtl::OUString::createFromAscii( ".log" ) );
141         return sLogLocation;
142     }
143 
init()144     void init()
145     {
146         // blow away previous logs?
147     }
148 
proccessDocument(const rtl::OUString & sUrl)149     void proccessDocument( const rtl::OUString& sUrl )
150     {
151             if ( !mxSFA->isFolder( sUrl ) && sUrl.endsWithIgnoreAsciiCaseAsciiL( ".xls", 4 ) )
152 
153             {
154                 try
155                 {
156                     OSL_TRACE( "processing %s",  rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
157                     printf( "processing %s\n",  rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
158                     // Loading the wanted document
159                     Sequence< PropertyValue > propertyValues(1);
160                     propertyValues[0].Name = rtl::OUString::createFromAscii( "Hidden" );
161                     propertyValues[0].Value <<= sal_False;
162 
163                     rtl::OUString sfileUrl = convertToURL( sUrl );
164                     printf( "try to get xDoc %s\n", rtl::OUStringToOString( sfileUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
165                     Reference< uno::XInterface > xDoc =
166                         mxCompLoader->loadComponentFromURL( sfileUrl, rtl::OUString::createFromAscii( "_blank" ), 0, propertyValues);
167                     printf( "got xDoc\n" );
168 
169                     OUString logFileURL = convertToURL( getLogLocation() );
170                     try
171                     {
172                         Reference< script::provider::XScriptProviderSupplier > xSupplier( xDoc, uno::UNO_QUERY_THROW ) ;
173                         if ( mxSFA->exists( logFileURL ) )
174                             mxSFA->kill( logFileURL );
175 
176                         printf("try to get the ScriptProvider\n");
177                         Reference< script::provider::XScriptProvider > xProv = xSupplier->getScriptProvider();
178                         printf("get the ScriptProvider\n");
179                         printf("try to get the Script\n");
180                         Reference< script::provider::XScript > xScript;
181                         try
182                         {
183                             xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.TestMacros.Main?language=Basic&location=document" ));
184                         } catch ( uno::Exception& e )
185                         {
186                             try
187                             {
188                                 xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.testMacro.Main?language=Basic&location=document" ));
189                             } catch ( uno::Exception& e2 )
190                             {
191                                 xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.testMain.Main?language=Basic&location=document" ));
192                             }
193                         }
194                         OSL_TRACE("Got script for doc %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
195                         printf("get the Script\n");
196                         Sequence< uno::Any > aArgs;
197                         Sequence< sal_Int16 > aOutArgsIndex;
198                         Sequence< uno::Any > aOutArgs;
199 
200                         xScript->invoke(aArgs, aOutArgsIndex, aOutArgs);
201 
202                         OUString fileName = sUrl.copy ( sUrl.lastIndexOf( '/' ) );
203                         OUString newLocation = msOutDirPath + fileName.copy ( 0, fileName.lastIndexOf( EXTN )  ) + rtl::OUString::createFromAscii( ".log" );
204                         try
205                         {
206                             printf("move log file\n");
207                             mxSFA->move( logFileURL, newLocation );
208                             OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
209                             printf("moved to new location\n");
210                         }
211                         catch ( uno::Exception& e )
212                         {
213                             logFileURL = convertToURL( getLogLocationWithName( fileName ) );
214                             printf("move log file from %s\n", rtl::OUStringToOString( logFileURL, RTL_TEXTENCODING_UTF8 ).getStr() );
215                             mxSFA->move( logFileURL, newLocation );
216                             OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
217                             printf("moved to new location\n");
218                         }
219 
220                     }
221                     catch ( uno::Exception& e )
222                     {
223                         std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
224                     }
225 
226                     // interface is supported, otherwise use XComponent.dispose
227                     Reference< util::XCloseable > xCloseable ( xDoc, uno::UNO_QUERY );
228 
229                     if ( xCloseable.is() )
230                     {
231                         printf("try to close\n");
232                         // will close application. and only run a test case for 3.0
233                         // maybe it is a bug. yes, it is a bug
234                         // if only one frame and model, click a button which related will colse.
235                         // will make a crash. It related with window listener.
236                         // so, for run all test cases, it should not close the document at this moment.
237                         xCloseable->close(sal_False);
238                         printf("closed\n");
239                     }
240                     else
241                     {
242                         printf("try to dispose\n");
243                         Reference< XComponent > xComp( xDoc, uno::UNO_QUERY_THROW );
244                         // same as close.
245                         xComp->dispose();
246                         printf("disposed\n");
247                     }
248                 }
249                 catch( uno::Exception& e )
250                 {
251                     std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
252                 }
253 
254             }
255         printf("complete processing %s\n", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
256     }
257 
traverse(const rtl::OUString & sFileDirectory)258     void traverse( const rtl::OUString& sFileDirectory )
259     {
260         rtl::OUString sFileDirectoryURL = convertToURL( sFileDirectory );
261         if ( !mxSFA->isFolder( sFileDirectoryURL) )
262         {
263             throw lang::IllegalArgumentException( rtl::OUString::createFromAscii( "not a directory: ").concat( sFileDirectoryURL ), Reference<uno::XInterface>(), 1 );
264         }
265         // Getting all files and directories in the current directory
266         Sequence<OUString> entries = mxSFA->getFolderContents( sFileDirectoryURL, sal_False );
267 
268         // Iterating for each file and directory
269         printf( "Entries %d\n", (int)entries.getLength() );
270         for ( sal_Int32 i = 0; i < entries.getLength(); ++i )
271         {
272             proccessDocument( entries[ i ] );
273         }
274     }
275 };
276 
tryDispose(Reference<uno::XInterface> xIF,const char * sComp)277 void tryDispose( Reference< uno::XInterface > xIF, const char* sComp )
278 {
279     Reference< lang::XComponent > xComponent( xIF, uno::UNO_QUERY );
280     if ( xComponent.is() )
281     {
282         try
283         {
284             xComponent->dispose();
285         }
286         catch( uno::Exception& e )
287         {
288             std::cerr << "tryDispose caught exception " <<rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << " while disposing " <<  sComp << std::endl;
289         }
290     }
291 }
main(int argv,char ** argc)292 int main( int argv, char** argc )
293 {
294     if ( !( argv > 2 ) )
295         return usage( argc[0] );
296     try
297     {
298 
299         OSL_TRACE("Attempting to bootstrap normal");
300         Reference<XComponentContext> xCC = ::cppu::bootstrap();
301         Reference<XMultiComponentFactory> xFactory = xCC->getServiceManager();
302         OSL_TRACE("got servicemanager");
303         std::cout << "got servicemanager" << std::endl;
304         Reference<XInterface> desktop = xFactory->createInstanceWithContext(
305         ascii("com.sun.star.frame.Desktop"), xCC);
306         OSL_TRACE("got desktop");
307         std::cout << "got desktop" << std::endl;
308         Reference<frame::XComponentLoader> xLoader(desktop, UNO_QUERY_THROW);
309         TestVBA* dTest = new TestVBA( xCC, xFactory, xLoader, ascii( argc[ 2 ] ) );
310         if ( argv == 4 )
311         {
312             std::cout << "before process" << std::endl;
313             dTest->proccessDocument( ascii( argc[ 3 ] ) );
314             std::cout << "after process" << std::endl;
315         }
316         else
317         {
318             dTest->traverse( ascii( argc[ 1 ] ) );
319         }
320         delete dTest;
321 //      tryDispose( xLoader, "desktop" );
322 //      tryDispose( xCC, "remote context" );
323 
324     }
325     catch( uno::Exception& e )
326     {
327         std::cerr << "Caught Exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
328     }
329 
330 }
331