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 .
20 #ifndef INCLUDED_UNOXML_SOURCE_DOM_DOCUMENT_HXX
21 #define INCLUDED_UNOXML_SOURCE_DOM_DOCUMENT_HXX
26 #include <libxml/tree.h>
28 #include <sal/types.h>
30 #include <cppuhelper/implbase.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>
54 class CEventDispatcher
;
59 typedef ::cppu::ImplInheritanceHelper
<
60 CNode
, css::xml::dom::XDocument
, css::xml::dom::events::XDocumentEvent
,
61 css::io::XActiveDataControl
, css::io::XActiveDataSource
,
62 css::xml::sax::XSAXSerializable
, css::xml::sax::XFastSAXSerializable
>
66 : public CDocument_Base
70 /// this Mutex is used for synchronization of all UNO wrapper
71 /// objects that belong to this document
73 /// the libxml document: freed in destructor
74 /// => all UNO wrapper objects must keep the CDocument alive
75 xmlDocPtr
const m_aDocPtr
;
77 // datacontrol/source state
78 typedef std::set
< css::uno::Reference
< css::io::XStreamListener
> > listenerlist_t
;
79 listenerlist_t m_streamListeners
;
80 css::uno::Reference
< css::io::XOutputStream
> m_rOutputStream
;
82 typedef std::map
< const xmlNodePtr
,
83 ::std::pair
< css::uno::WeakReference
<css::xml::dom::XNode
>, CNode
* > > nodemap_t
;
86 ::std::unique_ptr
<events::CEventDispatcher
> const m_pEventDispatcher
;
88 explicit CDocument(xmlDocPtr
const pDocPtr
);
92 /// factory: only way to create instance!
93 static ::rtl::Reference
<CDocument
>
94 CreateCDocument(xmlDocPtr
const pDoc
);
96 virtual ~CDocument() override
;
98 // needed by CXPathAPI
99 ::osl::Mutex
& GetMutex() { return m_Mutex
; }
101 events::CEventDispatcher
& GetEventDispatcher();
102 ::rtl::Reference
< CElement
> GetDocumentElement();
104 /// get UNO wrapper instance for a libxml node
105 ::rtl::Reference
<CNode
> GetCNode(
106 xmlNodePtr
const pNode
, bool const bCreate
= true);
107 /// remove a UNO wrapper instance
108 void RemoveCNode(xmlNodePtr
const pNode
, CNode
const*const pCNode
);
110 virtual CDocument
& GetOwnerDocument() override
;
112 virtual void saxify(const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& i_xHandler
) override
;
114 virtual void fastSaxify( Context
& rContext
) override
;
116 virtual bool IsChildTypeAllowed(css::xml::dom::NodeType
const nodeType
) override
;
119 Creates an Attr of the given name.
121 virtual css::uno::Reference
< css::xml::dom::XAttr
> SAL_CALL
createAttribute(const OUString
& name
) override
;
124 Creates an attribute of the given qualified name and namespace URI.
126 virtual css::uno::Reference
< css::xml::dom::XAttr
> SAL_CALL
createAttributeNS(const OUString
& namespaceURI
, const OUString
& qualifiedName
) override
;
129 Creates a CDATASection node whose value is the specified string.
131 virtual css::uno::Reference
< css::xml::dom::XCDATASection
> SAL_CALL
createCDATASection(const OUString
& data
) override
;
134 Creates a Comment node given the specified string.
136 virtual css::uno::Reference
< css::xml::dom::XComment
> SAL_CALL
createComment(const OUString
& data
) override
;
139 Creates an empty DocumentFragment object.
141 virtual css::uno::Reference
< css::xml::dom::XDocumentFragment
> SAL_CALL
createDocumentFragment() override
;
144 Creates an element of the type specified.
146 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
createElement(const OUString
& tagName
) override
;
149 Creates an element of the given qualified name and namespace URI.
151 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
createElementNS(const OUString
& namespaceURI
, const OUString
& qualifiedName
) override
;
154 Creates an EntityReference object.
156 virtual css::uno::Reference
< css::xml::dom::XEntityReference
> SAL_CALL
createEntityReference(const OUString
& name
) override
;
159 Creates a ProcessingInstruction node given the specified name and
162 virtual css::uno::Reference
< css::xml::dom::XProcessingInstruction
> SAL_CALL
createProcessingInstruction(
163 const OUString
& target
, const OUString
& data
) override
;
166 Creates a Text node given the specified string.
168 virtual css::uno::Reference
< css::xml::dom::XText
> SAL_CALL
createTextNode(const OUString
& data
) override
;
171 The Document Type Declaration (see DocumentType) associated with this
174 virtual css::uno::Reference
< css::xml::dom::XDocumentType
> SAL_CALL
getDoctype() override
;
177 This is a convenience attribute that allows direct access to the child
178 node that is the root element of the document.
180 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
getDocumentElement() override
;
183 Returns the Element whose ID is given by elementId.
185 virtual css::uno::Reference
< css::xml::dom::XElement
> SAL_CALL
getElementById(const OUString
& elementId
) override
;
188 Returns a NodeList of all the Elements with a given tag name in the
189 order in which they are encountered in a preorder traversal of the
192 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getElementsByTagName(const OUString
& tagname
) override
;
195 Returns a NodeList of all the Elements with a given local name and
196 namespace URI in the order in which they are encountered in a preorder
197 traversal of the Document tree.
199 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getElementsByTagNameNS(const OUString
& namespaceURI
, const OUString
& localName
) override
;
202 The DOMImplementation object that handles this document.
204 virtual css::uno::Reference
< css::xml::dom::XDOMImplementation
> SAL_CALL
getImplementation() override
;
207 Imports a node from another document to this document.
209 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 virtual css::uno::Reference
< css::xml::dom::events::XEvent
> SAL_CALL
createEvent(const OUString
& eventType
) override
;
214 // XActiveDataControl,
215 // see https://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataControl.html
216 virtual void SAL_CALL
addListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
217 virtual void SAL_CALL
removeListener(const css::uno::Reference
< css::io::XStreamListener
>& aListener
) override
;
218 virtual void SAL_CALL
start() override
;
219 virtual void SAL_CALL
terminate() override
;
222 // see https://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataSource.html
223 virtual void SAL_CALL
setOutputStream( const css::uno::Reference
< css::io::XOutputStream
>& aStream
) override
;
224 virtual css::uno::Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream() override
;
226 // ---- resolve uno inheritance problems...
227 // overrides for XNode base
228 virtual OUString SAL_CALL
getNodeName() override
;
229 virtual OUString SAL_CALL
getNodeValue() override
;
230 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
cloneNode(sal_Bool deep
) override
;
231 // --- delegation for XNode base.
232 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
appendChild(const css::uno::Reference
< css::xml::dom::XNode
>& newChild
) override
234 return CNode::appendChild(newChild
);
236 virtual css::uno::Reference
< css::xml::dom::XNamedNodeMap
> SAL_CALL
getAttributes() override
238 return CNode::getAttributes();
240 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getChildNodes() override
242 return CNode::getChildNodes();
244 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getFirstChild() override
246 return CNode::getFirstChild();
248 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getLastChild() override
250 return CNode::getLastChild();
252 virtual OUString SAL_CALL
getLocalName() override
254 return CNode::getLocalName();
256 virtual OUString SAL_CALL
getNamespaceURI() override
258 return CNode::getNamespaceURI();
260 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getNextSibling() override
262 return CNode::getNextSibling();
264 virtual css::xml::dom::NodeType SAL_CALL
getNodeType() override
266 return CNode::getNodeType();
268 virtual css::uno::Reference
< css::xml::dom::XDocument
> SAL_CALL
getOwnerDocument() override
270 return CNode::getOwnerDocument();
272 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getParentNode() override
274 return CNode::getParentNode();
276 virtual OUString SAL_CALL
getPrefix() override
278 return CNode::getPrefix();
280 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getPreviousSibling() override
282 return CNode::getPreviousSibling();
284 virtual sal_Bool SAL_CALL
hasAttributes() override
286 return CNode::hasAttributes();
288 virtual sal_Bool SAL_CALL
hasChildNodes() override
290 return CNode::hasChildNodes();
292 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
insertBefore(
293 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& refChild
) override
295 return CNode::insertBefore(newChild
, refChild
);
297 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
) override
299 return CNode::isSupported(feature
, ver
);
301 virtual void SAL_CALL
normalize() override
305 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
removeChild(const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
307 return CNode::removeChild(oldChild
);
309 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
replaceChild(
310 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
312 return CNode::replaceChild(newChild
, oldChild
);
314 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
) override
316 return CNode::setNodeValue(nodeValue
);
318 virtual void SAL_CALL
setPrefix(const OUString
& prefix
) override
320 return CNode::setPrefix(prefix
);
323 // css::xml::sax::XSAXSerializable
324 virtual void SAL_CALL
serialize(
325 const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& i_xHandler
,
326 const css::uno::Sequence
< css::beans::StringPair
>& i_rNamespaces
) override
;
328 // css::xml::sax::XFastSAXSerializable
329 virtual void SAL_CALL
fastSerialize( const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& handler
,
330 const css::uno::Reference
< css::xml::sax::XFastTokenHandler
>& tokenHandler
,
331 const css::uno::Sequence
< css::beans::StringPair
>& i_rNamespaces
,
332 const css::uno::Sequence
< css::beans::Pair
< OUString
, sal_Int32
> >& namespaces
) override
;
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */