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