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 137 /** 138 Creates an attribute of the given qualified name and namespace URI. 139 */ 140 virtual Reference< XAttr > SAL_CALL createAttributeNS(const OUString& namespaceURI, const OUString& qualifiedName); 141 142 /** 143 Creates a CDATASection node whose value is the specified string. 144 */ 145 virtual Reference< XCDATASection > SAL_CALL createCDATASection(const OUString& data); 146 147 /** 148 Creates a Comment node given the specified string. 149 */ 150 virtual Reference< XComment > SAL_CALL createComment(const OUString& data); 151 152 /** 153 Creates an empty DocumentFragment object. 154 */ 155 virtual Reference< XDocumentFragment > SAL_CALL createDocumentFragment(); 156 157 /** 158 Creates an element of the type specified. 159 */ 160 virtual Reference< XElement > SAL_CALL createElement(const OUString& tagName); 161 162 /** 163 Creates an element of the given qualified name and namespace URI. 164 */ 165 virtual Reference< XElement > SAL_CALL createElementNS(const OUString& namespaceURI, const OUString& qualifiedName); 166 167 /** 168 Creates an EntityReference object. 169 */ 170 virtual Reference< XEntityReference > SAL_CALL createEntityReference(const OUString& name); 171 172 /** 173 Creates a ProcessingInstruction node given the specified name and 174 data strings. 175 */ 176 virtual Reference< XProcessingInstruction > SAL_CALL createProcessingInstruction( 177 const OUString& target, const OUString& data); 178 179 /** 180 Creates a Text node given the specified string. 181 */ 182 virtual Reference< XText > SAL_CALL createTextNode(const OUString& data); 183 184 /** 185 The Document Type Declaration (see DocumentType) associated with this 186 document. 187 */ 188 virtual Reference< XDocumentType > SAL_CALL getDoctype(); 189 190 /** 191 This is a convenience attribute that allows direct access to the child 192 node that is the root element of the document. 193 */ 194 virtual Reference< XElement > SAL_CALL getDocumentElement(); 195 196 /** 197 Returns the Element whose ID is given by elementId. 198 */ 199 virtual Reference< XElement > SAL_CALL getElementById(const OUString& elementId); 200 201 /** 202 Returns a NodeList of all the Elements with a given tag name in the 203 order in which they are encountered in a preorder traversal of the 204 Document tree. 205 */ 206 virtual Reference< XNodeList > SAL_CALL getElementsByTagName(const OUString& tagname); 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 Reference< XNodeList > SAL_CALL getElementsByTagNameNS(const OUString& namespaceURI, const OUString& localName); 214 215 /** 216 The DOMImplementation object that handles this document. 217 */ 218 virtual Reference< XDOMImplementation > SAL_CALL getImplementation(); 219 220 /** 221 Imports a node from another document to this document. 222 */ 223 virtual Reference< XNode > SAL_CALL importNode(const Reference< XNode >& importedNode, sal_Bool deep); 224 225 // XDocumentEvent 226 virtual Reference< XEvent > SAL_CALL createEvent(const OUString& eventType); 227 228 // XActiveDataControl, 229 // see http://api.openoffice.org/docs/common/ref/com/sun/star/io/XActiveDataControl.html 230 virtual void SAL_CALL addListener(const Reference< XStreamListener >& aListener ); 231 virtual void SAL_CALL removeListener(const Reference< XStreamListener >& aListener ); 232 virtual void SAL_CALL start(); 233 virtual void SAL_CALL terminate(); 234 235 // XActiveDataSource 236 // see http://api.openoffice.org/docs/common/ref/com/sun/star/io/XActiveDataSource.html 237 virtual void SAL_CALL setOutputStream( const Reference< XOutputStream >& aStream ); 238 virtual Reference< XOutputStream > SAL_CALL getOutputStream(); 239 240 // ---- resolve uno inheritance problems... 241 // overrides for XNode base 242 virtual OUString SAL_CALL getNodeName(); 243 virtual OUString SAL_CALL getNodeValue(); 244 virtual Reference< XNode > SAL_CALL cloneNode(sal_Bool deep); 245 // --- delegation for XNde base. appendChild(const Reference<XNode> & newChild)246 virtual Reference< XNode > SAL_CALL appendChild(const Reference< XNode >& newChild) 247 { 248 return CNode::appendChild(newChild); 249 } getAttributes()250 virtual Reference< XNamedNodeMap > SAL_CALL getAttributes() 251 { 252 return CNode::getAttributes(); 253 } getChildNodes()254 virtual Reference< XNodeList > SAL_CALL getChildNodes() 255 { 256 return CNode::getChildNodes(); 257 } getFirstChild()258 virtual Reference< XNode > SAL_CALL getFirstChild() 259 { 260 return CNode::getFirstChild(); 261 } getLastChild()262 virtual Reference< XNode > SAL_CALL getLastChild() 263 { 264 return CNode::getLastChild(); 265 } getLocalName()266 virtual OUString SAL_CALL getLocalName() 267 { 268 return CNode::getLocalName(); 269 } getNamespaceURI()270 virtual OUString SAL_CALL getNamespaceURI() 271 { 272 return CNode::getNamespaceURI(); 273 } getNextSibling()274 virtual Reference< XNode > SAL_CALL getNextSibling() 275 { 276 return CNode::getNextSibling(); 277 } getNodeType()278 virtual NodeType SAL_CALL getNodeType() 279 { 280 return CNode::getNodeType(); 281 } getOwnerDocument()282 virtual Reference< XDocument > SAL_CALL getOwnerDocument() 283 { 284 return CNode::getOwnerDocument(); 285 } getParentNode()286 virtual Reference< XNode > SAL_CALL getParentNode() 287 { 288 return CNode::getParentNode(); 289 } getPrefix()290 virtual OUString SAL_CALL getPrefix() 291 { 292 return CNode::getPrefix(); 293 } getPreviousSibling()294 virtual Reference< XNode > SAL_CALL getPreviousSibling() 295 { 296 return CNode::getPreviousSibling(); 297 } hasAttributes()298 virtual sal_Bool SAL_CALL hasAttributes() 299 { 300 return CNode::hasAttributes(); 301 } hasChildNodes()302 virtual sal_Bool SAL_CALL hasChildNodes() 303 { 304 return CNode::hasChildNodes(); 305 } insertBefore(const Reference<XNode> & newChild,const Reference<XNode> & refChild)306 virtual Reference< XNode > SAL_CALL insertBefore( 307 const Reference< XNode >& newChild, const Reference< XNode >& refChild) 308 { 309 return CNode::insertBefore(newChild, refChild); 310 } isSupported(const OUString & feature,const OUString & ver)311 virtual sal_Bool SAL_CALL isSupported(const OUString& feature, const OUString& ver) 312 { 313 return CNode::isSupported(feature, ver); 314 } normalize()315 virtual void SAL_CALL normalize() 316 { 317 CNode::normalize(); 318 } removeChild(const Reference<XNode> & oldChild)319 virtual Reference< XNode > SAL_CALL removeChild(const Reference< XNode >& oldChild) 320 { 321 return CNode::removeChild(oldChild); 322 } replaceChild(const Reference<XNode> & newChild,const Reference<XNode> & oldChild)323 virtual Reference< XNode > SAL_CALL replaceChild( 324 const Reference< XNode >& newChild, const Reference< XNode >& oldChild) 325 { 326 return CNode::replaceChild(newChild, oldChild); 327 } setNodeValue(const OUString & nodeValue)328 virtual void SAL_CALL setNodeValue(const OUString& nodeValue) 329 { 330 return CNode::setNodeValue(nodeValue); 331 } setPrefix(const OUString & prefix)332 virtual void SAL_CALL setPrefix(const OUString& prefix) 333 { 334 return CNode::setPrefix(prefix); 335 } 336 337 // ::com::sun::star::xml::sax::XSAXSerializable 338 virtual void SAL_CALL serialize( 339 const Reference< XDocumentHandler >& i_xHandler, 340 const Sequence< beans::StringPair >& i_rNamespaces); 341 342 // ::com::sun::star::xml::sax::XFastSAXSerializable 343 virtual void SAL_CALL fastSerialize( const Reference< XFastDocumentHandler >& handler, 344 const Reference< XFastTokenHandler >& tokenHandler, 345 const Sequence< beans::StringPair >& i_rNamespaces, 346 const Sequence< beans::Pair< rtl::OUString, sal_Int32 > >& namespaces ); 347 }; 348 } 349 350 #endif 351