xref: /aoo41x/main/unoxml/source/dom/node.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef DOM_NODE_HXX
29 #define DOM_NODE_HXX
30 
31 #include <hash_map>
32 
33 #include <libxml/tree.h>
34 
35 #include <sal/types.h>
36 #include <rtl/ref.hxx>
37 #include <rtl/string.hxx>
38 #include <rtl/ustring.hxx>
39 
40 #include <cppuhelper/implbase3.hxx>
41 
42 #include <sax/fastattribs.hxx>
43 
44 #include <com/sun/star/uno/Reference.h>
45 #include <com/sun/star/uno/Sequence.h>
46 #include <com/sun/star/lang/XUnoTunnel.hpp>
47 #include <com/sun/star/xml/dom/XNode.hpp>
48 #include <com/sun/star/xml/dom/XNodeList.hpp>
49 #include <com/sun/star/xml/dom/XNamedNodeMap.hpp>
50 #include <com/sun/star/xml/dom/NodeType.hpp>
51 #include <com/sun/star/xml/dom/events/XEventTarget.hpp>
52 #include <com/sun/star/xml/dom/events/XEvent.hpp>
53 #include <com/sun/star/xml/dom/DOMException.hpp>
54 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
55 #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
56 
57 
58 using ::rtl::OUString;
59 using ::rtl::OString;
60 using namespace sax_fastparser;
61 using namespace com::sun::star::uno;
62 using namespace com::sun::star::xml::sax;
63 using namespace com::sun::star::xml::dom;
64 using namespace com::sun::star::xml::dom::events;
65 using com::sun::star::lang::XUnoTunnel;
66 
67 
68 namespace DOM
69 {
70     struct Context
71     {
72         Context( const Reference< XFastDocumentHandler >& i_xHandler,
73                  const Reference< XFastTokenHandler >& i_xTokenHandler ) :
74             maNamespaces( 1, std::vector<Namespace>() ),
75             maNamespaceMap(101),
76             mxAttribList(new FastAttributeList(i_xTokenHandler)),
77             mxCurrentHandler(i_xHandler, UNO_QUERY_THROW),
78             mxDocHandler(i_xHandler),
79             mxTokenHandler(i_xTokenHandler)
80         {}
81 
82         struct Namespace
83         {
84             OString	    maPrefix;
85             sal_Int32	mnToken;
86             OUString	maNamespaceURL;
87 
88             const OString& getPrefix() const { return maPrefix; }
89         };
90 
91         typedef std::vector< std::vector<Namespace> > NamespaceVectorType;
92         typedef std::hash_map< OUString,
93                                sal_Int32,
94                                rtl::OUStringHash > NamespaceMapType;
95 
96         /// outer vector: xml context; inner vector: current NS
97         NamespaceVectorType                 maNamespaces;
98         NamespaceMapType                    maNamespaceMap;
99         ::rtl::Reference<FastAttributeList> mxAttribList;
100         Reference<XFastContextHandler>      mxCurrentHandler;
101         Reference<XFastDocumentHandler>     mxDocHandler;
102         Reference<XFastTokenHandler>        mxTokenHandler;
103     };
104 
105     void pushContext(Context& io_rContext);
106     void popContext(Context& io_rContext);
107 
108     sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName );
109     sal_Int32 getToken( const Context& rContext, const sal_Char* xName );
110 
111     /// add namespaces on this node to context
112     void addNamespaces(Context& io_rContext, xmlNodePtr pNode);
113 
114     class CDocument;
115 
116     class CNode : public cppu::WeakImplHelper3< XNode, XUnoTunnel, XEventTarget >
117     {
118         friend class CDocument;
119         friend class CElement;
120         friend class CAttributesMap;
121 
122     private:
123         bool m_bUnlinked; /// node has been removed from document
124 
125     protected:
126         NodeType const m_aNodeType;
127         /// libxml node; NB: not const, because invalidate may reset it to 0!
128         xmlNodePtr m_aNodePtr;
129 
130         ::rtl::Reference< CDocument > const m_xDocument;
131         ::osl::Mutex & m_rMutex;
132 
133         // for initialization by classes derived through ImplInheritanceHelper
134         CNode(CDocument const& rDocument, ::osl::Mutex const& rMutex,
135                 NodeType const& reNodeType, xmlNodePtr const& rpNode);
136         void invalidate();
137 
138         void dispatchSubtreeModified();
139 
140     public:
141 
142         virtual ~CNode();
143 
144         static CNode * GetImplementation(::com::sun::star::uno::Reference<
145                 ::com::sun::star::uno::XInterface> const& xNode);
146 
147         xmlNodePtr GetNodePtr() { return m_aNodePtr; }
148 
149         virtual CDocument & GetOwnerDocument();
150 
151         // recursively create SAX events
152         virtual void saxify(const Reference< XDocumentHandler >& i_xHandler);
153 
154         // recursively create SAX events
155         virtual void fastSaxify( Context& io_rContext );
156 
157         // constrains child relationship between nodes based on type
158         virtual bool IsChildTypeAllowed(NodeType const nodeType);
159 
160         // ---- DOM interfaces
161 
162         /**
163         Adds the node newChild to the end of the list of children of this node.
164         */
165         virtual Reference< XNode > SAL_CALL
166             appendChild(Reference< XNode > const& xNewChild)
167             throw (RuntimeException, DOMException);
168 
169         /**
170         Returns a duplicate of this node, i.e., serves as a generic copy
171         constructor for nodes.
172         */
173         virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep)
174             throw (RuntimeException);
175 
176         /**
177         A NamedNodeMap containing the attributes of this node
178         (if it is an Element) or null otherwise.
179         */
180         virtual Reference< XNamedNodeMap > SAL_CALL getAttributes()
181             throw (RuntimeException);
182 
183         /**
184         A NodeList that contains all children of this node.
185         */
186         virtual Reference< XNodeList > SAL_CALL getChildNodes()
187             throw (RuntimeException);
188 
189         /**
190         The first child of this node.
191         */
192         virtual Reference< XNode > SAL_CALL getFirstChild()
193             throw (RuntimeException);
194 
195         /**
196         The last child of this node.
197         */
198         virtual Reference< XNode > SAL_CALL getLastChild()
199             throw (RuntimeException);
200 
201         /**
202         Returns the local part of the qualified name of this node.
203         */
204         virtual OUString SAL_CALL getLocalName()
205             throw (RuntimeException);
206 
207         /**
208         The namespace URI of this node, or null if it is unspecified.
209         */
210         virtual OUString SAL_CALL getNamespaceURI()
211             throw (RuntimeException);
212 
213         /**
214         The node immediately following this node.
215         */
216         virtual Reference< XNode > SAL_CALL getNextSibling()
217             throw (RuntimeException);
218 
219         /**
220         The name of this node, depending on its type; see the table above.
221         -- virtual implemented by actual node types
222         */
223         virtual OUString SAL_CALL getNodeName()
224             throw (RuntimeException);
225 
226         /**
227         A code representing the type of the underlying object, as defined above.
228         */
229         virtual NodeType SAL_CALL getNodeType()
230             throw (RuntimeException);
231 
232         /**
233         The value of this node, depending on its type; see the table above.
234         -- virtual implemented by actual node types
235         */
236         virtual OUString SAL_CALL getNodeValue()
237             throw (RuntimeException);
238 
239         /**
240         The Document object associated with this node.
241         */
242         virtual Reference< XDocument > SAL_CALL getOwnerDocument()
243             throw (RuntimeException);
244 
245         /**
246         The parent of this node.
247         */
248         virtual Reference< XNode > SAL_CALL getParentNode()
249             throw (RuntimeException);
250 
251         /**
252         The namespace prefix of this node, or null if it is unspecified.
253         */
254         virtual OUString SAL_CALL getPrefix()
255             throw (RuntimeException);
256 
257         /**
258         The node immediately preceding this node.
259         */
260         virtual Reference< XNode > SAL_CALL getPreviousSibling()
261             throw (RuntimeException);
262 
263         /**
264         Returns whether this node (if it is an element) has any attributes.
265         */
266         virtual sal_Bool SAL_CALL hasAttributes()
267             throw (RuntimeException);
268 
269         /**
270         Returns whether this node has any children.
271         */
272         virtual sal_Bool SAL_CALL hasChildNodes()
273             throw (RuntimeException);
274 
275         /**
276         Inserts the node newChild before the existing child node refChild.
277         */
278         virtual Reference< XNode > SAL_CALL insertBefore(
279                 const Reference< XNode >& newChild, const Reference< XNode >& refChild)
280             throw (RuntimeException, DOMException);
281 
282         /**
283         Tests whether the DOM implementation implements a specific feature and
284         that feature is supported by this node.
285         */
286         virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver)
287             throw (RuntimeException);
288 
289         /**
290         Puts all Text nodes in the full depth of the sub-tree underneath this
291         Node, including attribute nodes, into a "normal" form where only structure
292         (e.g., elements, comments, processing instructions, CDATA sections, and
293         entity references) separates Text nodes, i.e., there are neither adjacent
294         Text nodes nor empty Text nodes.
295         */
296         virtual void SAL_CALL normalize()
297             throw (RuntimeException);
298 
299         /**
300         Removes the child node indicated by oldChild from the list of children,
301         and returns it.
302         */
303         virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild)
304             throw (RuntimeException, DOMException);
305 
306         /**
307         Replaces the child node oldChild with newChild in the list of children,
308         and returns the oldChild node.
309         */
310         virtual Reference< XNode > SAL_CALL replaceChild(
311                 const Reference< XNode >& newChild, const Reference< XNode >& oldChild)
312             throw (RuntimeException, DOMException);
313 
314         /**
315         The value of this node, depending on its type; see the table above.
316         */
317         virtual void SAL_CALL setNodeValue(const OUString& nodeValue)
318             throw (RuntimeException, DOMException);
319 
320         /**
321         The namespace prefix of this node, or null if it is unspecified.
322         */
323         virtual void SAL_CALL setPrefix(const OUString& prefix)
324             throw (RuntimeException, DOMException);
325 
326 
327         // --- XEventTarget
328         virtual void SAL_CALL addEventListener(const OUString& eventType,
329             const Reference< XEventListener >& listener,
330             sal_Bool useCapture)
331             throw (RuntimeException);
332 
333         virtual void SAL_CALL removeEventListener(const OUString& eventType,
334             const Reference< XEventListener >& listener,
335             sal_Bool useCapture)
336             throw (RuntimeException);
337 
338         virtual sal_Bool SAL_CALL dispatchEvent(const Reference< XEvent >& evt)
339             throw(RuntimeException, EventException);
340 
341         // --- XUnoTunnel
342         virtual ::sal_Int64 SAL_CALL
343             getSomething(Sequence< ::sal_Int8 > const& rId)
344             throw (RuntimeException);
345     };
346 
347     /// eliminate redundant namespace declarations
348     void nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent);
349 }
350 
351 #endif
352