Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / unoxml / source / dom / attributesmap.cxx
blob4c0a86c170743cf03eb0c340ed65503d375a1132
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "attributesmap.hxx"
22 #include <string.h>
24 #include "element.hxx"
25 #include "document.hxx"
27 using namespace css::uno;
28 using namespace css::xml::dom;
30 namespace DOM
32 CAttributesMap::CAttributesMap(::rtl::Reference<CElement> const& pElement,
33 ::osl::Mutex & rMutex)
34 : m_pElement(pElement)
35 , m_rMutex(rMutex)
39 /**
40 The number of nodes in this map.
42 sal_Int32 SAL_CALL CAttributesMap::getLength()
44 ::osl::MutexGuard const g(m_rMutex);
46 sal_Int32 count = 0;
47 xmlNodePtr pNode = m_pElement->GetNodePtr();
48 if (pNode != nullptr)
50 xmlAttrPtr cur = pNode->properties;
51 while (cur != nullptr)
53 count++;
54 cur = cur->next;
57 return count;
60 /**
61 Retrieves a node specified by local name
63 Reference< XNode > SAL_CALL
64 CAttributesMap::getNamedItem(OUString const& name)
66 ::osl::MutexGuard const g(m_rMutex);
68 Reference< XNode > aNode;
69 xmlNodePtr pNode = m_pElement->GetNodePtr();
70 if (pNode != nullptr)
72 OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
73 xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
74 xmlAttrPtr cur = pNode->properties;
75 while (cur != nullptr)
77 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0)
79 aNode.set( m_pElement->GetOwnerDocument().GetCNode(
80 reinterpret_cast<xmlNodePtr>(cur)).get() );
81 break;
83 cur = cur->next;
86 return aNode;
89 /**
90 Retrieves a node specified by local name and namespace URI.
92 Reference< XNode > SAL_CALL
93 CAttributesMap::getNamedItemNS(
94 OUString const& namespaceURI, OUString const& localName)
96 ::osl::MutexGuard const g(m_rMutex);
98 Reference< XNode > aNode;
99 xmlNodePtr pNode = m_pElement->GetNodePtr();
100 if (pNode != nullptr)
102 OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
103 xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
104 OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
105 xmlChar const* pSearchNs =
106 reinterpret_cast<xmlChar const*>(o2.getStr());
107 xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, pSearchNs);
108 xmlAttrPtr cur = pNode->properties;
109 while (cur != nullptr && pNs != nullptr)
111 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0 &&
112 cur->ns == pNs)
114 aNode.set( m_pElement->GetOwnerDocument().GetCNode(
115 reinterpret_cast<xmlNodePtr>(cur)).get() );
116 break;
118 cur = cur->next;
121 return aNode;
125 Returns the indexth item in the map.
127 Reference< XNode > SAL_CALL
128 CAttributesMap::item(sal_Int32 index)
130 ::osl::MutexGuard const g(m_rMutex);
132 Reference< XNode > aNode;
133 xmlNodePtr pNode = m_pElement->GetNodePtr();
134 if (pNode != nullptr)
136 xmlAttrPtr cur = pNode->properties;
137 sal_Int32 count = 0;
138 while (cur != nullptr)
140 if (count == index)
142 aNode.set( m_pElement->GetOwnerDocument().GetCNode(
143 reinterpret_cast<xmlNodePtr>(cur)).get() );
144 break;
146 count++;
147 cur = cur->next;
150 return aNode;
154 Removes a node specified by name.
156 Reference< XNode > SAL_CALL
157 CAttributesMap::removeNamedItem(OUString const& name)
159 // no MutexGuard needed: m_pElement is const
160 Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
161 if (!xAttr.is()) {
162 throw DOMException(
163 "CAttributesMap::removeNamedItem: no such attribute",
164 static_cast<OWeakObject*>(this),
165 DOMExceptionType_NOT_FOUND_ERR);
167 Reference< XNode > const xRet(
168 m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
169 return xRet;
173 // Removes a node specified by local name and namespace URI.
175 Reference< XNode > SAL_CALL
176 CAttributesMap::removeNamedItemNS(
177 OUString const& namespaceURI, OUString const& localName)
179 // no MutexGuard needed: m_pElement is const
180 Reference< XAttr > const xAttr(
181 m_pElement->getAttributeNodeNS(namespaceURI, localName));
182 if (!xAttr.is()) {
183 throw DOMException(
184 "CAttributesMap::removeNamedItemNS: no such attribute",
185 static_cast<OWeakObject*>(this),
186 DOMExceptionType_NOT_FOUND_ERR);
188 Reference< XNode > const xRet(
189 m_pElement->removeAttributeNode(xAttr), UNO_QUERY);
190 return xRet;
194 // Adds a node using its nodeName attribute.
196 Reference< XNode > SAL_CALL
197 CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
199 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
200 if (!xNode.is()) {
201 throw DOMException(
202 "CAttributesMap::setNamedItem: XAttr argument expected",
203 static_cast<OWeakObject*>(this),
204 DOMExceptionType_HIERARCHY_REQUEST_ERR);
206 // no MutexGuard needed: m_pElement is const
207 Reference< XNode > const xRet(
208 m_pElement->setAttributeNode(xAttr), UNO_QUERY);
209 return xRet;
213 Adds a node using its namespaceURI and localName.
215 Reference< XNode > SAL_CALL
216 CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
218 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
219 if (!xNode.is()) {
220 throw DOMException(
221 "CAttributesMap::setNamedItemNS: XAttr argument expected",
222 static_cast<OWeakObject*>(this),
223 DOMExceptionType_HIERARCHY_REQUEST_ERR);
225 // no MutexGuard needed: m_pElement is const
226 Reference< XNode > const xRet(
227 m_pElement->setAttributeNodeNS(xAttr), UNO_QUERY);
228 return xRet;
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */