Update ooo320-m1
[ooovba.git] / writerfilter / unocomponent / debugservices / rtftok / XMLScanner.cxx
blob41397da31331bdb2fe99c8540bbe5e735d4b70ba
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: XMLScanner.cxx,v $
10 * $Revision: 1.9 $
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 "XMLScanner.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 <string>
54 #include <cppuhelper/implbase2.hxx>
55 #include <com/sun/star/embed/XTransactedObject.hpp>
56 #include <com/sun/star/embed/XStorage.hpp>
57 #include <com/sun/star/util/XCloseable.hpp>
58 #include <comphelper/storagehelper.hxx>
59 #include <com/sun/star/embed/XTransactedObject.hpp>
60 #include <com/sun/star/beans/PropertyValue.hpp>
61 #include <com/sun/star/beans/XPropertySet.hpp>
62 #include <comphelper/seqstream.hxx>
64 #include <ctype.h>
65 #include <iostream>
67 using namespace ::com::sun::star;
68 using namespace ::std;
70 namespace writerfilter { namespace rtftok {
72 const sal_Char XMLScanner::SERVICE_NAME[40] = "debugservices.rtftok.XMLScanner";
73 const sal_Char XMLScanner::IMPLEMENTATION_NAME[40] = "debugservices.rtftok.XMLScanner";
75 class XmlRtfScannerHandler : public writerfilter::rtftok::RTFScannerHandler
77 std::vector<unsigned char> binBuffer;
78 int objDataLevel;
79 int numOfOLEs;
80 unsigned char hb;
81 int numOfOLEChars;
82 uno::Reference<lang::XMultiServiceFactory> xServiceFactory;
83 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess;
84 string charBuffer;
85 string ucharBuffer;
86 vector<string> vCloseTags;
88 void xmlout(const string & str)
90 for (size_t n = 0; n < str.length(); ++n)
92 char c = str[n];
94 switch (c)
96 case '<': cout << "&lt;"; break;
97 case '>': cout << "&gt;"; break;
98 case '&': cout << "&amp;"; break;
99 default:
100 cout << c;
102 break;
107 void clearBuffers()
109 if (charBuffer.length() > 0)
111 cout << "<text>";
112 xmlout(charBuffer);
113 cout << "</text>" << endl;
116 charBuffer = "";
118 if (ucharBuffer.length() > 0)
120 cout << "<utext>";
121 xmlout(ucharBuffer);
122 cout << ucharBuffer << "</utext>" << endl;
125 ucharBuffer = "";
128 void dest(char* token, char* value)
130 clearBuffers();
131 cout << "<dest name=\"" << token << "\" value=\""<< value << "\">" << endl;
132 vCloseTags.push_back("</dest>");
134 void ctrl(char*token, char* value)
136 clearBuffers();
137 cout << "<" << token << ">" << value << "</" << token << ">"
138 << endl;
140 void lbrace(void)
142 clearBuffers();
143 cout << "<brace>" << endl;
144 vCloseTags.push_back("</brace>");
146 void rbrace(void)
148 clearBuffers();
149 cout << vCloseTags.back() << endl;
150 vCloseTags.pop_back();
152 void addSpaces(int count)
154 clearBuffers();
155 cout << "<spaces count=\"" << count << "\"/>" << endl;
157 void addBinData(unsigned char /*data*/)
159 clearBuffers();
160 cout << "<bindata/>" << endl;
162 void addChar(char ch)
164 charBuffer += ch;
166 void addCharU(sal_Unicode ch)
168 ucharBuffer += sal_Char(ch < 128 ? ch : '.');
170 void addHexChar(char* hexch)
172 clearBuffers();
173 cout << "<hexchar value=\"" << hexch << "\"/>" << endl;
176 public:
177 XmlRtfScannerHandler(uno::Reference<lang::XMultiServiceFactory> &xServiceFactory_, uno::Reference<com::sun::star::ucb::XSimpleFileAccess> &xFileAccess_) :
178 objDataLevel(0), numOfOLEs(0),
179 xServiceFactory(xServiceFactory_),
180 xFileAccess(xFileAccess_)
184 virtual ~XmlRtfScannerHandler() {}
186 void dump()
191 class RtfInputSourceImpl : public rtftok::RTFInputSource
193 private:
194 uno::Reference< io::XInputStream > xInputStream;
195 uno::Reference< io::XSeekable > xSeekable;
196 uno::Reference< task::XStatusIndicator > xStatusIndicator;
197 sal_Int64 bytesTotal;
198 sal_Int64 bytesRead;
199 public:
200 RtfInputSourceImpl(uno::Reference< io::XInputStream > &xInputStream_, uno::Reference< task::XStatusIndicator > &xStatusIndicator_) :
201 xInputStream(xInputStream_),
202 xStatusIndicator(xStatusIndicator_),
203 bytesRead(0)
205 xSeekable=uno::Reference< io::XSeekable >(xInputStream, uno::UNO_QUERY);
206 if (xSeekable.is())
207 bytesTotal=xSeekable->getLength();
208 if (xStatusIndicator.is() && xSeekable.is())
210 xStatusIndicator->start(::rtl::OUString::createFromAscii("Converting"), 100);
214 virtual ~RtfInputSourceImpl() {}
216 int read(void *buf, int maxlen)
218 uno::Sequence< sal_Int8 > buffer;
219 int len=xInputStream->readSomeBytes(buffer,maxlen);
220 if (len>0)
222 sal_Int8 *_buffer=buffer.getArray();
223 memcpy(buf, _buffer, len);
224 bytesRead+=len;
225 if (xStatusIndicator.is())
227 if (xSeekable.is())
229 xStatusIndicator->setValue((int)(bytesRead*100/bytesTotal));
231 else
233 char buf1[100];
234 sprintf(buf1, "Converted %" SAL_PRIdINT64 " KB", bytesRead/1024);
235 xStatusIndicator->start(::rtl::OUString::createFromAscii(buf1), 0);
238 return len;
240 else
242 if (xStatusIndicator.is())
244 xStatusIndicator->end();
246 return 0;
251 XMLScanner::XMLScanner(const uno::Reference< uno::XComponentContext > &xContext_) :
252 xContext( xContext_ )
256 sal_Int32 SAL_CALL XMLScanner::run( const uno::Sequence< rtl::OUString >& aArguments ) throw (uno::RuntimeException)
258 uno::Sequence<uno::Any> aUcbInitSequence(2);
259 aUcbInitSequence[0] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Local"));
260 aUcbInitSequence[1] <<= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Office"));
261 uno::Reference<lang::XMultiServiceFactory> xServiceFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW);
262 uno::Reference<lang::XMultiComponentFactory> xFactory(xContext->getServiceManager(), uno::UNO_QUERY_THROW );
263 if (::ucbhelper::ContentBroker::initialize(xServiceFactory, aUcbInitSequence))
265 rtl::OUString arg=aArguments[0];
267 uno::Reference<com::sun::star::ucb::XSimpleFileAccess> xFileAccess(
268 xFactory->createInstanceWithContext(
269 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.SimpleFileAccess")),
270 xContext), uno::UNO_QUERY_THROW );
272 rtl_uString *dir=NULL;
273 osl_getProcessWorkingDir(&dir);
274 rtl::OUString absFileUrl;
275 osl_getAbsoluteFileURL(dir, arg.pData, &absFileUrl.pData);
276 rtl_uString_release(dir);
278 uno::Reference <lang::XSingleServiceFactory> xStorageFactory(
279 xServiceFactory->createInstance (rtl::OUString::createFromAscii("com.sun.star.embed.StorageFactory")), uno::UNO_QUERY_THROW);
281 #if 0
282 rtl::OUString outFileUrl;
284 rtl_uString *dir1=NULL;
285 osl_getProcessWorkingDir(&dir1);
286 osl_getAbsoluteFileURL(dir1, aArguments[1].pData, &outFileUrl.pData);
287 rtl_uString_release(dir1);
290 uno::Sequence< uno::Any > aArgs( 2 );
291 aArgs[0] <<= outFileUrl;
292 aArgs[1] <<= embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE;
293 uno::Reference<embed::XStorage> xStorage(xStorageFactory->createInstanceWithArguments(aArgs), uno::UNO_QUERY_THROW);
294 uno::Reference<beans::XPropertySet> xPropSet(xStorage, uno::UNO_QUERY_THROW);
295 xPropSet->setPropertyValue(rtl::OUString::createFromAscii("MediaType"), uno::makeAny(rtl::OUString::createFromAscii("application/vnd.oasis.opendocument.text")));
296 #endif
297 uno::Reference<io::XInputStream> xInputStream = xFileAccess->openFileRead(absFileUrl);
298 uno::Reference< task::XStatusIndicator > xStatusIndicator;
300 RtfInputSourceImpl rtfInputSource(xInputStream, xStatusIndicator);
301 XmlRtfScannerHandler eventHandler(xServiceFactory, xFileAccess);
302 writerfilter::rtftok::RTFScanner *rtfScanner=writerfilter::rtftok::RTFScanner::createRTFScanner(rtfInputSource, eventHandler);
304 cout << "<out>" << endl;
305 rtfScanner->yylex();
306 cout << "</out>" << endl;
307 delete rtfScanner;
309 ::ucbhelper::ContentBroker::deinitialize();
311 else
313 fprintf(stderr, "can't initialize UCB");
315 return 0;
318 ::rtl::OUString XMLScanner_getImplementationName ()
320 return rtl::OUString::createFromAscii ( XMLScanner::IMPLEMENTATION_NAME );
323 sal_Bool SAL_CALL XMLScanner_supportsService( const ::rtl::OUString& ServiceName )
325 return ServiceName.equals( rtl::OUString::createFromAscii( XMLScanner::SERVICE_NAME ) );
327 uno::Sequence< rtl::OUString > SAL_CALL XMLScanner_getSupportedServiceNames( ) throw (uno::RuntimeException)
329 uno::Sequence < rtl::OUString > aRet(1);
330 rtl::OUString* pArray = aRet.getArray();
331 pArray[0] = rtl::OUString::createFromAscii ( XMLScanner::SERVICE_NAME );
332 return aRet;
335 uno::Reference< uno::XInterface > SAL_CALL XMLScanner_createInstance( const uno::Reference< uno::XComponentContext > & xContext) throw( uno::Exception )
337 return (cppu::OWeakObject*) new XMLScanner( xContext );
340 } } /* end namespace writerfilter::rtftok */