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_INC_NODE_HXX
21 #define INCLUDED_UNOXML_INC_NODE_HXX
23 #include <libxml/tree.h>
25 #include <sal/types.h>
26 #include <rtl/ref.hxx>
27 #include <rtl/string.hxx>
28 #include <rtl/ustring.hxx>
30 #include <cppuhelper/implbase.hxx>
32 #include <sax/fastattribs.hxx>
34 #include <com/sun/star/uno/Reference.h>
35 #include <com/sun/star/uno/Sequence.h>
36 #include <com/sun/star/lang/XUnoTunnel.hpp>
37 #include <com/sun/star/xml/dom/XNode.hpp>
38 #include <com/sun/star/xml/dom/XNodeList.hpp>
39 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
40 #include <com/sun/star/xml/dom/NodeType.hpp>
41 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
42 #include <com/sun/star/xml/dom/events/XEvent.hpp>
43 #include <com/sun/star/xml/dom/DOMException.hpp>
44 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
45 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
47 #include <unordered_map>
53 Context( const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& i_xHandler
,
54 const css::uno::Reference
< css::xml::sax::XFastTokenHandler
>& i_xTokenHandler
) :
55 maNamespaces( 1, std::vector
<Namespace
>() ),
57 mxAttribList(new sax_fastparser::FastAttributeList(i_xTokenHandler
)),
58 mxCurrentHandler(i_xHandler
),
59 mxDocHandler(i_xHandler
),
60 mxTokenHandler(i_xTokenHandler
)
67 OUString maNamespaceURL
;
69 const OString
& getPrefix() const { return maPrefix
; }
72 typedef std::vector
< std::vector
<Namespace
> > NamespaceVectorType
;
73 typedef std::unordered_map
< OUString
, sal_Int32
> NamespaceMapType
;
75 /// outer vector: xml context; inner vector: current NS
76 NamespaceVectorType maNamespaces
;
77 NamespaceMapType maNamespaceMap
;
78 ::rtl::Reference
<sax_fastparser::FastAttributeList
> mxAttribList
;
79 css::uno::Reference
<css::xml::sax::XFastContextHandler
> mxCurrentHandler
;
80 css::uno::Reference
<css::xml::sax::XFastDocumentHandler
> mxDocHandler
;
81 css::uno::Reference
<css::xml::sax::XFastTokenHandler
> mxTokenHandler
;
84 void pushContext(Context
& io_rContext
);
85 void popContext(Context
& io_rContext
);
87 sal_Int32
getTokenWithPrefix( const Context
& rContext
, const sal_Char
* xPrefix
, const sal_Char
* xName
);
88 sal_Int32
getToken( const Context
& rContext
, const sal_Char
* xName
);
90 /// add namespaces on this node to context
91 void addNamespaces(Context
& io_rContext
, xmlNodePtr pNode
);
95 class CNode
: public cppu::WeakImplHelper
< css::xml::dom::XNode
, css::lang::XUnoTunnel
, css::xml::dom::events::XEventTarget
>
97 friend class CDocument
;
98 friend class CElement
;
99 friend class CAttributesMap
;
102 bool m_bUnlinked
; /// node has been removed from document
105 css::xml::dom::NodeType
const m_aNodeType
;
106 /// libxml node; NB: not const, because invalidate may reset it to 0!
107 xmlNodePtr m_aNodePtr
;
109 ::rtl::Reference
< CDocument
> const m_xDocument
;
110 ::osl::Mutex
& m_rMutex
;
112 // for initialization by classes derived through ImplInheritanceHelper
113 CNode(CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
114 css::xml::dom::NodeType
const& reNodeType
, xmlNodePtr
const& rpNode
);
117 void dispatchSubtreeModified();
121 virtual ~CNode() override
;
123 static CNode
* GetImplementation(css::uno::Reference
<
124 css::uno::XInterface
> const& xNode
);
126 xmlNodePtr
GetNodePtr() { return m_aNodePtr
; }
128 virtual CDocument
& GetOwnerDocument();
130 // recursively create SAX events
131 virtual void saxify(const css::uno::Reference
< css::xml::sax::XDocumentHandler
>& i_xHandler
);
133 // recursively create SAX events
134 virtual void fastSaxify( Context
& io_rContext
);
136 // constrains child relationship between nodes based on type
137 virtual bool IsChildTypeAllowed(css::xml::dom::NodeType
const nodeType
);
139 // ---- DOM interfaces
142 Adds the node newChild to the end of the list of children of this node.
144 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
145 appendChild(css::uno::Reference
< css::xml::dom::XNode
> const& xNewChild
) override
;
148 Returns a duplicate of this node, i.e., serves as a generic copy
149 constructor for nodes.
151 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
cloneNode(sal_Bool deep
) override
;
154 A NamedNodeMap containing the attributes of this node
155 (if it is an Element) or null otherwise.
157 virtual css::uno::Reference
< css::xml::dom::XNamedNodeMap
> SAL_CALL
getAttributes() override
;
160 A NodeList that contains all children of this node.
162 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getChildNodes() override
;
165 The first child of this node.
167 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getFirstChild() override
;
170 The last child of this node.
172 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getLastChild() override
;
175 Returns the local part of the qualified name of this node.
177 virtual OUString SAL_CALL
getLocalName() override
;
180 The namespace URI of this node, or null if it is unspecified.
182 virtual OUString SAL_CALL
getNamespaceURI() override
;
185 The node immediately following this node.
187 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getNextSibling() override
;
190 The name of this node, depending on its type; see the table above.
191 -- virtual implemented by actual node types
193 virtual OUString SAL_CALL
getNodeName() override
;
196 A code representing the type of the underlying object, as defined above.
198 virtual css::xml::dom::NodeType SAL_CALL
getNodeType() override
;
201 The value of this node, depending on its type; see the table above.
202 -- virtual implemented by actual node types
204 virtual OUString SAL_CALL
getNodeValue() override
;
207 The Document object associated with this node.
209 virtual css::uno::Reference
< css::xml::dom::XDocument
> SAL_CALL
getOwnerDocument() override
;
212 The parent of this node.
214 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getParentNode() override
;
217 The namespace prefix of this node, or null if it is unspecified.
219 virtual OUString SAL_CALL
getPrefix() override
;
222 The node immediately preceding this node.
224 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getPreviousSibling() override
;
227 Returns whether this node (if it is an element) has any attributes.
229 virtual sal_Bool SAL_CALL
hasAttributes() override
;
232 Returns whether this node has any children.
234 virtual sal_Bool SAL_CALL
hasChildNodes() override
;
237 Inserts the node newChild before the existing child node refChild.
239 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
insertBefore(
240 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& refChild
) override
;
243 Tests whether the DOM implementation implements a specific feature and
244 that feature is supported by this node.
246 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
) override
;
249 Puts all Text nodes in the full depth of the sub-tree underneath this
250 Node, including attribute nodes, into a "normal" form where only structure
251 (e.g., elements, comments, processing instructions, CDATA sections, and
252 entity references) separates Text nodes, i.e., there are neither adjacent
253 Text nodes nor empty Text nodes.
255 virtual void SAL_CALL
normalize() override
;
258 Removes the child node indicated by oldChild from the list of children,
261 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
removeChild(const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
264 Replaces the child node oldChild with newChild in the list of children,
265 and returns the oldChild node.
267 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
replaceChild(
268 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
271 The value of this node, depending on its type; see the table above.
273 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
) override
;
276 The namespace prefix of this node, or null if it is unspecified.
278 virtual void SAL_CALL
setPrefix(const OUString
& prefix
) override
;
282 virtual void SAL_CALL
addEventListener(const OUString
& eventType
,
283 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
284 sal_Bool useCapture
) override
;
286 virtual void SAL_CALL
removeEventListener(const OUString
& eventType
,
287 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
288 sal_Bool useCapture
) override
;
290 virtual sal_Bool SAL_CALL
dispatchEvent(const css::uno::Reference
< css::xml::dom::events::XEvent
>& evt
) override
;
293 virtual ::sal_Int64 SAL_CALL
294 getSomething(css::uno::Sequence
< ::sal_Int8
> const& rId
) override
;
297 /// eliminate redundant namespace declarations
298 void nscleanup(const xmlNodePtr aNode
, const xmlNodePtr aParent
);
301 #endif // INCLUDED_UNOXML_INC_NODE_HXX
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */