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