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