1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
24 #include <com/sun/star/xml/dom/DOMException.hpp>
27 #include "element.hxx"
28 #include "document.hxx"
30 using namespace css::uno
;
31 using namespace css::xml::dom
;
35 CAttributesMap::CAttributesMap(::rtl::Reference
<CElement
> pElement
,
36 ::osl::Mutex
& rMutex
)
37 : m_pElement(std::move(pElement
))
43 The number of nodes in this map.
45 sal_Int32 SAL_CALL
CAttributesMap::getLength()
47 ::osl::MutexGuard
const g(m_rMutex
);
50 xmlNodePtr pNode
= m_pElement
->GetNodePtr();
53 xmlAttrPtr cur
= pNode
->properties
;
54 while (cur
!= nullptr)
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();
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
));
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 &&
117 aNode
= m_pElement
->GetOwnerDocument().GetCNode(
118 reinterpret_cast<xmlNodePtr
>(cur
));
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
;
141 while (cur
!= nullptr)
145 aNode
= m_pElement
->GetOwnerDocument().GetCNode(
146 reinterpret_cast<xmlNodePtr
>(cur
));
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
));
166 "CAttributesMap::removeNamedItem: no such attribute",
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
));
185 "CAttributesMap::removeNamedItemNS: no such attribute",
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
);
201 "CAttributesMap::setNamedItem: XAttr argument expected",
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
);
218 "CAttributesMap::setNamedItemNS: XAttr argument expected",
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: */