node.hxx 11.1 KB
Newer Older
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
Michael Meeks's avatar
Michael Meeks committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
19

20 21
#ifndef INCLUDED_UNOXML_INC_NODE_HXX
#define INCLUDED_UNOXML_INC_NODE_HXX
22

23 24 25
#include <libxml/tree.h>

#include <sal/types.h>
26 27 28
#include <rtl/ref.hxx>
#include <rtl/string.hxx>
#include <rtl/ustring.hxx>
29

30
#include <cppuhelper/implbase.hxx>
31 32 33

#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 38 39 40 41 42 43
#include <com/sun/star/xml/dom/XNode.hpp>
#include <com/sun/star/xml/dom/XNodeList.hpp>
#include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
#include <com/sun/star/xml/dom/NodeType.hpp>
#include <com/sun/star/xml/dom/events/XEventTarget.hpp>
#include <com/sun/star/xml/dom/events/XEvent.hpp>
#include <com/sun/star/xml/dom/DOMException.hpp>
44 45
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
46

47
#include <unordered_map>
48

49 50
namespace DOM
{
51 52
    struct Context
    {
53 54
        Context( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& i_xHandler,
                 const css::uno::Reference< css::xml::sax::XFastTokenHandler >& i_xTokenHandler ) :
55 56
            maNamespaces( 1, std::vector<Namespace>() ),
            maNamespaceMap(101),
57
            mxAttribList(new sax_fastparser::FastAttributeList(i_xTokenHandler)),
58
            mxCurrentHandler(i_xHandler),
59 60 61 62 63 64 65 66 67 68 69 70 71
            mxDocHandler(i_xHandler),
            mxTokenHandler(i_xTokenHandler)
        {}

        struct Namespace
        {
            OString     maPrefix;
            sal_Int32   mnToken;

            const OString& getPrefix() const { return maPrefix; }
        };

        typedef std::vector< std::vector<Namespace> > NamespaceVectorType;
72
        typedef std::unordered_map< OUString, sal_Int32 > NamespaceMapType;
73 74 75 76

        /// outer vector: xml context; inner vector: current NS
        NamespaceVectorType                 maNamespaces;
        NamespaceMapType                    maNamespaceMap;
77 78 79 80
        ::rtl::Reference<sax_fastparser::FastAttributeList> mxAttribList;
        css::uno::Reference<css::xml::sax::XFastContextHandler>      mxCurrentHandler;
        css::uno::Reference<css::xml::sax::XFastDocumentHandler>     mxDocHandler;
        css::uno::Reference<css::xml::sax::XFastTokenHandler>        mxTokenHandler;
81 82 83 84 85 86 87 88 89 90 91
    };

    void pushContext(Context& io_rContext);
    void popContext(Context& io_rContext);

    sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName );
    sal_Int32 getToken( const Context& rContext, const sal_Char* xName );

    /// add namespaces on this node to context
    void addNamespaces(Context& io_rContext, xmlNodePtr pNode);

92
    class CDocument;
93

94
    class CNode : public cppu::WeakImplHelper< css::xml::dom::XNode, css::lang::XUnoTunnel, css::xml::dom::events::XEventTarget >
95 96 97 98
    {
        friend class CDocument;
        friend class CElement;
        friend class CAttributesMap;
99

100
    private:
101
        bool m_bUnlinked; /// node has been removed from document
102 103

    protected:
104
        css::xml::dom::NodeType const m_aNodeType;
105
        /// libxml node; NB: not const, because invalidate may reset it to 0!
106 107
        xmlNodePtr m_aNodePtr;

108
        ::rtl::Reference< CDocument > const m_xDocument;
109
        ::osl::Mutex & m_rMutex;
110 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();
115

116 117
        void dispatchSubtreeModified();

118 119
    public:

120
        virtual ~CNode() override;
121

122 123
        static CNode * GetImplementation(css::uno::Reference<
                css::uno::XInterface> const& xNode);
124

125
        xmlNodePtr GetNodePtr() { return m_aNodePtr; }
126

127
        virtual CDocument & GetOwnerDocument();
128

129
        // recursively create SAX events
130
        virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler);
131

132
        // recursively create SAX events
133
        virtual void fastSaxify( Context& io_rContext );
134

135
        // constrains child relationship between nodes based on type
136
        virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType);
137

138 139 140 141 142
        // ---- DOM interfaces

        /**
        Adds the node newChild to the end of the list of children of this node.
        */
143
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL
144
            appendChild(css::uno::Reference< css::xml::dom::XNode > const& xNewChild) override;
145 146 147 148 149

        /**
        Returns a duplicate of this node, i.e., serves as a generic copy
        constructor for nodes.
        */
150
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL cloneNode(sal_Bool deep) override;
151 152

        /**
153 154
        A NamedNodeMap containing the attributes of this node
        (if it is an Element) or null otherwise.
155
        */
156
        virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes() override;
157 158 159 160

        /**
        A NodeList that contains all children of this node.
        */
161
        virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getChildNodes() override;
162 163 164 165

        /**
        The first child of this node.
        */
166
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getFirstChild() override;
167 168 169 170

        /**
        The last child of this node.
        */
171
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getLastChild() override;
172 173 174 175

        /**
        Returns the local part of the qualified name of this node.
        */
176
        virtual OUString SAL_CALL getLocalName() override;
177 178 179 180

        /**
        The namespace URI of this node, or null if it is unspecified.
        */
181
        virtual OUString SAL_CALL getNamespaceURI() override;
182 183 184 185

        /**
        The node immediately following this node.
        */
186
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNextSibling() override;
187 188 189 190 191

        /**
        The name of this node, depending on its type; see the table above.
        -- virtual implemented by actual node types
        */
192
        virtual OUString SAL_CALL getNodeName() override;
193 194 195 196

        /**
        A code representing the type of the underlying object, as defined above.
        */
197
        virtual css::xml::dom::NodeType SAL_CALL getNodeType() override;
198 199 200 201 202

        /**
        The value of this node, depending on its type; see the table above.
        -- virtual implemented by actual node types
        */
203
        virtual OUString SAL_CALL getNodeValue() override;
204 205 206 207

        /**
        The Document object associated with this node.
        */
208
        virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument() override;
209 210 211 212

        /**
        The parent of this node.
        */
213
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getParentNode() override;
214 215 216 217

        /**
        The namespace prefix of this node, or null if it is unspecified.
        */
218
        virtual OUString SAL_CALL getPrefix() override;
219 220 221 222

        /**
        The node immediately preceding this node.
        */
223
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getPreviousSibling() override;
224 225 226 227

        /**
        Returns whether this node (if it is an element) has any attributes.
        */
228
        virtual sal_Bool SAL_CALL hasAttributes() override;
229 230 231 232

        /**
        Returns whether this node has any children.
        */
233
        virtual sal_Bool SAL_CALL hasChildNodes() override;
234 235 236 237

        /**
        Inserts the node newChild before the existing child node refChild.
        */
238
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL insertBefore(
239
                const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& refChild) override;
240 241 242 243 244

        /**
        Tests whether the DOM implementation implements a specific feature and
        that feature is supported by this node.
        */
245
        virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) override;
246 247 248 249 250 251 252 253

        /**
        Puts all Text nodes in the full depth of the sub-tree underneath this
        Node, including attribute nodes, into a "normal" form where only structure
        (e.g., elements, comments, processing instructions, CDATA sections, and
        entity references) separates Text nodes, i.e., there are neither adjacent
        Text nodes nor empty Text nodes.
        */
254
        virtual void SAL_CALL normalize() override;
255 256 257 258 259

        /**
        Removes the child node indicated by oldChild from the list of children,
        and returns it.
        */
260
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeChild(const css::uno::Reference< css::xml::dom::XNode >& oldChild) override;
261 262 263 264 265

        /**
        Replaces the child node oldChild with newChild in the list of children,
        and returns the oldChild node.
        */
266
        virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL replaceChild(
267
                const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& oldChild) override;
268 269 270 271

        /**
        The value of this node, depending on its type; see the table above.
        */
272
        virtual void SAL_CALL setNodeValue(const OUString& nodeValue) override;
273 274 275 276

        /**
        The namespace prefix of this node, or null if it is unspecified.
        */
277
        virtual void SAL_CALL setPrefix(const OUString& prefix) override;
278 279 280 281


        // --- XEventTarget
        virtual void SAL_CALL addEventListener(const OUString& eventType,
282
            const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
283
            sal_Bool useCapture) override;
284 285

        virtual void SAL_CALL removeEventListener(const OUString& eventType,
286
            const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
287
            sal_Bool useCapture) override;
288

289
        virtual sal_Bool SAL_CALL dispatchEvent(const css::uno::Reference< css::xml::dom::events::XEvent >& evt) override;
290

291
        // --- XUnoTunnel
292
        virtual ::sal_Int64 SAL_CALL
293
            getSomething(css::uno::Sequence< ::sal_Int8 > const& rId) override;
294
    };
295 296

    /// eliminate redundant namespace declarations
297
    void nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent);
298 299
}

300
#endif // INCLUDED_UNOXML_INC_NODE_HXX
301 302

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */