LibreOffice Module unoxml (master) 1
attributesmap.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "attributesmap.hxx"
21
22#include <string.h>
23
24#include <com/sun/star/xml/dom/DOMException.hpp>
25#include <utility>
26
27#include "element.hxx"
28#include "document.hxx"
29
30using namespace css::uno;
31using namespace css::xml::dom;
32
33namespace DOM
34{
36 ::osl::Mutex & rMutex)
37 : m_pElement(std::move(pElement))
38 , m_rMutex(rMutex)
39 {
40 }
41
45 sal_Int32 SAL_CALL CAttributesMap::getLength()
46 {
47 ::osl::MutexGuard const g(m_rMutex);
48
49 sal_Int32 count = 0;
50 xmlNodePtr pNode = m_pElement->GetNodePtr();
51 if (pNode != nullptr)
52 {
53 xmlAttrPtr cur = pNode->properties;
54 while (cur != nullptr)
55 {
56 count++;
57 cur = cur->next;
58 }
59 }
60 return count;
61 }
62
66 Reference< XNode > SAL_CALL
67 CAttributesMap::getNamedItem(OUString const& name)
68 {
69 ::osl::MutexGuard const g(m_rMutex);
70
71 Reference< XNode > aNode;
72 xmlNodePtr pNode = m_pElement->GetNodePtr();
73 if (pNode != nullptr)
74 {
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)
79 {
80 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0)
81 {
82 aNode = m_pElement->GetOwnerDocument().GetCNode(
83 reinterpret_cast<xmlNodePtr>(cur));
84 break;
85 }
86 cur = cur->next;
87 }
88 }
89 return aNode;
90 }
91
95 Reference< XNode > SAL_CALL
97 OUString const& namespaceURI, OUString const& localName)
98 {
99 ::osl::MutexGuard const g(m_rMutex);
100
101 Reference< XNode > aNode;
102 xmlNodePtr pNode = m_pElement->GetNodePtr();
103 if (pNode != nullptr)
104 {
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)
113 {
114 if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0 &&
115 cur->ns == pNs)
116 {
117 aNode = m_pElement->GetOwnerDocument().GetCNode(
118 reinterpret_cast<xmlNodePtr>(cur));
119 break;
120 }
121 cur = cur->next;
122 }
123 }
124 return aNode;
125 }
126
130 Reference< XNode > SAL_CALL
131 CAttributesMap::item(sal_Int32 index)
132 {
133 ::osl::MutexGuard const g(m_rMutex);
134
135 Reference< XNode > aNode;
136 xmlNodePtr pNode = m_pElement->GetNodePtr();
137 if (pNode != nullptr)
138 {
139 xmlAttrPtr cur = pNode->properties;
140 sal_Int32 count = 0;
141 while (cur != nullptr)
142 {
143 if (count == index)
144 {
145 aNode = m_pElement->GetOwnerDocument().GetCNode(
146 reinterpret_cast<xmlNodePtr>(cur));
147 break;
148 }
149 count++;
150 cur = cur->next;
151 }
152 }
153 return aNode;
154 }
155
159 Reference< XNode > SAL_CALL
161 {
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);
169 }
170 return m_pElement->removeAttributeNode(xAttr);
171 }
172
176 Reference< XNode > SAL_CALL
178 OUString const& namespaceURI, OUString const& localName)
179 {
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);
188 }
189 return m_pElement->removeAttributeNode(xAttr);
190 }
191
195 Reference< XNode > SAL_CALL
196 CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
197 {
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);
204 }
205 // no MutexGuard needed: m_pElement is const
206 return m_pElement->setAttributeNode(xAttr);
207 }
208
212 Reference< XNode > SAL_CALL
213 CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
214 {
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);
221 }
222 // no MutexGuard needed: m_pElement is const
223 return m_pElement->setAttributeNodeNS(xAttr);
224 }
225}
226
227/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL item(sal_Int32 index) override
Returns the indexth item in the map.
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeNamedItemNS(OUString const &namespaceURI, OUString const &localName) override
Removes a node specified by local name and namespace URI.
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNamedItem(OUString const &name) override
Retrieves a node specified by local name.
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL setNamedItemNS(css::uno::Reference< css::xml::dom::XNode > const &arg) override
Adds a node using its namespaceURI and localName.
virtual sal_Int32 SAL_CALL getLength() override
The number of nodes in this map.
::rtl::Reference< CElement > const m_pElement
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL removeNamedItem(OUString const &name) override
Removes a node specified by name.
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL getNamedItemNS(OUString const &namespaceURI, OUString const &localName) override
Retrieves a node specified by local name and namespace URI.
CAttributesMap(::rtl::Reference< CElement > pElement, ::osl::Mutex &rMutex)
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL setNamedItem(css::uno::Reference< css::xml::dom::XNode > const &arg) override
Adds a node using its nodeName attribute.
::osl::Mutex & m_rMutex
::osl::Mutex & m_rMutex
const char * name
Definition: attr.cxx:38
index
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)