1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 #ifndef INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
9 #define INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
11 #include <libodfgen/libodfgen.hxx>
13 #include <librevenge/librevenge.h>
15 #include <librevenge-stream/librevenge-stream.h>
17 #include <com/sun/star/beans/PropertyValue.hpp>
18 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
19 #include <com/sun/star/document/XFilter.hpp>
20 #include <com/sun/star/document/XImporter.hpp>
21 #include <com/sun/star/io/XInputStream.hpp>
22 #include <com/sun/star/io/XSeekable.hpp>
23 #include <com/sun/star/lang/XInitialization.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/uno/Reference.h>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <com/sun/star/xml/sax/InputSource.hpp>
28 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
30 #include <osl/diagnose.h>
31 #include <cppuhelper/implbase.hxx>
33 #include <unotools/mediadescriptor.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/weld.hxx>
37 #include "DocumentHandler.hxx"
38 #include "WPXSvInputStream.hxx"
40 #include <xmloff/attrlist.hxx>
42 #include "DocumentHandlerFor.hxx"
44 namespace writerperfect
48 template <class Generator
>
49 class ImportFilterImpl
50 : public cppu::WeakImplHelper
<css::document::XFilter
, css::document::XImporter
,
51 css::document::XExtendedFilterDetection
,
52 css::lang::XInitialization
>
55 ImportFilterImpl(const css::uno::Reference
<css::uno::XComponentContext
>& rxContext
)
56 : mxContext(rxContext
)
60 const css::uno::Reference
<css::uno::XComponentContext
>& getXContext() const
66 virtual sal_Bool SAL_CALL
67 filter(const css::uno::Sequence
<css::beans::PropertyValue
>& rDescriptor
) override
69 utl::MediaDescriptor
aDescriptor(rDescriptor
);
70 css::uno::Reference
<css::io::XInputStream
> xInputStream
;
71 aDescriptor
[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream
;
72 if (!xInputStream
.is())
78 css::uno::Reference
<css::awt::XWindow
> xDialogParent
;
79 aDescriptor
["ParentWindow"] >>= xDialogParent
;
81 // An XML import service: what we push sax messages to...
82 css::uno::Reference
<css::xml::sax::XDocumentHandler
> xInternalHandler(
83 mxContext
->getServiceManager()->createInstanceWithContext(
84 DocumentHandlerFor
<Generator
>::name(), mxContext
),
85 css::uno::UNO_QUERY_THROW
);
87 // The XImporter sets up an empty target document for XDocumentHandler to write to...
88 css::uno::Reference
<css::document::XImporter
> xImporter(xInternalHandler
,
90 xImporter
->setTargetDocument(mxDoc
);
92 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
93 // writes to in-memory target doc
94 DocumentHandler
aHandler(xInternalHandler
);
96 WPXSvInputStream
input(xInputStream
);
99 exporter
.addDocumentHandler(&aHandler
, ODF_FLAT_XML
);
101 doRegisterHandlers(exporter
);
103 return doImportDocument(Application::GetFrameWeld(xDialogParent
), input
, exporter
,
107 virtual void SAL_CALL
cancel() override
{}
110 const css::uno::Reference
<css::lang::XComponent
>& getTargetDocument() const { return mxDoc
; }
111 virtual void SAL_CALL
112 setTargetDocument(const css::uno::Reference
<css::lang::XComponent
>& xDoc
) override
117 //XExtendedFilterDetection
118 virtual OUString SAL_CALL
119 detect(css::uno::Sequence
<css::beans::PropertyValue
>& Descriptor
) override
122 sal_Int32 nLength
= Descriptor
.getLength();
123 sal_Int32 location
= nLength
;
124 const css::beans::PropertyValue
* pValue
= Descriptor
.getConstArray();
125 css::uno::Reference
<css::io::XInputStream
> xInputStream
;
126 for (sal_Int32 i
= 0; i
< nLength
; i
++)
128 if (pValue
[i
].Name
== "TypeName")
130 else if (pValue
[i
].Name
== "InputStream")
131 pValue
[i
].Value
>>= xInputStream
;
134 if (!xInputStream
.is())
137 WPXSvInputStream
input(xInputStream
);
139 if (doDetectFormat(input
, sTypeName
))
141 assert(!sTypeName
.isEmpty());
143 if (location
== nLength
)
145 Descriptor
.realloc(nLength
+ 1);
146 Descriptor
[location
].Name
= "TypeName";
149 Descriptor
[location
].Value
<<= sTypeName
;
156 virtual void SAL_CALL
157 initialize(const css::uno::Sequence
<css::uno::Any
>& /*aArguments*/) override
162 virtual bool doDetectFormat(librevenge::RVNGInputStream
& rInput
, OUString
& rTypeName
) = 0;
163 virtual bool doImportDocument(weld::Window
* pParent
, librevenge::RVNGInputStream
& rInput
,
164 Generator
& rGenerator
, utl::MediaDescriptor
& rDescriptor
)
166 virtual void doRegisterHandlers(Generator
&){};
168 css::uno::Reference
<css::uno::XComponentContext
> mxContext
;
169 css::uno::Reference
<css::lang::XComponent
> mxDoc
;
173 /** A base class for import filters.
175 template <class Generator
>
176 struct ImportFilter
: public cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>,
177 css::lang::XServiceInfo
>
179 ImportFilter(const css::uno::Reference
<css::uno::XComponentContext
>& rxContext
)
180 : cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>, css::lang::XServiceInfo
>(
187 #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */