Bump version to 24.04.3.4
[LibreOffice.git] / unoxml / source / dom / elementlist.cxx
blob275b7adb8769beb6719ed99c3e6e4716275a5af1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "elementlist.hxx"
22 #include <string.h>
23 #include <string_view>
25 #include <cppuhelper/implbase.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <utility>
28 #include <comphelper/diagnose_ex.hxx>
30 #include "element.hxx"
31 #include "document.hxx"
33 using namespace css::uno;
34 using namespace css::xml::dom;
35 using namespace css::xml::dom::events;
37 namespace
39 class WeakEventListener : public ::cppu::WeakImplHelper<css::xml::dom::events::XEventListener>
41 private:
42 css::uno::WeakReference<css::xml::dom::events::XEventListener> mxOwner;
44 public:
45 explicit WeakEventListener(const css::uno::Reference<css::xml::dom::events::XEventListener>& rOwner)
46 : mxOwner(rOwner)
50 virtual void SAL_CALL handleEvent(const css::uno::Reference<css::xml::dom::events::XEvent>& rEvent) override
52 css::uno::Reference<css::xml::dom::events::XEventListener> xOwner(mxOwner.get(),
53 css::uno::UNO_QUERY);
54 if (xOwner.is())
55 xOwner->handleEvent(rEvent);
60 namespace DOM
63 static xmlChar* lcl_initXmlString(std::u16string_view rString)
65 OString const os =
66 OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
67 xmlChar *const pRet = new xmlChar[os.getLength() + 1];
68 strcpy(reinterpret_cast<char*>(pRet), os.getStr());
69 return pRet;
72 CElementList::CElementList(::rtl::Reference<CElement> const& pElement,
73 ::osl::Mutex & rMutex,
74 std::u16string_view rName, OUString const*const pURI)
75 : m_xImpl(new CElementListImpl(pElement, rMutex, rName, pURI))
77 if (pElement.is()) {
78 m_xImpl->registerListener(*pElement);
82 CElementListImpl::CElementListImpl(::rtl::Reference<CElement> pElement,
83 ::osl::Mutex & rMutex,
84 std::u16string_view rName, OUString const*const pURI)
85 : m_pElement(std::move(pElement))
86 , m_rMutex(rMutex)
87 , m_pName(lcl_initXmlString(rName))
88 , m_pURI(pURI ? lcl_initXmlString(*pURI) : nullptr)
89 , m_bRebuild(true)
93 CElementListImpl::~CElementListImpl()
95 if (m_xEventListener.is() && m_pElement.is())
97 Reference< XEventTarget > xTarget = m_pElement;
98 assert(xTarget.is());
99 if (!xTarget.is())
100 return;
101 xTarget->removeEventListener("DOMSubtreeModified", m_xEventListener, false/*capture*/);
105 void CElementListImpl::registerListener(CElement & rElement)
107 try {
108 Reference< XEventTarget > const xTarget(
109 static_cast<XElement*>(& rElement), UNO_QUERY_THROW);
110 m_xEventListener = new WeakEventListener(this);
111 xTarget->addEventListener("DOMSubtreeModified", m_xEventListener, false/*capture*/);
112 } catch (const Exception &){
113 TOOLS_WARN_EXCEPTION( "unoxml", "Exception caught while registering NodeList as listener");
117 void CElementListImpl::buildlist(xmlNodePtr pNode, bool start)
119 // bail out if no rebuild is needed
120 if (start) {
121 if (!m_bRebuild)
123 return;
124 } else {
125 m_nodevector.clear();
126 m_bRebuild = false; // don't rebuild until tree is mutated
130 while (pNode != nullptr )
132 if (pNode->type == XML_ELEMENT_NODE &&
133 (strcmp(reinterpret_cast<char const *>(pNode->name), reinterpret_cast<char*>(m_pName.get())) == 0))
135 if (!m_pURI) {
136 m_nodevector.push_back(pNode);
137 } else {
138 if (pNode->ns != nullptr && (0 ==
139 strcmp(reinterpret_cast<char const *>(pNode->ns->href), reinterpret_cast<char*>(m_pURI.get()))))
141 m_nodevector.push_back(pNode);
145 if (pNode->children != nullptr) buildlist(pNode->children, false);
147 if (!start) pNode = pNode->next;
148 else break; // fold back
153 The number of nodes in the list.
155 sal_Int32 SAL_CALL CElementListImpl::getLength()
157 ::osl::MutexGuard const g(m_rMutex);
159 if (!m_pElement.is()) { return 0; }
161 // this has to be 'live'
162 buildlist(m_pElement->GetNodePtr());
163 return m_nodevector.size();
166 Returns the indexth item in the collection.
168 Reference< XNode > SAL_CALL CElementListImpl::item(sal_Int32 index)
170 if (index < 0) throw RuntimeException();
172 ::osl::MutexGuard const g(m_rMutex);
174 if (!m_pElement.is()) { return nullptr; }
176 buildlist(m_pElement->GetNodePtr());
177 if (m_nodevector.size() <= o3tl::make_unsigned(index)) {
178 throw RuntimeException();
180 return m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]);
183 // tree mutations can change the list
184 void SAL_CALL CElementListImpl::handleEvent(Reference< XEvent > const&)
186 ::osl::MutexGuard const g(m_rMutex);
188 m_bRebuild = true;
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */