LibreOffice Module sc (master) 1
filterdetect.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <com/sun/star/document/XExtendedFilterDetection.hpp>
11#include <com/sun/star/lang/XServiceInfo.hpp>
12#include <com/sun/star/io/XInputStream.hpp>
14
16
17#include <tools/stream.hxx>
18
19#include <orcus/format_detection.hpp>
20
21namespace com::sun::star::uno { class XComponentContext; }
22
23namespace {
24
25class OrcusFormatDetect : public ::cppu::WeakImplHelper<
26 css::document::XExtendedFilterDetection,
27 css::lang::XServiceInfo >
28{
29public:
30 explicit OrcusFormatDetect();
31
32 virtual OUString SAL_CALL getImplementationName() override;
33
34 virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
35
36 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
37
38 virtual OUString SAL_CALL
39 detect( css::uno::Sequence< css::beans::PropertyValue >& rMediaDescSeq ) override;
40
41private:
42};
43
44OrcusFormatDetect::OrcusFormatDetect()
45{
46}
47
48OUString OrcusFormatDetect::getImplementationName()
49{
50 return OUString();
51}
52
53sal_Bool OrcusFormatDetect::supportsService(const OUString& /*rServiceName*/)
54{
55 return false;
56}
57
58css::uno::Sequence<OUString> OrcusFormatDetect::getSupportedServiceNames()
59{
60 return css::uno::Sequence<OUString>();
61}
62
63OUString OrcusFormatDetect::detect(css::uno::Sequence<css::beans::PropertyValue>& rMediaDescSeq)
64{
65 utl::MediaDescriptor aMediaDescriptor( rMediaDescSeq );
66 bool bAborted = aMediaDescriptor.getUnpackedValueOrDefault(utl::MediaDescriptor::PROP_ABORTED, false);
67 if (bAborted)
68 return OUString();
69
70 css::uno::Reference<css::io::XInputStream> xInputStream(aMediaDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM], css::uno::UNO_QUERY );
71 SvMemoryStream aContent(xInputStream->available());
72
73 static const sal_Int32 nBytes = 4096;
74 css::uno::Sequence<sal_Int8> aSeq(nBytes);
75 bool bEnd = false;
76 while(!bEnd)
77 {
78 sal_Int32 nReadBytes = xInputStream->readBytes(aSeq, nBytes);
79 bEnd = (nReadBytes != nBytes);
80 aContent.WriteBytes(aSeq.getConstArray(), nReadBytes);
81 }
82
83 std::string_view aStream(static_cast<const char*>(aContent.GetData()), aContent.GetSize());
84 orcus::format_t eFormat = orcus::detect(aStream);
85
86 switch (eFormat)
87 {
88 case orcus::format_t::gnumeric:
89 return "Gnumeric XML";
90 case orcus::format_t::xls_xml:
91 return "calc_MS_Excel_2003_XML";
92 default:
93 ;
94 }
95
96 return OUString();
97}
98
99}
100
101extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
103 css::uno::Sequence<css::uno::Any> const &)
104{
105 return cppu::acquire(new OrcusFormatDetect());
106}
107
108/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static constexpr OUStringLiteral PROP_INPUTSTREAM
static constexpr OUStringLiteral PROP_ABORTED
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sc_OrcusFormatDetect_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Sequence< sal_Int8 > aSeq
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool