1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLScanner.cxx,v $
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 ************************************************************************/
32 Copyright 2005 Sun Microsystems, Inc.
35 #include "XMLScanner.hxx"
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>
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>
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
;
82 uno::Reference
<lang::XMultiServiceFactory
> xServiceFactory
;
83 uno::Reference
<com::sun::star::ucb::XSimpleFileAccess
> xFileAccess
;
86 vector
<string
> vCloseTags
;
88 void xmlout(const string
& str
)
90 for (size_t n
= 0; n
< str
.length(); ++n
)
96 case '<': cout
<< "<"; break;
97 case '>': cout
<< ">"; break;
98 case '&': cout
<< "&"; break;
109 if (charBuffer
.length() > 0)
113 cout
<< "</text>" << endl
;
118 if (ucharBuffer
.length() > 0)
122 cout
<< ucharBuffer
<< "</utext>" << endl
;
128 void dest(char* token
, char* value
)
131 cout
<< "<dest name=\"" << token
<< "\" value=\""<< value
<< "\">" << endl
;
132 vCloseTags
.push_back("</dest>");
134 void ctrl(char*token
, char* value
)
137 cout
<< "<" << token
<< ">" << value
<< "</" << token
<< ">"
143 cout
<< "<brace>" << endl
;
144 vCloseTags
.push_back("</brace>");
149 cout
<< vCloseTags
.back() << endl
;
150 vCloseTags
.pop_back();
152 void addSpaces(int count
)
155 cout
<< "<spaces count=\"" << count
<< "\"/>" << endl
;
157 void addBinData(unsigned char /*data*/)
160 cout
<< "<bindata/>" << endl
;
162 void addChar(char ch
)
166 void addCharU(sal_Unicode ch
)
168 ucharBuffer
+= sal_Char(ch
< 128 ? ch
: '.');
170 void addHexChar(char* hexch
)
173 cout
<< "<hexchar value=\"" << hexch
<< "\"/>" << endl
;
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() {}
191 class RtfInputSourceImpl
: public rtftok::RTFInputSource
194 uno::Reference
< io::XInputStream
> xInputStream
;
195 uno::Reference
< io::XSeekable
> xSeekable
;
196 uno::Reference
< task::XStatusIndicator
> xStatusIndicator
;
197 sal_Int64 bytesTotal
;
200 RtfInputSourceImpl(uno::Reference
< io::XInputStream
> &xInputStream_
, uno::Reference
< task::XStatusIndicator
> &xStatusIndicator_
) :
201 xInputStream(xInputStream_
),
202 xStatusIndicator(xStatusIndicator_
),
205 xSeekable
=uno::Reference
< io::XSeekable
>(xInputStream
, uno::UNO_QUERY
);
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
);
222 sal_Int8
*_buffer
=buffer
.getArray();
223 memcpy(buf
, _buffer
, len
);
225 if (xStatusIndicator
.is())
229 xStatusIndicator
->setValue((int)(bytesRead
*100/bytesTotal
));
234 sprintf(buf1
, "Converted %" SAL_PRIdINT64
" KB", bytesRead
/1024);
235 xStatusIndicator
->start(::rtl::OUString::createFromAscii(buf1
), 0);
242 if (xStatusIndicator
.is())
244 xStatusIndicator
->end();
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
);
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")));
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
;
306 cout
<< "</out>" << endl
;
309 ::ucbhelper::ContentBroker::deinitialize();
313 fprintf(stderr
, "can't initialize UCB");
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
);
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 */