Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / unoxml / source / dom / attributesmap.cxx
blobf629d4cb3f31baf23d2a178924c1ffe8ed14d076
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 <com/sun/star/xml/dom/DOMException.hpp>
25 #include <utility>
27 #include "element.hxx"
28 #include "document.hxx"
30 using namespace css::uno;
31 using namespace css::xml::dom;
33 namespace DOM
35 CAttributesMap::CAttributesMap(::rtl::Reference<CElement> pElement,
36 ::osl::Mutex & rMutex)
37 : m_pElement(std::move(pElement))
38 , m_rMutex(rMutex)
42 /**
43 The number of nodes in this map.
45 sal_Int32 SAL_CALL CAttributesMap::getLength()
47 ::osl::MutexGuard const g(m_rMutex);
49 sal_Int32 count = 0;
50 xmlNodePtr pNode = m_pElement->GetNodePtr();
51 if (pNode != nullptr)
53 xmlAttrPtr cur = pNode->properties;
54 while (cur != nullptr)
56 count++;
57 cur = cur->next;
60 return count;
63 /**
64 Retrieves a node specified by local name
66 Reference< XNode > SAL_CALL
67 CAttributesMap::getNamedItem(OUString const& name)
69 ::osl::MutexGuard const g(m_rMutex);
71 Reference< XNode > aNode;
72 xmlNodePtr pNode = m_pElement->GetNodePtr();
73 if (pNode != nullptr)
75 OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
76 xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
77 xmlAttrPtr cur = pNode->properties;
78 while (cur != nullptr)
80 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0)
82 aNode = m_pElement->GetOwnerDocument().GetCNode(
83 reinterpret_cast<xmlNodePtr>(cur));
84 break;
86 cur = cur->next;
89 return aNode;
92 /**
93 Retrieves a node specified by local name and namespace URI.
95 Reference< XNode > SAL_CALL
96 CAttributesMap::getNamedItemNS(
97 OUString const& namespaceURI, OUString const& localName)
99 ::osl::MutexGuard const g(m_rMutex);
101 Reference< XNode > aNode;
102 xmlNodePtr pNode = m_pElement->GetNodePtr();
103 if (pNode != nullptr)
105 OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
106 xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
107 OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
108 xmlChar const* pSearchNs =
109 reinterpret_cast<xmlChar const*>(o2.getStr());
110 xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, pSearchNs);
111 xmlAttrPtr cur = pNode->properties;
112 while (cur != nullptr && pNs != nullptr)
114 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0 &&
115 cur->ns == pNs)
117 aNode = m_pElement->GetOwnerDocument().GetCNode(
118 reinterpret_cast<xmlNodePtr>(cur));
119 break;
121 cur = cur->next;
124 return aNode;
128 Returns the indexth item in the map.
130 Reference< XNode > SAL_CALL
131 CAttributesMap::item(sal_Int32 index)
133 ::osl::MutexGuard const g(m_rMutex);
135 Reference< XNode > aNode;
136 xmlNodePtr pNode = m_pElement->GetNodePtr();
137 if (pNode != nullptr)
139 xmlAttrPtr cur = pNode->properties;
140 sal_Int32 count = 0;
141 while (cur != nullptr)
143 if (count == index)
145 aNode = m_pElement->GetOwnerDocument().GetCNode(
146 reinterpret_cast<xmlNodePtr>(cur));
147 break;
149 count++;
150 cur = cur->next;
153 return aNode;
157 Removes a node specified by name.
159 Reference< XNode > SAL_CALL
160 CAttributesMap::removeNamedItem(OUString const& name)
162 // no MutexGuard needed: m_pElement is const
163 Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
164 if (!xAttr.is()) {
165 throw DOMException(
166 "CAttributesMap::removeNamedItem: no such attribute",
167 getXWeak(),
168 DOMExceptionType_NOT_FOUND_ERR);
170 return m_pElement->removeAttributeNode(xAttr);
174 // Removes a node specified by local name and namespace URI.
176 Reference< XNode > SAL_CALL
177 CAttributesMap::removeNamedItemNS(
178 OUString const& namespaceURI, OUString const& localName)
180 // no MutexGuard needed: m_pElement is const
181 Reference< XAttr > const xAttr(
182 m_pElement->getAttributeNodeNS(namespaceURI, localName));
183 if (!xAttr.is()) {
184 throw DOMException(
185 "CAttributesMap::removeNamedItemNS: no such attribute",
186 getXWeak(),
187 DOMExceptionType_NOT_FOUND_ERR);
189 return m_pElement->removeAttributeNode(xAttr);
193 // Adds a node using its nodeName attribute.
195 Reference< XNode > SAL_CALL
196 CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
198 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
199 if (!xNode.is()) {
200 throw DOMException(
201 "CAttributesMap::setNamedItem: XAttr argument expected",
202 getXWeak(),
203 DOMExceptionType_HIERARCHY_REQUEST_ERR);
205 // no MutexGuard needed: m_pElement is const
206 return m_pElement->setAttributeNode(xAttr);
210 Adds a node using its namespaceURI and localName.
212 Reference< XNode > SAL_CALL
213 CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
215 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
216 if (!xNode.is()) {
217 throw DOMException(
218 "CAttributesMap::setNamedItemNS: XAttr argument expected",
219 getXWeak(),
220 DOMExceptionType_HIERARCHY_REQUEST_ERR);
222 // no MutexGuard needed: m_pElement is const
223 return m_pElement->setAttributeNodeNS(xAttr);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */