summaryrefslogtreecommitdiffstats
path: root/sax/source/fastparser/legacyfastparser.cxx
blob: bb1045970e73b25630847c6c08af6081dd9f2898 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/xml/sax/XParser.hpp>
#include <com/sun/star/xml/sax/FastParser.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/beans/Pair.hpp>
#include <comphelper/attributelist.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <comphelper/processfactory.hxx>
#include <rtl/ref.hxx>
#include <memory>
#include <vector>

using namespace std;
using namespace ::cppu;
using namespace css;
using namespace uno;
using namespace lang;
using namespace xml::sax;
using namespace io;

namespace {

class NamespaceHandler : public WeakImplHelper< XFastNamespaceHandler >
{
private:
    struct NamespaceDefine
    {
        OUString    m_aPrefix;
        OUString    m_aNamespaceURI;

        NamespaceDefine( const OUString& rPrefix, const OUString& rNamespaceURI ) : m_aPrefix( rPrefix ), m_aNamespaceURI( rNamespaceURI ) {}
    };
    vector< unique_ptr< NamespaceDefine > > m_aNamespaceDefines;

public:
    NamespaceHandler();
    void addNSDeclAttributes( rtl::Reference < comphelper::AttributeList > const & rAttrList );

    //XFastNamespaceHandler
    virtual void SAL_CALL registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI ) override;
    virtual OUString SAL_CALL getNamespaceURI( const OUString& rNamespacePrefix ) override;
};

NamespaceHandler::NamespaceHandler()
{
}

void NamespaceHandler::addNSDeclAttributes( rtl::Reference < comphelper::AttributeList > const & rAttrList )
{
    for(const auto& aNamespaceDefine : m_aNamespaceDefines)
    {
        OUString& rPrefix = aNamespaceDefine->m_aPrefix;
        OUString& rNamespaceURI = aNamespaceDefine->m_aNamespaceURI;
        OUString sDecl;
        if ( rPrefix.isEmpty() )
            sDecl = "xmlns";
        else
            sDecl = "xmlns:" + rPrefix;
        rAttrList->AddAttribute( sDecl, "CDATA", rNamespaceURI );
    }
    m_aNamespaceDefines.clear();
}

void NamespaceHandler::registerNamespace( const OUString& rNamespacePrefix, const OUString& rNamespaceURI )
{
    m_aNamespaceDefines.push_back( std::make_unique<NamespaceDefine>(
                                    rNamespacePrefix, rNamespaceURI) );
}

OUString NamespaceHandler::getNamespaceURI( const OUString&/* rNamespacePrefix */ )
{
    return OUString();
}

class SaxLegacyFastParser : public WeakImplHelper< XInitialization, XServiceInfo, XParser >
{
private:
    rtl::Reference< NamespaceHandler > m_aNamespaceHandler;
public:
    SaxLegacyFastParser();

// css::lang::XInitialization:
    virtual void SAL_CALL initialize(css::uno::Sequence<css::uno::Any> const& rArguments) override;

// The SAX-Parser-Interface
    virtual void SAL_CALL parseStream(  const InputSource& structSource) override;
    virtual void SAL_CALL setDocumentHandler(const Reference< XDocumentHandler > & xHandler) override;
    virtual void SAL_CALL setErrorHandler(const Reference< XErrorHandler > & xHandler) override;
    virtual void SAL_CALL setDTDHandler(const Reference < XDTDHandler > & xHandler) override;
    virtual void SAL_CALL setEntityResolver(const Reference<  XEntityResolver >& xResolver) override;
    virtual void SAL_CALL setLocale( const Locale &locale ) override;

// XServiceInfo
    OUString SAL_CALL getImplementationName() override;
    Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
    sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;

private:
    Reference< XFastParser > m_xParser;
    Reference< XDocumentHandler > m_xDocumentHandler;
    Reference< XFastTokenHandler > m_xTokenHandler;

};


class CallbackDocumentHandler : public WeakImplHelper< XFastDocumentHandler >
{
private:
    Reference< XDocumentHandler > m_xDocumentHandler;
    Reference< XFastTokenHandler > m_xTokenHandler;
    rtl::Reference< NamespaceHandler > m_aNamespaceHandler;
    OUString getNamespacePrefixFromToken( sal_Int32 nToken );
    OUString getNameFromToken( sal_Int32 nToken );

    static const OUString aDefaultNamespace;
    static const OUString aNamespaceSeparator;

public:
    CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler,
                             rtl::Reference< NamespaceHandler > const & rNamespaceHandler,
                             Reference< XFastTokenHandler > const & xTokenHandler);

    // XFastDocumentHandler
    virtual void SAL_CALL startDocument() override;
    virtual void SAL_CALL endDocument() override;
    virtual void SAL_CALL processingInstruction( const OUString& rTarget, const OUString& rData ) override;
    virtual void SAL_CALL setDocumentLocator( const Reference< XLocator >& xLocator ) override;

    // XFastContextHandler
    virtual void SAL_CALL startFastElement( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) override;
    virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) override;
    virtual void SAL_CALL endFastElement( sal_Int32 Element ) override;
    virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) override;
    virtual Reference< XFastContextHandler > SAL_CALL createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& Attribs ) override;
    virtual Reference< XFastContextHandler > SAL_CALL createUnknownChildContext( const OUString& Namespace, const OUString& Name, const Reference< XFastAttributeList >& Attribs ) override;
    virtual void SAL_CALL characters( const OUString& aChars ) override;

};

const OUString CallbackDocumentHandler::aDefaultNamespace = OUString("");
const OUString CallbackDocumentHandler::aNamespaceSeparator = OUString(":");

OUString CallbackDocumentHandler::getNamespacePrefixFromToken( sal_Int32 nToken )
{
    if ( ( nToken & 0xffff0000 ) != 0 )
    {
        Sequence< sal_Int8 > aSeq = m_xTokenHandler->getUTF8Identifier( nToken & 0xffff0000 );
        return OUString( reinterpret_cast< const char* >(
                        aSeq.getConstArray() ), aSeq.getLength(), RTL_TEXTENCODING_UTF8 );
    }
    else
        return OUString();
}

OUString CallbackDocumentHandler::getNameFromToken( sal_Int32 nToken )
{
    Sequence< sal_Int8 > aSeq = m_xTokenHandler->getUTF8Identifier( nToken & 0xffff );
    return OUString( reinterpret_cast< const char* >(
                    aSeq.getConstArray() ), aSeq.getLength(), RTL_TEXTENCODING_UTF8 );
}

CallbackDocumentHandler::CallbackDocumentHandler( Reference< XDocumentHandler > const & xDocumentHandler,
                                                  rtl::Reference< NamespaceHandler > const & rNamespaceHandler,
                                                  Reference< XFastTokenHandler > const & xTokenHandler)
{
   m_xDocumentHandler.set( xDocumentHandler );
   m_aNamespaceHandler.set( rNamespaceHandler.get() );
   m_xTokenHandler.set( xTokenHandler );
}

void SAL_CALL CallbackDocumentHandler::startDocument()
{
    if ( m_xDocumentHandler.is() )
        m_xDocumentHandler->startDocument();
}

void SAL_CALL CallbackDocumentHandler::endDocument()
{
    if ( m_xDocumentHandler.is() )
        m_xDocumentHandler->endDocument();
}

void SAL_CALL CallbackDocumentHandler::processingInstruction( const OUString& rTarget, const OUString& rData )
{
    if ( m_xDocumentHandler.is() )
        m_xDocumentHandler->processingInstruction( rTarget, rData );
}

void SAL_CALL CallbackDocumentHandler::setDocumentLocator( const Reference< XLocator >& xLocator )
{
    if ( m_xDocumentHandler.is() )
        m_xDocumentHandler->setDocumentLocator( xLocator );
}

void SAL_CALL CallbackDocumentHandler::startFastElement( sal_Int32 nElement , const Reference< XFastAttributeList >& Attribs  )
{
    const OUString& rPrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nElement );
    const OUString& rLocalName = CallbackDocumentHandler::getNameFromToken( nElement );
    startUnknownElement( aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + aNamespaceSeparator + rLocalName, Attribs );
}

void SAL_CALL CallbackDocumentHandler::startUnknownElement( const OUString& /*Namespace*/, const OUString& Name, const Reference< XFastAttributeList >& Attribs  )
{
    if ( !m_xDocumentHandler.is() )
        return;

    rtl::Reference < comphelper::AttributeList > rAttrList = new comphelper::AttributeList;
    m_aNamespaceHandler->addNSDeclAttributes( rAttrList );

    const Sequence< xml::FastAttribute > fastAttribs = Attribs->getFastAttributes();
    for (const auto& rAttr : fastAttribs)
    {
        const OUString& rAttrValue = rAttr.Value;
        sal_Int32 nToken = rAttr.Token;
        const OUString& rAttrNamespacePrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nToken );
        OUString sAttrName = CallbackDocumentHandler::getNameFromToken( nToken );
        if ( !rAttrNamespacePrefix.isEmpty() )
            sAttrName = rAttrNamespacePrefix + aNamespaceSeparator + sAttrName;

        rAttrList->AddAttribute( sAttrName, "CDATA", rAttrValue );
    }

    const Sequence< xml::Attribute > unknownAttribs = Attribs->getUnknownAttributes();
    for (const auto& rAttr : unknownAttribs)
    {
        const OUString& rAttrValue = rAttr.Value;
        const OUString& rAttrName = rAttr.Name;

        rAttrList->AddAttribute( rAttrName, "CDATA", rAttrValue );
    }
    m_xDocumentHandler->startElement( Name, rAttrList.get() );
}

void SAL_CALL CallbackDocumentHandler::endFastElement( sal_Int32 nElement )
{
    const OUString& rPrefix = CallbackDocumentHandler::getNamespacePrefixFromToken( nElement );
    const OUString& rLocalName = CallbackDocumentHandler::getNameFromToken( nElement );
    endUnknownElement( aDefaultNamespace, (rPrefix.isEmpty())? rLocalName : rPrefix + aNamespaceSeparator + rLocalName );
}


void SAL_CALL CallbackDocumentHandler::endUnknownElement( const OUString& /*Namespace*/, const OUString& Name )
{
    if ( m_xDocumentHandler.is() )
    {
        m_xDocumentHandler->endElement( Name );
    }
}

Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createFastChildContext( sal_Int32/* nElement */, const Reference< XFastAttributeList >&/* Attribs */ )
{
    return this;
}


Reference< XFastContextHandler > SAL_CALL CallbackDocumentHandler::createUnknownChildContext( const OUString&/* Namespace */, const OUString&/* Name */, const Reference< XFastAttributeList >&/* Attribs */ )
{
    return this;
}

void SAL_CALL CallbackDocumentHandler::characters( const OUString& aChars )
{
    if ( m_xDocumentHandler.is() )
        m_xDocumentHandler->characters( aChars );
}

SaxLegacyFastParser::SaxLegacyFastParser( ) : m_aNamespaceHandler( new NamespaceHandler ),
  m_xParser(FastParser::create(::comphelper::getProcessComponentContext() ))
{
    m_xParser->setNamespaceHandler( m_aNamespaceHandler.get() );
}

void SAL_CALL SaxLegacyFastParser::initialize(Sequence< Any > const& rArguments )
{
    if (!rArguments.hasElements())
        return;

    Reference< XFastTokenHandler > xTokenHandler;
    OUString str;
    if ( ( rArguments[0] >>= xTokenHandler ) && xTokenHandler.is() )
    {
        m_xTokenHandler.set( xTokenHandler );
    }
    else if ( ( rArguments[0] >>= str ) && "registerNamespaces" == str )
    {
        css::beans::Pair< OUString, sal_Int32 > rPair;
        for (sal_Int32 i = 1; i < rArguments.getLength(); i++ )
        {
            rArguments[i] >>= rPair;
            m_xParser->registerNamespace( rPair.First, rPair.Second );
        }
    }
    else
    {
        uno::Reference<lang::XInitialization> const xInit(m_xParser,
                        uno::UNO_QUERY_THROW);
        xInit->initialize( rArguments );
    }
}

void SaxLegacyFastParser::parseStream( const InputSource& structSource )
{
    m_xParser->setFastDocumentHandler( new CallbackDocumentHandler( m_xDocumentHandler.get(),
                                       m_aNamespaceHandler.get(), m_xTokenHandler.get() ) );
    m_xParser->setTokenHandler( m_xTokenHandler );
    m_xParser->parseStream( structSource );
}

void SaxLegacyFastParser::setDocumentHandler( const Reference< XDocumentHandler > & xHandler )
{
    m_xDocumentHandler = xHandler;
}

void SaxLegacyFastParser::setErrorHandler( const Reference< XErrorHandler > & xHandler )
{
    m_xParser->setErrorHandler( xHandler );
}

void SaxLegacyFastParser::setDTDHandler( const Reference < XDTDHandler > &/* xHandler */ )
{

}

void SaxLegacyFastParser::setEntityResolver( const Reference<  XEntityResolver >& xResolver )
{
    m_xParser->setEntityResolver( xResolver );
}

void SaxLegacyFastParser::setLocale( const Locale &locale )
{
    m_xParser->setLocale( locale );
}

OUString SaxLegacyFastParser::getImplementationName()
{
    return "com.sun.star.comp.extensions.xml.sax.LegacyFastParser";
}

sal_Bool SaxLegacyFastParser::supportsService(const OUString& ServiceName)
{
    return cppu::supportsService(this, ServiceName);
}

Sequence< OUString > SaxLegacyFastParser::getSupportedServiceNames()
{
    return { "com.sun.star.xml.sax.LegacyFastParser" };
}

} //namespace

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_extensions_xml_sax_LegacyFastParser_get_implementation(
    css::uno::XComponentContext *,
    css::uno::Sequence<css::uno::Any> const &)
{
    return cppu::acquire(new SaxLegacyFastParser);
}

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