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/implbase1.hxx>
32 #include <cppuhelper/implbase4.hxx>
34 #include <unotools/mediadescriptor.hxx>
36 #include <DocumentHandler.hxx>
37 #include <WPXSvInputStream.hxx>
39 #include <xmloff/attrlist.hxx>
41 #include "DocumentHandlerFor.hxx"
43 namespace writerperfect
49 template<class Generator
>
50 class ImportFilterImpl
: public cppu::WeakImplHelper4
52 com::sun::star::document::XFilter
,
53 com::sun::star::document::XImporter
,
54 com::sun::star::document::XExtendedFilterDetection
,
55 com::sun::star::lang::XInitialization
59 ImportFilterImpl(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> &rxContext
)
60 : mxContext(rxContext
)
64 virtual ~ImportFilterImpl()
69 virtual sal_Bool SAL_CALL
filter(const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> &rDescriptor
)
70 throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
72 utl::MediaDescriptor
aDescriptor(rDescriptor
);
73 css::uno::Reference
< css::io::XInputStream
> xInputStream
;
74 aDescriptor
[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream
;
75 if (!xInputStream
.is())
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
, css::uno::UNO_QUERY
);
89 xImporter
->setTargetDocument(mxDoc
);
91 // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
92 // writes to in-memory target doc
93 DocumentHandler
xHandler(xInternalHandler
);
95 WPXSvInputStream
input(xInputStream
);
98 exporter
.addDocumentHandler(&xHandler
, ODF_FLAT_XML
);
100 this->doRegisterHandlers(exporter
);
102 return this->doImportDocument(input
, exporter
, aDescriptor
);
105 virtual void SAL_CALL
cancel()
106 throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
111 virtual void SAL_CALL
setTargetDocument(const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> &xDoc
)
112 throw (::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
117 //XExtendedFilterDetection
118 virtual OUString SAL_CALL
detect(com::sun::star::uno::Sequence
< com::sun::star::beans::PropertyValue
> &Descriptor
)
119 throw(com::sun::star::uno::RuntimeException
, std::exception
) SAL_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 (this->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
initialize(const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
> &aArguments
)
157 throw (::com::sun::star::uno::Exception
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
159 css::uno::Sequence
< css::beans::PropertyValue
> aAnySeq
;
160 sal_Int32 nLength
= aArguments
.getLength();
161 if (nLength
&& (aArguments
[0] >>= aAnySeq
))
163 const css::beans::PropertyValue
*pValue
= aAnySeq
.getConstArray();
164 nLength
= aAnySeq
.getLength();
165 for (sal_Int32 i
= 0 ; i
< nLength
; i
++)
167 if (pValue
[i
].Name
== "Type")
169 pValue
[i
].Value
>>= msFilterName
;
177 virtual bool doDetectFormat(librevenge::RVNGInputStream
&rInput
, OUString
&rTypeName
) = 0;
178 virtual bool doImportDocument(librevenge::RVNGInputStream
&rInput
, Generator
&rGenerator
, utl::MediaDescriptor
&rDescriptor
) = 0;
179 virtual void doRegisterHandlers(Generator
&)
184 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> mxContext
;
185 ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XComponent
> mxDoc
;
186 OUString msFilterName
;
187 ::com::sun::star::uno::Reference
< ::com::sun::star::xml::sax::XDocumentHandler
> mxHandler
;
192 /** A base class for import filters.
194 template<class Generator
>
195 struct ImportFilter
: public cppu::ImplInheritanceHelper1
<detail::ImportFilterImpl
<Generator
>, com::sun::star::lang::XServiceInfo
>
197 ImportFilter(const css::uno::Reference
<css::uno::XComponentContext
> &rxContext
)
198 : cppu::ImplInheritanceHelper1
<detail::ImportFilterImpl
<Generator
>, com::sun::star::lang::XServiceInfo
>(rxContext
)
205 #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */