update dev300-m58
[ooovba.git] / writerfilter / unocomponent / debugservices / rtftok / ScannerTestService.cxx
blobc7ef21b91f921f0ecbec29f175f49f1cc6549eb6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ScannerTestService.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 /**
32 Copyright 2005 Sun Microsystems, Inc.
35 #include "ScannerTestService.hxx"
36 #include <stdio.h>
37 #include <string.h>
38 #include <wchar.h>
39 #include <rtftok/RTFScanner.hxx>
40 #include <rtftok/RTFScannerHandler.hxx>
41 #include <com/sun/star/io/XStream.hpp>
42 #include <com/sun/star/io/XInputStream.hpp>
43 #include <com/sun/star/io/XSeekable.hpp>
44 #include <com/sun/star/io/XTruncate.hpp>
45 #include <com/sun/star/task/XStatusIndicator.hpp>
46 #include <com/sun/star/container/XNameContainer.hpp>
47 #include <ucbhelper/contentbroker.hxx>
48 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
49 #include <osl/process.h>
50 #include <rtl/string.hxx>
51 #include <hash_set>
52 #include <assert.h>
53 #include <cppuhelper/implbase2.hxx>
54 #include <com/sun/star/embed/XTransactedObject.hpp>
55 #include <com/sun/star/embed/XStorage.hpp>
56 #include <com/sun/star/util/XCloseable.hpp>
57 #include <comphelper/storagehelper.hxx>
58 #include <com/sun/star/embed/XTransactedObject.hpp>
59 #include <com/sun/star/beans/PropertyValue.hpp>
60 #include <com/sun/star/beans/XPropertySet.hpp>
61 #include <comphelper/seqstream.hxx>
63 #include <ctype.h>
65 using namespace ::com::sun::star;
67 namespace writerfilter { namespace rtftok {
69 const sal_Char ScannerTestService::SERVICE_NAME[40] = "debugservices.rtftok.ScannerTestService";
70 const sal_Char ScannerTestService::IMPLEMENTATION_NAME[40] = "debugservices.rtftok.ScannerTestService";
72 struct ScannerTestServiceHelper
74 size_t operator()(const rtl::OString &str) const
76 return str.hashCode();
78 bool operator()(const rtl::OString &str1, const rtl::OString &str2) const
80 return str1.compareTo(str2)==0;
84 typedef ::std::hash_set< ::rtl::OString, ScannerTestServiceHelper, ScannerTestServiceHelper > ScannerTestServiceTokenMap;
86 class MyRtfScannerHandler : public writerfilter::rtftok::RTFScannerHandler
88 ScannerTestServiceTokenMap destMap;
89 ScannerTestServiceTokenMap ctrlMap;
90 std::vector<unsigned char> binBuffer;
91 int objDataLevel;
92 int numOfOLEs;
93 unsigned char hb;
94 int numOfOLEChars;
95 uno::Reference<lang::XMultiServiceFactory> xServiceFactory;
96 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess;
97 uno::Reference<embed::XStorage> xStorage;
99 void dest(char* token, char* /*value*/)
101 destMap.insert(rtl::OString(token));
102 // printf("{\\*\\%s%s ", token, value);
103 if (strcmp(token, "objdata")==0)
105 binBuffer.clear();
106 objDataLevel=1;
107 numOfOLEChars=0;
110 void ctrl(char*token, char* /*value*/)
112 ctrlMap.insert(rtl::OString(token));
113 // printf("\\%s%s ", token, value);
115 void lbrace(void)
117 // printf("{");
119 void rbrace(void)
121 #ifndef LINUX
122 unsigned char * binBufferStr = ((unsigned char*)&(*binBuffer.begin()));
124 if (objDataLevel)
126 int o=0;
127 unsigned int type=((unsigned int)binBuffer[o]) | ((unsigned int)binBuffer[o+1])<<8 | ((unsigned int)binBuffer[o+2])<<16 | ((unsigned int)binBuffer[o+3]<<24); o+=4;
128 unsigned int recType=((unsigned int)binBuffer[o]) | ((unsigned int)binBuffer[o+1])<<8 | ((unsigned int)binBuffer[o+2])<<16 | ((unsigned int)binBuffer[o+3]<<24); o+=4;
129 unsigned int strLen=((unsigned int)binBuffer[o]) | ((unsigned int)binBuffer[o+1])<<8 | ((unsigned int)binBuffer[o+2])<<16 | ((unsigned int)binBuffer[o+3]<<24); o+=4;
130 unsigned char *str=binBufferStr+o;
131 o+=strLen;
132 o+=4; // dummy1
133 o+=4; // dummy2
134 unsigned int binLen=((unsigned int)binBuffer[o]) | ((unsigned int)binBuffer[o+1])<<8 | ((unsigned int)binBuffer[o+2])<<16 | ((unsigned int)binBuffer[o+3]<<24); o+=4;
135 printf("OLE%i \"%s\" type=%i recType=%i binBuffer.size()=%u len=%u\n", numOfOLEs, str, type, recType, (unsigned int)(binBuffer.size()), o+binLen);
136 //assert(binBuffer.size()==o+binLen);
137 char buf[100];
138 sprintf(buf, "ole%02i.ole", numOfOLEs);
139 /* if 0{
140 FILE *f=fopen(buf, "w+b");
141 unsigned char *data=binBuffer.begin();
142 fwrite(data+o, 1, binLen, f);
143 fclose(f);
146 rtl_uString *dir=NULL;
147 osl_getProcessWorkingDir(&dir);
148 rtl::OUString absFileUrl;
149 rtl::OUString fileUrl=rtl::OUString::createFromAscii(buf);
150 osl_getAbsoluteFileURL(dir, fileUrl.pData, &absFileUrl.pData);
151 rtl_uString_release(dir);
153 comphelper::ByteSequence seq(binLen);
154 unsigned char *data0=binBufferStr;
155 memcpy(seq.getArray(), data0+o, binLen);
156 uno::Reference<io::XInputStream> myStream=new comphelper::SequenceInputStream(seq);
157 // uno::Reference<io::XStream> myStream=xFileAccess->openFileReadWrite(absFileUrl);
158 // uno::Reference<io::XStream> myStream(new MyStreamImpl(binBuffer, o));
159 uno::Sequence< uno::Any > aArgs0( 1 );
160 aArgs0[0] <<= myStream;
161 uno::Reference< container::XNameContainer > xNameContainer(
162 xServiceFactory->createInstanceWithArguments(
163 ::rtl::OUString::createFromAscii("com.sun.star.embed.OLESimpleStorage" ),
164 aArgs0 ),
165 uno::UNO_QUERY_THROW );
166 try {
167 printf("TRY\n");
168 ::com::sun::star::uno::Sequence< ::rtl::OUString > names=xNameContainer->getElementNames();
169 printf("OK\n");
171 for(int i=0;i<names.getLength();i++)
173 rtl::OUString &name=names[i];
174 wprintf(L"name=%s\n", name.getStr());
177 uno::Reference< io::XStream > xContentStream = xStorage->openStreamElement(
178 rtl::OUString::createFromAscii(buf), embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
179 uno::Reference<beans::XPropertySet> xContentStreamPropSet(xContentStream, uno::UNO_QUERY_THROW);
180 xContentStreamPropSet->setPropertyValue(rtl::OUString::createFromAscii("MediaType"), uno::makeAny(rtl::OUString::createFromAscii("application/vnd.sun.star.oleobject")));
181 uno::Reference<io::XOutputStream> myOutStream=xContentStream->getOutputStream();
182 uno::Sequence< ::sal_Int8 > seq1(binLen);
183 unsigned char *data1=binBufferStr;
184 memcpy(seq1.getArray(), data1+o, binLen);
185 myOutStream->writeBytes(seq1);
186 myOutStream->closeOutput();
189 } catch(com::sun::star::uno::RuntimeException &)
191 printf("NOT OK\n");
192 comphelper::ByteSequence seq2(4+binLen);
193 // memcpy(seq2.getArray(), &binLen, 4); assert(0); //TODO linux
194 seq2[0]= sal::static_int_cast<sal_Int8>(binLen&0xFF);
195 seq2[1]= sal::static_int_cast<sal_Int8>((binLen>>8)&0xFF);
196 seq2[2]= sal::static_int_cast<sal_Int8>((binLen>>16)&0xFF);
197 seq2[3]= sal::static_int_cast<sal_Int8>((binLen>>24)&0xFF);
198 unsigned char *data2=binBufferStr;
199 memcpy(seq2.getArray()+4, data2+o, binLen);
200 uno::Reference<io::XInputStream> myInStream=new comphelper::SequenceInputStream(seq2);
201 printf("SEQ OK\n");
203 uno::Reference< io::XStream > xContentStream = xStorage->openStreamElement(
204 rtl::OUString::createFromAscii(buf), embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
205 uno::Reference<beans::XPropertySet> xContentStreamPropSet(xContentStream, uno::UNO_QUERY_THROW);
206 xContentStreamPropSet->setPropertyValue(rtl::OUString::createFromAscii("MediaType"), uno::makeAny(rtl::OUString::createFromAscii("application/vnd.sun.star.oleobject")));
207 printf("CONTENT STREAM OK\n");
209 uno::Sequence< uno::Any > aArgs1( 1 );
210 aArgs1[0] <<= xContentStream;
211 uno::Reference< container::XNameContainer > xNameContainer2(
212 xServiceFactory->createInstanceWithArguments(
213 ::rtl::OUString::createFromAscii("com.sun.star.embed.OLESimpleStorage" ),
214 aArgs1 ),
215 uno::UNO_QUERY_THROW );
216 printf("OLE STORAGE OK\n");
218 uno::Any anyStream;
219 anyStream <<= myInStream;
220 xNameContainer2->insertByName(rtl::OUString::createFromAscii("\1Ole10Native"), anyStream);
221 printf("INSERT OK\n");
223 uno::Reference<embed::XTransactedObject> xTransact(xNameContainer2, uno::UNO_QUERY);
224 xTransact->commit();
226 objDataLevel--;
227 numOfOLEs++;
229 #endif
230 // printf("}");
232 void addSpaces(int /*count*/)
234 // for(int i=0;i<count;i++)
235 // printf(" ");
238 void addBinData(unsigned char /*data*/)
240 // printf("%02Xh", data);
242 void addChar(char ch)
244 // printf("%c", ch);
245 if (objDataLevel)
247 if (numOfOLEChars%2==0)
249 char c=sal::static_int_cast<char>(toupper(ch));
250 assert((c<='F' && c>='A') || (c<='9' && c>='0'));
251 if(c>='A') hb=(unsigned char)(c-'A'+10); else hb=(unsigned char)(c-'0');
253 else
255 unsigned char lb;
256 char c=sal::static_int_cast<char>(toupper(ch));
257 assert((c<='F' && c>='A') || (c<='9' && c>='0'));
258 if(c>='A') lb=(unsigned char)(c-'A'+10); else lb=(unsigned char)(c-'0');
259 unsigned char r=(hb<<4)|lb;
260 binBuffer.push_back(r);
262 numOfOLEChars++;
265 void addCharU(sal_Unicode /*ch*/)
267 // printf("\\u%i ", ch);
269 void addHexChar(char* /*hexch*/)
271 // printf("\'%s ", hexch);
275 public:
276 MyRtfScannerHandler(uno::Reference<lang::XMultiServiceFactory> &xServiceFactory_, uno::Reference<com::sun::star::ucb::XSimpleFileAccess> &xFileAccess_, uno::Reference<embed::XStorage> &xStorage_) :
277 objDataLevel(0), numOfOLEs(0),
278 xServiceFactory(xServiceFactory_),
279 xFileAccess(xFileAccess_),
280 xStorage(xStorage_)
284 virtual ~MyRtfScannerHandler() {}
286 void dump()
288 printf("Destinations:\n");
289 for(ScannerTestServiceTokenMap::iterator i=destMap.begin();i!=destMap.end();i++)
291 printf(" %s\n", i->getStr());
293 printf("Ctrls:\n");
294 for(ScannerTestServiceTokenMap::iterator i=ctrlMap.begin();i!=ctrlMap.end();i++)
296 printf(" %s\n", i->getStr());
301 class RtfInputSourceImpl : public rtftok::RTFInputSource
303 private:
304 uno::Reference< io::XInputStream > xInputStream;
305 uno::Reference< io::XSeekable > xSeekable;
306 uno::Reference< task::XStatusIndicator > xStatusIndicator;
307 sal_Int64 bytesTotal;
308 sal_Int64 bytesRead;
309 public:
310 RtfInputSourceImpl(uno::Reference< io::XInputStream > &xInputStream_, uno::Reference< task::XStatusIndicator > &xStatusIndicator_) :
311 xInputStream(xInputStream_),
312 xStatusIndicator(xStatusIndicator_),
313 bytesRead(0)
315 xSeekable=uno::Reference< io::XSeekable >(xInputStream, uno::UNO_QUERY);
316 if (xSeekable.is())
317 bytesTotal=xSeekable->getLength();
318 if (xStatusIndicator.is() && xSeekable.is())
320 xStatusIndicator->start(::rtl::OUString::createFromAscii("Converting"), 100);
324 virtual ~RtfInputSourceImpl() {}
326 int read(void *buf, int maxlen)
328 uno::Sequence< sal_Int8 > buffer;
329 int len=xInputStream->readSomeBytes(buffer,maxlen);
330 if (len>0)
332 sal_Int8 *_buffer=buffer.getArray();
333 memcpy(buf, _buffer, len);
334 bytesRead+=len;
335 if (xStatusIndicator.is())
337 if (xSeekable.is())
339 xStatusIndicator->setValue((int)(bytesRead*100/bytesTotal));
341 else
343 char buf1[100];
344 sprintf(buf1, "Converted %" SAL_PRIdINT64 " KB", bytesRead/1024);
345 xStatusIndicator->start(::rtl::OUString::createFromAscii(buf1), 0);
348 return len;
350 else
352 if (xStatusIndicator.is())
354 xStatusIndicator->end();
356 return 0;
361 ScannerTestService::ScannerTestService(const uno::Reference< uno::XComponentContext > &xContext_) :
362 xContext( xContext_ )
366 sal_Int32 SAL_CALL ScannerTestService::run( const uno::Sequence< rtl::OUString >& aArguments ) throw (uno::RuntimeException)
369 printf("TEST\n");
371 uno::Sequence<uno::Any> aUcbInitSequence(2);
372 aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
373 aUcbInitSequence[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
374 uno::Reference<lang::XMultiServiceFactory> xServiceFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW);
375 printf("A\n");
376 uno::Reference<lang::XMultiComponentFactory> xFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW );
377 printf("B\n");
378 if (::ucbhelper::ContentBroker::initialize(xServiceFactory, aUcbInitSequence))
380 printf("C\n");
381 rtl::OUString arg=aArguments[0];
383 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess(
384 xFactory->createInstanceWithContext(
385 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.SimpleFileAccess")),
386 xContext), uno::UNO_QUERY_THROW );
388 rtl_uString *dir=NULL;
389 osl_getProcessWorkingDir(&dir);
390 rtl::OUString absFileUrl;
391 osl_getAbsoluteFileURL(dir, arg.pData, &absFileUrl.pData);
392 rtl_uString_release(dir);
394 uno::Reference <lang::XSingleServiceFactory> xStorageFactory(
395 xServiceFactory->createInstance (rtl::OUString::createFromAscii("com.sun.star.embed.StorageFactory")), uno::UNO_QUERY_THROW);
397 rtl::OUString outFileUrl;
399 rtl_uString *dir1=NULL;
400 osl_getProcessWorkingDir(&dir1);
401 osl_getAbsoluteFileURL(dir1, aArguments[1].pData, &outFileUrl.pData);
402 rtl_uString_release(dir1);
405 uno::Sequence< uno::Any > aArgs2( 2 );
406 aArgs2[0] <<= outFileUrl;
407 aArgs2[1] <<= embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE;
408 uno::Reference<embed::XStorage> xStorage(xStorageFactory->createInstanceWithArguments(aArgs2), uno::UNO_QUERY_THROW);
409 uno::Reference<beans::XPropertySet> xPropSet(xStorage, uno::UNO_QUERY_THROW);
410 xPropSet->setPropertyValue(rtl::OUString::createFromAscii("MediaType"), uno::makeAny(rtl::OUString::createFromAscii("application/vnd.oasis.opendocument.text")));
411 uno::Reference<io::XInputStream> xInputStream = xFileAccess->openFileRead(absFileUrl);
412 uno::Reference< task::XStatusIndicator > xStatusIndicator;
414 TimeValue t1; osl_getSystemTime(&t1);
416 RtfInputSourceImpl rtfInputSource(xInputStream, xStatusIndicator);
417 MyRtfScannerHandler eventHandler(xServiceFactory, xFileAccess, xStorage);
418 writerfilter::rtftok::RTFScanner *rtfScanner=writerfilter::rtftok::RTFScanner::createRTFScanner(rtfInputSource, eventHandler);
420 rtfScanner->yylex();
421 delete rtfScanner;
423 TimeValue t2; osl_getSystemTime(&t2);
424 printf("time=%" SAL_PRIuUINT32 "s\n", t2.Seconds-t1.Seconds);
426 // eventHandler.dump();
427 uno::Reference<embed::XTransactedObject> xTransact(xStorage, uno::UNO_QUERY);
428 xTransact->commit();
431 ::ucbhelper::ContentBroker::deinitialize();
433 else
435 fprintf(stderr, "can't initialize UCB");
437 return 0;
440 ::rtl::OUString ScannerTestService_getImplementationName ()
442 return rtl::OUString::createFromAscii ( ScannerTestService::IMPLEMENTATION_NAME );
445 sal_Bool SAL_CALL ScannerTestService_supportsService( const ::rtl::OUString& ServiceName )
447 return ServiceName.equals( rtl::OUString::createFromAscii( ScannerTestService::SERVICE_NAME ) );
449 uno::Sequence< rtl::OUString > SAL_CALL ScannerTestService_getSupportedServiceNames( ) throw (uno::RuntimeException)
451 uno::Sequence < rtl::OUString > aRet(1);
452 rtl::OUString* pArray = aRet.getArray();
453 pArray[0] = rtl::OUString::createFromAscii ( ScannerTestService::SERVICE_NAME );
454 return aRet;
457 uno::Reference< uno::XInterface > SAL_CALL ScannerTestService_createInstance( const uno::Reference< uno::XComponentContext > & xContext) throw( uno::Exception )
459 return (cppu::OWeakObject*) new ScannerTestService( xContext );
462 } } /* end namespace writerfilter::rtftok */