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 .
22 #include <libxml/tree.h>
24 #include <sal/types.h>
25 #include <rtl/ref.hxx>
26 #include <rtl/string.hxx>
27 #include <rtl/ustring.hxx>
29 #include <cppuhelper/implbase.hxx>
31 #include <sax/fastattribs.hxx>
33 #include <com/sun/star/uno/Reference.h>
34 #include <com/sun/star/uno/Sequence.h>
35 #include <com/sun/star/lang/XUnoTunnel.hpp>
36 #include <com/sun/star/xml/dom/XNode.hpp>
37 #include <com/sun/star/xml/dom/XNodeList.hpp>
38 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
39 #include <com/sun/star/xml/dom/NodeType.hpp>
40 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
41 #include <com/sun/star/xml/dom/events/XEvent.hpp>
42 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
43 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
45 #include <unordered_map>
51 Context( const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& i_xHandler
,
52 sax_fastparser::FastTokenHandlerBase
* pTokenHandler
) :
53 maNamespaces( 1, std::vector
<Namespace
>() ),
55 mxAttribList(new sax_fastparser::FastAttributeList(pTokenHandler
)),
56 mxCurrentHandler(i_xHandler
),
57 mxDocHandler(i_xHandler
),
58 mxTokenHandler(pTokenHandler
)
66 const OString
& getPrefix() const { return maPrefix
; }
69 typedef std::vector
< std::vector
<Namespace
> > NamespaceVectorType
;
70 typedef std::unordered_map
< OUString
, sal_Int32
> NamespaceMapType
;
72 /// outer vector: xml context; inner vector: current NS
73 NamespaceVectorType maNamespaces
;
74 NamespaceMapType maNamespaceMap
;
75 ::rtl::Reference
<sax_fastparser::FastAttributeList
> mxAttribList
;
76 css::uno::Reference
<css::xml::sax::XFastContextHandler
> mxCurrentHandler
;
77 css::uno::Reference
<css::xml::sax::XFastDocumentHandler
> mxDocHandler
;
78 rtl::Reference
<sax_fastparser::FastTokenHandlerBase
> mxTokenHandler
;
81 void pushContext(Context
& io_rContext
);
82 void popContext(Context
& io_rContext
);
84 sal_Int32
getTokenWithPrefix( const Context
& rContext
, const char* xPrefix
, const char* xName
);
85 sal_Int32
getToken( const Context
& rContext
, const char* xName
);
87 /// add namespaces on this node to context
88 void addNamespaces(Context
& io_rContext
, xmlNodePtr pNode
);
92 class CNode
: public cppu::WeakImplHelper
< css::xml::dom::XNode
, css::lang::XUnoTunnel
, css::xml::dom::events::XEventTarget
>
94 friend class CDocument
;
95 friend class CElement
;
96 friend class CAttributesMap
;
99 bool m_bUnlinked
; /// node has been removed from document
102 css::xml::dom::NodeType
const m_aNodeType
;
103 /// libxml node; NB: not const, because invalidate may reset it to 0!
104 xmlNodePtr m_aNodePtr
;
106 ::rtl::Reference
< CDocument
> const m_xDocument
;
107 ::osl::Mutex
& m_rMutex
;
109 // for initialization by classes derived through ImplInheritanceHelper
110 CNode(CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
111 css::xml::dom::NodeType
const& reNodeType
, xmlNodePtr
const& rpNode
);
114 void dispatchSubtreeModified();
118 virtual ~CNode() override
;
120 static const css::uno::Sequence
< sal_Int8
> & getUnoTunnelId() noexcept
;
122 xmlNodePtr
GetNodePtr() { return m_aNodePtr
; }
124 virtual CDocument
& GetOwnerDocument();
126 // recursively create SAX events
127 virtual void saxify(const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& i_xHandler
);
129 // recursively create SAX events
130 virtual void fastSaxify( Context
& io_rContext
);
132 // constrains child relationship between nodes based on type
133 virtual bool IsChildTypeAllowed(css::xml::dom::NodeType
const nodeType
);
135 // ---- DOM interfaces
138 Adds the node newChild to the end of the list of children of this node.
140 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
141 appendChild(css::uno::Reference
< css::xml::dom::XNode
> const& xNewChild
) override
;
144 Returns a duplicate of this node, i.e., serves as a generic copy
145 constructor for nodes.
147 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
cloneNode(sal_Bool deep
) override
;
150 A NamedNodeMap containing the attributes of this node
151 (if it is an Element) or null otherwise.
153 virtual css::uno::Reference
< css::xml::dom::XNamedNodeMap
> SAL_CALL
getAttributes() override
;
156 A NodeList that contains all children of this node.
158 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getChildNodes() override
;
161 The first child of this node.
163 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getFirstChild() override
;
166 The last child of this node.
168 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getLastChild() override
;
171 Returns the local part of the qualified name of this node.
173 virtual OUString SAL_CALL
getLocalName() override
;
176 The namespace URI of this node, or null if it is unspecified.
178 virtual OUString SAL_CALL
getNamespaceURI() override
;
181 The node immediately following this node.
183 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getNextSibling() override
;
186 The name of this node, depending on its type; see the table above.
187 -- virtual implemented by actual node types
189 virtual OUString SAL_CALL
getNodeName() override
;
192 A code representing the type of the underlying object, as defined above.
194 virtual css::xml::dom::NodeType SAL_CALL
getNodeType() override
;
197 The value of this node, depending on its type; see the table above.
198 -- virtual implemented by actual node types
200 virtual OUString SAL_CALL
getNodeValue() override
;
203 The Document object associated with this node.
205 virtual css::uno::Reference
< css::xml::dom::XDocument
> SAL_CALL
getOwnerDocument() override
;
208 The parent of this node.
210 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getParentNode() override
;
213 The namespace prefix of this node, or null if it is unspecified.
215 virtual OUString SAL_CALL
getPrefix() override
;
218 The node immediately preceding this node.
220 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getPreviousSibling() override
;
223 Returns whether this node (if it is an element) has any attributes.
225 virtual sal_Bool SAL_CALL
hasAttributes() override
;
228 Returns whether this node has any children.
230 virtual sal_Bool SAL_CALL
hasChildNodes() override
;
233 Inserts the node newChild before the existing child node refChild.
235 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
insertBefore(
236 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& refChild
) override
;
239 Tests whether the DOM implementation implements a specific feature and
240 that feature is supported by this node.
242 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
) override
;
245 Puts all Text nodes in the full depth of the sub-tree underneath this
246 Node, including attribute nodes, into a "normal" form where only structure
247 (e.g., elements, comments, processing instructions, CDATA sections, and
248 entity references) separates Text nodes, i.e., there are neither adjacent
249 Text nodes nor empty Text nodes.
251 virtual void SAL_CALL
normalize() override
;
254 Removes the child node indicated by oldChild from the list of children,
257 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
removeChild(const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
260 Replaces the child node oldChild with newChild in the list of children,
261 and returns the oldChild node.
263 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
replaceChild(
264 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
267 The value of this node, depending on its type; see the table above.
269 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
) override
;
272 The namespace prefix of this node, or null if it is unspecified.
274 virtual void SAL_CALL
setPrefix(const OUString
& prefix
) override
;
278 virtual void SAL_CALL
addEventListener(const OUString
& eventType
,
279 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
280 sal_Bool useCapture
) override
;
282 virtual void SAL_CALL
removeEventListener(const OUString
& eventType
,
283 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
284 sal_Bool useCapture
) override
;
286 virtual sal_Bool SAL_CALL
dispatchEvent(const css::uno::Reference
< css::xml::dom::events::XEvent
>& evt
) override
;
289 virtual ::sal_Int64 SAL_CALL
290 getSomething(css::uno::Sequence
< ::sal_Int8
> const& rId
) override
;
293 /// eliminate redundant namespace declarations
294 void nscleanup(const xmlNodePtr aNode
, const xmlNodePtr aParent
);
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */