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_DOCUMENT_HXX
21 : #define INCLUDED_UNOXML_SOURCE_DOM_DOCUMENT_HXX
22 :
23 : #include <set>
24 : #include <memory>
25 :
26 : #include <libxml/tree.h>
27 :
28 : #include <sal/types.h>
29 :
30 : #include <cppuhelper/implbase6.hxx>
31 :
32 : #include <com/sun/star/uno/Reference.h>
33 : #include <com/sun/star/beans/StringPair.hpp>
34 : #include <com/sun/star/xml/dom/XNode.hpp>
35 : #include <com/sun/star/xml/dom/XAttr.hpp>
36 : #include <com/sun/star/xml/dom/XElement.hpp>
37 : #include <com/sun/star/xml/dom/XDOMImplementation.hpp>
38 : #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
39 : #include <com/sun/star/xml/dom/events/XEvent.hpp>
40 : #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
41 : #include <com/sun/star/xml/sax/XFastSAXSerializable.hpp>
42 : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
43 : #include <com/sun/star/xml/sax/XFastDocumentHandler.hpp>
44 : #include <com/sun/star/io/XActiveDataSource.hpp>
45 : #include <com/sun/star/io/XActiveDataControl.hpp>
46 : #include <com/sun/star/io/XOutputStream.hpp>
47 : #include <com/sun/star/io/XStreamListener.hpp>
48 :
49 : #include "node.hxx"
50 :
51 : namespace DOM
52 : {
53 : namespace events {
54 : class CEventDispatcher;
55 : }
56 :
57 : class CElement;
58 :
59 : typedef ::cppu::ImplInheritanceHelper6<
60 : CNode, css::xml::dom::XDocument, css::xml::dom::events::XDocumentEvent,
61 : css::io::XActiveDataControl, css::io::XActiveDataSource,
62 : css::xml::sax::XSAXSerializable, css::xml::sax::XFastSAXSerializable>
63 : CDocument_Base;
64 :
65 : class CDocument
66 : : public CDocument_Base
67 : {
68 :
69 : private:
70 : /// this Mutex is used for synchronization of all UNO wrapper
71 : /// objects that belong to this document
72 : ::osl::Mutex m_Mutex;
73 : /// the libxml document: freed in destructor
74 : /// => all UNO wrapper objects must keep the CDocument alive
75 : xmlDocPtr const m_aDocPtr;
76 :
77 : // datacontrol/source state
78 : typedef std::set< css::uno::Reference< css::io::XStreamListener > > listenerlist_t;
79 : listenerlist_t m_streamListeners;
80 : css::uno::Reference< css::io::XOutputStream > m_rOutputStream;
81 :
82 : typedef std::map< const xmlNodePtr,
83 : ::std::pair< css::uno::WeakReference<css::xml::dom::XNode>, CNode* > > nodemap_t;
84 : nodemap_t m_NodeMap;
85 :
86 : ::std::unique_ptr<events::CEventDispatcher> const m_pEventDispatcher;
87 :
88 : explicit CDocument(xmlDocPtr const pDocPtr);
89 :
90 :
91 : public:
92 : /// factory: only way to create instance!
93 : static ::rtl::Reference<CDocument>
94 : CreateCDocument(xmlDocPtr const pDoc);
95 :
96 : virtual ~CDocument();
97 :
98 : // needed by CXPathAPI
99 410153 : ::osl::Mutex & GetMutex() { return m_Mutex; }
100 :
101 : events::CEventDispatcher & GetEventDispatcher();
102 : ::rtl::Reference< CElement > GetDocumentElement();
103 :
104 : /// get UNO wrapper instance for a libxml node
105 : ::rtl::Reference<CNode> GetCNode(
106 : xmlNodePtr const pNode, bool const bCreate = true);
107 : /// remove a UNO wrapper instance
108 : void RemoveCNode(xmlNodePtr const pNode, CNode const*const pCNode);
109 :
110 : virtual CDocument & GetOwnerDocument() SAL_OVERRIDE;
111 :
112 : virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler) SAL_OVERRIDE;
113 :
114 : virtual void fastSaxify( Context& rContext ) SAL_OVERRIDE;
115 :
116 : virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType) SAL_OVERRIDE;
117 :
118 : /**
119 : Creates an Attr of the given name.
120 : */
121 : virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL createAttribute(const OUString& name)
122 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
123 :
124 : /**
125 : Creates an attribute of the given qualified name and namespace URI.
126 : */
127 : virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL createAttributeNS(const OUString& namespaceURI, const OUString& qualifiedName)
128 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
129 :
130 : /**
131 : Creates a CDATASection node whose value is the specified string.
132 : */
133 : virtual css::uno::Reference< css::xml::dom::XCDATASection > SAL_CALL createCDATASection(const OUString& data)
134 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
135 :
136 : /**
137 : Creates a Comment node given the specified string.
138 : */
139 : virtual css::uno::Reference< css::xml::dom::XComment > SAL_CALL createComment(const OUString& data)
140 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
141 :
142 : /**
143 : Creates an empty DocumentFragment object.
144 : */
145 : virtual css::uno::Reference< css::xml::dom::XDocumentFragment > SAL_CALL createDocumentFragment()
146 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
147 :
148 : /**
149 : Creates an element of the type specified.
150 : */
151 : virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL createElement(const OUString& tagName)
152 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
153 :
154 : /**
155 : Creates an element of the given qualified name and namespace URI.
156 : */
157 : virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL createElementNS(const OUString& namespaceURI, const OUString& qualifiedName)
158 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
159 :
160 : /**
161 : Creates an EntityReference object.
162 : */
163 : virtual css::uno::Reference< css::xml::dom::XEntityReference > SAL_CALL createEntityReference(const OUString& name)
164 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
165 :
166 : /**
167 : Creates a ProcessingInstruction node given the specified name and
168 : data strings.
169 : */
170 : virtual css::uno::Reference< css::xml::dom::XProcessingInstruction > SAL_CALL createProcessingInstruction(
171 : const OUString& target, const OUString& data)
172 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
173 :
174 : /**
175 : Creates a Text node given the specified string.
176 : */
177 : virtual css::uno::Reference< css::xml::dom::XText > SAL_CALL createTextNode(const OUString& data)
178 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
179 :
180 : /**
181 : The Document Type Declaration (see DocumentType) associated with this
182 : document.
183 : */
184 : virtual css::uno::Reference< css::xml::dom::XDocumentType > SAL_CALL getDoctype()
185 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
186 :
187 : /**
188 : This is a convenience attribute that allows direct access to the child
189 : node that is the root element of the document.
190 : */
191 : virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL getDocumentElement()
192 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
193 :
194 : /**
195 : Returns the Element whose ID is given by elementId.
196 : */
197 : virtual css::uno::Reference< css::xml::dom::XElement > SAL_CALL getElementById(const OUString& elementId)
198 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
199 :
200 : /**
201 : Returns a NodeList of all the Elements with a given tag name in the
202 : order in which they are encountered in a preorder traversal of the
203 : Document tree.
204 : */
205 : virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagName(const OUString& tagname)
206 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
207 :
208 : /**
209 : Returns a NodeList of all the Elements with a given local name and
210 : namespace URI in the order in which they are encountered in a preorder
211 : traversal of the Document tree.
212 : */
213 : virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagNameNS(const OUString& namespaceURI, const OUString& localName)
214 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
215 :
216 : /**
217 : The DOMImplementation object that handles this document.
218 : */
219 : virtual css::uno::Reference< css::xml::dom::XDOMImplementation > SAL_CALL getImplementation()
220 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
221 :
222 : /**
223 : Imports a node from another document to this document.
224 : */
225 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL importNode(const css::uno::Reference< css::xml::dom::XNode >& importedNode, sal_Bool deep)
226 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE;
227 :
228 : // XDocumentEvent
229 : virtual css::uno::Reference< css::xml::dom::events::XEvent > SAL_CALL createEvent(const OUString& eventType) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
230 :
231 : // XActiveDataControl,
232 : // see http://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataControl.html
233 : virtual void SAL_CALL addListener(const css::uno::Reference< css::io::XStreamListener >& aListener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
234 : virtual void SAL_CALL removeListener(const css::uno::Reference< css::io::XStreamListener >& aListener ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
235 : virtual void SAL_CALL start() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
236 : virtual void SAL_CALL terminate() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
237 :
238 : // XActiveDataSource
239 : // see http://api.libreoffice.org/docs/common/ref/com/sun/star/io/XActiveDataSource.html
240 : virtual void SAL_CALL setOutputStream( const css::uno::Reference< css::io::XOutputStream >& aStream ) throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
241 : virtual css::uno::Reference< css::io::XOutputStream > SAL_CALL getOutputStream() throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
242 :
243 : // ---- resolve uno inheritance problems...
244 : // overrides for XNode base
245 : virtual OUString SAL_CALL getNodeName()
246 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
247 : virtual OUString SAL_CALL getNodeValue()
248 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
249 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL cloneNode(sal_Bool deep)
250 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
251 : // --- delegation for XNode base.
252 11033 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL appendChild(const css::uno::Reference< css::xml::dom::XNode >& newChild)
253 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
254 : {
255 11033 : return CNode::appendChild(newChild);
256 : }
257 1 : virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes()
258 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
259 : {
260 1 : return CNode::getAttributes();
261 : }
262 1 : virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getChildNodes()
263 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
264 : {
265 1 : return CNode::getChildNodes();
266 : }
267 9931 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getFirstChild()
268 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
269 : {
270 9931 : return CNode::getFirstChild();
271 : }
272 4 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getLastChild()
273 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
274 : {
275 4 : return CNode::getLastChild();
276 : }
277 1 : virtual OUString SAL_CALL getLocalName()
278 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
279 : {
280 1 : return CNode::getLocalName();
281 : }
282 1 : virtual OUString SAL_CALL getNamespaceURI()
283 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
284 : {
285 1 : return CNode::getNamespaceURI();
286 : }
287 1 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNextSibling()
288 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
289 : {
290 1 : return CNode::getNextSibling();
291 : }
292 699 : virtual css::xml::dom::NodeType SAL_CALL getNodeType()
293 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
294 : {
295 699 : return CNode::getNodeType();
296 : }
297 32572 : virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument()
298 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
299 : {
300 32572 : return CNode::getOwnerDocument();
301 : }
302 1 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getParentNode()
303 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
304 : {
305 1 : return CNode::getParentNode();
306 : }
307 1 : virtual OUString SAL_CALL getPrefix()
308 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
309 : {
310 1 : return CNode::getPrefix();
311 : }
312 1 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getPreviousSibling()
313 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
314 : {
315 1 : return CNode::getPreviousSibling();
316 : }
317 1 : virtual sal_Bool SAL_CALL hasAttributes()
318 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
319 : {
320 1 : return CNode::hasAttributes();
321 : }
322 4 : virtual sal_Bool SAL_CALL hasChildNodes()
323 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
324 : {
325 4 : return CNode::hasChildNodes();
326 : }
327 4 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL insertBefore(
328 : const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& refChild)
329 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
330 : {
331 4 : return CNode::insertBefore(newChild, refChild);
332 : }
333 1 : virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver)
334 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
335 : {
336 1 : return CNode::isSupported(feature, ver);
337 : }
338 1 : virtual void SAL_CALL normalize()
339 : throw (css::uno::RuntimeException, std::exception) SAL_OVERRIDE
340 : {
341 1 : CNode::normalize();
342 1 : }
343 3 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeChild(const css::uno::Reference< css::xml::dom::XNode >& oldChild)
344 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
345 : {
346 3 : return CNode::removeChild(oldChild);
347 : }
348 5 : virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL replaceChild(
349 : const css::uno::Reference< css::xml::dom::XNode >& newChild, const css::uno::Reference< css::xml::dom::XNode >& oldChild)
350 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
351 : {
352 5 : return CNode::replaceChild(newChild, oldChild);
353 : }
354 1 : virtual void SAL_CALL setNodeValue(const OUString& nodeValue)
355 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
356 : {
357 1 : return CNode::setNodeValue(nodeValue);
358 : }
359 1 : virtual void SAL_CALL setPrefix(const OUString& prefix)
360 : throw (css::uno::RuntimeException, css::xml::dom::DOMException, std::exception) SAL_OVERRIDE
361 : {
362 1 : return CNode::setPrefix(prefix);
363 : }
364 :
365 : // css::xml::sax::XSAXSerializable
366 : virtual void SAL_CALL serialize(
367 : const css::uno::Reference< css::xml::sax::XDocumentHandler >& i_xHandler,
368 : const css::uno::Sequence< css::beans::StringPair >& i_rNamespaces)
369 : throw (css::uno::RuntimeException, css::xml::sax::SAXException, std::exception) SAL_OVERRIDE;
370 :
371 : // css::xml::sax::XFastSAXSerializable
372 : virtual void SAL_CALL fastSerialize( const css::uno::Reference< css::xml::sax::XFastDocumentHandler >& handler,
373 : const css::uno::Reference< css::xml::sax::XFastTokenHandler >& tokenHandler,
374 : const css::uno::Sequence< css::beans::StringPair >& i_rNamespaces,
375 : const css::uno::Sequence< css::beans::Pair< OUString, sal_Int32 > >& namespaces )
376 : throw (css::xml::sax::SAXException, css::uno::RuntimeException, std::exception) SAL_OVERRIDE;
377 : };
378 : }
379 :
380 : #endif
381 :
382 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|