1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/uno/Sequence.h>
22 #include "document.hxx"
24 #include "element.hxx"
25 #include "cdatasection.hxx"
26 #include "documentfragment.hxx"
28 #include "comment.hxx"
29 #include "processinginstruction.hxx"
30 #include "entityreference.hxx"
31 #include "documenttype.hxx"
32 #include "elementlist.hxx"
33 #include "domimplementation.hxx"
35 #include "notation.hxx"
38 #include <mutationevent.hxx>
39 #include <uievent.hxx>
40 #include <mouseevent.hxx>
41 #include <eventdispatcher.hxx>
45 #include <osl/diagnose.h>
47 #include <com/sun/star/xml/sax/FastToken.hpp>
48 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
51 using namespace css::io
;
52 using namespace css::uno
;
53 using namespace css::xml::dom
;
54 using namespace css::xml::dom::events
;
55 using namespace css::xml::sax
;
59 static xmlNodePtr
lcl_getDocumentType(xmlDocPtr
const i_pDocument
)
62 xmlNodePtr cur
= i_pDocument
->children
;
63 while (cur
!= nullptr)
65 if ((cur
->type
== XML_DOCUMENT_TYPE_NODE
) ||
66 (cur
->type
== XML_DTD_NODE
)) {
73 /// get the pointer to the root element node of the document
74 static xmlNodePtr
lcl_getDocumentRootPtr(xmlDocPtr
const i_pDocument
)
76 // find the document element
77 xmlNodePtr cur
= i_pDocument
->children
;
78 while (cur
!= nullptr)
80 if (cur
->type
== XML_ELEMENT_NODE
)
87 CDocument::CDocument(xmlDocPtr
const pDoc
)
88 : CDocument_Base(*this, m_Mutex
,
89 NodeType_DOCUMENT_NODE
, reinterpret_cast<xmlNodePtr
>(pDoc
))
92 , m_pEventDispatcher(new events::CEventDispatcher
)
96 ::rtl::Reference
<CDocument
> CDocument::CreateCDocument(xmlDocPtr
const pDoc
)
98 ::rtl::Reference
<CDocument
> const xDoc(new CDocument(pDoc
));
99 // add the doc itself to its nodemap!
100 xDoc
->m_NodeMap
.emplace(
101 reinterpret_cast<xmlNodePtr
>(pDoc
),
103 WeakReference
<XNode
>(static_cast<XDocument
*>(xDoc
.get())),
108 CDocument::~CDocument()
110 ::osl::MutexGuard
const g(m_Mutex
);
112 // node map must be empty now, otherwise CDocument must not die!
113 for (const auto& rEntry
: m_NodeMap
)
115 Reference
<XNode
> const xNode(rEntry
.second
.first
);
116 OSL_ENSURE(!xNode
.is(),
117 "CDocument::~CDocument(): ERROR: live node in document node map!");
120 xmlFreeDoc(m_aDocPtr
);
124 events::CEventDispatcher
& CDocument::GetEventDispatcher()
126 return *m_pEventDispatcher
;
129 ::rtl::Reference
< CElement
> CDocument::GetDocumentElement()
131 xmlNodePtr
const pNode
= lcl_getDocumentRootPtr(m_aDocPtr
);
132 ::rtl::Reference
< CElement
> const xRet(
133 dynamic_cast<CElement
*>(GetCNode(pNode
).get()));
138 CDocument::RemoveCNode(xmlNodePtr
const pNode
, CNode
const*const pCNode
)
140 nodemap_t::iterator
const i
= m_NodeMap
.find(pNode
);
141 if (i
!= m_NodeMap
.end()) {
142 // #i113681# consider this scenario:
144 // T2 calls getCNode: lookup will find i->second->first invalid
145 // so a new CNode is created and inserted
146 // T1 calls removeCNode: i->second->second now points to a
147 // different CNode instance!
149 // check that the CNode is the right one
150 CNode
*const pCurrent
= i
->second
.second
;
151 if (pCurrent
== pCNode
) {
157 /** NB: this is the CNode factory.
158 it is the only place where CNodes may be instantiated.
159 all CNodes must be registered at the m_NodeMap.
161 ::rtl::Reference
<CNode
>
162 CDocument::GetCNode(xmlNodePtr
const pNode
, bool const bCreate
)
164 if (nullptr == pNode
) {
167 //check whether there is already an instance for this node
168 nodemap_t::const_iterator
const i
= m_NodeMap
.find(pNode
);
169 if (i
!= m_NodeMap
.end()) {
170 // #i113681# check that the CNode is still alive
171 uno::Reference
<XNode
> const xNode(i
->second
.first
);
174 ::rtl::Reference
<CNode
> ret(i
->second
.second
);
175 OSL_ASSERT(ret
.is());
180 if (!bCreate
) { return nullptr; }
182 // there is not yet an instance wrapping this node,
183 // create it and store it in the map
185 ::rtl::Reference
<CNode
> pCNode
;
188 case XML_ELEMENT_NODE
:
189 // m_aNodeType = NodeType::ELEMENT_NODE;
190 pCNode
= new CElement(*this, m_Mutex
, pNode
);
193 // m_aNodeType = NodeType::TEXT_NODE;
194 pCNode
= new CText(*this, m_Mutex
, pNode
);
196 case XML_CDATA_SECTION_NODE
:
197 // m_aNodeType = NodeType::CDATA_SECTION_NODE;
198 pCNode
= new CCDATASection(*this, m_Mutex
, pNode
);
200 case XML_ENTITY_REF_NODE
:
201 // m_aNodeType = NodeType::ENTITY_REFERENCE_NODE;
202 pCNode
= new CEntityReference(*this, m_Mutex
, pNode
);
204 case XML_ENTITY_NODE
:
205 // m_aNodeType = NodeType::ENTITY_NODE;
206 pCNode
= new CEntity(*this, m_Mutex
, reinterpret_cast<xmlEntityPtr
>(pNode
));
209 // m_aNodeType = NodeType::PROCESSING_INSTRUCTION_NODE;
210 pCNode
= new CProcessingInstruction(*this, m_Mutex
, pNode
);
212 case XML_COMMENT_NODE
:
213 // m_aNodeType = NodeType::COMMENT_NODE;
214 pCNode
= new CComment(*this, m_Mutex
, pNode
);
216 case XML_DOCUMENT_NODE
:
217 // m_aNodeType = NodeType::DOCUMENT_NODE;
218 OSL_ENSURE(false, "CDocument::GetCNode is not supposed to"
219 " create a CDocument!!!");
220 pCNode
= new CDocument(reinterpret_cast<xmlDocPtr
>(pNode
));
222 case XML_DOCUMENT_TYPE_NODE
:
224 // m_aNodeType = NodeType::DOCUMENT_TYPE_NODE;
225 pCNode
= new CDocumentType(*this, m_Mutex
, reinterpret_cast<xmlDtdPtr
>(pNode
));
227 case XML_DOCUMENT_FRAG_NODE
:
228 // m_aNodeType = NodeType::DOCUMENT_FRAGMENT_NODE;
229 pCNode
= new CDocumentFragment(*this, m_Mutex
, pNode
);
231 case XML_NOTATION_NODE
:
232 // m_aNodeType = NodeType::NOTATION_NODE;
233 pCNode
= new CNotation(*this, m_Mutex
, reinterpret_cast<xmlNotationPtr
>(pNode
));
235 case XML_ATTRIBUTE_NODE
:
236 // m_aNodeType = NodeType::ATTRIBUTE_NODE;
237 pCNode
= new CAttr(*this, m_Mutex
, reinterpret_cast<xmlAttrPtr
>(pNode
));
239 // unsupported node types
240 case XML_HTML_DOCUMENT_NODE
:
241 case XML_ELEMENT_DECL
:
242 case XML_ATTRIBUTE_DECL
:
243 case XML_ENTITY_DECL
:
244 case XML_NAMESPACE_DECL
:
249 if (pCNode
!= nullptr) {
250 bool const bInserted
= m_NodeMap
.emplace(
252 ::std::make_pair(WeakReference
<XNode
>(pCNode
.get()), pCNode
.get())
254 OSL_ASSERT(bInserted
);
256 // if insertion failed, delete new instance and return null
261 OSL_ENSURE(pCNode
.is(), "no node produced during CDocument::GetCNode!");
266 CDocument
& CDocument::GetOwnerDocument()
271 void CDocument::saxify(const Reference
< XDocumentHandler
>& i_xHandler
)
273 i_xHandler
->startDocument();
274 for (xmlNodePtr pChild
= m_aNodePtr
->children
;
275 pChild
!= nullptr; pChild
= pChild
->next
) {
276 ::rtl::Reference
<CNode
> const pNode
= GetCNode(pChild
);
277 OSL_ENSURE(pNode
!= nullptr, "CNode::get returned 0");
278 pNode
->saxify(i_xHandler
);
280 i_xHandler
->endDocument();
283 void CDocument::fastSaxify( Context
& rContext
)
285 rContext
.mxDocHandler
->startDocument();
286 for (xmlNodePtr pChild
= m_aNodePtr
->children
;
287 pChild
!= nullptr; pChild
= pChild
->next
) {
288 ::rtl::Reference
<CNode
> const pNode
= GetCNode(pChild
);
289 OSL_ENSURE(pNode
!= nullptr, "CNode::get returned 0");
290 pNode
->fastSaxify(rContext
);
292 rContext
.mxDocHandler
->endDocument();
295 bool CDocument::IsChildTypeAllowed(NodeType
const nodeType
)
298 case NodeType_PROCESSING_INSTRUCTION_NODE
:
299 case NodeType_COMMENT_NODE
:
301 case NodeType_ELEMENT_NODE
:
302 // there may be only one!
303 return nullptr == lcl_getDocumentRootPtr(m_aDocPtr
);
304 case NodeType_DOCUMENT_TYPE_NODE
:
305 // there may be only one!
306 return nullptr == lcl_getDocumentType(m_aDocPtr
);
313 void SAL_CALL
CDocument::addListener(const Reference
< XStreamListener
>& aListener
)
315 ::osl::MutexGuard
const g(m_Mutex
);
317 m_streamListeners
.insert(aListener
);
320 void SAL_CALL
CDocument::removeListener(const Reference
< XStreamListener
>& aListener
)
322 ::osl::MutexGuard
const g(m_Mutex
);
324 m_streamListeners
.erase(aListener
);
327 // IO context functions for libxml2 interaction
329 Reference
< XOutputStream
> stream
;
330 bool const allowClose
;
335 // int xmlOutputWriteCallback (void * context, const char * buffer, int len)
336 static int writeCallback(void *context
, const char* buffer
, int len
){
337 // create a sequence and write it to the stream
338 IOContext
*pContext
= static_cast<IOContext
*>(context
);
339 Sequence
<sal_Int8
> bs(reinterpret_cast<const sal_Int8
*>(buffer
), len
);
340 pContext
->stream
->writeBytes(bs
);
345 //int xmlOutputCloseCallback (void * context)
346 static int closeCallback(void *context
)
348 IOContext
*pContext
= static_cast<IOContext
*>(context
);
349 if (pContext
->allowClose
) {
350 pContext
->stream
->closeOutput();
356 void SAL_CALL
CDocument::start()
358 listenerlist_t streamListeners
;
360 ::osl::MutexGuard
const g(m_Mutex
);
362 if (! m_rOutputStream
.is()) { throw RuntimeException(); }
363 streamListeners
= m_streamListeners
;
366 // notify listeners about start
367 for (const Reference
< XStreamListener
>& aListener
: streamListeners
) {
368 aListener
->started();
372 ::osl::MutexGuard
const g(m_Mutex
);
374 // check again! could have been reset...
375 if (! m_rOutputStream
.is()) { throw RuntimeException(); }
377 // setup libxml IO and write data to output stream
378 IOContext ioctx
= {m_rOutputStream
, false};
379 xmlOutputBufferPtr pOut
= xmlOutputBufferCreateIO(
380 writeCallback
, closeCallback
, &ioctx
, nullptr);
381 xmlSaveFileTo(pOut
, m_aNodePtr
->doc
, nullptr);
385 for (const Reference
< XStreamListener
>& aListener
: streamListeners
) {
390 void SAL_CALL
CDocument::terminate()
395 void SAL_CALL
CDocument::setOutputStream( const Reference
< XOutputStream
>& aStream
)
397 ::osl::MutexGuard
const g(m_Mutex
);
399 m_rOutputStream
= aStream
;
402 Reference
< XOutputStream
> SAL_CALL
CDocument::getOutputStream()
404 ::osl::MutexGuard
const g(m_Mutex
);
406 return m_rOutputStream
;
409 // Creates an Attr of the given name.
410 Reference
< XAttr
> SAL_CALL
CDocument::createAttribute(const OUString
& name
)
412 ::osl::MutexGuard
const g(m_Mutex
);
414 OString o1
= OUStringToOString(name
, RTL_TEXTENCODING_UTF8
);
415 xmlChar
const *pName
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
416 xmlAttrPtr
const pAttr
= xmlNewDocProp(m_aDocPtr
, pName
, nullptr);
417 ::rtl::Reference
< CAttr
> const pCAttr(
418 dynamic_cast< CAttr
* >(GetCNode(
419 reinterpret_cast<xmlNodePtr
>(pAttr
)).get()));
420 if (!pCAttr
.is()) { throw RuntimeException(); }
421 pCAttr
->m_bUnlinked
= true;
425 // Creates an attribute of the given qualified name and namespace URI.
426 Reference
< XAttr
> SAL_CALL
CDocument::createAttributeNS(
427 const OUString
& ns
, const OUString
& qname
)
429 ::osl::MutexGuard
const g(m_Mutex
);
431 // libxml does not allow a NS definition to be attached to an
432 // attribute node - which is a good thing, since namespaces are
433 // only defined as parts of element nodes
434 // thus the namespace data is stored in CAttr::m_pNamespace
435 sal_Int32 i
= qname
.indexOf(':');
436 OString oPrefix
, oName
, oUri
;
439 oPrefix
= OUStringToOString(qname
.copy(0, i
), RTL_TEXTENCODING_UTF8
);
440 oName
= OUStringToOString(qname
.copy(i
+1), RTL_TEXTENCODING_UTF8
);
444 oName
= OUStringToOString(qname
, RTL_TEXTENCODING_UTF8
);
446 oUri
= OUStringToOString(ns
, RTL_TEXTENCODING_UTF8
);
447 xmlAttrPtr
const pAttr
= xmlNewDocProp(m_aDocPtr
,
448 reinterpret_cast<xmlChar
const*>(oName
.getStr()), nullptr);
449 ::rtl::Reference
< CAttr
> const pCAttr(
450 dynamic_cast< CAttr
* >(GetCNode(
451 reinterpret_cast<xmlNodePtr
>(pAttr
)).get()));
452 if (!pCAttr
.is()) { throw RuntimeException(); }
453 // store the namespace data!
454 pCAttr
->m_pNamespace
.reset( new stringpair_t(oUri
, oPrefix
) );
455 pCAttr
->m_bUnlinked
= true;
460 // Creates a CDATASection node whose value is the specified string.
461 Reference
< XCDATASection
> SAL_CALL
CDocument::createCDATASection(const OUString
& data
)
463 ::osl::MutexGuard
const g(m_Mutex
);
466 OUStringToOString(data
, RTL_TEXTENCODING_UTF8
));
467 xmlChar
const*const pData
=
468 reinterpret_cast<xmlChar
const*>(oData
.getStr());
469 xmlNodePtr
const pText
=
470 xmlNewCDataBlock(m_aDocPtr
, pData
, oData
.getLength());
471 Reference
< XCDATASection
> const xRet(
472 static_cast< XNode
* >(GetCNode(pText
).get()),
477 // Creates a Comment node given the specified string.
478 Reference
< XComment
> SAL_CALL
CDocument::createComment(const OUString
& data
)
480 ::osl::MutexGuard
const g(m_Mutex
);
482 OString o1
= OUStringToOString(data
, RTL_TEXTENCODING_UTF8
);
483 xmlChar
const *pData
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
484 xmlNodePtr pComment
= xmlNewDocComment(m_aDocPtr
, pData
);
485 Reference
< XComment
> const xRet(
486 static_cast< XNode
* >(GetCNode(pComment
).get()),
491 //Creates an empty DocumentFragment object.
492 Reference
< XDocumentFragment
> SAL_CALL
CDocument::createDocumentFragment()
494 ::osl::MutexGuard
const g(m_Mutex
);
496 xmlNodePtr pFrag
= xmlNewDocFragment(m_aDocPtr
);
497 Reference
< XDocumentFragment
> const xRet(
498 static_cast< XNode
* >(GetCNode(pFrag
).get()),
503 // Creates an element of the type specified.
504 Reference
< XElement
> SAL_CALL
CDocument::createElement(const OUString
& tagName
)
506 ::osl::MutexGuard
const g(m_Mutex
);
508 OString o1
= OUStringToOString(tagName
, RTL_TEXTENCODING_UTF8
);
509 xmlChar
const *pName
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
510 xmlNodePtr
const pNode
= xmlNewDocNode(m_aDocPtr
, nullptr, pName
, nullptr);
511 Reference
< XElement
> const xRet(
512 static_cast< XNode
* >(GetCNode(pNode
).get()),
517 // Creates an element of the given qualified name and namespace URI.
518 Reference
< XElement
> SAL_CALL
CDocument::createElementNS(
519 const OUString
& ns
, const OUString
& qname
)
521 ::osl::MutexGuard
const g(m_Mutex
);
523 sal_Int32 i
= qname
.indexOf(':');
524 if (ns
.isEmpty()) throw RuntimeException();
525 xmlChar
const *pPrefix
;
526 xmlChar
const *pName
;
529 o1
= OUStringToOString(qname
.copy(0, i
), RTL_TEXTENCODING_UTF8
);
530 pPrefix
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
531 o2
= OUStringToOString(qname
.copy(i
+1), RTL_TEXTENCODING_UTF8
);
532 pName
= reinterpret_cast<xmlChar
const *>(o2
.getStr());
535 pPrefix
= reinterpret_cast<xmlChar
const *>("");
536 o2
= OUStringToOString(qname
, RTL_TEXTENCODING_UTF8
);
537 pName
= reinterpret_cast<xmlChar
const *>(o2
.getStr());
539 o3
= OUStringToOString(ns
, RTL_TEXTENCODING_UTF8
);
540 xmlChar
const *pUri
= reinterpret_cast<xmlChar
const *>(o3
.getStr());
542 // xmlNsPtr aNsPtr = xmlNewReconciledNs?
543 // xmlNsPtr aNsPtr = xmlNewGlobalNs?
544 xmlNodePtr
const pNode
= xmlNewDocNode(m_aDocPtr
, nullptr, pName
, nullptr);
545 xmlNsPtr
const pNs
= xmlNewNs(pNode
, pUri
, pPrefix
);
546 xmlSetNs(pNode
, pNs
);
547 Reference
< XElement
> const xRet(
548 static_cast< XNode
* >(GetCNode(pNode
).get()),
553 //Creates an EntityReference object.
554 Reference
< XEntityReference
> SAL_CALL
CDocument::createEntityReference(const OUString
& name
)
556 ::osl::MutexGuard
const g(m_Mutex
);
558 OString o1
= OUStringToOString(name
, RTL_TEXTENCODING_UTF8
);
559 xmlChar
const *pName
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
560 xmlNodePtr
const pNode
= xmlNewReference(m_aDocPtr
, pName
);
561 Reference
< XEntityReference
> const xRet(
562 static_cast< XNode
* >(GetCNode(pNode
).get()),
567 // Creates a ProcessingInstruction node given the specified name and
569 Reference
< XProcessingInstruction
> SAL_CALL
CDocument::createProcessingInstruction(
570 const OUString
& target
, const OUString
& data
)
572 ::osl::MutexGuard
const g(m_Mutex
);
574 OString o1
= OUStringToOString(target
, RTL_TEXTENCODING_UTF8
);
575 xmlChar
const *pTarget
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
576 OString o2
= OUStringToOString(data
, RTL_TEXTENCODING_UTF8
);
577 xmlChar
const *pData
= reinterpret_cast<xmlChar
const *>(o2
.getStr());
578 xmlNodePtr
const pNode
= xmlNewDocPI(m_aDocPtr
, pTarget
, pData
);
579 pNode
->doc
= m_aDocPtr
;
580 Reference
< XProcessingInstruction
> const xRet(
581 static_cast< XNode
* >(GetCNode(pNode
).get()),
586 // Creates a Text node given the specified string.
587 Reference
< XText
> SAL_CALL
CDocument::createTextNode(const OUString
& data
)
589 ::osl::MutexGuard
const g(m_Mutex
);
591 OString o1
= OUStringToOString(data
, RTL_TEXTENCODING_UTF8
);
592 xmlChar
const *pData
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
593 xmlNodePtr
const pNode
= xmlNewDocText(m_aDocPtr
, pData
);
594 Reference
< XText
> const xRet(
595 static_cast< XNode
* >(GetCNode(pNode
).get()),
600 // The Document Type Declaration (see DocumentType) associated with this
602 Reference
< XDocumentType
> SAL_CALL
CDocument::getDoctype()
604 ::osl::MutexGuard
const g(m_Mutex
);
606 xmlNodePtr
const pDocType(lcl_getDocumentType(m_aDocPtr
));
607 Reference
< XDocumentType
> const xRet(
608 static_cast< XNode
* >(GetCNode(pDocType
).get()),
613 // This is a convenience attribute that allows direct access to the child
614 // node that is the root element of the document.
615 Reference
< XElement
> SAL_CALL
CDocument::getDocumentElement()
617 ::osl::MutexGuard
const g(m_Mutex
);
619 xmlNodePtr
const pNode
= lcl_getDocumentRootPtr(m_aDocPtr
);
620 if (!pNode
) { return nullptr; }
621 Reference
< XElement
> const xRet(
622 static_cast< XNode
* >(GetCNode(pNode
).get()),
628 lcl_search_element_by_id(const xmlNodePtr cur
, const xmlChar
* id
)
632 // look in current node
633 if (cur
->type
== XML_ELEMENT_NODE
)
635 xmlAttrPtr a
= cur
->properties
;
638 if (a
->atype
== XML_ATTRIBUTE_ID
) {
639 if (strcmp(reinterpret_cast<char*>(a
->children
->content
), reinterpret_cast<char const *>(id
)) == 0)
646 xmlNodePtr result
= lcl_search_element_by_id(cur
->children
, id
);
647 if (result
!= nullptr)
649 result
= lcl_search_element_by_id(cur
->next
, id
);
653 // Returns the Element whose ID is given by elementId.
654 Reference
< XElement
> SAL_CALL
655 CDocument::getElementById(const OUString
& elementId
)
657 ::osl::MutexGuard
const g(m_Mutex
);
659 // search the tree for an element with the given ID
660 OString o1
= OUStringToOString(elementId
, RTL_TEXTENCODING_UTF8
);
661 xmlChar
const *pId
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
662 xmlNodePtr
const pStart
= lcl_getDocumentRootPtr(m_aDocPtr
);
663 if (!pStart
) { return nullptr; }
664 xmlNodePtr
const pNode
= lcl_search_element_by_id(pStart
, pId
);
665 Reference
< XElement
> const xRet(
666 static_cast< XNode
* >(GetCNode(pNode
).get()),
672 Reference
< XNodeList
> SAL_CALL
673 CDocument::getElementsByTagName(OUString
const& rTagname
)
675 ::osl::MutexGuard
const g(m_Mutex
);
677 Reference
< XNodeList
> const xRet(
678 new CElementList(GetDocumentElement(), m_Mutex
, rTagname
));
682 Reference
< XNodeList
> SAL_CALL
CDocument::getElementsByTagNameNS(
683 OUString
const& rNamespaceURI
, OUString
const& rLocalName
)
685 ::osl::MutexGuard
const g(m_Mutex
);
687 Reference
< XNodeList
> const xRet(
688 new CElementList(GetDocumentElement(), m_Mutex
,
689 rLocalName
, &rNamespaceURI
));
693 Reference
< XDOMImplementation
> SAL_CALL
CDocument::getImplementation()
695 // does not need mutex currently
696 return Reference
< XDOMImplementation
>(CDOMImplementation::get());
699 // helper function to recursively import siblings
700 static void lcl_ImportSiblings(
701 Reference
< XDocument
> const& xTargetDocument
,
702 Reference
< XNode
> const& xTargetParent
,
703 Reference
< XNode
> const& xChild
)
705 Reference
< XNode
> xSibling
= xChild
;
706 while (xSibling
.is())
708 Reference
< XNode
> const xTmp(
709 xTargetDocument
->importNode(xSibling
, true));
710 xTargetParent
->appendChild(xTmp
);
711 xSibling
= xSibling
->getNextSibling();
715 static Reference
< XNode
>
716 lcl_ImportNode( Reference
< XDocument
> const& xDocument
,
717 Reference
< XNode
> const& xImportedNode
, bool deep
)
719 Reference
< XNode
> xNode
;
720 NodeType aNodeType
= xImportedNode
->getNodeType();
723 case NodeType_ATTRIBUTE_NODE
:
725 Reference
< XAttr
> const xAttr(xImportedNode
, UNO_QUERY_THROW
);
726 Reference
< XAttr
> const xNew
=
727 xDocument
->createAttribute(xAttr
->getName());
728 xNew
->setValue(xAttr
->getValue());
732 case NodeType_CDATA_SECTION_NODE
:
734 Reference
< XCDATASection
> const xCData(xImportedNode
,
736 Reference
< XCDATASection
> const xNewCData
=
737 xDocument
->createCDATASection(xCData
->getData());
741 case NodeType_COMMENT_NODE
:
743 Reference
< XComment
> const xComment(xImportedNode
,
745 Reference
< XComment
> const xNewComment
=
746 xDocument
->createComment(xComment
->getData());
750 case NodeType_DOCUMENT_FRAGMENT_NODE
:
752 Reference
< XDocumentFragment
> const xFrag(xImportedNode
,
754 Reference
< XDocumentFragment
> const xNewFrag
=
755 xDocument
->createDocumentFragment();
759 case NodeType_ELEMENT_NODE
:
761 Reference
< XElement
> const xElement(xImportedNode
,
763 OUString
const aNsUri
= xImportedNode
->getNamespaceURI();
764 OUString
const aNsPrefix
= xImportedNode
->getPrefix();
765 OUString aQName
= xElement
->getTagName();
766 Reference
< XElement
> xNewElement
;
767 if (!aNsUri
.isEmpty())
769 if (!aNsPrefix
.isEmpty()) {
770 aQName
= aNsPrefix
+ ":" + aQName
;
772 xNewElement
= xDocument
->createElementNS(aNsUri
, aQName
);
774 xNewElement
= xDocument
->createElement(aQName
);
778 if (xElement
->hasAttributes())
780 Reference
< XNamedNodeMap
> attribs
= xElement
->getAttributes();
781 for (sal_Int32 i
= 0; i
< attribs
->getLength(); i
++)
783 Reference
< XAttr
> const curAttr(attribs
->item(i
),
785 OUString
const aAttrUri
= curAttr
->getNamespaceURI();
786 OUString
const aAttrPrefix
= curAttr
->getPrefix();
787 OUString aAttrName
= curAttr
->getName();
788 OUString
const sValue
= curAttr
->getValue();
789 if (!aAttrUri
.isEmpty())
791 if (!aAttrPrefix
.isEmpty()) {
792 aAttrName
= aAttrPrefix
+ ":" + aAttrName
;
794 xNewElement
->setAttributeNS(
795 aAttrUri
, aAttrName
, sValue
);
797 xNewElement
->setAttribute(aAttrName
, sValue
);
804 case NodeType_ENTITY_REFERENCE_NODE
:
806 Reference
< XEntityReference
> const xRef(xImportedNode
,
808 Reference
< XEntityReference
> const xNewRef(
809 xDocument
->createEntityReference(xRef
->getNodeName()));
813 case NodeType_PROCESSING_INSTRUCTION_NODE
:
815 Reference
< XProcessingInstruction
> const xPi(xImportedNode
,
817 Reference
< XProcessingInstruction
> const xNewPi(
818 xDocument
->createProcessingInstruction(
819 xPi
->getTarget(), xPi
->getData()));
823 case NodeType_TEXT_NODE
:
825 Reference
< XText
> const xText(xImportedNode
, UNO_QUERY_THROW
);
826 Reference
< XText
> const xNewText(
827 xDocument
->createTextNode(xText
->getData()));
831 case NodeType_ENTITY_NODE
:
832 case NodeType_DOCUMENT_NODE
:
833 case NodeType_DOCUMENT_TYPE_NODE
:
834 case NodeType_NOTATION_NODE
:
837 throw RuntimeException();
842 // get children and import them
843 Reference
< XNode
> const xChild
= xImportedNode
->getFirstChild();
846 lcl_ImportSiblings(xDocument
, xNode
, xChild
);
850 /* DOMNodeInsertedIntoDocument
851 * Fired when a node is being inserted into a document,
852 * either through direct insertion of the Node or insertion of a
853 * subtree in which it is contained. This event is dispatched after
854 * the insertion has taken place. The target of this event is the node
855 * being inserted. If the Node is being directly inserted the DOMNodeInserted
856 * event will fire before the DOMNodeInsertedIntoDocument event.
863 Reference
< XDocumentEvent
> const xDocevent(xDocument
, UNO_QUERY
);
864 Reference
< XMutationEvent
> const event(xDocevent
->createEvent(
865 "DOMNodeInsertedIntoDocument"), UNO_QUERY_THROW
);
866 event
->initMutationEvent(
867 "DOMNodeInsertedIntoDocument", true, false, Reference
< XNode
>(),
868 OUString(), OUString(), OUString(), AttrChangeType(0) );
869 Reference
< XEventTarget
> const xDocET(xDocument
, UNO_QUERY
);
870 xDocET
->dispatchEvent(event
);
876 Reference
< XNode
> SAL_CALL
CDocument::importNode(
877 Reference
< XNode
> const& xImportedNode
, sal_Bool deep
)
879 if (!xImportedNode
.is()) { throw RuntimeException(); }
881 // NB: this whole operation inherently accesses 2 distinct documents.
882 // The imported node could even be from a different DOM implementation,
883 // so this implementation cannot make any assumptions about the
884 // locking strategy of the imported node.
885 // So the import takes no lock on this document;
886 // it only calls UNO methods on this document that temporarily
887 // lock the document, and UNO methods on the imported node that
888 // may temporarily lock the other document.
889 // As a consequence, the import is not atomic with regard to
890 // concurrent modifications of either document, but it should not
892 // To ensure that no members are accessed, the implementation is in
893 // static non-member functions.
895 Reference
< XDocument
> const xDocument(this);
897 if (xImportedNode
->getOwnerDocument() == xDocument
) {
898 return xImportedNode
;
901 Reference
< XNode
> const xNode(
902 lcl_ImportNode(xDocument
, xImportedNode
, deep
) );
906 OUString SAL_CALL
CDocument::getNodeName()
908 // does not need mutex currently
912 OUString SAL_CALL
CDocument::getNodeValue()
914 // does not need mutex currently
918 Reference
< XNode
> SAL_CALL
CDocument::cloneNode(sal_Bool bDeep
)
920 ::osl::MutexGuard
const g(m_rMutex
);
922 OSL_ASSERT(nullptr != m_aNodePtr
);
923 if (nullptr == m_aNodePtr
) {
926 xmlDocPtr
const pClone(xmlCopyDoc(m_aDocPtr
, bDeep
? 1 : 0));
927 if (nullptr == pClone
) { return nullptr; }
928 Reference
< XNode
> const xRet(
929 static_cast<CNode
*>(CDocument::CreateCDocument(pClone
).get()));
933 Reference
< XEvent
> SAL_CALL
CDocument::createEvent(const OUString
& aType
)
935 // does not need mutex currently
936 events::CEvent
*pEvent
= nullptr;
937 if ( aType
== "DOMSubtreeModified" || aType
== "DOMNodeInserted" || aType
== "DOMNodeRemoved"
938 || aType
== "DOMNodeRemovedFromDocument" || aType
== "DOMNodeInsertedIntoDocument" || aType
== "DOMAttrModified"
939 || aType
== "DOMCharacterDataModified")
941 pEvent
= new events::CMutationEvent
;
943 } else if ( aType
== "DOMFocusIn" || aType
== "DOMFocusOut" || aType
== "DOMActivate")
945 pEvent
= new events::CUIEvent
;
946 } else if ( aType
== "click" || aType
== "mousedown" || aType
== "mouseup"
947 || aType
== "mouseover" || aType
== "mousemove" || aType
== "mouseout" )
949 pEvent
= new events::CMouseEvent
;
951 else // generic event
953 pEvent
= new events::CEvent
;
955 return Reference
< XEvent
>(pEvent
);
958 // css::xml::sax::XSAXSerializable
959 void SAL_CALL
CDocument::serialize(
960 const Reference
< XDocumentHandler
>& i_xHandler
,
961 const Sequence
< beans::StringPair
>& i_rNamespaces
)
963 ::osl::MutexGuard
const g(m_Mutex
);
965 // add new namespaces to root node
966 xmlNodePtr
const pRoot
= lcl_getDocumentRootPtr(m_aDocPtr
);
967 if (nullptr != pRoot
) {
968 for (const beans::StringPair
& rNsDef
: i_rNamespaces
) {
969 OString prefix
= OUStringToOString(rNsDef
.First
,
970 RTL_TEXTENCODING_UTF8
);
971 OString href
= OUStringToOString(rNsDef
.Second
,
972 RTL_TEXTENCODING_UTF8
);
973 // this will only add the ns if it does not exist already
974 xmlNewNs(pRoot
, reinterpret_cast<const xmlChar
*>(href
.getStr()),
975 reinterpret_cast<const xmlChar
*>(prefix
.getStr()));
977 // eliminate duplicate namespace declarations
978 nscleanup(pRoot
->children
, pRoot
);
983 // css::xml::sax::XFastSAXSerializable
984 void SAL_CALL
CDocument::fastSerialize( const Reference
< XFastDocumentHandler
>& i_xHandler
,
985 const Reference
< XFastTokenHandler
>& i_xTokenHandler
,
986 const Sequence
< beans::StringPair
>& i_rNamespaces
,
987 const Sequence
< beans::Pair
< OUString
, sal_Int32
> >& i_rRegisterNamespaces
)
989 ::osl::MutexGuard
const g(m_Mutex
);
991 // add new namespaces to root node
992 xmlNodePtr
const pRoot
= lcl_getDocumentRootPtr(m_aDocPtr
);
993 if (nullptr != pRoot
) {
994 for (const beans::StringPair
& rNsDef
: i_rNamespaces
) {
995 OString prefix
= OUStringToOString(rNsDef
.First
,
996 RTL_TEXTENCODING_UTF8
);
997 OString href
= OUStringToOString(rNsDef
.Second
,
998 RTL_TEXTENCODING_UTF8
);
999 // this will only add the ns if it does not exist already
1000 xmlNewNs(pRoot
, reinterpret_cast<const xmlChar
*>(href
.getStr()),
1001 reinterpret_cast<const xmlChar
*>(prefix
.getStr()));
1003 // eliminate duplicate namespace declarations
1004 nscleanup(pRoot
->children
, pRoot
);
1007 Context
aContext(i_xHandler
,
1010 // register namespace ids
1011 for (const beans::Pair
<OUString
,sal_Int32
>& rNs
: i_rRegisterNamespaces
)
1013 OSL_ENSURE(rNs
.Second
>= FastToken::NAMESPACE
,
1014 "CDocument::fastSerialize(): invalid NS token id");
1015 aContext
.maNamespaceMap
[ rNs
.First
] = rNs
.Second
;
1018 fastSaxify(aContext
);
1022 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */