26#include <osl/diagnose.h>
27#include <rtl/ustrbuf.hxx>
29#include <com/sun/star/xml/dom/DOMException.hpp>
30#include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
31#include <com/sun/star/xml/sax/FastToken.hpp>
42using namespace css::uno;
43using namespace css::xml::dom;
44using namespace css::xml::dom::events;
45using namespace css::xml::sax;
51 xmlNodePtr
const pNode)
52 :
CElement_Base(rDocument, rMutex, NodeType_ELEMENT_NODE, pNode)
62 for (xmlNsPtr pNs = m_aNodePtr->nsDef; pNs !=
nullptr; pNs = pNs->next) {
63 const xmlChar *pPrefix = pNs->prefix ? pNs->prefix :
reinterpret_cast<const xmlChar*
>(
"");
64 OUString
prefix(
reinterpret_cast<const char*
>(pPrefix),
65 strlen(
reinterpret_cast<const char*
>(pPrefix)),
66 RTL_TEXTENCODING_UTF8);
68 ? OUString(
"xmlns" ) :
"xmlns:" +
prefix;
69 const xmlChar *pHref = pNs->href;
70 OUString val(
reinterpret_cast<const char*
>(pHref),
71 strlen(
reinterpret_cast<const char*
>(pHref)),
72 RTL_TEXTENCODING_UTF8);
73 pAttrs->AddAttribute(
name, val);
76 for (xmlAttrPtr pAttr = m_aNodePtr->properties;
77 pAttr !=
nullptr; pAttr = pAttr->next) {
79 reinterpret_cast<xmlNodePtr
>(pAttr));
80 OSL_ENSURE(pNode !=
nullptr,
"CNode::get returned 0");
81 OUString
prefix = pNode->getPrefix();
83 ? pNode->getLocalName()
84 :
prefix +
":" + pNode->getLocalName();
85 OUString val = pNode->getNodeValue();
86 pAttrs->AddAttribute(
name, val);
92 i_xHandler->startElement(
name, pAttrs);
94 for (xmlNodePtr pChild = m_aNodePtr->children;
95 pChild !=
nullptr; pChild = pChild->next) {
97 GetOwnerDocument().GetCNode(pChild));
98 OSL_ENSURE(pNode !=
nullptr,
"CNode::get returned 0");
99 pNode->saxify(i_xHandler);
101 i_xHandler->endElement(
name);
112 for (xmlAttrPtr pAttr = m_aNodePtr->properties;
113 pAttr !=
nullptr; pAttr = pAttr->next) {
115 reinterpret_cast<xmlNodePtr
>(pAttr));
116 OSL_ENSURE(pNode !=
nullptr,
"CNode::get returned 0");
118 const xmlChar*
pName = pAttr->name;
119 sal_Int32 nAttributeToken=FastToken::DONTKNOW;
121 if( pAttr->ns && strlen(
reinterpret_cast<char const *
>(pAttr->ns->prefix)) )
123 reinterpret_cast<char const *
>(pAttr->ns->prefix),
124 reinterpret_cast<char const *
>(
pName) );
126 nAttributeToken =
getToken( i_rContext,
reinterpret_cast<char const *
>(
pName) );
128 if( nAttributeToken != FastToken::DONTKNOW )
131 RTL_TEXTENCODING_UTF8));
134 const xmlChar* pPrefix = (m_aNodePtr->ns && m_aNodePtr->ns->prefix) ? m_aNodePtr->ns->prefix :
reinterpret_cast<const xmlChar*
>(
"");
135 const xmlChar*
pName = m_aNodePtr->name;
136 sal_Int32 nElementToken=FastToken::DONTKNOW;
137 if( strlen(
reinterpret_cast<char const *
>(pPrefix)) )
138 nElementToken =
getTokenWithPrefix( i_rContext,
reinterpret_cast<char const *
>(pPrefix),
reinterpret_cast<char const *
>(
pName) );
140 nElementToken =
getToken( i_rContext,
reinterpret_cast<char const *
>(
pName) );
145 Reference< XFastAttributeList > xAttr( i_rContext.
mxAttribList );
146 if( nElementToken == FastToken::DONTKNOW )
148 const OUString aNamespace;
149 const OUString aElementName(
reinterpret_cast<char const *
>(pPrefix),
150 strlen(
reinterpret_cast<char const *
>(pPrefix)),
151 RTL_TEXTENCODING_UTF8 );
153 if( xParentHandler.is() )
154 i_rContext.
mxCurrentHandler = xParentHandler->createUnknownChildContext( aNamespace, aElementName, xAttr );
159 i_rContext.
mxCurrentHandler->startUnknownElement( aNamespace, aElementName, xAttr );
163 if( xParentHandler.is() )
164 i_rContext.
mxCurrentHandler = xParentHandler->createFastChildContext( nElementToken, xAttr );
176 for (xmlNodePtr pChild = m_aNodePtr->children;
177 pChild !=
nullptr; pChild = pChild->next) {
179 GetOwnerDocument().GetCNode(pChild));
180 OSL_ENSURE(pNode !=
nullptr,
"CNode::get returned 0");
181 pNode->fastSaxify(i_rContext);
186 if( nElementToken != FastToken::DONTKNOW )
190 const OUString aElementName(
reinterpret_cast<char const *
>(pPrefix),
191 strlen(
reinterpret_cast<char const *
>(pPrefix)),
192 RTL_TEXTENCODING_UTF8 );
208 case NodeType_ELEMENT_NODE:
209 case NodeType_TEXT_NODE:
210 case NodeType_COMMENT_NODE:
211 case NodeType_PROCESSING_INSTRUCTION_NODE:
212 case NodeType_CDATA_SECTION_NODE:
213 case NodeType_ENTITY_REFERENCE_NODE:
215 case NodeType_ATTRIBUTE_NODE:
233 ::osl::MutexGuard
const g(
m_rMutex);
235 if (
nullptr == m_aNodePtr) {
240 std::shared_ptr<xmlChar const>
const pValue(
241 xmlGetProp(m_aNodePtr,
reinterpret_cast<xmlChar
const *
>(o1.getStr())), xmlFree);
242 OUString
const ret( pValue
243 ? OUString(
reinterpret_cast<char const*
>(pValue.get()),
244 strlen(
reinterpret_cast<char const*
>(pValue.get())),
245 RTL_TEXTENCODING_UTF8)
255 ::osl::MutexGuard
const g(
m_rMutex);
257 if (
nullptr == m_aNodePtr) {
261 xmlChar
const*
const pName =
262 reinterpret_cast<xmlChar const*
>(o1.getStr());
263 xmlAttrPtr
const pAttr = xmlHasProp(m_aNodePtr,
pName);
264 if (
nullptr == pAttr) {
267 Reference< XAttr >
const xRet(
268 static_cast< XNode*
>(GetOwnerDocument().GetCNode(
269 reinterpret_cast<xmlNodePtr
>(pAttr)).
get()),
278 const OUString& namespaceURI,
const OUString& localName)
280 ::osl::MutexGuard
const g(
m_rMutex);
282 if (
nullptr == m_aNodePtr) {
286 xmlChar
const*
const pName =
287 reinterpret_cast<xmlChar const*
>(o1.getStr());
289 xmlChar
const*
const pNS =
290 reinterpret_cast<xmlChar const*
>(o2.getStr());
291 xmlAttrPtr
const pAttr = xmlHasNsProp(m_aNodePtr,
pName, pNS);
292 if (
nullptr == pAttr) {
295 Reference< XAttr >
const xRet(
296 static_cast< XNode*
>(GetOwnerDocument().GetCNode(
297 reinterpret_cast<xmlNodePtr
>(pAttr)).
get()),
308 OUString
const& namespaceURI, OUString
const& localName)
310 ::osl::MutexGuard
const g(
m_rMutex);
312 if (
nullptr == m_aNodePtr) {
316 xmlChar
const*
const pName =
317 reinterpret_cast<xmlChar const*
>(o1.getStr());
319 xmlChar
const*
const pNS =
320 reinterpret_cast<xmlChar const*
>(o2.getStr());
321 std::shared_ptr<xmlChar const>
const pValue(
322 xmlGetNsProp(m_aNodePtr,
pName, pNS), xmlFree);
323 if (
nullptr == pValue) {
326 OUString
const ret(
reinterpret_cast<char const*
>(pValue.get()),
327 strlen(
reinterpret_cast<char const*
>(pValue.get())),
328 RTL_TEXTENCODING_UTF8);
337 Reference< XNodeList > SAL_CALL
340 ::osl::MutexGuard
const g(
m_rMutex);
342 Reference< XNodeList >
const xList(
352 Reference< XNodeList > SAL_CALL
354 OUString
const& rNamespaceURI, OUString
const& rLocalName)
356 ::osl::MutexGuard
const g(
m_rMutex);
358 Reference< XNodeList >
const xList(
368 ::osl::MutexGuard
const g(
m_rMutex);
370 if (
nullptr == m_aNodePtr) {
373 OUString
const ret(
reinterpret_cast<char const *
>(m_aNodePtr->name),
374 strlen(
reinterpret_cast<char const *
>(m_aNodePtr->name)), RTL_TEXTENCODING_UTF8);
385 ::osl::MutexGuard
const g(
m_rMutex);
388 xmlChar
const *
pName =
reinterpret_cast<xmlChar
const *
>(o1.getStr());
389 return (m_aNodePtr !=
nullptr && xmlHasProp(m_aNodePtr,
pName) !=
nullptr);
397 OUString
const& namespaceURI, OUString
const& localName)
399 ::osl::MutexGuard
const g(
m_rMutex);
402 xmlChar
const *
pName =
reinterpret_cast<xmlChar
const *
>(o1.getStr());
404 xmlChar
const *pNs =
reinterpret_cast<xmlChar
const *
>(o2.getStr());
405 return (m_aNodePtr !=
nullptr && xmlHasNsProp(m_aNodePtr,
pName, pNs) !=
nullptr);
413 ::osl::MutexGuard
const g(
m_rMutex);
415 if (
nullptr == m_aNodePtr) {
419 xmlChar
const*
const pName =
420 reinterpret_cast<xmlChar const*
>(o1.getStr());
421 xmlAttrPtr
const pAttr = xmlHasProp(m_aNodePtr,
pName);
422 if (0 == xmlUnsetProp(m_aNodePtr,
pName)) {
424 reinterpret_cast<xmlNodePtr
>(pAttr),
false));
426 pCNode->invalidate();
435 OUString
const& namespaceURI, OUString
const& localName)
437 ::osl::MutexGuard
const g(
m_rMutex);
439 if (
nullptr == m_aNodePtr) {
443 xmlChar
const*
const pName =
444 reinterpret_cast<xmlChar const*
>(o1.getStr());
446 xmlChar
const*
const pURI =
447 reinterpret_cast<xmlChar const*
>(o2.getStr());
449 xmlSearchNsByHref(m_aNodePtr->doc, m_aNodePtr, pURI);
450 xmlAttrPtr
const pAttr = xmlHasNsProp(m_aNodePtr,
pName, pURI);
451 if (0 == xmlUnsetNsProp(m_aNodePtr, pNs,
pName)) {
453 reinterpret_cast<xmlNodePtr
>(pAttr),
false));
455 pCNode->invalidate();
463 Reference< XAttr > SAL_CALL
466 ::osl::MutexGuard
const g(
m_rMutex);
468 if (
nullptr == m_aNodePtr) {
473 dynamic_cast<CNode*
>(oldAttr.get()));
476 xmlNodePtr
const pNode = pCNode->GetNodePtr();
477 xmlAttrPtr
const pAttr =
reinterpret_cast<xmlAttrPtr
>(pNode);
480 if (pAttr->parent != m_aNodePtr)
483 e.Code = DOMExceptionType_HIERARCHY_REQUEST_ERR;
486 if (pAttr->doc != m_aNodePtr->doc)
489 e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
493 Reference< XAttr > aAttr;
494 if (!oldAttr->getNamespaceURI().isEmpty()) {
495 OUStringBuffer qname(oldAttr->getPrefix());
496 if (!qname.isEmpty()) {
499 qname.append(oldAttr->getName());
500 aAttr = GetOwnerDocument().createAttributeNS(
501 oldAttr->getNamespaceURI(), qname.makeStringAndClear());
503 aAttr = GetOwnerDocument().createAttribute(oldAttr->getName());
505 aAttr->setValue(oldAttr->getValue());
506 xmlRemoveProp(pAttr);
507 pCNode->invalidate();
517 Reference< XAttr >
const& xNewAttr,
bool const bNS)
521 e.Code = DOMExceptionType_WRONG_DOCUMENT_ERR;
525 ::osl::ClearableMutexGuard guard(
m_rMutex);
527 if (
nullptr == m_aNodePtr) {
532 CAttr *
const pCAttr =
dynamic_cast<CAttr*
>(xNewAttr.get());
534 xmlAttrPtr
const pAttr =
535 reinterpret_cast<xmlAttrPtr
>(pCAttr->GetNodePtr());
541 e.Code = DOMExceptionType_INUSE_ATTRIBUTE_ERR;
545 xmlAttrPtr res =
nullptr;
546 xmlChar
const*
const pContent(
547 (pAttr->children) ? pAttr->children->content :
nullptr);
551 res = xmlNewNsProp(m_aNodePtr, pNs, pAttr->name, pContent);
553 res = xmlNewProp(m_aNodePtr, pAttr->name, pContent);
557 Reference< XAttr >
const xAttr(
558 static_cast< XNode*
>(GetOwnerDocument().GetCNode(
559 reinterpret_cast<xmlNodePtr
>(res)).
get()),
565 Reference< XMutationEvent > event(docevent->createEvent(
566 "DOMAttrModified"), UNO_QUERY);
567 event->initMutationEvent(
"DOMAttrModified",
569 OUString(), xAttr->getValue(), xAttr->getName(),
570 AttrChangeType_ADDITION);
574 dispatchEvent(event);
575 dispatchSubtreeModified();
601 ::osl::ClearableMutexGuard guard(
m_rMutex);
604 xmlChar
const *
pName =
reinterpret_cast<xmlChar
const *
>(o1.getStr());
606 xmlChar
const *pValue =
reinterpret_cast<xmlChar
const *
>(o2.getStr());
608 if (
nullptr == m_aNodePtr) {
612 AttrChangeType aChangeType = AttrChangeType_MODIFICATION;
613 std::shared_ptr<xmlChar const>
const pOld(
614 xmlGetProp(m_aNodePtr,
pName), xmlFree);
615 if (pOld ==
nullptr) {
616 aChangeType = AttrChangeType_ADDITION;
617 xmlNewProp(m_aNodePtr,
pName, pValue);
619 oldValue = OUString(
reinterpret_cast<char const*
>(pOld.get()),
620 strlen(
reinterpret_cast<char const*
>(pOld.get())),
621 RTL_TEXTENCODING_UTF8);
622 xmlSetProp(m_aNodePtr,
pName, pValue);
627 Reference< XMutationEvent > event(docevent->createEvent(
628 "DOMAttrModified"), UNO_QUERY);
629 event->initMutationEvent(
"DOMAttrModified",
635 dispatchEvent(event);
636 dispatchSubtreeModified();
644 OUString
const& qualifiedName, OUString
const& value)
648 ::osl::ClearableMutexGuard guard(
m_rMutex);
650 OString o1, o2, o3, o4, o5;
651 xmlChar
const *pPrefix =
nullptr;
652 xmlChar
const *pLName =
nullptr;
654 xmlChar
const *pQName =
reinterpret_cast<xmlChar
const *
>(o1.getStr());
655 sal_Int32
idx = qualifiedName.indexOf(
':');
659 qualifiedName.subView(0,
idx),
660 RTL_TEXTENCODING_UTF8);
661 pPrefix =
reinterpret_cast<xmlChar
const *
>(o2.getStr());
663 qualifiedName.subView(
idx+1),
664 RTL_TEXTENCODING_UTF8);
665 pLName =
reinterpret_cast<xmlChar
const *
>(o3.getStr());
667 pPrefix =
reinterpret_cast<xmlChar
const *
>(
"");
672 xmlChar
const *pURI=
reinterpret_cast<xmlChar
const *
>(o4.getStr());
673 xmlChar
const *pValue =
reinterpret_cast<xmlChar
const *
>(o5.getStr());
675 if (
nullptr == m_aNodePtr) {
680 xmlNsPtr pNs = xmlSearchNs(m_aNodePtr->doc, m_aNodePtr, pPrefix);
682 if (pNs ==
nullptr) {
683 pNs = xmlNewNs(m_aNodePtr, pURI, pPrefix);
686 if (strcmp(
reinterpret_cast<char const *
>(pNs->href),
reinterpret_cast<char const *
>(pURI)) != 0) {
694 AttrChangeType aChangeType = AttrChangeType_MODIFICATION;
695 std::shared_ptr<xmlChar const>
const pOld(
696 xmlGetNsProp(m_aNodePtr, pLName, pNs->href), xmlFree);
697 if (pOld ==
nullptr) {
698 aChangeType = AttrChangeType_ADDITION;
699 xmlNewNsProp(m_aNodePtr, pNs, pLName, pValue);
701 oldValue = OUString(
reinterpret_cast<char const*
>(pOld.get()),
702 strlen(
reinterpret_cast<char const*
>(pOld.get())),
703 RTL_TEXTENCODING_UTF8);
704 xmlSetNsProp(m_aNodePtr, pNs, pLName, pValue);
708 Reference< XMutationEvent > event(docevent->createEvent(
709 "DOMAttrModified"), UNO_QUERY);
710 event->initMutationEvent(
711 "DOMAttrModified",
true,
false,
712 getAttributeNodeNS(namespaceURI, OUString(
reinterpret_cast<char const *
>(pLName), strlen(
reinterpret_cast<char const *
>(pLName)), RTL_TEXTENCODING_UTF8)),
713 oldValue,
value, qualifiedName, aChangeType);
716 dispatchEvent(event);
717 dispatchSubtreeModified();
720 Reference< XNamedNodeMap > SAL_CALL
723 ::osl::MutexGuard
const g(
m_rMutex);
725 Reference< XNamedNodeMap >
const xMap(
737 ::osl::MutexGuard
const g(
m_rMutex);
740 if (m_aNodePtr !=
nullptr)
742 const xmlChar*
pName = m_aNodePtr->name;
743 aName = OUString(
reinterpret_cast<const char*
>(
pName), strlen(
reinterpret_cast<const char*
>(
pName)), RTL_TEXTENCODING_UTF8);
xmlNsPtr GetNamespace(xmlNodePtr const pNode)
return the libxml namespace corresponding to m_pNamespace on pNode
virtual void SAL_CALL removeAttributeNS(const OUString &namespaceURI, const OUString &localName) override
Removes an attribute by local name and namespace URI.
virtual void fastSaxify(Context &i_rContext) override
virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL removeAttributeNode(const css::uno::Reference< css::xml::dom::XAttr > &oldAttr) override
Removes the specified attribute node.
virtual OUString SAL_CALL getAttribute(const OUString &name) override
Retrieves an attribute value by name.
virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagNameNS(const OUString &namespaceURI, const OUString &localName) override
Returns a NodeList of all the descendant Elements with a given local name and namespace URI in the or...
virtual OUString SAL_CALL getNodeValue() override
virtual OUString SAL_CALL getNodeName() override
virtual css::uno::Reference< css::xml::dom::XNodeList > SAL_CALL getElementsByTagName(const OUString &name) override
Returns a NodeList of all descendant Elements with a given tag name, in the order in which they are e...
virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL setAttributeNodeNS(const css::uno::Reference< css::xml::dom::XAttr > &newAttr) override
Adds a new attribute.
virtual css::uno::Reference< css::xml::dom::XNamedNodeMap > SAL_CALL getAttributes() override
virtual bool IsChildTypeAllowed(css::xml::dom::NodeType const nodeType, css::xml::dom::NodeType const *) override
virtual void SAL_CALL removeAttribute(const OUString &name) override
Removes an attribute by name.
virtual void SAL_CALL setAttribute(const OUString &name, const OUString &value) override
Adds a new attribute.
virtual OUString SAL_CALL getLocalName() override
virtual OUString SAL_CALL getPrefix() override
CElement(CDocument const &rDocument, ::osl::Mutex const &rMutex, xmlNodePtr const pNode)
virtual void SAL_CALL setAttributeNS(const OUString &namespaceURI, const OUString &qualifiedName, const OUString &value) override
Adds a new attribute.
virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL setAttributeNode(const css::uno::Reference< css::xml::dom::XAttr > &newAttr) override
Adds a new attribute node.
virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL getAttributeNodeNS(const OUString &namespaceURI, const OUString &localName) override
Retrieves an Attr node by local name and namespace URI.
virtual OUString SAL_CALL getAttributeNS(const OUString &namespaceURI, const OUString &localName) override
Retrieves an attribute value by local name and namespace URI.
virtual OUString SAL_CALL getTagName() override
The name of the element.
virtual css::uno::Reference< css::xml::dom::XAttr > SAL_CALL getAttributeNode(const OUString &name) override
Retrieves an attribute node by name.
virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument() override
virtual sal_Bool SAL_CALL hasAttributeNS(const OUString &namespaceURI, const OUString &localName) override
Returns true when an attribute with a given local name and namespace URI is specified on this element...
virtual void saxify(const css::uno::Reference< css::xml::sax::XDocumentHandler > &i_xHandler) override
css::uno::Reference< css::xml::dom::XAttr > setAttributeNode_Impl_Lock(css::uno::Reference< css::xml::dom::XAttr > const &xNewAttr, bool const bNS)
Adds a new attribute node.
virtual sal_Bool SAL_CALL hasAttribute(const OUString &name) override
Returns true when an attribute with a given name is specified on this element or has a default value,...
::cppu::ImplInheritanceHelper< CNode, css::xml::dom::XElement > CElement_Base
void popContext(Context &io_rContext)
void pushContext(Context &io_rContext)
sal_Int32 getTokenWithPrefix(const Context &rContext, const char *pPrefix, const char *pName)
sal_Int32 getToken(const Context &rContext, const char *pToken)
void addNamespaces(Context &io_rContext, xmlNodePtr pNode)
add namespaces on this node to context
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
css::uno::Reference< css::xml::sax::XFastContextHandler > mxCurrentHandler
css::uno::Reference< css::xml::sax::XFastDocumentHandler > mxDocHandler
::rtl::Reference< sax_fastparser::FastAttributeList > mxAttribList