1
#include "cppuhelper/bootstrap.hxx"
3
#include <com/sun/star/beans/Property.hpp>
4
#include <com/sun/star/beans/XPropertySet.hpp>
5
#include <com/sun/star/beans/XPropertySetInfo.hpp>
6
#include <com/sun/star/container/XNameAccess.hpp>
7
#include <com/sun/star/container/XNameContainer.hpp>
8
#include <com/sun/star/frame/XComponentLoader.hpp>
9
#include <com/sun/star/lang/XMultiComponentFactory.hpp>
10
#include <com/sun/star/sheet/XSpreadsheet.hpp>
11
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
12
#include <com/sun/star/util/XCloseable.hpp>
13
#include <com/sun/star/uno/XComponentContext.hpp>
14
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
15
#include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
17
#include <tools/urlobj.hxx>
18
#include <osl/file.hxx>
23
using namespace ::com::sun::star;
24
using namespace ::com::sun::star::sheet;
26
using ::com::sun::star::beans::Property;
27
using ::com::sun::star::beans::PropertyValue;
28
using ::com::sun::star::beans::XPropertySet;
29
using ::com::sun::star::beans::XPropertySetInfo;
30
using ::com::sun::star::container::XNameContainer;
31
using ::com::sun::star::lang::XComponent;
32
using ::com::sun::star::lang::XMultiComponentFactory;
33
using ::com::sun::star::frame::XComponentLoader;
34
using ::com::sun::star::uno::Reference;
35
using ::com::sun::star::uno::Sequence;
36
using ::com::sun::star::uno::UNO_QUERY;
37
using ::com::sun::star::uno::UNO_QUERY_THROW;
38
using ::com::sun::star::uno::XComponentContext;
39
using ::com::sun::star::uno::XInterface;
40
using ::com::sun::star::ucb::XSimpleFileAccess;
41
using ::rtl::OUString;
43
using ::std::auto_ptr;
45
const OUString EXTN = rtl::OUString::createFromAscii(".xls");
47
OUString convertToURL( const OUString& rPath )
52
bool bIsURL = aObj.GetProtocol() != INET_PROT_NOT_VALID;
57
osl::FileBase::getFileURLFromSystemPath( rPath, aURL );
58
if ( aURL.equals( rPath ) )
59
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 >() );
64
OUString ascii(const sal_Char* cstr)
66
return OUString::createFromAscii(cstr);
69
const sal_Char* getStr(const OUString& ou)
71
return OUStringToOString(ou, RTL_TEXTENCODING_UTF8).getStr();
75
int usage( const char* pName )
77
std::cerr << "usage: " << pName << "<path to testdocument dir> <output_directory>" << std::endl;
85
Reference< XComponentContext > mxContext;
86
Reference< XMultiComponentFactory > mxMCF;
87
Reference< XComponentLoader > mxCompLoader;
88
Reference< XSimpleFileAccess > mxSFA;
89
rtl::OUString msOutDirPath;
92
TestVBA( const Reference< XComponentContext >& _xContext,
93
const Reference< XMultiComponentFactory >& _xMCF,
94
const Reference< XComponentLoader >& _xCompLoader,
95
const rtl::OUString& _outDirPath ) : mxContext( _xContext ), mxMCF( _xMCF ),
96
mxCompLoader( _xCompLoader ), msOutDirPath( convertToURL( _outDirPath ) )
98
mxSFA.set( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ), mxContext), uno::UNO_QUERY_THROW );
101
rtl::OUString getLogLocation() throw ( beans::UnknownPropertyException, lang::IllegalArgumentException, lang::WrappedTargetException, uno::Exception )
103
rtl::OUString sLogLocation;
104
Reference< XPropertySet > pathSettings( mxMCF->createInstanceWithContext( rtl::OUString::createFromAscii( "com.sun.star.comp.framework.PathSettings" ), mxContext), uno::UNO_QUERY_THROW );
105
pathSettings->getPropertyValue( rtl::OUString::createFromAscii( "Work" ) ) >>= sLogLocation;
106
sLogLocation = sLogLocation.concat( rtl::OUString::createFromAscii( "/" ) ).concat( rtl::OUString::createFromAscii( "HelperAPI-test.log" ) );
112
// blow away previous logs?
115
void traverse( const rtl::OUString& sFileDirectory )
117
rtl::OUString sFileDirectoryURL = convertToURL( sFileDirectory );
118
if ( !mxSFA->isFolder( sFileDirectoryURL) )
120
throw lang::IllegalArgumentException( rtl::OUString::createFromAscii( "not a directory: ").concat( sFileDirectoryURL ), Reference<uno::XInterface>(), 1 );
122
// Getting all files and directories in the current directory
123
Sequence<OUString> entries = mxSFA->getFolderContents( sFileDirectoryURL, sal_False );
125
// Iterating for each file and directory
126
for ( sal_Int32 i = 0; i < entries.getLength(); ++i )
128
// Testing, if the entry in the list is a directory
130
// if ( entries[ i ].isDirectory() )
132
// // ignore Recursive call for the new directory
133
// // traverse( entries[ i ] );
136
rtl::OUString sUrl = entries[ i ];
137
if ( !mxSFA->isFolder( sUrl ) && sUrl.endsWithIgnoreAsciiCaseAsciiL( ".xls", 4 ) )
142
OSL_TRACE( "processing %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
143
// Loading the wanted document
144
Sequence< PropertyValue > propertyValues(1);
145
propertyValues[0].Name = rtl::OUString::createFromAscii( "Hidden" );
146
propertyValues[0].Value <<= sal_False;
148
Reference< uno::XInterface > xDoc =
149
mxCompLoader->loadComponentFromURL( sUrl, rtl::OUString::createFromAscii( "_blank" ), 0, propertyValues);
150
OUString logFileURL = convertToURL( getLogLocation() );
153
Reference< script::provider::XScriptProviderSupplier > xSupplier( xDoc, uno::UNO_QUERY_THROW ) ;
154
if ( mxSFA->exists( logFileURL ) )
155
mxSFA->kill( logFileURL );
157
Reference< script::provider::XScriptProvider > xProv = xSupplier->getScriptProvider();
158
Reference< script::provider::XScript > xScript = xProv->getScript( rtl::OUString::createFromAscii( "vnd.sun.star.script:Standard.TestMacros.Main?language=Basic&location=document" ));
159
OSL_TRACE("Got script for doc %s", rtl::OUStringToOString( sUrl, RTL_TEXTENCODING_UTF8 ).getStr() );
160
Sequence< uno::Any > aArgs;
161
Sequence< sal_Int16 > aOutArgsIndex;
162
Sequence< uno::Any > aOutArgs;
164
xScript->invoke(aArgs, aOutArgsIndex, aOutArgs);
166
OUString fileName = sUrl.copy ( sUrl.lastIndexOf( '/' ) );
167
OUString newLocation = msOutDirPath + fileName.copy ( 0, fileName.lastIndexOf( EXTN ) ) + rtl::OUString::createFromAscii( ".log" );
168
mxSFA->move( logFileURL, newLocation );
169
OSL_TRACE("new logfile location is %s ", rtl::OUStringToOString( newLocation, RTL_TEXTENCODING_UTF8 ).getStr() );
172
catch ( uno::Exception& e )
174
std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
177
// interface is supported, otherwise use XComponent.dispose
178
Reference< util::XCloseable > xCloseable ( xDoc, uno::UNO_QUERY );
180
if ( xCloseable.is() )
182
xCloseable->close(sal_False);
186
Reference< XComponent > xComp( xDoc, uno::UNO_QUERY_THROW );
191
catch( uno::Exception& e )
193
std::cerr << "Caught exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;
201
void tryDispose( Reference< uno::XInterface > xIF, const char* sComp )
203
Reference< lang::XComponent > xComponent( xIF, uno::UNO_QUERY );
204
if ( xComponent.is() )
208
xComponent->dispose();
210
catch( uno::Exception& e )
212
std::cerr << "tryDispose caught exception " <<rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << " while disposing " << sComp << std::endl;
216
int main( int argv, char** argc )
219
return usage( argc[0] );
223
OSL_TRACE("Attempting to bootstrap normal");
224
Reference<XComponentContext> xCC = ::cppu::bootstrap();
225
Reference<XMultiComponentFactory> xFactory = xCC->getServiceManager();
226
OSL_TRACE("got servicemanager");
227
Reference<XInterface> desktop = xFactory->createInstanceWithContext(
228
ascii("com.sun.star.frame.Desktop"), xCC);
229
OSL_TRACE("got desktop");
230
Reference<frame::XComponentLoader> xLoader(desktop, UNO_QUERY_THROW);
231
TestVBA* dTest = new TestVBA( xCC, xFactory, xLoader, ascii( argc[ 2 ] ) );
232
dTest->traverse( ascii( argc[ 1 ] ) );
234
// tryDispose( xLoader, "desktop" );
235
// tryDispose( xCC, "remote context" );
238
catch( uno::Exception& e )
240
std::cerr << "Caught Exception " << rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_UTF8 ).getStr() << std::endl;