Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
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/.
6 : */
7 :
8 : #ifndef INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
9 : #define INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
10 :
11 : #include <libodfgen/libodfgen.hxx>
12 :
13 : #include <librevenge/librevenge.h>
14 :
15 : #include <librevenge-stream/librevenge-stream.h>
16 :
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>
29 :
30 : #include <cppuhelper/implbase1.hxx>
31 : #include <cppuhelper/implbase4.hxx>
32 :
33 : #include <unotools/mediadescriptor.hxx>
34 :
35 : #include <writerperfect/DocumentHandler.hxx>
36 : #include <writerperfect/WPXSvInputStream.hxx>
37 :
38 : #include <xmloff/attrlist.hxx>
39 :
40 : #include "writerperfect/DocumentHandlerFor.hxx"
41 :
42 : namespace writerperfect
43 : {
44 :
45 : namespace detail
46 : {
47 :
48 : template<class Generator>
49 : class ImportFilterImpl : public cppu::WeakImplHelper4
50 : <
51 : com::sun::star::document::XFilter,
52 : com::sun::star::document::XImporter,
53 : com::sun::star::document::XExtendedFilterDetection,
54 : com::sun::star::lang::XInitialization
55 : >
56 : {
57 : public:
58 26 : ImportFilterImpl(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > &rxContext)
59 26 : : mxContext(rxContext)
60 : {
61 26 : }
62 :
63 26 : virtual ~ImportFilterImpl()
64 : {
65 26 : }
66 :
67 : // XFilter
68 202 : virtual sal_Bool SAL_CALL filter(const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > &rDescriptor)
69 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
70 : {
71 202 : utl::MediaDescriptor aDescriptor(rDescriptor);
72 404 : css::uno::Reference < css::io::XInputStream > xInputStream;
73 202 : aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
74 202 : if (!xInputStream.is())
75 : {
76 : OSL_ASSERT(false);
77 0 : return sal_False;
78 : }
79 :
80 : // An XML import service: what we push sax messages to..
81 : css::uno::Reference < css::xml::sax::XDocumentHandler > xInternalHandler(
82 202 : mxContext->getServiceManager()->createInstanceWithContext(
83 : DocumentHandlerFor<Generator>::name(), mxContext),
84 404 : css::uno::UNO_QUERY_THROW);
85 :
86 : // The XImporter sets up an empty target document for XDocumentHandler to write to..
87 404 : css::uno::Reference < css::document::XImporter > xImporter(xInternalHandler, css::uno::UNO_QUERY);
88 202 : xImporter->setTargetDocument(mxDoc);
89 :
90 : // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here
91 : // writes to in-memory target doc
92 404 : DocumentHandler xHandler(xInternalHandler);
93 :
94 404 : WPXSvInputStream input(xInputStream);
95 :
96 404 : Generator exporter;
97 202 : exporter.addDocumentHandler(&xHandler, ODF_FLAT_XML);
98 :
99 202 : this->doRegisterHandlers(exporter);
100 :
101 404 : return this->doImportDocument(input, exporter, aDescriptor);
102 : }
103 :
104 0 : virtual void SAL_CALL cancel()
105 : throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
106 : {
107 0 : }
108 :
109 : // XImporter
110 204 : virtual void SAL_CALL setTargetDocument(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > &xDoc)
111 : throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
112 : {
113 204 : mxDoc = xDoc;
114 204 : }
115 :
116 : //XExtendedFilterDetection
117 204 : virtual OUString SAL_CALL detect(com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue > &Descriptor)
118 : throw(com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
119 : {
120 204 : OUString sTypeName;
121 204 : sal_Int32 nLength = Descriptor.getLength();
122 204 : sal_Int32 location = nLength;
123 204 : const css::beans::PropertyValue *pValue = Descriptor.getConstArray();
124 408 : css::uno::Reference < css::io::XInputStream > xInputStream;
125 816 : for (sal_Int32 i = 0 ; i < nLength; i++)
126 : {
127 612 : if (pValue[i].Name == "TypeName")
128 0 : location=i;
129 612 : else if (pValue[i].Name == "InputStream")
130 204 : pValue[i].Value >>= xInputStream;
131 : }
132 :
133 204 : if (!xInputStream.is())
134 0 : return OUString();
135 :
136 408 : WPXSvInputStream input(xInputStream);
137 :
138 204 : if (this->doDetectFormat(input, sTypeName))
139 : {
140 : assert(!sTypeName.isEmpty());
141 :
142 202 : if (location == nLength)
143 : {
144 202 : Descriptor.realloc(nLength+1);
145 202 : Descriptor[location].Name = "TypeName";
146 : }
147 :
148 202 : Descriptor[location].Value <<=sTypeName;
149 : }
150 :
151 408 : return sTypeName;
152 : }
153 :
154 : // XInitialization
155 0 : virtual void SAL_CALL initialize(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any > &aArguments)
156 : throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
157 : {
158 0 : css::uno::Sequence < css::beans::PropertyValue > aAnySeq;
159 0 : sal_Int32 nLength = aArguments.getLength();
160 0 : if (nLength && (aArguments[0] >>= aAnySeq))
161 : {
162 0 : const css::beans::PropertyValue *pValue = aAnySeq.getConstArray();
163 0 : nLength = aAnySeq.getLength();
164 0 : for (sal_Int32 i = 0 ; i < nLength; i++)
165 : {
166 0 : if (pValue[i].Name == "Type")
167 : {
168 0 : pValue[i].Value >>= msFilterName;
169 0 : break;
170 : }
171 : }
172 0 : }
173 0 : }
174 :
175 : private:
176 : virtual bool doDetectFormat(librevenge::RVNGInputStream &rInput, OUString &rTypeName) = 0;
177 : virtual bool doImportDocument(librevenge::RVNGInputStream &rInput, Generator &rGenerator, utl::MediaDescriptor &rDescriptor) = 0;
178 30 : virtual void doRegisterHandlers(Generator &)
179 : {
180 30 : }
181 :
182 : private:
183 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext;
184 : ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > mxDoc;
185 : OUString msFilterName;
186 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > mxHandler;
187 : };
188 :
189 : }
190 :
191 : /** A base class for import filters.
192 : */
193 : template<class Generator>
194 26 : struct ImportFilter : public cppu::ImplInheritanceHelper1<detail::ImportFilterImpl<Generator>, com::sun::star::lang::XServiceInfo>
195 : {
196 26 : ImportFilter(const css::uno::Reference<css::uno::XComponentContext> &rxContext)
197 26 : : cppu::ImplInheritanceHelper1<detail::ImportFilterImpl<Generator>, com::sun::star::lang::XServiceInfo>(rxContext)
198 : {
199 26 : }
200 : };
201 :
202 : }
203 :
204 : #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX
205 :
206 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|