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/.
10 #include <com/sun/star/awt/XWindow.hpp>
11 #include <com/sun/star/beans/PropertyValue.hpp>
12 #include <com/sun/star/document/XExtendedFilterDetection.hpp>
13 #include <com/sun/star/document/XFilter.hpp>
14 #include <com/sun/star/document/XImporter.hpp>
15 #include <com/sun/star/io/XInputStream.hpp>
16 #include <com/sun/star/lang/XInitialization.hpp>
17 #include <com/sun/star/lang/XServiceInfo.hpp>
18 #include <com/sun/star/uno/Reference.h>
19 #include <com/sun/star/uno/XComponentContext.hpp>
20 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
22 #include <osl/diagnose.h>
23 #include <cppuhelper/implbase.hxx>
25 #include <unotools/mediadescriptor.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
29 #include <xmloff/xmlimp.hxx>
31 #include "DocumentHandler.hxx"
32 #include "WPXSvInputStream.hxx"
34 #include "DocumentHandlerFor.hxx"
36 namespace writerperfect
40 template <class Generator
>
41 class ImportFilterImpl
42 : public cppu::WeakImplHelper
<css::document::XFilter
, css::document::XImporter
,
43 css::document::XExtendedFilterDetection
,
44 css::lang::XInitialization
>
47 ImportFilterImpl(css::uno::Reference
<css::uno::XComponentContext
> xContext
)
48 : mxContext(std::move(xContext
))
52 const css::uno::Reference
<css::uno::XComponentContext
>& getXContext() const
58 virtual sal_Bool SAL_CALL
59 filter(const css::uno::Sequence
<css::beans::PropertyValue
>& rDescriptor
) override
61 utl::MediaDescriptor
aDescriptor(rDescriptor
);
62 css::uno::Reference
<css::io::XInputStream
> xInputStream
;
63 aDescriptor
[utl::MediaDescriptor::PROP_INPUTSTREAM
] >>= xInputStream
;
64 if (!xInputStream
.is())
70 css::uno::Reference
<css::awt::XWindow
> xDialogParent
;
71 aDescriptor
["ParentWindow"] >>= xDialogParent
;
73 // An XML import service: what we push sax messages to...
74 css::uno::Reference
<XInterface
> xInternalFilter
75 = mxContext
->getServiceManager()->createInstanceWithContext(
76 DocumentHandlerFor
<Generator
>::name(), mxContext
);
77 assert(xInternalFilter
);
78 css::uno::Reference
<css::xml::sax::XFastDocumentHandler
> xInternalHandler(
79 xInternalFilter
, css::uno::UNO_QUERY
);
80 assert(xInternalHandler
);
82 // The XImporter sets up an empty target document for XDocumentHandler to write to...
83 css::uno::Reference
<css::document::XImporter
> xImporter(xInternalHandler
,
86 xImporter
->setTargetDocument(mxDoc
);
88 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
89 // writes to in-memory target doc
90 DocumentHandler
aHandler(
91 new SvXMLLegacyToFastDocHandler(static_cast<SvXMLImport
*>(xInternalHandler
.get())));
93 WPXSvInputStream
input(xInputStream
);
96 exporter
.addDocumentHandler(&aHandler
, ODF_FLAT_XML
);
98 doRegisterHandlers(exporter
);
100 return doImportDocument(Application::GetFrameWeld(xDialogParent
), input
, exporter
,
104 virtual void SAL_CALL
cancel() override
{}
107 const css::uno::Reference
<css::lang::XComponent
>& getTargetDocument() const { return mxDoc
; }
108 virtual void SAL_CALL
109 setTargetDocument(const css::uno::Reference
<css::lang::XComponent
>& xDoc
) override
114 //XExtendedFilterDetection
115 virtual OUString SAL_CALL
116 detect(css::uno::Sequence
<css::beans::PropertyValue
>& Descriptor
) override
119 sal_Int32 nLength
= Descriptor
.getLength();
120 sal_Int32 location
= nLength
;
121 const css::beans::PropertyValue
* pValue
= Descriptor
.getConstArray();
122 css::uno::Reference
<css::io::XInputStream
> xInputStream
;
123 for (sal_Int32 i
= 0; i
< nLength
; i
++)
125 if (pValue
[i
].Name
== "TypeName")
127 else if (pValue
[i
].Name
== "InputStream")
128 pValue
[i
].Value
>>= xInputStream
;
131 if (!xInputStream
.is())
134 WPXSvInputStream
input(xInputStream
);
136 if (doDetectFormat(input
, sTypeName
))
138 assert(!sTypeName
.isEmpty());
140 if (location
== nLength
)
142 Descriptor
.realloc(nLength
+ 1);
143 Descriptor
.getArray()[location
].Name
= "TypeName";
146 Descriptor
.getArray()[location
].Value
<<= sTypeName
;
153 virtual void SAL_CALL
154 initialize(const css::uno::Sequence
<css::uno::Any
>& /*aArguments*/) override
159 virtual bool doDetectFormat(librevenge::RVNGInputStream
& rInput
, OUString
& rTypeName
) = 0;
160 virtual bool doImportDocument(weld::Window
* pParent
, librevenge::RVNGInputStream
& rInput
,
161 Generator
& rGenerator
, utl::MediaDescriptor
& rDescriptor
)
163 virtual void doRegisterHandlers(Generator
&){};
165 css::uno::Reference
<css::uno::XComponentContext
> mxContext
;
166 css::uno::Reference
<css::lang::XComponent
> mxDoc
;
170 /** A base class for import filters.
172 template <class Generator
>
173 struct ImportFilter
: public cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>,
174 css::lang::XServiceInfo
>
176 ImportFilter(const css::uno::Reference
<css::uno::XComponentContext
>& rxContext
)
177 : cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>, css::lang::XServiceInfo
>(
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */