Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/unoxml/source/dom/document.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#pragma once
21
22
#include <memory>
23
#include <unordered_map>
24
25
#include <libxml/tree.h>
26
27
#include <sal/types.h>
28
29
#include <cppuhelper/implbase.hxx>
30
#include <unotools/weakref.hxx>
31
32
#include <com/sun/star/uno/Reference.h>
33
#include <com/sun/star/beans/StringPair.hpp>
34
#include <com/sun/star/xml/dom/XNode.hpp>
35
#include <com/sun/star/xml/dom/XAttr.hpp>
36
#include <com/sun/star/xml/dom/XElement.hpp>
37
#include <com/sun/star/xml/dom/XDOMImplementation.hpp>
38
#include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
39
#include <com/sun/star/xml/dom/events/XEvent.hpp>
40
#include <com/sun/star/xml/sax/XSAXSerializable.hpp>
41
#include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
42
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
43
#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
44
#include <com/sun/star/io/XActiveDataSource.hpp>
45
#include <com/sun/star/io/XActiveDataControl.hpp>
46
#include <com/sun/star/io/XOutputStream.hpp>
47
#include <com/sun/star/io/XStreamListener.hpp>
48
#include <o3tl/sorted_vector.hxx>
49
50
#include <node.hxx>
51
52
namespace DOM
53
{
54
    namespace events {
55
        class CEventDispatcher;
56
    }
57
58
    class CElement;
59
60
    typedef ::cppu::ImplInheritanceHelper<
61
            CNode, css::xml::dom::XDocument, css::xml::dom::events::XDocumentEvent,
62
            css::io::XActiveDataControl, css::io::XActiveDataSource,
63
            css::xml::sax::XSAXSerializable, css::xml::sax::XFastSAXSerializable>
64
        CDocument_Base;
65
66
    class CDocument
67
        : public CDocument_Base
68
    {
69
70
    private:
71
        /// this Mutex is used for synchronization of all UNO wrapper
72
        /// objects that belong to this document
73
        ::osl::Mutex m_Mutex;
74
        /// the libxml document: freed in destructor
75
        /// => all UNO wrapper objects must keep the CDocument alive
76
        xmlDocPtr const m_aDocPtr;
77
78
        // datacontrol/source state
79
        typedef o3tl::sorted_vector< css::uno::Reference< css::io::XStreamListener > > listenerlist_t;
80
        listenerlist_t m_streamListeners;
81
        css::uno::Reference< css::io::XOutputStream > m_rOutputStream;
82
83
        typedef std::unordered_map< xmlNodePtr,
84
                    ::std::pair< unotools::WeakReference<CNode>, CNode* > > nodemap_t;
85
        nodemap_t m_NodeMap;
86
87
        ::std::unique_ptr<events::CEventDispatcher> const m_pEventDispatcher;
88
89
        explicit CDocument(xmlDocPtr const pDocPtr);
90
91
92
    public:
93
        /// factory: only way to create instance!
94
        static ::rtl::Reference<CDocument>
95
            CreateCDocument(xmlDocPtr const pDoc);
96
97
        virtual ~CDocument() override;
98
99
        // needed by CXPathAPI
100
1.21k
        ::osl::Mutex & GetMutex() { return m_Mutex; }
101
102
        events::CEventDispatcher & GetEventDispatcher();
103
        ::rtl::Reference< CElement > GetDocumentElement();
104
105
        /// get UNO wrapper instance for a libxml node
106
        ::rtl::Reference<CNode> GetCNode(
107
                xmlNodePtr const pNode, bool const bCreate = true);
108
        /// remove a UNO wrapper instance
109
        void RemoveCNode(xmlNodePtr const pNode, CNode const*const pCNode);
110
111
        virtual CDocument & GetOwnerDocument() override;
112
113
        virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler) override;
114
115
        virtual void fastSaxify( Context& rContext ) override;
116
117
        virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType,
118
                css::xml::dom::NodeType const* pReplacedNodeType) override;
119
120
        /**
121
        Creates an Attr of the given name.
122
        */
123
        virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL createAttribute(const OUString& name) override;
124
125
        /**
126
        Creates an attribute of the given qualified name and namespace URI.
127
        */
128
        virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL createAttributeNS(const OUString& namespaceURI, const OUString& qualifiedName) override;
129
130
        /**
131
        Creates a CDATASection node whose value is the specified string.
132
        */
133
        virtual css::uno::Reference< css::xml::dom::XCDATASection > SAL_CALL createCDATASection(const OUString& data) override;
134
135
        /**
136
        Creates a Comment node given the specified string.
137
        */
138
        virtual css::uno::Reference< css::xml::dom::XComment > SAL_CALL createComment(const OUString& data) override;
139
140
        /**
141
        Creates an empty DocumentFragment object.
142
        */
143
        virtual css::uno::Reference< css::xml::dom::XDocumentFragment > SAL_CALL createDocumentFragment() override;
144
145
        /**
146
        Creates an element of the type specified.
147
        */
148
        virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL createElement(const OUString& tagName) override;
149
150
        /**
151
        Creates an element of the given qualified name and namespace URI.
152
        */
153
        virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL createElementNS(const OUString& namespaceURI, const OUString& qualifiedName) override;
154
155
        /**
156
        Creates an EntityReference object.
157
        */
158
        virtual css::uno::Reference< css::xml::dom::XEntityReference > SAL_CALL createEntityReference(const OUString& name) override;
159
160
        /**
161
        Creates a ProcessingInstruction node given the specified name and
162
        data strings.
163
        */
164
        virtual css::uno::Reference< css::xml::dom::XProcessingInstruction > SAL_CALL createProcessingInstruction(
165
                const OUString& target, const OUString& data) override;
166
167
        /**
168
        Creates a Text node given the specified string.
169
        */
170
        virtual css::uno::Reference< css::xml::dom::XText > SAL_CALL createTextNode(const OUString& data) override;
171
172
        /**
173
        The Document Type Declaration (see DocumentType) associated with this
174
        document.
175
        */
176
        virtual css::uno::Reference< css::xml::dom::XDocumentType > SAL_CALL getDoctype() override;
177
178
        /**
179
        This is a convenience attribute that allows direct access to the child
180
        node that is the root element of the document.
181
        */
182
        virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL getDocumentElement() override;
183
184
        /**
185
        Returns the Element whose ID is given by elementId.
186
        */
187
        virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL getElementById(const OUString& elementId) override;
188
189
        /**
190
        Returns a NodeList of all the Elements with a given tag name in the
191
        order in which they are encountered in a preorder traversal of the
192
        Document tree.
193
        */
194
        virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagName(const OUString& tagname) override;
195
196
        /**
197
        Returns a NodeList of all the Elements with a given local name and
198
        namespace URI in the order in which they are encountered in a preorder
199
        traversal of the Document tree.
200
        */
201
        virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagNameNS(const OUString& namespaceURI, const OUString& localName) override;
202
203
        /**
204
        The DOMImplementation object that handles this document.
205
        */
206
        virtual css::uno::Reference< css::xml::dom::XDOMImplementation > SAL_CALL getImplementation() override;
207
208
        /**
209
        Imports a node from another document to this document.
210
        */
211
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL importNode(const css::uno::Reference< css::xml::dom::XNode >& importedNode, sal_Bool deep) override;
212
213
        // XDocumentEvent
214
        virtual css::uno::Reference< css::xml::dom::events::XEvent > SAL_CALL createEvent(const OUString& eventType) override;
215
216
        // XActiveDataControl,
217
        // see https://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataControl.html
218
        virtual void SAL_CALL addListener(const css::uno::Reference< css::io::XStreamListener >& aListener ) override;
219
        virtual void SAL_CALL removeListener(const css::uno::Reference< css::io::XStreamListener >& aListener ) override;
220
        virtual void SAL_CALL start() override;
221
        virtual void SAL_CALL terminate() override;
222
223
        // XActiveDataSource
224
        // see https://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataSource.html
225
        virtual void SAL_CALL setOutputStream(  const css::uno::Reference< css::io::XOutputStream >& aStream ) override;
226
        virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override;
227
228
        // ---- resolve uno inheritance problems...
229
        // overrides for XNode base
230
        virtual OUString SAL_CALL getNodeName() override;
231
        virtual OUString SAL_CALL getNodeValue() override;
232
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL cloneNode(sal_Bool deep) override;
233
        // --- delegation for XNode base.
234
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL appendChild(const css::uno::Reference< css::xml::dom::XNode >& newChild) override
235
144k
        {
236
144k
            return CNode::appendChild(newChild);
237
144k
        }
238
        virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes() override
239
0
        {
240
0
            return CNode::getAttributes();
241
0
        }
242
        virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getChildNodes() override
243
133k
        {
244
133k
            return CNode::getChildNodes();
245
133k
        }
246
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getFirstChild() override
247
127k
        {
248
127k
            return CNode::getFirstChild();
249
127k
        }
250
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getLastChild() override
251
0
        {
252
0
            return CNode::getLastChild();
253
0
        }
254
        virtual OUString SAL_CALL getLocalName() override
255
0
        {
256
0
            return CNode::getLocalName();
257
0
        }
258
        virtual OUString SAL_CALL getNamespaceURI() override
259
0
        {
260
0
            return CNode::getNamespaceURI();
261
0
        }
262
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNextSibling() override
263
0
        {
264
0
            return CNode::getNextSibling();
265
0
        }
266
        virtual css::xml::dom::NodeType SAL_CALL getNodeType() override
267
6.37k
        {
268
6.37k
            return CNode::getNodeType();
269
6.37k
        }
270
        virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument() override
271
608
        {
272
608
            return CNode::getOwnerDocument();
273
608
        }
274
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getParentNode() override
275
0
        {
276
0
            return CNode::getParentNode();
277
0
        }
278
        virtual OUString SAL_CALL getPrefix() override
279
0
        {
280
0
           return CNode::getPrefix();
281
0
        }
282
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getPreviousSibling() override
283
0
        {
284
0
            return CNode::getPreviousSibling();
285
0
        }
286
        virtual sal_Bool SAL_CALL hasAttributes() override
287
0
        {
288
0
            return CNode::hasAttributes();
289
0
        }
290
        virtual sal_Bool SAL_CALL hasChildNodes() override
291
0
        {
292
0
            return CNode::hasChildNodes();
293
0
        }
294
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL insertBefore(
295
                const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& refChild) override
296
0
        {
297
0
            return CNode::insertBefore(newChild, refChild);
298
0
        }
299
        virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) override
300
0
        {
301
0
            return CNode::isSupported(feature, ver);
302
0
        }
303
        virtual void SAL_CALL normalize() override
304
0
        {
305
0
            CNode::normalize();
306
0
        }
307
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeChild(const css::uno::Reference< css::xml::dom::XNode >& oldChild) override
308
0
        {
309
0
            return CNode::removeChild(oldChild);
310
0
        }
311
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL replaceChild(
312
                const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& oldChild) override
313
0
        {
314
0
            return CNode::replaceChild(newChild, oldChild);
315
0
        }
316
        virtual void SAL_CALL setNodeValue(const OUString& nodeValue) override
317
0
        {
318
0
            return CNode::setNodeValue(nodeValue);
319
0
        }
320
        virtual void SAL_CALL setPrefix(const OUString& prefix) override
321
0
        {
322
0
            return CNode::setPrefix(prefix);
323
0
        }
324
325
        // css::xml::sax::XSAXSerializable
326
        virtual void SAL_CALL serialize(
327
            const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler,
328
            const css::uno::Sequence< css::beans::StringPair >& i_rNamespaces) override;
329
330
        // css::xml::sax::XFastSAXSerializable
331
        virtual void SAL_CALL fastSerialize( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& handler,
332
                                             const css::uno::Reference< css::xml::sax::XFastTokenHandler >& tokenHandler,
333
                                             const css::uno::Sequence< css::beans::StringPair >& i_rNamespaces,
334
                                             const css::uno::Sequence< css::beans::Pair< OUString, sal_Int32 > >& namespaces ) override;
335
    };
336
}
337
338
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */