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>
35 #include <DocumentHandler.hxx>
36 #include <WPXSvInputStream.hxx>
38 #include <xmloff/attrlist.hxx>
40 #include "DocumentHandlerFor.hxx"
42 namespace writerperfect
48 template<class Generator
>
49 class ImportFilterImpl
: public cppu::WeakImplHelper
51 css::document::XFilter
,
52 css::document::XImporter
,
53 css::document::XExtendedFilterDetection
,
54 css::lang::XInitialization
58 ImportFilterImpl(const css::uno::Reference
< css::uno::XComponentContext
> &rxContext
)
59 : mxContext(rxContext
)
63 virtual ~ImportFilterImpl()
68 virtual sal_Bool SAL_CALL
filter(const css::uno::Sequence
< css::beans::PropertyValue
> &rDescriptor
)
69 throw (css::uno::RuntimeException
, std::exception
) override
71 utl::MediaDescriptor
aDescriptor(rDescriptor
);
72 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
73 aDescriptor
[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream
;
74 if (!xInputStream
.is())
80 // An XML import service: what we push sax messages to..
81 css::uno::Reference
< css::xml::sax::XDocumentHandler
> xInternalHandler(
82 mxContext
->getServiceManager()->createInstanceWithContext(
83 DocumentHandlerFor
<Generator
>::name(), mxContext
),
84 css::uno::UNO_QUERY_THROW
);
86 // The XImporter sets up an empty target document for XDocumentHandler to write to..
87 css::uno::Reference
< css::document::XImporter
> xImporter(xInternalHandler
, css::uno::UNO_QUERY
);
88 xImporter
->setTargetDocument(mxDoc
);
90 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
91 // writes to in-memory target doc
92 DocumentHandler
aHandler(xInternalHandler
);
94 WPXSvInputStream
input(xInputStream
);
97 exporter
.addDocumentHandler(&aHandler
, ODF_FLAT_XML
);
99 this->doRegisterHandlers(exporter
);
101 return this->doImportDocument(input
, exporter
, aDescriptor
);
104 virtual void SAL_CALL
cancel()
105 throw (css::uno::RuntimeException
, std::exception
) override
110 virtual void SAL_CALL
setTargetDocument(const css::uno::Reference
< css::lang::XComponent
> &xDoc
)
111 throw (css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
116 //XExtendedFilterDetection
117 virtual OUString SAL_CALL
detect(css::uno::Sequence
< css::beans::PropertyValue
> &Descriptor
)
118 throw(css::uno::RuntimeException
, std::exception
) override
121 sal_Int32 nLength
= Descriptor
.getLength();
122 sal_Int32 location
= nLength
;
123 const css::beans::PropertyValue
*pValue
= Descriptor
.getConstArray();
124 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
125 for (sal_Int32 i
= 0 ; i
< nLength
; i
++)
127 if (pValue
[i
].Name
== "TypeName")
129 else if (pValue
[i
].Name
== "InputStream")
130 pValue
[i
].Value
>>= xInputStream
;
133 if (!xInputStream
.is())
136 WPXSvInputStream
input(xInputStream
);
138 if (this->doDetectFormat(input
, sTypeName
))
140 assert(!sTypeName
.isEmpty());
142 if (location
== nLength
)
144 Descriptor
.realloc(nLength
+1);
145 Descriptor
[location
].Name
= "TypeName";
148 Descriptor
[location
].Value
<<=sTypeName
;
155 virtual void SAL_CALL
initialize(const css::uno::Sequence
< css::uno::Any
> &aArguments
)
156 throw (css::uno::Exception
, css::uno::RuntimeException
, std::exception
) override
158 css::uno::Sequence
< css::beans::PropertyValue
> aAnySeq
;
159 sal_Int32 nLength
= aArguments
.getLength();
160 if (nLength
&& (aArguments
[0] >>= aAnySeq
))
162 const css::beans::PropertyValue
*pValue
= aAnySeq
.getConstArray();
163 nLength
= aAnySeq
.getLength();
164 for (sal_Int32 i
= 0 ; i
< nLength
; i
++)
166 if (pValue
[i
].Name
== "Type")
168 pValue
[i
].Value
>>= msFilterName
;
176 virtual bool doDetectFormat(librevenge::RVNGInputStream
&rInput
, OUString
&rTypeName
) = 0;
177 virtual bool doImportDocument(librevenge::RVNGInputStream
&rInput
, Generator
&rGenerator
, utl::MediaDescriptor
&rDescriptor
) = 0;
178 virtual void doRegisterHandlers(Generator
&) {};
180 css::uno::Reference
< css::uno::XComponentContext
> mxContext
;
181 css::uno::Reference
< css::lang::XComponent
> mxDoc
;
182 OUString msFilterName
;
187 /** A base class for import filters.
189 template<class Generator
>
190 struct ImportFilter
: public cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>, css::lang::XServiceInfo
>
192 ImportFilter(const css::uno::Reference
<css::uno::XComponentContext
> &rxContext
)
193 : cppu::ImplInheritanceHelper
<detail::ImportFilterImpl
<Generator
>, css::lang::XServiceInfo
>(rxContext
)
200 #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */