diff options
author | Michael Stahl <mst@openoffice.org> | 2011-01-19 20:27:15 +0100 |
---|---|---|
committer | Michael Stahl <mst@openoffice.org> | 2011-01-19 20:27:15 +0100 |
commit | e2108d556163e7ca1b5bc8ebaa46d86f77f3fbee (patch) | |
tree | f30128411c298524fd839bc1ec6de3848849cb4e /unoxml/source | |
parent | 8f5bbf5b121339f21c3859b8476a994c4cfc841a (diff) |
xmlfix3: #i113663#: unoxml: fix leaks caused by CNode::get returning CNode*:
CNode::getCNode now returns rtl::Reference<CNode>, preventing leaks.
Diffstat (limited to 'unoxml/source')
-rw-r--r-- | unoxml/source/dom/attr.cxx | 10 | ||||
-rw-r--r-- | unoxml/source/dom/attributesmap.cxx | 15 | ||||
-rw-r--r-- | unoxml/source/dom/childlist.cxx | 10 | ||||
-rw-r--r-- | unoxml/source/dom/childlist.hxx | 2 | ||||
-rw-r--r-- | unoxml/source/dom/document.cxx | 107 | ||||
-rw-r--r-- | unoxml/source/dom/documentbuilder.cxx | 18 | ||||
-rw-r--r-- | unoxml/source/dom/element.cxx | 97 | ||||
-rw-r--r-- | unoxml/source/dom/elementlist.cxx | 7 | ||||
-rw-r--r-- | unoxml/source/dom/node.cxx | 280 | ||||
-rw-r--r-- | unoxml/source/dom/node.hxx | 5 | ||||
-rw-r--r-- | unoxml/source/events/eventdispatcher.cxx | 9 | ||||
-rw-r--r-- | unoxml/source/xpath/nodelist.cxx | 10 |
12 files changed, 338 insertions, 232 deletions
diff --git a/unoxml/source/dom/attr.cxx b/unoxml/source/dom/attr.cxx index a1773a7db99f..1bbd5234e6a0 100644 --- a/unoxml/source/dom/attr.cxx +++ b/unoxml/source/dom/attr.cxx @@ -76,12 +76,14 @@ namespace DOM Reference< XElement > SAL_CALL CAttr::getOwnerElement() throw (RuntimeException) { - Reference< XElement > aElement; - if (m_aAttrPtr != NULL && m_aAttrPtr->parent != NULL) + if ((m_aAttrPtr == 0) || (m_aAttrPtr->parent == 0)) { - aElement = Reference< XElement >(static_cast< CElement* >(CNode::get(m_aAttrPtr->parent))); + return 0; } - return aElement; + Reference< XElement > const xRet( + static_cast< XNode* >(CNode::getCNode(m_aAttrPtr->parent).get()), + UNO_QUERY_THROW); + return xRet; } /** diff --git a/unoxml/source/dom/attributesmap.cxx b/unoxml/source/dom/attributesmap.cxx index c411365cf1b0..6f3ac9639cd3 100644 --- a/unoxml/source/dom/attributesmap.cxx +++ b/unoxml/source/dom/attributesmap.cxx @@ -71,7 +71,8 @@ namespace DOM { if( strcmp((char*)xName, (char*)cur->name) == 0) { - aNode = Reference< XNode >(static_cast<CNode*>(CNode::get((xmlNodePtr)cur))); + aNode = Reference< XNode >( CNode::getCNode( + reinterpret_cast<xmlNodePtr>(cur)).get() ); break; } cur = cur->next; @@ -100,7 +101,8 @@ namespace DOM if( strcmp((char*)xName, (char*)cur->name) == 0 && cur->ns == pNs) { - aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + aNode = Reference< XNode >( CNode::getCNode( + reinterpret_cast<xmlNodePtr>(cur)).get() ); break; } cur = cur->next; @@ -124,7 +126,8 @@ namespace DOM { if (count == index) { - aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + aNode = Reference< XNode >( CNode::getCNode( + reinterpret_cast<xmlNodePtr>(cur)).get() ); break; } count++; @@ -151,7 +154,8 @@ namespace DOM { if( strcmp((char*)xName, (char*)cur->name) == 0) { - aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + aNode = Reference< XNode >( CNode::getCNode( + reinterpret_cast<xmlNodePtr>(cur)).get() ); xmlUnlinkNode((xmlNodePtr)cur); break; } @@ -181,7 +185,8 @@ namespace DOM if( strcmp((char*)xName, (char*)cur->name) == 0 && cur->ns == pNs) { - aNode = Reference< XNode >(static_cast< CNode* >(CNode::get((xmlNodePtr)cur))); + aNode = Reference< XNode >( CNode::getCNode( + reinterpret_cast<xmlNodePtr>(cur)).get() ); xmlUnlinkNode((xmlNodePtr)cur); break; } diff --git a/unoxml/source/dom/childlist.cxx b/unoxml/source/dom/childlist.cxx index aaa6e157225d..007cf3d2ad5a 100644 --- a/unoxml/source/dom/childlist.cxx +++ b/unoxml/source/dom/childlist.cxx @@ -28,8 +28,8 @@ #include "childlist.hxx" namespace DOM { - CChildList::CChildList(const CNode* base) - : m_pNode(base->m_aNodePtr) + CChildList::CChildList(CNode const& rBase) + : m_pNode(rBase.m_aNodePtr) { } @@ -62,8 +62,10 @@ namespace DOM xmlNodePtr cur = m_pNode->children; while (cur != NULL) { - if (index-- == 0) - aNode = Reference< XNode >(CNode::get(cur)); + if (index-- == 0) { + aNode = Reference< XNode >(CNode::getCNode(cur).get()); + break; + } cur = cur->next; } } diff --git a/unoxml/source/dom/childlist.hxx b/unoxml/source/dom/childlist.hxx index 89b73ff2e113..c639d327a7d2 100644 --- a/unoxml/source/dom/childlist.hxx +++ b/unoxml/source/dom/childlist.hxx @@ -49,7 +49,7 @@ namespace DOM private: const xmlNodePtr m_pNode; public: - CChildList(const CNode* base); + CChildList(CNode const& rBase); /** The number of nodes in the list. */ diff --git a/unoxml/source/dom/document.cxx b/unoxml/source/dom/document.cxx index fcd43832adf0..6abf780d10d8 100644 --- a/unoxml/source/dom/document.cxx +++ b/unoxml/source/dom/document.cxx @@ -72,7 +72,7 @@ namespace DOM i_xHandler->startDocument(); for (xmlNodePtr pChild = m_aNodePtr->children; pChild != 0; pChild = pChild->next) { - CNode * pNode = CNode::get(pChild); + ::rtl::Reference<CNode> const pNode = CNode::getCNode(pChild); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); pNode->saxify(i_xHandler); } @@ -83,7 +83,7 @@ namespace DOM rContext.mxDocHandler->startDocument(); for (xmlNodePtr pChild = m_aNodePtr->children; pChild != 0; pChild = pChild->next) { - CNode * pNode = CNode::get(pChild); + ::rtl::Reference<CNode> const pNode = CNode::getCNode(pChild); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); pNode->fastSaxify(rContext); } @@ -183,8 +183,12 @@ namespace DOM { OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); xmlChar *xName = (xmlChar*)o1.getStr(); - return Reference< XAttr >(static_cast< CAttr* >( - CNode::get((xmlNodePtr)xmlNewDocProp(m_aDocPtr, xName, NULL)))); + xmlAttrPtr const pAttr = xmlNewDocProp(m_aDocPtr, xName, NULL); + Reference< XAttr > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pAttr)).get()), + UNO_QUERY_THROW); + return xRet; }; // Creates an attribute of the given qualified name and namespace URI. @@ -221,7 +225,11 @@ namespace DOM xmlNodePtr pNode = xmlNewDocNode(m_aDocPtr, NULL, (xmlChar*)"__private", NULL); xmlNsPtr pNs = xmlNewNs(pNode, xUri, xPrefix); xmlAttrPtr pAttr = xmlNewNsProp(pNode, pNs, xName, NULL); - return Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + Reference< XAttr > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pAttr)).get()), + UNO_QUERY_THROW); + return xRet; }; // Creates a CDATASection node whose value is the specified string. @@ -230,7 +238,10 @@ namespace DOM { xmlChar *xData = (xmlChar*)OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr(); xmlNodePtr pText = xmlNewCDataBlock(m_aDocPtr, xData, strlen((char*)xData)); - return Reference< XCDATASection >(static_cast< CCDATASection* >(CNode::get(pText))); + Reference< XCDATASection > const xRet( + static_cast< XNode* >(CNode::getCNode(pText).get()), + UNO_QUERY_THROW); + return xRet; } // Creates a Comment node given the specified string. @@ -240,7 +251,10 @@ namespace DOM OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); xmlChar *xData = (xmlChar*)o1.getStr(); xmlNodePtr pComment = xmlNewDocComment(m_aDocPtr, xData); - return Reference< XComment >(static_cast< CComment* >(CNode::get(pComment))); + Reference< XComment > const xRet( + static_cast< XNode* >(CNode::getCNode(pComment).get()), + UNO_QUERY_THROW); + return xRet; } //Creates an empty DocumentFragment object. @@ -248,7 +262,10 @@ namespace DOM throw (RuntimeException) { xmlNodePtr pFrag = xmlNewDocFragment(m_aDocPtr); - return Reference< XDocumentFragment >(static_cast< CDocumentFragment* >(CNode::get(pFrag))); + Reference< XDocumentFragment > const xRet( + static_cast< XNode* >(CNode::getCNode(pFrag).get()), + UNO_QUERY_THROW); + return xRet; } // Creates an element of the type specified. @@ -257,8 +274,11 @@ namespace DOM { OString o1 = OUStringToOString(tagName, RTL_TEXTENCODING_UTF8); xmlChar *xName = (xmlChar*)o1.getStr(); - xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); - return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); + Reference< XElement > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } // Creates an element of the given qualified name and namespace URI. @@ -287,10 +307,13 @@ namespace DOM // xmlNsPtr aNsPtr = xmlNewReconciledNs? // xmlNsPtr aNsPtr = xmlNewGlobalNs? - xmlNodePtr aNodePtr = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); - xmlNsPtr pNs = xmlNewNs(aNodePtr, xUri, xPrefix); - xmlSetNs(aNodePtr, pNs); - return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = xmlNewDocNode(m_aDocPtr, NULL, xName, NULL); + xmlNsPtr const pNs = xmlNewNs(pNode, xUri, xPrefix); + xmlSetNs(pNode, pNs); + Reference< XElement > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } //Creates an EntityReference object. @@ -299,8 +322,11 @@ namespace DOM { OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); xmlChar *xName = (xmlChar*)o1.getStr(); - xmlNodePtr aNodePtr = xmlNewReference(m_aDocPtr, xName); - return Reference< XEntityReference >(static_cast< CEntityReference* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = xmlNewReference(m_aDocPtr, xName); + Reference< XEntityReference > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } // Creates a ProcessingInstruction node given the specified name and @@ -313,9 +339,12 @@ namespace DOM xmlChar *xTarget = (xmlChar*)o1.getStr(); OString o2 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); xmlChar *xData = (xmlChar*)o2.getStr(); - xmlNodePtr aNodePtr = xmlNewPI(xTarget, xData); - aNodePtr->doc = m_aDocPtr; - return Reference< XProcessingInstruction >(static_cast< CProcessingInstruction* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = xmlNewPI(xTarget, xData); + pNode->doc = m_aDocPtr; + Reference< XProcessingInstruction > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } // Creates a Text node given the specified string. @@ -324,8 +353,11 @@ namespace DOM { OString o1 = OUStringToOString(data, RTL_TEXTENCODING_UTF8); xmlChar *xData = (xmlChar*)o1.getStr(); - xmlNodePtr aNodePtr = xmlNewDocText(m_aDocPtr, xData); - return Reference< XText >(static_cast< CText* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = xmlNewDocText(m_aDocPtr, xData); + Reference< XText > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } // The Document Type Declaration (see DocumentType) associated with this @@ -340,7 +372,10 @@ namespace DOM if (cur->type == XML_DOCUMENT_TYPE_NODE || cur->type == XML_DTD_NODE) break; } - return Reference< XDocumentType >(static_cast< CDocumentType* >(CNode::get(cur))); + Reference< XDocumentType > const xRet( + static_cast< XNode* >(CNode::getCNode(cur).get()), + UNO_QUERY_THROW); + return xRet; } /// get the pointer to the root element node of the document @@ -361,8 +396,11 @@ namespace DOM Reference< XElement > SAL_CALL CDocument::getDocumentElement() throw (RuntimeException) { - xmlNodePtr cur = _getDocumentRootPtr(m_aDocPtr); - return Reference< XElement >(static_cast< CElement* >(CNode::get(cur))); + xmlNodePtr const pNode = _getDocumentRootPtr(m_aDocPtr); + Reference< XElement > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } static xmlNodePtr _search_element_by_id(const xmlNodePtr cur, const xmlChar* id) @@ -399,8 +437,11 @@ namespace DOM OString o1 = OUStringToOString(elementId, RTL_TEXTENCODING_UTF8); xmlChar *xId = (xmlChar*)o1.getStr(); xmlNodePtr pStart = CNode::getNodePtr(getDocumentElement().get()); - xmlNodePtr aNodePtr = _search_element_by_id(pStart, xId); - return Reference< XElement >(static_cast< CElement* >(CNode::get(aNodePtr))); + xmlNodePtr const pNode = _search_element_by_id(pStart, xId); + Reference< XElement > const xRet( + static_cast< XNode* >(CNode::getCNode(pNode).get()), + UNO_QUERY_THROW); + return xRet; } @@ -454,10 +495,16 @@ namespace DOM // this node could be from another memory model // only use uno interfaces to access is!!! - // allready in doc? - if ( importedNode->getOwnerDocument() == - Reference< XDocument>(static_cast< CDocument* >(CNode::get((xmlNodePtr)m_aDocPtr)))) - return importedNode; + { + // already in doc? + Reference< XDocument > const xDocument( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(m_aDocPtr)).get()), + UNO_QUERY_THROW); + if (importedNode->getOwnerDocument() == xDocument) { + return importedNode; + } + } Reference< XNode > aNode; NodeType aNodeType = importedNode->getNodeType(); diff --git a/unoxml/source/dom/documentbuilder.cxx b/unoxml/source/dom/documentbuilder.cxx index 484c04b7f23b..e9f10249292e 100644 --- a/unoxml/source/dom/documentbuilder.cxx +++ b/unoxml/source/dom/documentbuilder.cxx @@ -184,7 +184,11 @@ namespace DOM { // create a new document xmlDocPtr pDocument = xmlNewDoc((const xmlChar*)"1.0"); - return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDocument))); + Reference< XDocument > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pDocument)).get()), + UNO_QUERY_THROW); + return xRet; } static OUString make_error_message(xmlParserCtxtPtr ctxt) @@ -356,7 +360,11 @@ namespace DOM throwEx(ctxt); } xmlFreeParserCtxt(ctxt); - return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDoc))); + Reference< XDocument > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pDoc)).get()), + UNO_QUERY_THROW); + return xRet; } Reference< XDocument > SAL_CALL CDocumentBuilder::parseSource(const InputSource& is) @@ -405,7 +413,11 @@ namespace DOM throwEx(ctxt); } xmlFreeParserCtxt(ctxt); - return Reference< XDocument >(static_cast< CDocument* >(CNode::get((xmlNodePtr)pDoc))); + Reference< XDocument > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pDoc)).get()), + UNO_QUERY_THROW); + return xRet; } void SAL_CALL CDocumentBuilder::setEntityResolver(const Reference< XEntityResolver >& er) diff --git a/unoxml/source/dom/element.cxx b/unoxml/source/dom/element.cxx index e116f3883e5d..66ec7842e408 100644 --- a/unoxml/source/dom/element.cxx +++ b/unoxml/source/dom/element.cxx @@ -71,7 +71,8 @@ namespace DOM // add attributes for (xmlAttrPtr pAttr = m_aNodePtr->properties; pAttr != 0; pAttr = pAttr->next) { - CNode * pNode = CNode::get(reinterpret_cast<xmlNodePtr>(pAttr)); + ::rtl::Reference<CNode> const pNode = + CNode::getCNode(reinterpret_cast<xmlNodePtr>(pAttr)); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); OUString prefix = pNode->getPrefix(); OUString name = (prefix.getLength() == 0) @@ -89,7 +90,7 @@ namespace DOM // recurse for (xmlNodePtr pChild = m_aNodePtr->children; pChild != 0; pChild = pChild->next) { - CNode * pNode = CNode::get(pChild); + ::rtl::Reference<CNode> const pNode = CNode::getCNode(pChild); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); pNode->saxify(i_xHandler); } @@ -105,7 +106,8 @@ namespace DOM i_rContext.mxAttribList->clear(); for (xmlAttrPtr pAttr = m_aNodePtr->properties; pAttr != 0; pAttr = pAttr->next) { - CNode * pNode = CNode::get(reinterpret_cast<xmlNodePtr>(pAttr)); + ::rtl::Reference<CNode> const pNode = + CNode::getCNode(reinterpret_cast<xmlNodePtr>(pAttr)); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); const xmlChar* xName = pAttr->name; @@ -168,7 +170,7 @@ namespace DOM // recurse for (xmlNodePtr pChild = m_aNodePtr->children; pChild != 0; pChild = pChild->next) { - CNode * pNode = CNode::get(pChild); + ::rtl::Reference<CNode> const pNode = CNode::getCNode(pChild); OSL_ENSURE(pNode != 0, "CNode::get returned 0"); pNode->fastSaxify(i_rContext); } @@ -221,15 +223,21 @@ namespace DOM Reference< XAttr > CElement::getAttributeNode(const OUString& name) throw (RuntimeException) { - Reference< XAttr > aAttr; - if (m_aNodePtr != NULL) - { - OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); - xmlChar *xName = (xmlChar*)o1.getStr(); - xmlAttrPtr pAttr = xmlHasProp(m_aNodePtr, xName); - aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + if (0 == m_aNodePtr) { + return 0; } - return aAttr; + OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); + xmlChar const*const pName = + reinterpret_cast<xmlChar const*>(o1.getStr()); + xmlAttrPtr const pAttr = xmlHasProp(m_aNodePtr, pName); + if (0 == pAttr) { + return 0; + } + Reference< XAttr > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pAttr)).get()), + UNO_QUERY_THROW); + return xRet; } /** @@ -239,17 +247,24 @@ namespace DOM const OUString& namespaceURI, const OUString& localName) throw (RuntimeException) { - Reference< XAttr > aAttr; - if (m_aNodePtr != NULL) - { - OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); - xmlChar *xName = (xmlChar*)o1.getStr(); - OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); - xmlChar *xNS = (xmlChar*)o2.getStr(); - xmlAttrPtr pAttr = xmlHasNsProp(m_aNodePtr, xName, xNS); - aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)pAttr))); + if (0 == m_aNodePtr) { + return 0; } - return aAttr; + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar const*const pName = + reinterpret_cast<xmlChar const*>(o1.getStr()); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar const*const pNS = + reinterpret_cast<xmlChar const*>(o2.getStr()); + xmlAttrPtr const pAttr = xmlHasNsProp(m_aNodePtr, pName, pNS); + if (0 == pAttr) { + return 0; + } + Reference< XAttr > const xRet( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(pAttr)).get()), + UNO_QUERY_THROW); + return xRet; } /** @@ -259,21 +274,24 @@ namespace DOM OUString CElement::getAttributeNS(const OUString& namespaceURI, const OUString& localName) throw (RuntimeException) { - OUString aValue; - // search properties - if (m_aNodePtr != NULL) - { - OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); - xmlChar *xName = (xmlChar*)o1.getStr(); - OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); - xmlChar *xNS = (xmlChar*)o2.getStr(); - xmlChar *xValue = (xmlChar*)xmlGetNsProp(m_aNodePtr, xName, xNS); - if (xValue != NULL) { - aValue = OUString((sal_Char*)xValue, strlen((char*)xValue), RTL_TEXTENCODING_UTF8); - xmlFree(xValue); - } + if (0 == m_aNodePtr) { + return ::rtl::OUString(); } - return aValue; + OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); + xmlChar const*const pName = + reinterpret_cast<xmlChar const*>(o1.getStr()); + OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); + xmlChar const*const pNS = + reinterpret_cast<xmlChar const*>(o2.getStr()); + xmlChar *const pValue = xmlGetNsProp(m_aNodePtr, pName, pNS); + if (0 == pValue) { + return ::rtl::OUString(); + } + OUString const ret(reinterpret_cast<sal_Char const*>(pValue), + strlen(reinterpret_cast<char const*>(pValue)), + RTL_TEXTENCODING_UTF8); + xmlFree(pValue); + return ret; } /** @@ -403,7 +421,7 @@ namespace DOM aAttr = oldAttr->getOwnerDocument()->createAttribute(oldAttr->getName()); aAttr->setValue(oldAttr->getValue()); xmlRemoveProp(pAttr); - CNode *const pCNode( CNode::get(pNode) ); + ::rtl::Reference<CNode> const pCNode( CNode::getCNode(pNode) ); pCNode->m_aNodePtr = NULL; // freed by xmlRemoveProp } return aAttr; @@ -455,7 +473,10 @@ namespace DOM CNode::remove((xmlNodePtr)pAttr); // get the new attr node - aAttr = Reference< XAttr >(static_cast< CAttr* >(CNode::get((xmlNodePtr)res))); + aAttr = Reference< XAttr >( + static_cast< XNode* >(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(res)).get()), + UNO_QUERY_THROW); } if (aAttr.is()) diff --git a/unoxml/source/dom/elementlist.cxx b/unoxml/source/dom/elementlist.cxx index 8db7b2d3bcb4..03f2f0f5ae8b 100644 --- a/unoxml/source/dom/elementlist.cxx +++ b/unoxml/source/dom/elementlist.cxx @@ -63,7 +63,8 @@ namespace DOM { try { // get the XNode - Reference< XNode > xNode(CNode::get(static_cast<const CNode*>(pElement)->m_aNodePtr)); + Reference< XNode > const xNode( CNode::getCNode( + static_cast<const CNode*>(pElement)->m_aNodePtr).get() ); Reference< XEventTarget > xTarget(xNode, UNO_QUERY_THROW); OUString aType = OUString::createFromAscii("DOMSubtreeModified"); sal_Bool capture = sal_False; @@ -121,7 +122,9 @@ namespace DOM { if (index < 0) throw RuntimeException(); buildlist(static_cast<const CNode*>(m_pElement)->m_aNodePtr); - return Reference< XNode >(CNode::get(m_nodevector[index])); + Reference< XNode > const xRet( + CNode::getCNode(m_nodevector[index]).get()); + return xRet; } // tree mutations can change the list diff --git a/unoxml/source/dom/node.cxx b/unoxml/source/dom/node.cxx index c14389ed4ba9..7f7c7eb11f67 100644 --- a/unoxml/source/dom/node.cxx +++ b/unoxml/source/dom/node.cxx @@ -158,99 +158,105 @@ namespace DOM } - CNode* CNode::get(const xmlNodePtr aNode, sal_Bool bCreate) + ::rtl::Reference<CNode> + CNode::getCNode(xmlNodePtr const pNode, bool const bCreate) { - CNode* pNode = 0; - if (aNode == NULL) + if (0 == pNode) { return 0; + } //see CNode::remove ::osl::MutexGuard guard(NodeMutex::get()); //check whether there is already an instance for this node - nodemap_t::const_iterator i = CNode::theNodeMap.find(aNode); - if (i != CNode::theNodeMap.end()) - { - pNode = i->second; - } else - { + nodemap_t::const_iterator i = CNode::theNodeMap.find(pNode); + if (i != CNode::theNodeMap.end()) { + OSL_ASSERT(i->second); + return i->second; + } - // there is not yet an instance wrapping this node, - // create it and store it in the map - if (!bCreate) return NULL; + if (!bCreate) { return 0; } - switch (aNode->type) - { - case XML_ELEMENT_NODE: - // m_aNodeType = NodeType::ELEMENT_NODE; - pNode = static_cast< CNode* >(new CElement(aNode)); - break; - case XML_TEXT_NODE: - // m_aNodeType = NodeType::TEXT_NODE; - pNode = static_cast< CNode* >(new CText(aNode)); - break; - case XML_CDATA_SECTION_NODE: - // m_aNodeType = NodeType::CDATA_SECTION_NODE; - pNode = static_cast< CNode* >(new CCDATASection(aNode)); - break; - case XML_ENTITY_REF_NODE: - // m_aNodeType = NodeType::ENTITY_REFERENCE_NODE; - pNode = static_cast< CNode* >(new CEntityReference(aNode)); - break; - case XML_ENTITY_NODE: - // m_aNodeType = NodeType::ENTITY_NODE; - pNode = static_cast< CNode* >(new CEntity((xmlEntityPtr)aNode)); - break; - case XML_PI_NODE: - // m_aNodeType = NodeType::PROCESSING_INSTRUCTION_NODE; - pNode = static_cast< CNode* >(new CProcessingInstruction(aNode)); - break; - case XML_COMMENT_NODE: - // m_aNodeType = NodeType::COMMENT_NODE; - pNode = static_cast< CNode* >(new CComment(aNode)); - break; - case XML_DOCUMENT_NODE: - // m_aNodeType = NodeType::DOCUMENT_NODE; - pNode = static_cast< CNode* >(new CDocument((xmlDocPtr)aNode)); - break; - case XML_DOCUMENT_TYPE_NODE: - case XML_DTD_NODE: - // m_aNodeType = NodeType::DOCUMENT_TYPE_NODE; - pNode = static_cast< CNode* >(new CDocumentType((xmlDtdPtr)aNode)); - break; - case XML_DOCUMENT_FRAG_NODE: - // m_aNodeType = NodeType::DOCUMENT_FRAGMENT_NODE; - pNode = static_cast< CNode* >(new CDocumentFragment(aNode)); - break; - case XML_NOTATION_NODE: - // m_aNodeType = NodeType::NOTATION_NODE; - pNode = static_cast< CNode* >(new CNotation((xmlNotationPtr)aNode)); - break; - case XML_ATTRIBUTE_NODE: - // m_aNodeType = NodeType::NOTATION_NODE; - pNode = static_cast< CNode* >(new CAttr((xmlAttrPtr)aNode)); - break; - // unsupported node types - case XML_HTML_DOCUMENT_NODE: - case XML_ELEMENT_DECL: - case XML_ATTRIBUTE_DECL: - case XML_ENTITY_DECL: - case XML_NAMESPACE_DECL: - default: - pNode = 0; - break; - } + // there is not yet an instance wrapping this node, + // create it and store it in the map - if ( pNode != 0 ) - { - if(!CNode::theNodeMap.insert(nodemap_t::value_type(aNode, pNode)).second) - { - // if insertion failed, delete the new instance and return null - delete pNode; - pNode = 0; - } + ::rtl::Reference<CNode> pCNode; + switch (pNode->type) + { + case XML_ELEMENT_NODE: + // m_aNodeType = NodeType::ELEMENT_NODE; + pCNode = static_cast< CNode* >(new CElement(pNode)); + break; + case XML_TEXT_NODE: + // m_aNodeType = NodeType::TEXT_NODE; + pCNode = static_cast< CNode* >(new CText(pNode)); + break; + case XML_CDATA_SECTION_NODE: + // m_aNodeType = NodeType::CDATA_SECTION_NODE; + pCNode = static_cast< CNode* >(new CCDATASection(pNode)); + break; + case XML_ENTITY_REF_NODE: + // m_aNodeType = NodeType::ENTITY_REFERENCE_NODE; + pCNode = static_cast< CNode* >(new CEntityReference(pNode)); + break; + case XML_ENTITY_NODE: + // m_aNodeType = NodeType::ENTITY_NODE; + pCNode = static_cast< CNode* >(new CEntity( + reinterpret_cast<xmlEntityPtr>(pNode))); + break; + case XML_PI_NODE: + // m_aNodeType = NodeType::PROCESSING_INSTRUCTION_NODE; + pCNode = static_cast< CNode* >(new CProcessingInstruction(pNode)); + break; + case XML_COMMENT_NODE: + // m_aNodeType = NodeType::COMMENT_NODE; + pCNode = static_cast< CNode* >(new CComment(pNode)); + break; + case XML_DOCUMENT_NODE: + // m_aNodeType = NodeType::DOCUMENT_NODE; + pCNode = static_cast< CNode* >(new CDocument( + reinterpret_cast<xmlDocPtr>(pNode))); + break; + case XML_DOCUMENT_TYPE_NODE: + case XML_DTD_NODE: + // m_aNodeType = NodeType::DOCUMENT_TYPE_NODE; + pCNode = static_cast< CNode* >(new CDocumentType( + reinterpret_cast<xmlDtdPtr>(pNode))); + break; + case XML_DOCUMENT_FRAG_NODE: + // m_aNodeType = NodeType::DOCUMENT_FRAGMENT_NODE; + pCNode = static_cast< CNode* >(new CDocumentFragment(pNode)); + break; + case XML_NOTATION_NODE: + // m_aNodeType = NodeType::NOTATION_NODE; + pCNode = static_cast< CNode* >(new CNotation( + reinterpret_cast<xmlNotationPtr>(pNode))); + break; + case XML_ATTRIBUTE_NODE: + // m_aNodeType = NodeType::ATTRIBUTE_NODE; + pCNode = static_cast< CNode* >(new CAttr( + reinterpret_cast<xmlAttrPtr>(pNode))); + break; + // unsupported node types + case XML_HTML_DOCUMENT_NODE: + case XML_ELEMENT_DECL: + case XML_ATTRIBUTE_DECL: + case XML_ENTITY_DECL: + case XML_NAMESPACE_DECL: + default: + break; + } + + if (pCNode != 0) { + bool const bInserted = CNode::theNodeMap.insert( + nodemap_t::value_type(pNode, pCNode.get())).second; + OSL_ASSERT(bInserted); + if (!bInserted) { + // if insertion failed, delete new instance and return null + return 0; } } - OSL_ENSURE(pNode, "no node produced during CNode::get!"); - return pNode; + + OSL_ENSURE(pCNode.is(), "no node produced during CNode::getCNode!"); + return pCNode; } xmlNodePtr CNode::getNodePtr(const Reference< XNode >& aNode) @@ -383,7 +389,7 @@ namespace DOM Reference< XNode > CNode::appendChild(const Reference< XNode >& newChild) throw (RuntimeException, DOMException) { - CNode * pNode(0); + ::rtl::Reference<CNode> pNode; if (m_aNodePtr != NULL) { xmlNodePtr cur = CNode::getNodePtr(newChild.get()); @@ -459,28 +465,29 @@ namespace DOM // because that will not remove unneeded ns decls _nscleanup(res, m_aNodePtr); - pNode = CNode::get(res); + pNode = CNode::getCNode(res); } //XXX check for errors // dispatch DOMNodeInserted event, target is the new node // this node is the related node // does bubble - if (pNode) + if (pNode.is()) { pNode->m_bUnlinked = false; // will be deleted by xmlFreeDoc Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); Reference< XMutationEvent > event(docevent->createEvent( OUString::createFromAscii("DOMNodeInserted")), UNO_QUERY); event->initMutationEvent(OUString::createFromAscii("DOMNodeInserted") - , sal_True, sal_False, Reference< XNode >(CNode::get(m_aNodePtr)), + , sal_True, sal_False, + Reference< XNode >(CNode::getCNode(m_aNodePtr).get()), OUString(), OUString(), OUString(), (AttrChangeType)0 ); dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); // dispatch subtree modified for this node dispatchSubtreeModified(); } - return pNode; + return pNode.get(); } /** @@ -490,15 +497,14 @@ namespace DOM Reference< XNode > CNode::cloneNode(sal_Bool bDeep) throw (RuntimeException) { - CNode * pNode(0); - if (m_aNodePtr != NULL) - { - pNode = CNode::get( - xmlCopyNode(m_aNodePtr, (bDeep) ? 1 : 0)); - pNode->m_bUnlinked = true; // not linked yet + if (0 == m_aNodePtr) { + return 0; } + ::rtl::Reference<CNode> const pNode = CNode::getCNode( + xmlCopyNode(m_aNodePtr, (bDeep) ? 1 : 0)); + pNode->m_bUnlinked = true; // not linked yet //XXX check for errors - return pNode; + return pNode.get(); } /** @@ -525,13 +531,13 @@ namespace DOM Reference< XNodeList > CNode::getChildNodes() throw (RuntimeException) { - Reference< XNodeList > aNodeList; - if (m_aNodePtr != NULL) - { - aNodeList = Reference< XNodeList >(new CChildList(CNode::get(m_aNodePtr))); + if (0 == m_aNodePtr) { + return 0; } + Reference< XNodeList > const xNodeList( + new CChildList(*CNode::getCNode(m_aNodePtr))); // XXX check for errors? - return aNodeList; + return xNodeList; } /** @@ -540,11 +546,12 @@ namespace DOM Reference< XNode > CNode::getFirstChild() throw (RuntimeException) { - Reference< XNode > aNode; - if (m_aNodePtr != NULL) { - aNode = Reference< XNode >(CNode::get(m_aNodePtr->children)); + if (0 == m_aNodePtr) { + return 0; } - return aNode; + Reference< XNode > const xNode( + CNode::getCNode(m_aNodePtr->children).get()); + return xNode; } /** @@ -553,11 +560,12 @@ namespace DOM Reference< XNode > SAL_CALL CNode::getLastChild() throw (RuntimeException) { - Reference< XNode > aNode; - if (m_aNodePtr != NULL) { - aNode = Reference< XNode >(CNode::get(xmlGetLastChild(m_aNodePtr))); + if (0 == m_aNodePtr) { + return 0; } - return aNode; + Reference< XNode > const xNode( + CNode::getCNode(xmlGetLastChild(m_aNodePtr)).get()); + return xNode; } /** @@ -603,12 +611,11 @@ namespace DOM Reference< XNode > SAL_CALL CNode::getNextSibling() throw (RuntimeException) { - Reference< XNode > aNode; - if(m_aNodePtr != NULL) - { - aNode = Reference< XNode >(CNode::get(m_aNodePtr->next)); + if (0 == m_aNodePtr) { + return 0; } - return aNode; + Reference< XNode > const xNode(CNode::getCNode(m_aNodePtr->next).get()); + return xNode; } /** @@ -664,14 +671,14 @@ namespace DOM Reference< XDocument > SAL_CALL CNode::getOwnerDocument() throw (RuntimeException) { - Reference<XDocument> aDoc; - if (m_aNodePtr != NULL) - { - aDoc = Reference< XDocument >(static_cast< CDocument* >( - CNode::get((xmlNodePtr)m_aNodePtr->doc))); + if (0 == m_aNodePtr) { + return 0; } - return aDoc; - + Reference< XDocument > const xDoc( + static_cast<XNode*>(CNode::getCNode( + reinterpret_cast<xmlNodePtr>(m_aNodePtr->doc)).get()), + UNO_QUERY_THROW); + return xDoc; } /** @@ -680,12 +687,12 @@ namespace DOM Reference< XNode > SAL_CALL CNode::getParentNode() throw (RuntimeException) { - Reference<XNode> aNode; - if (m_aNodePtr != NULL) - { - aNode = Reference< XNode >(CNode::get(m_aNodePtr->parent)); + if (0 == m_aNodePtr) { + return 0; } - return aNode; + Reference< XNode > const xNode( + CNode::getCNode(m_aNodePtr->parent).get()); + return xNode; } /** @@ -713,12 +720,12 @@ namespace DOM Reference< XNode > SAL_CALL CNode::getPreviousSibling() throw (RuntimeException) { - Reference< XNode > aNode; - if (m_aNodePtr != NULL) - { - aNode = Reference< XNode >(CNode::get(m_aNodePtr->prev)); + if (0 == m_aNodePtr) { + return 0; } - return aNode; + Reference< XNode > const xNode( + CNode::getCNode(m_aNodePtr->prev).get()); + return xNode; } /** @@ -772,7 +779,7 @@ namespace DOM cur->prev = pNewChild; if( pNewChild->prev != NULL) pNewChild->prev->next = pNewChild; - CNode *const pNode( CNode::get(pNewChild) ); + ::rtl::Reference<CNode> const pNode(CNode::getCNode(pNewChild)); pNode->m_bUnlinked = false; // will be deleted by xmlFreeDoc } cur = cur->next; @@ -824,7 +831,7 @@ namespace DOM Reference<XNode> xReturn( oldChild ); xmlNodePtr old = CNode::getNodePtr(oldChild); - CNode *const pOld( CNode::get(old) ); + ::rtl::Reference<CNode> const pOld( CNode::getCNode(old) ); pOld->m_bUnlinked = true; if( old->type == XML_ATTRIBUTE_NODE ) @@ -853,7 +860,8 @@ namespace DOM Reference< XMutationEvent > event(docevent->createEvent( OUString::createFromAscii("DOMNodeRemoved")), UNO_QUERY); event->initMutationEvent(OUString::createFromAscii("DOMNodeRemoved"), sal_True, - sal_False, Reference< XNode >(CNode::get(m_aNodePtr)), + sal_False, + Reference< XNode >(CNode::getCNode(m_aNodePtr).get()), OUString(), OUString(), OUString(), (AttrChangeType)0 ); dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); @@ -901,7 +909,7 @@ namespace DOM xmlAttrPtr pAttr = (xmlAttrPtr)pOld; xmlRemoveProp( pAttr ); - CNode *const pOldNode( CNode::get(pOld) ); + ::rtl::Reference<CNode> const pOldNode( CNode::getCNode(pOld) ); pOldNode->m_aNodePtr = NULL; // freed by xmlRemoveProp appendChild( newChild ); } @@ -929,9 +937,9 @@ namespace DOM pOld->next = NULL; pOld->prev = NULL; pOld->parent = NULL; - CNode *const pOldNode( CNode::get(pOld) ); + ::rtl::Reference<CNode> const pOldNode(CNode::getCNode(pOld)); pOldNode->m_bUnlinked = true; - CNode *const pNewNode( CNode::get(pNew) ); + ::rtl::Reference<CNode> const pNewNode(CNode::getCNode(pNew)); pNewNode->m_bUnlinked = false; // will be deleted by xmlFreeDoc } cur = cur->next; diff --git a/unoxml/source/dom/node.hxx b/unoxml/source/dom/node.hxx index 72702bd9128d..f8fff76a49c3 100644 --- a/unoxml/source/dom/node.hxx +++ b/unoxml/source/dom/node.hxx @@ -146,8 +146,9 @@ namespace DOM virtual ~CNode(); - // get a representaion for a libxml node - static CNode* get(const xmlNodePtr aNode, sal_Bool bCreate = sal_True); + /// get UNO wrapper instance for a libxml node + static ::rtl::Reference<CNode> getCNode( + xmlNodePtr const pNode, bool const bCreate = true); // remove a wrapper instance static void remove(const xmlNodePtr aNode); diff --git a/unoxml/source/events/eventdispatcher.cxx b/unoxml/source/events/eventdispatcher.cxx index 4b1c1548bf38..10047a874998 100644 --- a/unoxml/source/events/eventdispatcher.cxx +++ b/unoxml/source/events/eventdispatcher.cxx @@ -136,7 +136,8 @@ namespace DOM { namespace events { pEvent->initEvent( aType, aEvent->getBubbles(), aEvent->getCancelable()); } - pEvent->m_target = Reference< XEventTarget >(DOM::CNode::get(aNodePtr)); + pEvent->m_target = + Reference< XEventTarget >(DOM::CNode::getCNode(aNodePtr).get()); pEvent->m_currentTarget = aEvent->getCurrentTarget(); pEvent->m_time = aEvent->getTimeStamp(); @@ -170,7 +171,8 @@ namespace DOM { namespace events { while (inode != captureVector.begin()) { //pEvent->m_currentTarget = *inode; - pEvent->m_currentTarget = Reference< XEventTarget >(DOM::CNode::get(*inode)); + pEvent->m_currentTarget = Reference< XEventTarget >( + DOM::CNode::getCNode(*inode).get()); callListeners(*inode, aType, xEvent, sal_True); if (pEvent->m_canceled) return sal_True; inode--; @@ -186,7 +188,8 @@ namespace DOM { namespace events { pEvent->m_phase = PhaseType_BUBBLING_PHASE; while (inode != captureVector.end()) { - pEvent->m_currentTarget = Reference< XEventTarget >(DOM::CNode::get(*inode)); + pEvent->m_currentTarget = Reference< XEventTarget >( + DOM::CNode::getCNode(*inode).get()); callListeners(*inode, aType, xEvent, sal_False); if (pEvent->m_canceled) return sal_True; inode++; diff --git a/unoxml/source/xpath/nodelist.cxx b/unoxml/source/xpath/nodelist.cxx index 0c18a65ab0e9..6240d01d7101 100644 --- a/unoxml/source/xpath/nodelist.cxx +++ b/unoxml/source/xpath/nodelist.cxx @@ -59,10 +59,12 @@ namespace XPath */ Reference< XNode > SAL_CALL CNodeList::item(sal_Int32 index) throw (RuntimeException) { - Reference< XNode > aNode; - if (m_pNodeSet != NULL) - aNode = Reference< XNode >(DOM::CNode::get(xmlXPathNodeSetItem(m_pNodeSet, index))); - return aNode; + if (0 == m_pNodeSet) { + return 0; + } + Reference< XNode > const xNode( + DOM::CNode::getCNode(xmlXPathNodeSetItem(m_pNodeSet, index)).get()); + return xNode; } } |