Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / unoxml / inc / node.hxx
blobe35ab3aafda556be7b18066cba5650eb648cf16c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
49 namespace DOM
51 struct Context
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>() ),
56 maNamespaceMap(101),
57 mxAttribList(new sax_fastparser::FastAttributeList(i_xTokenHandler)),
58 mxCurrentHandler(i_xHandler),
59 mxDocHandler(i_xHandler),
60 mxTokenHandler(i_xTokenHandler)
63 struct Namespace
65 OString maPrefix;
66 sal_Int32 mnToken;
68 const OString& getPrefix() const { return maPrefix; }
71 typedef std::vector< std::vector<Namespace> > NamespaceVectorType;
72 typedef std::unordered_map< OUString, sal_Int32 > NamespaceMapType;
74 /// outer vector: xml context; inner vector: current NS
75 NamespaceVectorType maNamespaces;
76 NamespaceMapType maNamespaceMap;
77 ::rtl::Reference<sax_fastparser::FastAttributeList> mxAttribList;
78 css::uno::Reference<css::xml::sax::XFastContextHandler> mxCurrentHandler;
79 css::uno::Reference<css::xml::sax::XFastDocumentHandler> mxDocHandler;
80 css::uno::Reference<css::xml::sax::XFastTokenHandler> mxTokenHandler;
83 void pushContext(Context& io_rContext);
84 void popContext(Context& io_rContext);
86 sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName );
87 sal_Int32 getToken( const Context& rContext, const sal_Char* xName );
89 /// add namespaces on this node to context
90 void addNamespaces(Context& io_rContext, xmlNodePtr pNode);
92 class CDocument;
94 class CNode : public cppu::WeakImplHelper< css::xml::dom::XNode, css::lang::XUnoTunnel, css::xml::dom::events::XEventTarget >
96 friend class CDocument;
97 friend class CElement;
98 friend class CAttributesMap;
100 private:
101 bool m_bUnlinked; /// node has been removed from document
103 protected:
104 css::xml::dom::NodeType const m_aNodeType;
105 /// libxml node; NB: not const, because invalidate may reset it to 0!
106 xmlNodePtr m_aNodePtr;
108 ::rtl::Reference< CDocument > const m_xDocument;
109 ::osl::Mutex & m_rMutex;
111 // for initialization by classes derived through ImplInheritanceHelper
112 CNode(CDocument const& rDocument, ::osl::Mutex const& rMutex,
113 css::xml::dom::NodeType const& reNodeType, xmlNodePtr const& rpNode);
114 void invalidate();
116 void dispatchSubtreeModified();
118 public:
120 virtual ~CNode() override;
122 static const css::uno::Sequence< sal_Int8 > & getUnoTunnelId() throw();
124 xmlNodePtr GetNodePtr() { return m_aNodePtr; }
126 virtual CDocument & GetOwnerDocument();
128 // recursively create SAX events
129 virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler);
131 // recursively create SAX events
132 virtual void fastSaxify( Context& io_rContext );
134 // constrains child relationship between nodes based on type
135 virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType);
137 // ---- DOM interfaces
140 Adds the node newChild to the end of the list of children of this node.
142 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL
143 appendChild(css::uno::Reference< css::xml::dom::XNode > const& xNewChild) override;
146 Returns a duplicate of this node, i.e., serves as a generic copy
147 constructor for nodes.
149 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL cloneNode(sal_Bool deep) override;
152 A NamedNodeMap containing the attributes of this node
153 (if it is an Element) or null otherwise.
155 virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes() override;
158 A NodeList that contains all children of this node.
160 virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getChildNodes() override;
163 The first child of this node.
165 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getFirstChild() override;
168 The last child of this node.
170 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getLastChild() override;
173 Returns the local part of the qualified name of this node.
175 virtual OUString SAL_CALL getLocalName() override;
178 The namespace URI of this node, or null if it is unspecified.
180 virtual OUString SAL_CALL getNamespaceURI() override;
183 The node immediately following this node.
185 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNextSibling() override;
188 The name of this node, depending on its type; see the table above.
189 -- virtual implemented by actual node types
191 virtual OUString SAL_CALL getNodeName() override;
194 A code representing the type of the underlying object, as defined above.
196 virtual css::xml::dom::NodeType SAL_CALL getNodeType() override;
199 The value of this node, depending on its type; see the table above.
200 -- virtual implemented by actual node types
202 virtual OUString SAL_CALL getNodeValue() override;
205 The Document object associated with this node.
207 virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument() override;
210 The parent of this node.
212 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getParentNode() override;
215 The namespace prefix of this node, or null if it is unspecified.
217 virtual OUString SAL_CALL getPrefix() override;
220 The node immediately preceding this node.
222 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getPreviousSibling() override;
225 Returns whether this node (if it is an element) has any attributes.
227 virtual sal_Bool SAL_CALL hasAttributes() override;
230 Returns whether this node has any children.
232 virtual sal_Bool SAL_CALL hasChildNodes() override;
235 Inserts the node newChild before the existing child node refChild.
237 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL insertBefore(
238 const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& refChild) override;
241 Tests whether the DOM implementation implements a specific feature and
242 that feature is supported by this node.
244 virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) override;
247 Puts all Text nodes in the full depth of the sub-tree underneath this
248 Node, including attribute nodes, into a "normal" form where only structure
249 (e.g., elements, comments, processing instructions, CDATA sections, and
250 entity references) separates Text nodes, i.e., there are neither adjacent
251 Text nodes nor empty Text nodes.
253 virtual void SAL_CALL normalize() override;
256 Removes the child node indicated by oldChild from the list of children,
257 and returns it.
259 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeChild(const css::uno::Reference< css::xml::dom::XNode >& oldChild) override;
262 Replaces the child node oldChild with newChild in the list of children,
263 and returns the oldChild node.
265 virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL replaceChild(
266 const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& oldChild) override;
269 The value of this node, depending on its type; see the table above.
271 virtual void SAL_CALL setNodeValue(const OUString& nodeValue) override;
274 The namespace prefix of this node, or null if it is unspecified.
276 virtual void SAL_CALL setPrefix(const OUString& prefix) override;
279 // --- XEventTarget
280 virtual void SAL_CALL addEventListener(const OUString& eventType,
281 const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
282 sal_Bool useCapture) override;
284 virtual void SAL_CALL removeEventListener(const OUString& eventType,
285 const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
286 sal_Bool useCapture) override;
288 virtual sal_Bool SAL_CALL dispatchEvent(const css::uno::Reference< css::xml::dom::events::XEvent >& evt) override;
290 // --- XUnoTunnel
291 virtual ::sal_Int64 SAL_CALL
292 getSomething(css::uno::Sequence< ::sal_Int8 > const& rId) override;
295 /// eliminate redundant namespace declarations
296 void nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent);
299 #endif // INCLUDED_UNOXML_INC_NODE_HXX
301 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */