Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/unoxml/source/dom/attributesmap.cxx
Line
Count
Source
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
30
using namespace css::uno;
31
using namespace css::xml::dom;
32
33
namespace DOM
34
{
35
    CAttributesMap::CAttributesMap(::rtl::Reference<CElement> pElement,
36
                ::osl::Mutex & rMutex)
37
0
        : m_pElement(std::move(pElement))
38
0
        , m_rMutex(rMutex)
39
0
    {
40
0
    }
41
42
    /**
43
    The number of nodes in this map.
44
    */
45
    sal_Int32 SAL_CALL CAttributesMap::getLength()
46
0
    {
47
0
        ::osl::MutexGuard const g(m_rMutex);
48
49
0
        sal_Int32 count = 0;
50
0
        xmlNodePtr pNode = m_pElement->GetNodePtr();
51
0
        if (pNode != nullptr)
52
0
        {
53
0
            xmlAttrPtr cur = pNode->properties;
54
0
            while (cur != nullptr)
55
0
            {
56
0
                count++;
57
0
                cur = cur->next;
58
0
            }
59
0
        }
60
0
        return count;
61
0
    }
62
63
    /**
64
    Retrieves a node specified by local name
65
    */
66
    Reference< XNode > SAL_CALL
67
    CAttributesMap::getNamedItem(OUString const& name)
68
0
    {
69
0
        ::osl::MutexGuard const g(m_rMutex);
70
71
0
        xmlNodePtr pNode = m_pElement->GetNodePtr();
72
0
        if (pNode == nullptr)
73
0
            return nullptr;
74
75
0
        rtl::Reference< CNode > aNode;
76
0
        OString o1 = OUStringToOString(name, RTL_TEXTENCODING_UTF8);
77
0
        xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
78
0
        xmlAttrPtr cur = pNode->properties;
79
0
        while (cur != nullptr)
80
0
        {
81
0
            if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0)
82
0
            {
83
0
                aNode = m_pElement->GetOwnerDocument().GetCNode(
84
0
                               reinterpret_cast<xmlNodePtr>(cur));
85
0
                break;
86
0
            }
87
0
            cur = cur->next;
88
0
        }
89
0
        return aNode;
90
0
    }
91
92
    /**
93
    Retrieves a node specified by local name and namespace URI.
94
    */
95
    Reference< XNode > SAL_CALL
96
    CAttributesMap::getNamedItemNS(
97
            OUString const& namespaceURI, OUString const& localName)
98
0
    {
99
0
        ::osl::MutexGuard const g(m_rMutex);
100
101
0
        xmlNodePtr pNode = m_pElement->GetNodePtr();
102
0
        if (pNode == nullptr)
103
0
            return nullptr;
104
105
0
        rtl::Reference< CNode > aNode;
106
0
        OString o1 = OUStringToOString(localName, RTL_TEXTENCODING_UTF8);
107
0
        xmlChar const * pName = reinterpret_cast<xmlChar const *>(o1.getStr());
108
0
        OString o2 = OUStringToOString(namespaceURI, RTL_TEXTENCODING_UTF8);
109
0
        xmlChar const* pSearchNs =
110
0
            reinterpret_cast<xmlChar const*>(o2.getStr());
111
0
        xmlNsPtr const pNs = xmlSearchNsByHref(pNode->doc, pNode, pSearchNs);
112
0
        xmlAttrPtr cur = pNode->properties;
113
0
        while (cur != nullptr && pNs != nullptr)
114
0
        {
115
0
            if( strcmp(reinterpret_cast<char const *>(pName), reinterpret_cast<char const *>(cur->name)) == 0 &&
116
0
                cur->ns == pNs)
117
0
            {
118
0
                aNode = m_pElement->GetOwnerDocument().GetCNode(
119
0
                              reinterpret_cast<xmlNodePtr>(cur));
120
0
                break;
121
0
            }
122
0
            cur = cur->next;
123
0
        }
124
0
        return aNode;
125
0
    }
126
127
    /**
128
    Returns the indexth item in the map.
129
    */
