1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include <unordered_map>
25 #include <libxml/tree.h>
27 #include <sal/types.h>
29 #include <cppuhelper/implbase.hxx>
30 #include <cppuhelper/weakref.hxx>
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>
55 class CEventDispatcher
;
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
>
67 : public CDocument_Base
71 /// this Mutex is used for synchronization of all UNO wrapper
72 /// objects that belong to this document
74 /// the libxml document: freed in destructor
75 /// => all UNO wrapper objects must keep the CDocument alive
76 xmlDocPtr
const m_aDocPtr
;
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
;
83 typedef std::unordered_map
< xmlNodePtr
,
84 ::std::pair
< css::uno::WeakReference
<css::xml::dom::XNode
>, CNode
* > > nodemap_t
;
87 ::std::unique_ptr
<events::CEventDispatcher
> const m_pEventDispatcher
;
89 explicit CDocument(xmlDocPtr
const pDocPtr
);
93 /// factory: only way to create instance!
94 static ::rtl::Reference
<CDocument
>
95 CreateCDocument(xmlDocPtr
const pDoc
);
97 virtual ~CDocument() override
;
99 // needed by CXPathAPI
100 ::osl::Mutex
& GetMutex() { return m_Mutex
; }
102 events::CEventDispatcher
& GetEventDispatcher();
103 ::rtl::Reference
< CElement
> GetDocumentElement();
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
);
111 virtual CDocument
& GetOwnerDocument() override
;
113 virtual void saxify(const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& i_xHandler
) override
;
115 virtual void fastSaxify( Context
& rContext
) override
;
117 virtual bool IsChildTypeAllowed(css::xml::dom::NodeType
const nodeType
,
118 css::xml::dom::NodeType
const* pReplacedNodeType
) override
;
121 Creates an Attr of the given name.
123 virtual css::uno::Reference
< css::xml::dom::XAttr
> SAL_CALL
createAttribute(const OUString
& name
) override
;
126 Creates an attribute of the given qualified name and namespace URI.
128 virtual css::uno::Reference
< css::xml::dom::XAttr
> SAL_CALL
createAttributeNS(const OUString
& namespaceURI
, const OUString
& qualifiedName
) override
;
131 Creates a CDATASection node whose value is the specified string.
133 virtual css::uno::Reference
< css::xml::dom::XCDATASection
> SAL_CALL
createCDATASection(const OUString
& data
) override
;
136 Creates a Comment node given the specified string.
138 virtual css::uno::Reference
< css::xml::dom::XComment
> SAL_CALL
createComment(const OUString
& data
) override
;
141 Creates an empty DocumentFragment object.
143 virtual css::uno::Reference
< css::xml::dom::XDocumentFragment
> SAL_CALL
createDocumentFragment() override
;
146 Creates an element of the type specified.
148 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
createElement(const OUString
& tagName
) override
;
151 Creates an element of the given qualified name and namespace URI.
153 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
createElementNS(const OUString
& namespaceURI
, const OUString
& qualifiedName
) override
;
156 Creates an EntityReference object.
158 virtual css::uno::Reference
< css::xml::dom::XEntityReference
> SAL_CALL
createEntityReference(const OUString
& name
) override
;
161 Creates a ProcessingInstruction node given the specified name and
164 virtual css::uno::Reference
< css::xml::dom::XProcessingInstruction
> SAL_CALL
createProcessingInstruction(
165 const OUString
& target
, const OUString
& data
) override
;
168 Creates a Text node given the specified string.
170 virtual css::uno::Reference
< css::xml::dom::XText
> SAL_CALL
createTextNode(const OUString
& data
) override
;
173 The Document Type Declaration (see DocumentType) associated with this
176 virtual css::uno::Reference
< css::xml::dom::XDocumentType
> SAL_CALL
getDoctype() override
;
179 This is a convenience attribute that allows direct access to the child
180 node that is the root element of the document.
182 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
getDocumentElement() override
;
185 Returns the Element whose ID is given by elementId.
187 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
getElementById(const OUString
& elementId
) override
;
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
194 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getElementsByTagName(const OUString
& tagname
) override
;
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.
201 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getElementsByTagNameNS(const OUString
& namespaceURI
, const OUString
& localName
) override
;
204 The DOMImplementation object that handles this document.
206 virtual css::uno::Reference
< css::xml::dom::XDOMImplementation
> SAL_CALL
getImplementation() override
;
209 Imports a node from another document to this document.
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
;
214 virtual css::uno::Reference
< css::xml::dom::events::XEvent
> SAL_CALL
createEvent(const OUString
& eventType
) override
;
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
;
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
;
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
236 return CNode::appendChild(newChild
);
238 virtual css::uno::Reference
< css::xml::dom::XNamedNodeMap
> SAL_CALL
getAttributes() override
240 return CNode::getAttributes();
242 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getChildNodes() override
244 return CNode::getChildNodes();
246 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getFirstChild() override
248 return CNode::getFirstChild();
250 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getLastChild() override
252 return CNode::getLastChild();
254 virtual OUString SAL_CALL
getLocalName() override
256 return CNode::getLocalName();
258 virtual OUString SAL_CALL
getNamespaceURI() override
260 return CNode::getNamespaceURI();
262 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getNextSibling() override
264 return CNode::getNextSibling();
266 virtual css::xml::dom::NodeType SAL_CALL
getNodeType() override
268 return CNode::getNodeType();
270 virtual css::uno::Reference
< css::xml::dom::XDocument
> SAL_CALL
getOwnerDocument() override
272 return CNode::getOwnerDocument();
274 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getParentNode() override
276 return CNode::getParentNode();
278 virtual OUString SAL_CALL
getPrefix() override
280 return CNode::getPrefix();
282 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getPreviousSibling() override
284 return CNode::getPreviousSibling();
286 virtual sal_Bool SAL_CALL
hasAttributes() override
288 return CNode::hasAttributes();
290 virtual sal_Bool SAL_CALL
hasChildNodes() override
292 return CNode::hasChildNodes();
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
297 return CNode::insertBefore(newChild
, refChild
);
299 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
) override
301 return CNode::isSupported(feature
, ver
);
303 virtual void SAL_CALL
normalize() override
307 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
removeChild(const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
309 return CNode::removeChild(oldChild
);
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
314 return CNode::replaceChild(newChild
, oldChild
);
316 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
) override
318 return CNode::setNodeValue(nodeValue
);
320 virtual void SAL_CALL
setPrefix(const OUString
& prefix
) override
322 return CNode::setPrefix(prefix
);
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
;
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
;
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */