Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_UNOXML_SOURCE_DOM_NODE_HXX
21 : #define INCLUDED_UNOXML_SOURCE_DOM_NODE_HXX
22 :
23 : #include <libxml/tree.h>
24 :
25 : #include <sal/types.h>
26 : #include <rtl/ref.hxx>
27 : #include <rtl/string.hxx>
28 : #include <rtl/ustring.hxx>
29 :
30 : #include <cppuhelper/implbase3.hxx>
31 :
32 : #include <sax/fastattribs.hxx>
33 :
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>
46 :
47 : #include <boost/unordered_map.hpp>
48 :
49 : namespace DOM
50 : {
51 2720 : struct Context
52 : {
53 2720 : 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 2720 : mxAttribList(new sax_fastparser::FastAttributeList(i_xTokenHandler)),
58 : mxCurrentHandler(i_xHandler),
59 : mxDocHandler(i_xHandler),
60 5440 : mxTokenHandler(i_xTokenHandler)
61 2720 : {}
62 :
63 2815480 : struct Namespace
64 : {
65 : OString maPrefix;
66 : sal_Int32 mnToken;
67 : OUString maNamespaceURL;
68 :
69 644064 : const OString& getPrefix() const { return maPrefix; }
70 : };
71 :
72 : typedef std::vector< std::vector<Namespace> > NamespaceVectorType;
73 : typedef boost::unordered_map< OUString,
74 : sal_Int32,
75 : OUStringHash > NamespaceMapType;
76 :
77 : /// outer vector: xml context; inner vector: current NS
78 : NamespaceVectorType maNamespaces;
79 : NamespaceMapType maNamespaceMap;
80 : ::rtl::Reference<sax_fastparser::FastAttributeList> mxAttribList;
81 : css::uno::Reference<css::xml::sax::XFastContextHandler> mxCurrentHandler;
82 : css::uno::Reference<css::xml::sax::XFastDocumentHandler> mxDocHandler;
83 : css::uno::Reference<css::xml::sax::XFastTokenHandler> mxTokenHandler;
84 : };
85 :
86 : void pushContext(Context& io_rContext);
87 : void popContext(Context& io_rContext);
88 :
89 : sal_Int32 getTokenWithPrefix( const Context& rContext, const sal_Char* xPrefix, const sal_Char* xName );
90 : sal_Int32 getToken( const Context& rContext, const sal_Char* xName );
91 :
92 : /// add namespaces on this node to context
93 : void addNamespaces(Context& io_rContext, xmlNodePtr pNode);
94 :
95 : class CDocument;
96 :
97 : class CNode : public cppu::WeakImplHelper3< css::xml::dom::XNode, css::lang::XUnoTunnel, css::xml::dom::events::XEventTarget >
98 : {
99 : friend class CDocument;
100 : friend class CElement;
101 : friend class CAttributesMap;
102 :
103 : private:
104 : bool m_bUnlinked; /// node has been removed from document
105 :
106 : protected:
107 : css::xml::dom::NodeType const m_aNodeType;
108 : /// libxml node; NB: not const, because invalidate may reset it to 0!
109 : xmlNodePtr m_aNodePtr;
110 :
111 : ::rtl::Reference< CDocument > const m_xDocument;
112 : ::osl::Mutex & m_rMutex;
113 :
114 : // for initialization by classes derived through ImplInheritanceHelper
115 : CNode(CDocument const& rDocument, ::osl::Mutex const& rMutex,
116 : css::xml::dom::NodeType const& reNodeType, xmlNodePtr const& rpNode);
117 : void invalidate();
118 :
119 : void dispatchSubtreeModified();
120 :
121 : public:
122 :
123 : virtual ~CNode();
124 :
125 : static CNode * GetImplementation(css::uno::Reference<
126 : css::uno::XInterface> const& xNode);
127 :
128 642274 : xmlNodePtr GetNodePtr() { return m_aNodePtr; }
129 :
130 : virtual CDocument & GetOwnerDocument();
131 :
132 : // recursively create SAX events
133 : virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler);
134 :
135 : // recursively create SAX events
136 : virtual void fastSaxify( Context& io_rContext );
137 :
138 : // constrains child relationship between nodes based on type
139 : virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType);
140 :
141 : // ---- DOM interfaces
142 :
143 : /**
144 : Adds the node newChild to the end of the list of children of this node.
145 : */
146 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL
147 : appendChild(css::uno::Reference< css::xml::dom::XNode > const& xNewChild)
148 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
149 :
150 : /**
151 : Returns a duplicate of this node, i.e., serves as a generic copy
152 : constructor for nodes.
153 : */
154 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL cloneNode(sal_Bool deep)
155 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
156 :
157 : /**
158 : A NamedNodeMap containing the attributes of this node
159 : (if it is an Element) or null otherwise.
160 : */
161 : virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes()
162 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
163 :
164 : /**
165 : A NodeList that contains all children of this node.
166 : */
167 : virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getChildNodes()
168 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
169 :
170 : /**
171 : The first child of this node.
172 : */
173 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getFirstChild()
174 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
175 :
176 : /**
177 : The last child of this node.
178 : */
179 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getLastChild()
180 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
181 :
182 : /**
183 : Returns the local part of the qualified name of this node.
184 : */
185 : virtual OUString SAL_CALL getLocalName()
186 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
187 :
188 : /**
189 : The namespace URI of this node, or null if it is unspecified.
190 : */
191 : virtual OUString SAL_CALL getNamespaceURI()
192 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
193 :
194 : /**
195 : The node immediately following this node.
196 : */
197 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNextSibling()
198 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
199 :
200 : /**
201 : The name of this node, depending on its type; see the table above.
202 : -- virtual implemented by actual node types
203 : */
204 : virtual OUString SAL_CALL getNodeName()
205 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
206 :
207 : /**
208 : A code representing the type of the underlying object, as defined above.
209 : */
210 : virtual css::xml::dom::NodeType SAL_CALL getNodeType()
211 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
212 :
213 : /**
214 : The value of this node, depending on its type; see the table above.
215 : -- virtual implemented by actual node types
216 : */
217 : virtual OUString SAL_CALL getNodeValue()
218 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
219 :
220 : /**
221 : The Document object associated with this node.
222 : */
223 : virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument()
224 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
225 :
226 : /**
227 : The parent of this node.
228 : */
229 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getParentNode()
230 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
231 :
232 : /**
233 : The namespace prefix of this node, or null if it is unspecified.
234 : */
235 : virtual OUString SAL_CALL getPrefix()
236 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
237 :
238 : /**
239 : The node immediately preceding this node.
240 : */
241 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getPreviousSibling()
242 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
243 :
244 : /**
245 : Returns whether this node (if it is an element) has any attributes.
246 : */
247 : virtual sal_Bool SAL_CALL hasAttributes()
248 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
249 :
250 : /**
251 : Returns whether this node has any children.
252 : */
253 : virtual sal_Bool SAL_CALL hasChildNodes()
254 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
255 :
256 : /**
257 : Inserts the node newChild before the existing child node refChild.
258 : */
259 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL insertBefore(
260 : const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& refChild)
261 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
262 :
263 : /**
264 : Tests whether the DOM implementation implements a specific feature and
265 : that feature is supported by this node.
266 : */
267 : virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver)
268 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
269 :
270 : /**
271 : Puts all Text nodes in the full depth of the sub-tree underneath this
272 : Node, including attribute nodes, into a "normal" form where only structure
273 : (e.g., elements, comments, processing instructions, CDATA sections, and
274 : entity references) separates Text nodes, i.e., there are neither adjacent
275 : Text nodes nor empty Text nodes.
276 : */
277 : virtual void SAL_CALL normalize()
278 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
279 :
280 : /**
281 : Removes the child node indicated by oldChild from the list of children,
282 : and returns it.
283 : */
284 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeChild(const css::uno::Reference< css::xml::dom::XNode >& oldChild)
285 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
286 :
287 : /**
288 : Replaces the child node oldChild with newChild in the list of children,
289 : and returns the oldChild node.
290 : */
291 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL replaceChild(
292 : const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& oldChild)
293 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
294 :
295 : /**
296 : The value of this node, depending on its type; see the table above.
297 : */
298 : virtual void SAL_CALL setNodeValue(const OUString& nodeValue)
299 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
300 :
301 : /**
302 : The namespace prefix of this node, or null if it is unspecified.
303 : */
304 : virtual void SAL_CALL setPrefix(const OUString& prefix)
305 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
306 :
307 :
308 : // --- XEventTarget
309 : virtual void SAL_CALL addEventListener(const OUString& eventType,
310 : const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
311 : sal_Bool useCapture)
312 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
313 :
314 : virtual void SAL_CALL removeEventListener(const OUString& eventType,
315 : const css::uno::Reference< css::xml::dom::events::XEventListener >& listener,
316 : sal_Bool useCapture)
317 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
318 :
319 : virtual sal_Bool SAL_CALL dispatchEvent(const css::uno::Reference< css::xml::dom::events::XEvent >& evt)
320 : throw(css::uno::RuntimeException, css::xml::dom::events::EventException, std::exception) SAL_OVERRIDE;
321 :
322 : // --- XUnoTunnel
323 : virtual ::sal_Int64 SAL_CALL
324 : getSomething(css::uno::Sequence< ::sal_Int8 > const& rId)
325 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
326 : };
327 :
328 : /// eliminate redundant namespace declarations
329 : void nscleanup(const xmlNodePtr aNode, const xmlNodePtr aParent);
330 : }
331 :
332 : #endif
333 :
334 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|