Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / unoxml / source / dom / attributesmap.cxx
blob9ff0298bdc7b755c5611696107b9bf3c62de7347
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 return m_pElement->removeAttributeNode(xAttr);
171 // Removes a node specified by local name and namespace URI.
173 Reference< XNode > SAL_CALL
174 CAttributesMap::removeNamedItemNS(
175 OUString const& namespaceURI, OUString const& localName)
177 // no MutexGuard needed: m_pElement is const
178 Reference< XAttr > const xAttr(
179 m_pElement->getAttributeNodeNS(namespaceURI, localName));
180 if (!xAttr.is()) {
181 throw DOMException(
182 "CAttributesMap::removeNamedItemNS: no such attribute",
183 static_cast<OWeakObject*>(this),
184 DOMExceptionType_NOT_FOUND_ERR);
186 return m_pElement->removeAttributeNode(xAttr);
190 // Adds a node using its nodeName attribute.
192 Reference< XNode > SAL_CALL
193 CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
195 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
196 if (!xNode.is()) {
197 throw DOMException(
198 "CAttributesMap::setNamedItem: XAttr argument expected",
199 static_cast<OWeakObject*>(this),
200 DOMExceptionType_HIERARCHY_REQUEST_ERR);
202 // no MutexGuard needed: m_pElement is const
203 return m_pElement->setAttributeNode(xAttr);
207 Adds a node using its namespaceURI and localName.
209 Reference< XNode > SAL_CALL
210 CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
212 Reference< XAttr > const xAttr(xNode, UNO_QUERY);
213 if (!xNode.is()) {
214 throw DOMException(
215 "CAttributesMap::setNamedItemNS: XAttr argument expected",
216 static_cast<OWeakObject*>(this),
217 DOMExceptionType_HIERARCHY_REQUEST_ERR);
219 // no MutexGuard needed: m_pElement is const
220 return m_pElement->setAttributeNodeNS(xAttr);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */