/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX #define INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "DocumentHandler.hxx" #include "WPXSvInputStream.hxx" #include #include "DocumentHandlerFor.hxx" namespace writerperfect { namespace detail { template class ImportFilterImpl : public cppu::WeakImplHelper { public: ImportFilterImpl(const css::uno::Reference& rxContext) : mxContext(rxContext) { } const css::uno::Reference& getXContext() const { return mxContext; } // XFilter virtual sal_Bool SAL_CALL filter(const css::uno::Sequence& rDescriptor) override { utl::MediaDescriptor aDescriptor(rDescriptor); css::uno::Reference xInputStream; aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream; if (!xInputStream.is()) { OSL_ASSERT(false); return false; } css::uno::Reference xDialogParent; aDescriptor["ParentWindow"] >>= xDialogParent; // An XML import service: what we push sax messages to.. css::uno::Reference xInternalHandler( mxContext->getServiceManager()->createInstanceWithContext( DocumentHandlerFor::name(), mxContext), css::uno::UNO_QUERY_THROW); // The XImporter sets up an empty target document for XDocumentHandler to write to.. css::uno::Reference xImporter(xInternalHandler, css::uno::UNO_QUERY); xImporter->setTargetDocument(mxDoc); // OO Graphics Handler: abstract class to handle document SAX messages, concrete implementation here // writes to in-memory target doc DocumentHandler aHandler(xInternalHandler); WPXSvInputStream input(xInputStream); Generator exporter; exporter.addDocumentHandler(&aHandler, ODF_FLAT_XML); doRegisterHandlers(exporter); return doImportDocument(Application::GetFrameWeld(xDialogParent), input, exporter, aDescriptor); } virtual void SAL_CALL cancel() override {} // XImporter const css::uno::Reference& getTargetDocument() const { return mxDoc; } virtual void SAL_CALL setTargetDocument(const css::uno::Reference& xDoc) override { mxDoc = xDoc; } //XExtendedFilterDetection virtual OUString SAL_CALL detect(css::uno::Sequence& Descriptor) override { OUString sTypeName; sal_Int32 nLength = Descriptor.getLength(); sal_Int32 location = nLength; const css::beans::PropertyValue* pValue = Descriptor.getConstArray(); css::uno::Reference xInputStream; for (sal_Int32 i = 0; i < nLength; i++) { if (pValue[i].Name == "TypeName") location = i; else if (pValue[i].Name == "InputStream") pValue[i].Value >>= xInputStream; } if (!xInputStream.is()) return OUString(); WPXSvInputStream input(xInputStream); if (doDetectFormat(input, sTypeName)) { assert(!sTypeName.isEmpty()); if (location == nLength) { Descriptor.realloc(nLength + 1); Descriptor[location].Name = "TypeName"; } Descriptor[location].Value <<= sTypeName; } return sTypeName; } // XInitialization virtual void SAL_CALL initialize(const css::uno::Sequence& aArguments) override { css::uno::Sequence aAnySeq; sal_Int32 nLength = aArguments.getLength(); if (nLength && (aArguments[0] >>= aAnySeq)) { const css::beans::PropertyValue* pValue = aAnySeq.getConstArray(); nLength = aAnySeq.getLength(); for (sal_Int32 i = 0; i < nLength; i++) { if (pValue[i].Name == "Type") { pValue[i].Value >>= msFilterName; break; } } } } private: virtual bool doDetectFormat(librevenge::RVNGInputStream& rInput, OUString& rTypeName) = 0; virtual bool doImportDocument(weld::Window* pParent, librevenge::RVNGInputStream& rInput, Generator& rGenerator, utl::MediaDescriptor& rDescriptor) = 0; virtual void doRegisterHandlers(Generator&){}; css::uno::Reference mxContext; css::uno::Reference mxDoc; OUString msFilterName; }; } /** A base class for import filters. */ template struct ImportFilter : public cppu::ImplInheritanceHelper, css::lang::XServiceInfo> { ImportFilter(const css::uno::Reference& rxContext) : cppu::ImplInheritanceHelper, css::lang::XServiceInfo>( rxContext) { } }; } #endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX /* vim:set shiftwidth=4 softtabstop=4 expandtab: */