LibreOffice Module unoxml (master) 1
characterdata.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 "characterdata.hxx"
21
22#include <string.h>
23
24#include <memory>
25
26#include <osl/diagnose.h>
27
28#include <com/sun/star/xml/dom/DOMException.hpp>
29#include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
30#include <com/sun/star/xml/dom/events/XMutationEvent.hpp>
31
32using namespace css::uno;
33using namespace css::xml::dom;
34using namespace css::xml::dom::events;
35
36namespace DOM
37{
38
40 CDocument const& rDocument, ::osl::Mutex const& rMutex,
41 NodeType const& reNodeType, xmlNodePtr const& rpNode)
42 : CCharacterData_Base(rDocument, rMutex, reNodeType, rpNode)
43 {
44 }
45
47 OUString const& prevValue, OUString const& newValue)
48 {
49 Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY);
50 Reference< XMutationEvent > event(docevent->createEvent(
51 "DOMCharacterDataModified"), UNO_QUERY);
52 event->initMutationEvent(
53 "DOMCharacterDataModified",
54 true, false, Reference< XNode >(),
55 prevValue, newValue, OUString(), AttrChangeType(0) );
56 dispatchEvent(event);
57 dispatchSubtreeModified();
58 }
59
63 void SAL_CALL CCharacterData::appendData(const OUString& arg)
64 {
65 ::osl::ClearableMutexGuard guard(m_rMutex);
66
67 if (m_aNodePtr != nullptr)
68 {
69 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
70 xmlNodeAddContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr()));
71 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
72
73 guard.clear(); // release mutex before calling event handlers
74 dispatchEvent_Impl(oldValue, newValue);
75 }
76 }
77
81 void SAL_CALL CCharacterData::deleteData(sal_Int32 offset, sal_Int32 count)
82 {
83 ::osl::ClearableMutexGuard guard(m_rMutex);
84
85 if (m_aNodePtr == nullptr)
86 return;
87
88 // get current data
89 std::shared_ptr<xmlChar const> const pContent(
90 xmlNodeGetContent(m_aNodePtr), xmlFree);
91 std::string_view aData(reinterpret_cast<char const*>(pContent.get()));
92 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
93 if (offset > tmp.getLength() || offset < 0 || count < 0) {
94 DOMException e;
95 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
96 throw e;
97 }
98 if ((offset+count) > tmp.getLength())
99 count = tmp.getLength() - offset;
100
101 OUString tmp2 = OUString::Concat(tmp.subView(0, offset)) + tmp.subView(offset+count);
102 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
103 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
104 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
105
106 guard.clear(); // release mutex before calling event handlers
107 dispatchEvent_Impl(oldValue, newValue);
108
109 }
110
111
115 OUString SAL_CALL CCharacterData::getData()
116 {
117 ::osl::MutexGuard const g(m_rMutex);
118
119 OUString aData;
120 if (m_aNodePtr != nullptr)
121 {
122 OSL_ENSURE(m_aNodePtr->content, "character data node with NULL content, please inform lars.oppermann@sun.com!");
123 if (m_aNodePtr->content != nullptr)
124 {
125 aData = OUString(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
126 }
127 }
128 return aData;
129 }
130
135 sal_Int32 SAL_CALL CCharacterData::getLength()
136 {
137 ::osl::MutexGuard const g(m_rMutex);
138
139 sal_Int32 length = 0;
140 if (m_aNodePtr != nullptr)
141 {
142 OUString aData(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
143 length = aData.getLength();
144 }
145 return length;
146 }
147
151 void SAL_CALL CCharacterData::insertData(sal_Int32 offset, const OUString& arg)
152 {
153 ::osl::ClearableMutexGuard guard(m_rMutex);
154
155 if (m_aNodePtr == nullptr)
156 return;
157
158 // get current data
159 std::shared_ptr<xmlChar const> const pContent(
160 xmlNodeGetContent(m_aNodePtr), xmlFree);
161 std::string_view aData(reinterpret_cast<char const*>(pContent.get()));
162 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
163 if (offset > tmp.getLength() || offset < 0) {
164 DOMException e;
165 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
166 throw e;
167 }
168
169 OUString tmp2 = tmp.subView(0, offset) +
170 arg +
171 tmp.subView(offset);
172 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
173 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
174 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
175
176 guard.clear(); // release mutex before calling event handlers
177 dispatchEvent_Impl(oldValue, newValue);
178
179 }
180
181
186 void SAL_CALL CCharacterData::replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg)
187 {
188 ::osl::ClearableMutexGuard guard(m_rMutex);
189
190 if (m_aNodePtr == nullptr)
191 return;
192
193 // get current data
194 std::shared_ptr<xmlChar const> const pContent(
195 xmlNodeGetContent(m_aNodePtr), xmlFree);
196 std::string_view aData(reinterpret_cast<char const*>(pContent.get()));
197 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
198 if (offset > tmp.getLength() || offset < 0 || count < 0){
199 DOMException e;
200 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
201 throw e;
202 }
203 if ((offset+count) > tmp.getLength())
204 count = tmp.getLength() - offset;
205
206 OUString tmp2 = tmp.subView(0, offset) +
207 arg +
208 tmp.subView(offset+count);
209 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
210 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
211 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
212
213 guard.clear(); // release mutex before calling event handlers
214 dispatchEvent_Impl(oldValue, newValue);
215
216 }
217
221 void SAL_CALL CCharacterData::setData(const OUString& data)
222 {
223 ::osl::ClearableMutexGuard guard(m_rMutex);
224
225 if (m_aNodePtr != nullptr)
226 {
227 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
228 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr()));
229 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
230
231 guard.clear(); // release mutex before calling event handlers
232 dispatchEvent_Impl(oldValue, newValue);
233 }
234 }
235
239 OUString SAL_CALL CCharacterData::subStringData(sal_Int32 offset, sal_Int32 count)
240 {
241 ::osl::MutexGuard const g(m_rMutex);
242
243 OUString aStr;
244 if (m_aNodePtr != nullptr)
245 {
246 // get current data
247 std::shared_ptr<xmlChar const> const pContent(
248 xmlNodeGetContent(m_aNodePtr), xmlFree);
249 std::string_view aData(reinterpret_cast<char const*>(pContent.get()));
250 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
251 if (offset > tmp.getLength() || offset < 0 || count < 0) {
252 DOMException e;
253 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
254 throw e;
255 }
256 aStr = tmp.copy(offset, count);
257 }
258 return aStr;
259 }
260
261
262} // namespace DOM
263
264/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual OUString SAL_CALL getData() override
Return the character data of the node that implements this interface.
CCharacterData(CDocument const &rDocument, ::osl::Mutex const &rMutex, css::xml::dom::NodeType const &reNodeType, xmlNodePtr const &rpNode)
virtual void SAL_CALL replaceData(sal_Int32 offset, sal_Int32 count, const OUString &arg) override
Replace the characters starting at the specified 16-bit unit offset with the specified string.
virtual sal_Int32 SAL_CALL getLength() override
The number of 16-bit units that are available through data and the substringData method below.
virtual void SAL_CALL setData(const OUString &data) override
Set the character data of the node that implements this interface.
void dispatchEvent_Impl(OUString const &prevValue, OUString const &newValue)
virtual void SAL_CALL insertData(sal_Int32 offset, const OUString &arg) override
Insert a string at the specified 16-bit unit offset.
virtual OUString SAL_CALL subStringData(sal_Int32 offset, sal_Int32 count) override
Extracts a range of data from the node.
virtual css::uno::Reference< css::xml::dom::XDocument > SAL_CALL getOwnerDocument() override
virtual void SAL_CALL appendData(const OUString &arg) override
Append the string to the end of the character data of the node.
virtual void SAL_CALL deleteData(sal_Int32 offset, sal_Int32 count) override
Remove a range of 16-bit units from the node.
::osl::Mutex & m_rMutex
aStr
Definition: attr.cxx:38
::cppu::ImplInheritanceHelper< CNode, css::xml::dom::XCharacterData > CCharacterData_Base
constexpr OUStringLiteral aData
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
NodeType