summaryrefslogtreecommitdiff
path: root/writerperfect/inc/ImportFilter.hxx
blob: 1b2bca9dd99598926300f82d0c2511d2c8e2d61e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- 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 <libodfgen/libodfgen.hxx>

#include <librevenge/librevenge.h>

#include <librevenge-stream/librevenge-stream.h>

#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/document/XExtendedFilterDetection.hpp>
#include <com/sun/star/document/XFilter.hpp>
#include <com/sun/star/document/XImporter.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/xml/sax/InputSource.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>

#include <osl/diagnose.h>
#include <cppuhelper/implbase.hxx>

#include <unotools/mediadescriptor.hxx>
#include <vcl/svapp.hxx>
#include <vcl/weld.hxx>

#include "DocumentHandler.hxx"
#include "WPXSvInputStream.hxx"

#include <xmloff/attrlist.hxx>

#include "DocumentHandlerFor.hxx"

namespace writerperfect
{
namespace detail
{
template <class Generator>
class ImportFilterImpl
    : public cppu::WeakImplHelper<css::document::XFilter, css::document::XImporter,
                                  css::document::XExtendedFilterDetection,
                                  css::lang::XInitialization>
{
public:
    ImportFilterImpl(const css::uno::Reference<css::uno::XComponentContext>& rxContext)
        : mxContext(rxContext)
    {
    }

    const css::uno::Reference<css::uno::XComponentContext>& getXContext() const
    {
        return mxContext;
    }

    // XFilter
    virtual sal_Bool SAL_CALL
    filter(const css::uno::Sequence<css::beans::PropertyValue>& rDescriptor) override
    {
        utl::MediaDescriptor aDescriptor(rDescriptor);
        css::uno::Reference<css::io::XInputStream> xInputStream;
        aDescriptor[utl::MediaDescriptor::PROP_INPUTSTREAM()] >>= xInputStream;
        if (!xInputStream.is())
        {
            OSL_ASSERT(false);
            return false;
        }

        css::uno::Reference<css::awt::XWindow> xDialogParent;
        aDescriptor["ParentWindow"] >>= xDialogParent;

        // An XML import service: what we push sax messages to..
        css::uno::Reference<css::xml::sax::XDocumentHandler> xInternalHandler(
            mxContext->getServiceManager()->createInstanceWithContext(
                DocumentHandlerFor<Generator>::name(), mxContext),
            css::uno::UNO_QUERY_THROW);

        // The XImporter sets up an empty target document for XDocumentHandler to write to..
        css::uno::Reference<css::document::XImporter> 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<css::lang::XComponent>& getTargetDocument() const { return mxDoc; }
    virtual void SAL_CALL
    setTargetDocument(const css::uno::Reference<css::lang::XComponent>& xDoc) override
    {
        mxDoc = xDoc;
    }

    //XExtendedFilterDetection
    virtual OUString SAL_CALL
    detect(css::uno::Sequence<css::beans::PropertyValue>& Descriptor) override
    {
        OUString sTypeName;
        sal_Int32 nLength = Descriptor.getLength();
        sal_Int32 location = nLength;
        const css::beans::PropertyValue* pValue = Descriptor.getConstArray();
        css::uno::Reference<css::io::XInputStream> 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<css::uno::Any>& aArguments) override
    {
        css::uno::Sequence<css::beans::PropertyValue> 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<css::uno::XComponentContext> mxContext;
    css::uno::Reference<css::lang::XComponent> mxDoc;
    OUString msFilterName;
};
}

/** A base class for import filters.
 */
template <class Generator>
struct ImportFilter : public cppu::ImplInheritanceHelper<detail::ImportFilterImpl<Generator>,
                                                         css::lang::XServiceInfo>
{
    ImportFilter(const css::uno::Reference<css::uno::XComponentContext>& rxContext)
        : cppu::ImplInheritanceHelper<detail::ImportFilterImpl<Generator>, css::lang::XServiceInfo>(
              rxContext)
    {
    }
};
}

#endif // INCLUDED_WRITERPERFECT_INC_WRITERPERFECT_IMPORTFILTER_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */