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 "element.hxx"
25 #include "document.hxx"
27 using namespace css::uno
;
28 using namespace css::xml::dom
;
32 CAttributesMap::CAttributesMap(::rtl::Reference
<CElement
> const& pElement
,
33 ::osl::Mutex
& rMutex
)
34 : m_pElement(pElement
)
40 The number of nodes in this map.
42 sal_Int32 SAL_CALL
CAttributesMap::getLength()
44 ::osl::MutexGuard
const g(m_rMutex
);
47 xmlNodePtr pNode
= m_pElement
->GetNodePtr();
50 xmlAttrPtr cur
= pNode
->properties
;
51 while (cur
!= nullptr)
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();
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() );
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 &&
114 aNode
.set( m_pElement
->GetOwnerDocument().GetCNode(
115 reinterpret_cast<xmlNodePtr
>(cur
)).get() );
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
;
138 while (cur
!= nullptr)
142 aNode
.set( m_pElement
->GetOwnerDocument().GetCNode(
143 reinterpret_cast<xmlNodePtr
>(cur
)).get() );
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
));
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
));
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
);
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
);
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: */