130
    Reference< XNode > SAL_CALL
131
    CAttributesMap::item(sal_Int32 index)
132
0
    {
133
0
        ::osl::MutexGuard const g(m_rMutex);
134
135
0
        xmlNodePtr pNode = m_pElement->GetNodePtr();
136
0
        if (pNode == nullptr)
137
0
            return nullptr;
138
139
0
        rtl::Reference< CNode > aNode;
140
0
        xmlAttrPtr cur = pNode->properties;
141
0
        sal_Int32 count = 0;
142
0
        while (cur != nullptr)
143
0
        {
144
0
            if (count == index)
145
0
            {
146
0
                aNode = m_pElement->GetOwnerDocument().GetCNode(
147
0
                            reinterpret_cast<xmlNodePtr>(cur));
148
0
                break;
149
0
            }
150
0
            count++;
151
0
            cur = cur->next;
152
0
        }
153
0
        return aNode;
154
0
    }
155
156
    /**
157
    Removes a node specified by name.
158
    */
159
    Reference< XNode > SAL_CALL
160
    CAttributesMap::removeNamedItem(OUString const& name)
161
0
    {
162
        // no MutexGuard needed: m_pElement is const
163
0
        Reference< XAttr > const xAttr(m_pElement->getAttributeNode(name));
164
0
        if (!xAttr.is()) {
165
0
            throw DOMException(
166
0
                u"CAttributesMap::removeNamedItem: no such attribute"_ustr,
167
0
                getXWeak(),
168
0
                DOMExceptionType_NOT_FOUND_ERR);
169
0
        }
170
0
        return m_pElement->removeAttributeNode(xAttr);
171
0
    }
172
173
    /**
174
    // Removes a node specified by local name and namespace URI.
175
    */
176
    Reference< XNode > SAL_CALL
177
    CAttributesMap::removeNamedItemNS(
178
            OUString const& namespaceURI, OUString const& localName)
179
0
    {
180
        // no MutexGuard needed: m_pElement is const
181
0
        Reference< XAttr > const xAttr(
182
0
            m_pElement->getAttributeNodeNS(namespaceURI, localName));
183
0
        if (!xAttr.is()) {
184
0
            throw DOMException(
185
0
                u"CAttributesMap::removeNamedItemNS: no such attribute"_ustr,
186
0
                getXWeak(),
187
0
                DOMExceptionType_NOT_FOUND_ERR);
188
0
        }
189
0
        return m_pElement->removeAttributeNode(xAttr);
190
0
    }
191
192
    /**
193
    // Adds a node using its nodeName attribute.
194
    */
195
    Reference< XNode > SAL_CALL
196
    CAttributesMap::setNamedItem(Reference< XNode > const& xNode)
197
0
    {
198
0
        Reference< XAttr > const xAttr(xNode, UNO_QUERY);
199
0
        if (!xNode.is()) {
200
0
            throw DOMException(
201
0
                u"CAttributesMap::setNamedItem: XAttr argument expected"_ustr,
202
0
                getXWeak(),
203
0
                DOMExceptionType_HIERARCHY_REQUEST_ERR);
204
0
        }
205
        // no MutexGuard needed: m_pElement is const
206
0
        return m_pElement->setAttributeNode(xAttr);
207
0
    }
208
209
    /**
210
    Adds a node using its namespaceURI and localName.
211
    */
212
    Reference< XNode > SAL_CALL
213
    CAttributesMap::setNamedItemNS(Reference< XNode > const& xNode)
214
0
    {
215
0
        Reference< XAttr > const xAttr(xNode, UNO_QUERY);
216
0
        if (!xNode.is()) {
217
0
            throw DOMException(
218
0
                u"CAttributesMap::setNamedItemNS: XAttr argument expected"_ustr,
219
0
                getXWeak(),
220
0
                DOMExceptionType_HIERARCHY_REQUEST_ERR);
221
0
        }
222
        // no MutexGuard needed: m_pElement is const
223
0
        return m_pElement->setAttributeNodeNS(xAttr);
224
0
    }
225
}
226
227
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */