1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include <attributesmap.hxx> 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <string.h> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <element.hxx> 33*cdf0e10cSrcweir #include <document.hxx> 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir namespace DOM 37*cdf0e10cSrcweir { 38*cdf0e10cSrcweir CAttributesMap::CAttributesMap(::rtl::Reference<CElement> const& pElement, 39*cdf0e10cSrcweir ::osl::Mutex & rMutex) 40*cdf0e10cSrcweir : m_pElement(pElement) 41*cdf0e10cSrcweir , m_rMutex(rMutex) 42*cdf0e10cSrcweir { 43*cdf0e10cSrcweir } 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir /** 46*cdf0e10cSrcweir The number of nodes in this map. 47*cdf0e10cSrcweir */ 48*cdf0e10cSrcweir sal_Int32 SAL_CALL CAttributesMap::getLength() throw (RuntimeException) 49*cdf0e10cSrcweir { 50*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir sal_Int32 count = 0; 53*cdf0e10cSrcweir xmlNodePtr pNode = m_pElement->GetNodePtr(); 54*cdf0e10cSrcweir if (pNode != NULL) 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir xmlAttrPtr cur = pNode->properties; 57*cdf0e10cSrcweir while (cur != NULL) 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir count++; 60*cdf0e10cSrcweir cur = cur->next; 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir return count; 64*cdf0e10cSrcweir } 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir /** 67*cdf0e10cSrcweir Retrieves a node specified by local name 68*cdf0e10cSrcweir */ 69*cdf0e10cSrcweir Reference< XNode > SAL_CALL 70*cdf0e10cSrcweir CAttributesMap::getNamedItem(OUString const& name) throw (RuntimeException) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir Reference< XNode > aNode; 75*cdf0e10cSrcweir xmlNodePtr pNode = m_pElement->GetNodePtr(); 76*cdf0e10cSrcweir if (pNode != NULL) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8); 79*cdf0e10cSrcweir xmlChar* xName = (xmlChar*)o1.getStr(); 80*cdf0e10cSrcweir xmlAttrPtr cur = pNode->properties; 81*cdf0e10cSrcweir while (cur != NULL) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir if( strcmp((char*)xName, (char*)cur->name) == 0) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir aNode = Reference< XNode >( 86*cdf0e10cSrcweir m_pElement->GetOwnerDocument().GetCNode( 87*cdf0e10cSrcweir reinterpret_cast<xmlNodePtr>(cur)).get() ); 88*cdf0e10cSrcweir break; 89*cdf0e10cSrcweir } 90*cdf0e10cSrcweir cur = cur->next; 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir } 93*cdf0e10cSrcweir return aNode; 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir /** 97*cdf0e10cSrcweir Retrieves a node specified by local name and namespace URI. 98*cdf0e10cSrcweir */ 99*cdf0e10cSrcweir Reference< XNode > SAL_CALL 100*cdf0e10cSrcweir CAttributesMap::getNamedItemNS( 101*cdf0e10cSrcweir OUString const& namespaceURI, OUString const& localName) 102*cdf0e10cSrcweir throw (RuntimeException) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir Reference< XNode > aNode; 107*cdf0e10cSrcweir xmlNodePtr pNode = m_pElement->GetNodePtr(); 108*cdf0e10cSrcweir if (pNode != NULL) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8); 111*cdf0e10cSrcweir xmlChar* xName = (xmlChar*)o1.getStr(); 112*cdf0e10cSrcweir OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8); 113*cdf0e10cSrcweir xmlChar const*const xNs = 114*cdf0e10cSrcweir reinterpret_cast<xmlChar const*>(o2.getStr()); 115*cdf0e10cSrcweir xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, xNs); 116*cdf0e10cSrcweir xmlAttrPtr cur = pNode->properties; 117*cdf0e10cSrcweir while (cur != NULL && pNs != NULL) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir if( strcmp((char*)xName, (char*)cur->name) == 0 && 120*cdf0e10cSrcweir cur->ns == pNs) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir aNode = Reference< XNode >( 123*cdf0e10cSrcweir m_pElement->GetOwnerDocument().GetCNode( 124*cdf0e10cSrcweir reinterpret_cast<xmlNodePtr>(cur)).get() ); 125*cdf0e10cSrcweir break; 126*cdf0e10cSrcweir } 127*cdf0e10cSrcweir cur = cur->next; 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir return aNode; 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir /** 134*cdf0e10cSrcweir Returns the indexth item in the map. 135*cdf0e10cSrcweir */ 136*cdf0e10cSrcweir Reference< XNode > SAL_CALL 137*cdf0e10cSrcweir CAttributesMap::item(sal_Int32 index) throw (RuntimeException) 138*cdf0e10cSrcweir { 139*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir Reference< XNode > aNode; 142*cdf0e10cSrcweir xmlNodePtr pNode = m_pElement->GetNodePtr(); 143*cdf0e10cSrcweir if (pNode != NULL) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir xmlAttrPtr cur = pNode->properties; 146*cdf0e10cSrcweir sal_Int32 count = 0; 147*cdf0e10cSrcweir while (cur != NULL) 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir if (count == index) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir aNode = Reference< XNode >( 152*cdf0e10cSrcweir m_pElement->GetOwnerDocument().GetCNode( 153*cdf0e10cSrcweir reinterpret_cast<xmlNodePtr>(cur)).get() ); 154*cdf0e10cSrcweir break; 155*cdf0e10cSrcweir } 156*cdf0e10cSrcweir count++; 157*cdf0e10cSrcweir cur = cur->next; 158*cdf0e10cSrcweir } 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir return aNode; 161*cdf0e10cSrcweir } 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir /** 164*cdf0e10cSrcweir Removes a node specified by name. 165*cdf0e10cSrcweir */ 166*cdf0e10cSrcweir Reference< XNode > SAL_CALL 167*cdf0e10cSrcweir CAttributesMap::removeNamedItem(OUString const& name) 168*cdf0e10cSrcweir throw (RuntimeException) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir // no MutexGuard needed: m_pElement is const 171*cdf0e10cSrcweir Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name)); 172*cdf0e10cSrcweir if (!xAttr.is()) { 173*cdf0e10cSrcweir throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM( 174*cdf0e10cSrcweir "CAttributesMap::removeNamedItem: no such attribute")), 175*cdf0e10cSrcweir static_cast<OWeakObject*>(this), 176*cdf0e10cSrcweir DOMExceptionType_NOT_FOUND_ERR); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir Reference< XNode > const xRet( 179*cdf0e10cSrcweir m_pElement->removeAttributeNode(xAttr), UNO_QUERY); 180*cdf0e10cSrcweir return xRet; 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir /** 184*cdf0e10cSrcweir // Removes a node specified by local name and namespace URI. 185*cdf0e10cSrcweir */ 186*cdf0e10cSrcweir Reference< XNode > SAL_CALL 187*cdf0e10cSrcweir CAttributesMap::removeNamedItemNS( 188*cdf0e10cSrcweir OUString const& namespaceURI, OUString const& localName) 189*cdf0e10cSrcweir throw (RuntimeException) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir // no MutexGuard needed: m_pElement is const 192*cdf0e10cSrcweir Reference< XAttr > const xAttr( 193*cdf0e10cSrcweir m_pElement->getAttributeNodeNS(namespaceURI, localName)); 194*cdf0e10cSrcweir if (!xAttr.is()) { 195*cdf0e10cSrcweir throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM( 196*cdf0e10cSrcweir "CAttributesMap::removeNamedItemNS: no such attribute")), 197*cdf0e10cSrcweir static_cast<OWeakObject*>(this), 198*cdf0e10cSrcweir DOMExceptionType_NOT_FOUND_ERR); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir Reference< XNode > const xRet( 201*cdf0e10cSrcweir m_pElement->removeAttributeNode(xAttr), UNO_QUERY); 202*cdf0e10cSrcweir return xRet; 203*cdf0e10cSrcweir } 204*cdf0e10cSrcweir 205*cdf0e10cSrcweir /** 206*cdf0e10cSrcweir // Adds a node using its nodeName attribute. 207*cdf0e10cSrcweir */ 208*cdf0e10cSrcweir Reference< XNode > SAL_CALL 209*cdf0e10cSrcweir CAttributesMap::setNamedItem(Reference< XNode > const& xNode) 210*cdf0e10cSrcweir throw (RuntimeException) 211*cdf0e10cSrcweir { 212*cdf0e10cSrcweir Reference< XAttr > const xAttr(xNode, UNO_QUERY); 213*cdf0e10cSrcweir if (!xNode.is()) { 214*cdf0e10cSrcweir throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM( 215*cdf0e10cSrcweir "CAttributesMap::setNamedItem: XAttr argument expected")), 216*cdf0e10cSrcweir static_cast<OWeakObject*>(this), 217*cdf0e10cSrcweir DOMExceptionType_HIERARCHY_REQUEST_ERR); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir // no MutexGuard needed: m_pElement is const 220*cdf0e10cSrcweir Reference< XNode > const xRet( 221*cdf0e10cSrcweir m_pElement->setAttributeNode(xAttr), UNO_QUERY); 222*cdf0e10cSrcweir return xRet; 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir 225*cdf0e10cSrcweir /** 226*cdf0e10cSrcweir Adds a node using its namespaceURI and localName. 227*cdf0e10cSrcweir */ 228*cdf0e10cSrcweir Reference< XNode > SAL_CALL 229*cdf0e10cSrcweir CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode) 230*cdf0e10cSrcweir throw (RuntimeException) 231*cdf0e10cSrcweir { 232*cdf0e10cSrcweir Reference< XAttr > const xAttr(xNode, UNO_QUERY); 233*cdf0e10cSrcweir if (!xNode.is()) { 234*cdf0e10cSrcweir throw DOMException(OUString(RTL_CONSTASCII_USTRINGPARAM( 235*cdf0e10cSrcweir "CAttributesMap::setNamedItemNS: XAttr argument expected")), 236*cdf0e10cSrcweir static_cast<OWeakObject*>(this), 237*cdf0e10cSrcweir DOMExceptionType_HIERARCHY_REQUEST_ERR); 238*cdf0e10cSrcweir } 239*cdf0e10cSrcweir // no MutexGuard needed: m_pElement is const 240*cdf0e10cSrcweir Reference< XNode > const xRet( 241*cdf0e10cSrcweir m_pElement->setAttributeNodeNS(xAttr), UNO_QUERY); 242*cdf0e10cSrcweir return xRet; 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir } 245