Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / unoxml / source / dom / elementlist.cxx
blob11afe2a14a16642a642b97efeaca04e46d0f299d
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>
24 #include <cppuhelper/implbase.hxx>
25 #include <osl/diagnose.h>
26 #include <sal/log.hxx>
27 #include <tools/diagnose_ex.h>
29 #include "element.hxx"
30 #include "document.hxx"
32 using namespace css::uno;
33 using namespace css::xml::dom;
34 using namespace css::xml::dom::events;
36 namespace
38 class WeakEventListener : public ::cppu::WeakImplHelper<css::xml::dom::events::XEventListener>
40 private:
41 css::uno::WeakReference<css::xml::dom::events::XEventListener> mxOwner;
43 public:
44 explicit WeakEventListener(const css::uno::Reference<css::xml::dom::events::XEventListener>& rOwner)
45 : mxOwner(rOwner)
49 virtual void SAL_CALL handleEvent(const css::uno::Reference<css::xml::dom::events::XEvent>& rEvent) override
51 css::uno::Reference<css::xml::dom::events::XEventListener> xOwner(mxOwner.get(),
52 css::uno::UNO_QUERY);
53 if (xOwner.is())
54 xOwner->handleEvent(rEvent);
59 namespace DOM
62 static xmlChar* lcl_initXmlString(OUString const& rString)
64 OString const os =
65 OUStringToOString(rString, RTL_TEXTENCODING_UTF8);
66 xmlChar *const pRet = new xmlChar[os.getLength() + 1];
67 strcpy(reinterpret_cast<char*>(pRet), os.getStr());
68 return pRet;
71 CElementList::CElementList(::rtl::Reference<CElement> const& pElement,
72 ::osl::Mutex & rMutex,
73 OUString const& rName, OUString const*const pURI)
74 : m_xImpl(new CElementListImpl(pElement, rMutex, rName, pURI))
76 if (pElement.is()) {
77 m_xImpl->registerListener(*pElement);
81 CElementListImpl::CElementListImpl(::rtl::Reference<CElement> const& pElement,
82 ::osl::Mutex & rMutex,
83 OUString const& rName, OUString const*const pURI)
84 : m_pElement(pElement)
85 , m_rMutex(rMutex)
86 , m_pName(lcl_initXmlString(rName))
87 , m_pURI(pURI ? lcl_initXmlString(*pURI) : nullptr)
88 , m_bRebuild(true)
92 CElementListImpl::~CElementListImpl()
94 if (m_xEventListener.is() && m_pElement.is())
96 Reference< XEventTarget > xTarget(static_cast<XElement*>(m_pElement.get()), UNO_QUERY);
97 assert(xTarget.is());
98 if (!xTarget.is())
99 return;
100 xTarget->removeEventListener("DOMSubtreeModified", m_xEventListener, false/*capture*/);
104 void CElementListImpl::registerListener(CElement & rElement)
106 try {
107 Reference< XEventTarget > const xTarget(
108 static_cast<XElement*>(& rElement), UNO_QUERY_THROW);
109 m_xEventListener = new WeakEventListener(this);
110 xTarget->addEventListener("DOMSubtreeModified", m_xEventListener, false/*capture*/);
111 } catch (const Exception &){
112 TOOLS_WARN_EXCEPTION( "unoxml", "Exception caught while registering NodeList as listener");
116 void CElementListImpl::buildlist(xmlNodePtr pNode, bool start)
118 // bail out if no rebuild is needed
119 if (start) {
120 if (!m_bRebuild)
122 return;
123 } else {
124 m_nodevector.clear();
125 m_bRebuild = false; // don't rebuild until tree is mutated
129 while (pNode != nullptr )
131 if (pNode->type == XML_ELEMENT_NODE &&
132 (strcmp(reinterpret_cast<char const *>(pNode->name), reinterpret_cast<char*>(m_pName.get())) == 0))
134 if (!m_pURI) {
135 m_nodevector.push_back(pNode);
136 } else {
137 if (pNode->ns != nullptr && (0 ==
138 strcmp(reinterpret_cast<char const *>(pNode->ns->href), reinterpret_cast<char*>(m_pURI.get()))))
140 m_nodevector.push_back(pNode);
144 if (pNode->children != nullptr) buildlist(pNode->children, false);
146 if (!start) pNode = pNode->next;
147 else break; // fold back
152 The number of nodes in the list.
154 sal_Int32 SAL_CALL CElementListImpl::getLength()
156 ::osl::MutexGuard const g(m_rMutex);
158 if (!m_pElement.is()) { return 0; }
160 // this has to be 'live'
161 buildlist(m_pElement->GetNodePtr());
162 return m_nodevector.size();
165 Returns the indexth item in the collection.
167 Reference< XNode > SAL_CALL CElementListImpl::item(sal_Int32 index)
169 if (index < 0) throw RuntimeException();
171 ::osl::MutexGuard const g(m_rMutex);
173 if (!m_pElement.is()) { return nullptr; }
175 buildlist(m_pElement->GetNodePtr());
176 if (m_nodevector.size() <= static_cast<size_t>(index)) {
177 throw RuntimeException();
179 Reference< XNode > const xRet(
180 m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]).get());
181 return xRet;
184 // tree mutations can change the list
185 void SAL_CALL CElementListImpl::handleEvent(Reference< XEvent > const&)
187 ::osl::MutexGuard const g(m_rMutex);
189 m_bRebuild = true;
193 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */