LibreOffice Module unoxml (master) 1
elementlist.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 "elementlist.hxx"
21
22#include <string.h>
23#include <string_view>
24
26#include <o3tl/safeint.hxx>
27#include <utility>
29
30#include "element.hxx"
31#include "document.hxx"
32
33using namespace css::uno;
34using namespace css::xml::dom;
35using namespace css::xml::dom::events;
36
37namespace
38{
39 class WeakEventListener : public ::cppu::WeakImplHelper<css::xml::dom::events::XEventListener>
40 {
41 private:
42 css::uno::WeakReference<css::xml::dom::events::XEventListener> mxOwner;
43
44 public:
45 explicit WeakEventListener(const css::uno::Reference<css::xml::dom::events::XEventListener>& rOwner)
46 : mxOwner(rOwner)
47 {
48 }
49
50 virtual void SAL_CALL handleEvent(const css::uno::Reference<css::xml::dom::events::XEvent>& rEvent) override
51 {
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);
56 }
57 };
58}
59
60namespace DOM
61{
62
63 static xmlChar* lcl_initXmlString(std::u16string_view rString)
64 {
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;
70 }
71
73 ::osl::Mutex & rMutex,
74 std::u16string_view rName, OUString const*const pURI)
75 : m_xImpl(new CElementListImpl(pElement, rMutex, rName, pURI))
76 {
77 if (pElement.is()) {
78 m_xImpl->registerListener(*pElement);
79 }
80 }
81
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)
90 {
91 }
92
94 {
95 if (m_xEventListener.is() && m_pElement.is())
96 {
97 Reference< XEventTarget > xTarget = m_pElement;
98 assert(xTarget.is());
99 if (!xTarget.is())
100 return;
101 xTarget->removeEventListener("DOMSubtreeModified", m_xEventListener, false/*capture*/);
102 }
103 }
104
106 {
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");
114 }
115 }
116
117 void CElementListImpl::buildlist(xmlNodePtr pNode, bool start)
118 {
119 // bail out if no rebuild is needed
120 if (start) {
121 if (!m_bRebuild)
122 {
123 return;
124 } else {
125 m_nodevector.clear();
126 m_bRebuild = false; // don't rebuild until tree is mutated
127 }
128 }
129
130 while (pNode != nullptr )
131 {
132 if (pNode->type == XML_ELEMENT_NODE &&
133 (strcmp(reinterpret_cast<char const *>(pNode->name), reinterpret_cast<char*>(m_pName.get())) == 0))
134 {
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()))))
140 {
141 m_nodevector.push_back(pNode);
142 }
143 }
144 }
145 if (pNode->children != nullptr) buildlist(pNode->children, false);
146
147 if (!start) pNode = pNode->next;
148 else break; // fold back
149 }
150 }
151
155 sal_Int32 SAL_CALL CElementListImpl::getLength()
156 {
157 ::osl::MutexGuard const g(m_rMutex);
158
159 if (!m_pElement.is()) { return 0; }
160
161 // this has to be 'live'
162 buildlist(m_pElement->GetNodePtr());
163 return m_nodevector.size();
164 }
168 Reference< XNode > SAL_CALL CElementListImpl::item(sal_Int32 index)
169 {
170 if (index < 0) throw RuntimeException();
171
172 ::osl::MutexGuard const g(m_rMutex);
173
174 if (!m_pElement.is()) { return nullptr; }
175
176 buildlist(m_pElement->GetNodePtr());
177 if (m_nodevector.size() <= o3tl::make_unsigned(index)) {
178 throw RuntimeException();
179 }
180 return m_pElement->GetOwnerDocument().GetCNode(m_nodevector[index]);
181 }
182
183 // tree mutations can change the list
184 void SAL_CALL CElementListImpl::handleEvent(Reference< XEvent > const&)
185 {
186 ::osl::MutexGuard const g(m_rMutex);
187
188 m_bRebuild = true;
189 }
190}
191
192/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Reference< css::xml::dom::XNode > SAL_CALL item(sal_Int32 index) override
Returns the indexth item in the collection.
::rtl::Reference< CElement > const m_pElement
Definition: elementlist.hxx:53
css::uno::Reference< css::xml::dom::events::XEventListener > m_xEventListener
proxy weak binding to forward Events to ourself without an ownership cycle
Definition: elementlist.hxx:51
virtual void SAL_CALL handleEvent(const css::uno::Reference< css::xml::dom::events::XEvent > &evt) override
::std::unique_ptr< xmlChar[]> const m_pURI
Definition: elementlist.hxx:56
virtual sal_Int32 SAL_CALL getLength() override
The number of nodes in the list.
virtual ~CElementListImpl() override
Definition: elementlist.cxx:93
::osl::Mutex & m_rMutex
Definition: elementlist.hxx:54
::std::unique_ptr< xmlChar[]> const m_pName
Definition: elementlist.hxx:55
void buildlist(xmlNodePtr pNode, bool start=true)
CElementListImpl(::rtl::Reference< CElement > pElement, ::osl::Mutex &rMutex, std::u16string_view rName, OUString const *const pURI)
Definition: elementlist.cxx:82
std::vector< xmlNodePtr > m_nodevector
Definition: elementlist.hxx:58
void registerListener(CElement &rElement)
rtl::Reference< CElementListImpl > m_xImpl
Definition: elementlist.hxx:89
CElementList(::rtl::Reference< CElement > const &pElement, ::osl::Mutex &rMutex, std::u16string_view rName, OUString const *const pURI=nullptr)
Definition: elementlist.cxx:72
::osl::Mutex & m_rMutex
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XInterface > xTarget
Definition: attr.cxx:38
static xmlChar * lcl_initXmlString(std::u16string_view rString)
Definition: elementlist.cxx:63
@ Exception
index
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)