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() throw (RuntimeException
, std::exception
)
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
) throw (RuntimeException
, std::exception
)
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
)
95 throw (RuntimeException
, std::exception
)
97 ::osl::MutexGuard
const g(m_rMutex
);
99 Reference
< XNode
> aNode
;
100 xmlNodePtr pNode
= m_pElement
->GetNodePtr();
101 if (pNode
!= nullptr)
103 OString o1
= OUStringToOString(localName
, RTL_TEXTENCODING_UTF8
);
104 xmlChar
const * pName
= reinterpret_cast<xmlChar
const *>(o1
.getStr());
105 OString o2
= OUStringToOString(namespaceURI
, RTL_TEXTENCODING_UTF8
);
106 xmlChar
const* pSearchNs
=
107 reinterpret_cast<xmlChar
const*>(o2
.getStr());
108 xmlNsPtr
const pNs
= xmlSearchNsByHref(pNode
->doc
, pNode
, pSearchNs
);
109 xmlAttrPtr cur
= pNode
->properties
;
110 while (cur
!= nullptr && pNs
!= nullptr)
112 if( strcmp(reinterpret_cast<char const *>(pName
), reinterpret_cast<char const *>(cur
->name
)) == 0 &&
115 aNode
.set( m_pElement
->GetOwnerDocument().GetCNode(
116 reinterpret_cast<xmlNodePtr
>(cur
)).get() );
126 Returns the indexth item in the map.
128 Reference
< XNode
> SAL_CALL
129 CAttributesMap::item(sal_Int32 index
) throw (RuntimeException
, std::exception
)
131 ::osl::MutexGuard
const g(m_rMutex
);
133 Reference
< XNode
> aNode
;
134 xmlNodePtr pNode
= m_pElement
->GetNodePtr();
135 if (pNode
!= nullptr)
137 xmlAttrPtr cur
= pNode
->properties
;
139 while (cur
!= nullptr)
143 aNode
.set( m_pElement
->GetOwnerDocument().GetCNode(
144 reinterpret_cast<xmlNodePtr
>(cur
)).get() );
155 Removes a node specified by name.
157 Reference
< XNode
> SAL_CALL
158 CAttributesMap::removeNamedItem(OUString
const& name
)
159 throw (DOMException
, RuntimeException
, std::exception
)
161 // no MutexGuard needed: m_pElement is const
162 Reference
< XAttr
> const xAttr(m_pElement
->getAttributeNode(name
));
165 "CAttributesMap::removeNamedItem: no such attribute",
166 static_cast<OWeakObject
*>(this),
167 DOMExceptionType_NOT_FOUND_ERR
);
169 Reference
< XNode
> const xRet(
170 m_pElement
->removeAttributeNode(xAttr
), UNO_QUERY
);
175 // Removes a node specified by local name and namespace URI.
177 Reference
< XNode
> SAL_CALL
178 CAttributesMap::removeNamedItemNS(
179 OUString
const& namespaceURI
, OUString
const& localName
)
180 throw (DOMException
, RuntimeException
, std::exception
)
182 // no MutexGuard needed: m_pElement is const
183 Reference
< XAttr
> const xAttr(
184 m_pElement
->getAttributeNodeNS(namespaceURI
, localName
));
187 "CAttributesMap::removeNamedItemNS: no such attribute",
188 static_cast<OWeakObject
*>(this),
189 DOMExceptionType_NOT_FOUND_ERR
);
191 Reference
< XNode
> const xRet(
192 m_pElement
->removeAttributeNode(xAttr
), UNO_QUERY
);
197 // Adds a node using its nodeName attribute.
199 Reference
< XNode
> SAL_CALL
200 CAttributesMap::setNamedItem(Reference
< XNode
> const& xNode
)
201 throw (DOMException
, RuntimeException
, std::exception
)
203 Reference
< XAttr
> const xAttr(xNode
, UNO_QUERY
);
206 "CAttributesMap::setNamedItem: XAttr argument expected",
207 static_cast<OWeakObject
*>(this),
208 DOMExceptionType_HIERARCHY_REQUEST_ERR
);
210 // no MutexGuard needed: m_pElement is const
211 Reference
< XNode
> const xRet(
212 m_pElement
->setAttributeNode(xAttr
), UNO_QUERY
);
217 Adds a node using its namespaceURI and localName.
219 Reference
< XNode
> SAL_CALL
220 CAttributesMap::setNamedItemNS(Reference
< XNode
> const& xNode
)
221 throw (DOMException
, RuntimeException
, std::exception
)
223 Reference
< XAttr
> const xAttr(xNode
, UNO_QUERY
);
226 "CAttributesMap::setNamedItemNS: XAttr argument expected",
227 static_cast<OWeakObject
*>(this),
228 DOMExceptionType_HIERARCHY_REQUEST_ERR
);
230 // no MutexGuard needed: m_pElement is const
231 Reference
< XNode
> const xRet(
232 m_pElement
->setAttributeNodeNS(xAttr
), UNO_QUERY
);
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */