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/xml/dom/XNode.hpp>
36 #include <com/sun/star/xml/dom/XNodeList.hpp>
37 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
38 #include <com/sun/star/xml/dom/NodeType.hpp>
39 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
40 #include <com/sun/star/xml/dom/events/XEvent.hpp>
41 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
42 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
44 #include <unordered_map>
50 Context( const css::uno::Reference
< css::xml::sax::XFastDocumentHandler
>& i_xHandler
,
51 sax_fastparser::FastTokenHandlerBase
* pTokenHandler
) :
52 maNamespaces( 1, std::vector
<Namespace
>() ),
54 mxAttribList(new sax_fastparser::FastAttributeList(pTokenHandler
)),
55 mxCurrentHandler(i_xHandler
),
56 mxDocHandler(i_xHandler
),
57 mxTokenHandler(pTokenHandler
)
65 const OString
& getPrefix() const { return maPrefix
; }
68 typedef std::vector
< std::vector
<Namespace
> > NamespaceVectorType
;
69 typedef std::unordered_map
< OUString
, sal_Int32
> NamespaceMapType
;
71 /// outer vector: xml context; inner vector: current NS
72 NamespaceVectorType maNamespaces
;
73 NamespaceMapType maNamespaceMap
;
74 ::rtl::Reference
<sax_fastparser::FastAttributeList
> mxAttribList
;
75 css::uno::Reference
<css::xml::sax::XFastContextHandler
> mxCurrentHandler
;
76 css::uno::Reference
<css::xml::sax::XFastDocumentHandler
> mxDocHandler
;
77 rtl::Reference
<sax_fastparser::FastTokenHandlerBase
> mxTokenHandler
;
80 void pushContext(Context
& io_rContext
);
81 void popContext(Context
& io_rContext
);
83 sal_Int32
getTokenWithPrefix( const Context
& rContext
, const char* xPrefix
, const char* xName
);
84 sal_Int32
getToken( const Context
& rContext
, const char* xName
);
86 /// add namespaces on this node to context
87 void addNamespaces(Context
& io_rContext
, xmlNodePtr pNode
);
91 class SAL_LOPLUGIN_ANNOTATE("crosscast") CNode
92 : public cppu::WeakImplHelper
< css::xml::dom::XNode
, 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();
116 void checkNoParent(css::uno::Reference
< css::xml::dom::XNode
>const& xNode
);
118 static void checkNoParent(const xmlNodePtr pNode
);
120 void checkSameOwner(css::uno::Reference
< css::xml::dom::XNode
>const& xNode
);
124 virtual ~CNode() override
;
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 nodeType
,
138 css::xml::dom::NodeType
const* pReplacedNodeType
);
140 // ---- DOM interfaces
143 Adds the node newChild to the end of the list of children of this node.
145 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
146 appendChild(css::uno::Reference
< css::xml::dom::XNode
> const& xNewChild
) override
;
149 Returns a duplicate of this node, i.e., serves as a generic copy
150 constructor for nodes.
152 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
cloneNode(sal_Bool deep
) override
;
155 A NamedNodeMap containing the attributes of this node
156 (if it is an Element) or null otherwise.
158 virtual css::uno::Reference
< css::xml::dom::XNamedNodeMap
> SAL_CALL
getAttributes() override
;
161 A NodeList that contains all children of this node.
163 virtual css::uno::Reference
< css::xml::dom::XNodeList
> SAL_CALL
getChildNodes() override
;
166 The first child of this node.
168 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getFirstChild() override
;
171 The last child of this node.
173 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getLastChild() override
;
176 Returns the local part of the qualified name of this node.
178 virtual OUString SAL_CALL
getLocalName() override
;
181 The namespace URI of this node, or null if it is unspecified.
183 virtual OUString SAL_CALL
getNamespaceURI() override
;
186 The node immediately following this node.
188 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getNextSibling() override
;
191 The name of this node, depending on its type; see the table above.
192 -- virtual implemented by actual node types
194 virtual OUString SAL_CALL
getNodeName() override
;
197 A code representing the type of the underlying object, as defined above.
199 virtual css::xml::dom::NodeType SAL_CALL
getNodeType() override
;
202 The value of this node, depending on its type; see the table above.
203 -- virtual implemented by actual node types
205 virtual OUString SAL_CALL
getNodeValue() override
;
208 The Document object associated with this node.
210 virtual css::uno::Reference
< css::xml::dom::XDocument
> SAL_CALL
getOwnerDocument() override
;
213 The parent of this node.
215 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getParentNode() override
;
218 The namespace prefix of this node, or null if it is unspecified.
220 virtual OUString SAL_CALL
getPrefix() override
;
223 The node immediately preceding this node.
225 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
getPreviousSibling() override
;
228 Returns whether this node (if it is an element) has any attributes.
230 virtual sal_Bool SAL_CALL
hasAttributes() override
;
233 Returns whether this node has any children.
235 virtual sal_Bool SAL_CALL
hasChildNodes() override
;
238 Inserts the node newChild before the existing child node refChild.
240 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
insertBefore(
241 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& refChild
) override
;
244 Tests whether the DOM implementation implements a specific feature and
245 that feature is supported by this node.
247 virtual sal_Bool SAL_CALL
isSupported(const OUString
& feature
, const OUString
& ver
) override
;
250 Puts all Text nodes in the full depth of the sub-tree underneath this
251 Node, including attribute nodes, into a "normal" form where only structure
252 (e.g., elements, comments, processing instructions, CDATA sections, and
253 entity references) separates Text nodes, i.e., there are neither adjacent
254 Text nodes nor empty Text nodes.
256 virtual void SAL_CALL
normalize() override
;
259 Removes the child node indicated by oldChild from the list of children,
262 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
removeChild(const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
265 Replaces the child node oldChild with newChild in the list of children,
266 and returns the oldChild node.
268 virtual css::uno::Reference
< css::xml::dom::XNode
> SAL_CALL
replaceChild(
269 const css::uno::Reference
< css::xml::dom::XNode
>& newChild
, const css::uno::Reference
< css::xml::dom::XNode
>& oldChild
) override
;
272 The value of this node, depending on its type; see the table above.
274 virtual void SAL_CALL
setNodeValue(const OUString
& nodeValue
) override
;
277 The namespace prefix of this node, or null if it is unspecified.
279 virtual void SAL_CALL
setPrefix(const OUString
& prefix
) override
;
283 virtual void SAL_CALL
addEventListener(const OUString
& eventType
,
284 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
285 sal_Bool useCapture
) override
;
287 virtual void SAL_CALL
removeEventListener(const OUString
& eventType
,
288 const css::uno::Reference
< css::xml::dom::events::XEventListener
>& listener
,
289 sal_Bool useCapture
) override
;
291 virtual sal_Bool SAL_CALL
dispatchEvent(const css::uno::Reference
< css::xml::dom::events::XEvent
>& evt
) override
;
294 /// eliminate redundant namespace declarations
295 void nscleanup(const xmlNodePtr aNode
, const xmlNodePtr aParent
);
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */