1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "characterdata.hxx"
26 #include <osl/diagnose.h>
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>
32 using namespace css::uno
;
33 using namespace css::xml::dom
;
34 using namespace css::xml::dom::events
;
39 CCharacterData::CCharacterData(
40 CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
41 NodeType
const& reNodeType
, xmlNodePtr
const& rpNode
)
42 : CCharacterData_Base(rDocument
, rMutex
, reNodeType
, rpNode
)
46 void CCharacterData::dispatchEvent_Impl(
47 OUString
const& prevValue
, OUString
const& newValue
)
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) );
57 dispatchSubtreeModified();
61 Append the string to the end of the character data of the node.
63 void SAL_CALL
CCharacterData::appendData(const OUString
& arg
)
65 ::osl::ClearableMutexGuard
guard(m_rMutex
);
67 if (m_aNodePtr
!= nullptr)
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
);
73 guard
.clear(); // release mutex before calling event handlers
74 dispatchEvent_Impl(oldValue
, newValue
);
79 Remove a range of 16-bit units from the node.
81 void SAL_CALL
CCharacterData::deleteData(sal_Int32 offset
, sal_Int32 count
)
83 ::osl::ClearableMutexGuard
guard(m_rMutex
);
85 if (m_aNodePtr
== nullptr)
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) {
95 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
98 if ((offset
+count
) > tmp
.getLength())
99 count
= tmp
.getLength() - offset
;
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
);
106 guard
.clear(); // release mutex before calling event handlers
107 dispatchEvent_Impl(oldValue
, newValue
);
113 Return the character data of the node that implements this interface.
115 OUString SAL_CALL
CCharacterData::getData()
117 ::osl::MutexGuard
const g(m_rMutex
);
120 if (m_aNodePtr
!= nullptr)
122 OSL_ENSURE(m_aNodePtr
->content
, "character data node with NULL content, please inform lars.oppermann@sun.com!");
123 if (m_aNodePtr
->content
!= nullptr)
125 aData
= OUString(reinterpret_cast<char*>(m_aNodePtr
->content
), strlen(reinterpret_cast<char*>(m_aNodePtr
->content
)), RTL_TEXTENCODING_UTF8
);
132 The number of 16-bit units that are available through data and the
133 substringData method below.
135 sal_Int32 SAL_CALL
CCharacterData::getLength()
137 ::osl::MutexGuard
const g(m_rMutex
);
139 sal_Int32 length
= 0;
140 if (m_aNodePtr
!= nullptr)
142 OUString
aData(reinterpret_cast<char*>(m_aNodePtr
->content
), strlen(reinterpret_cast<char*>(m_aNodePtr
->content
)), RTL_TEXTENCODING_UTF8
);
143 length
= aData
.getLength();
149 Insert a string at the specified 16-bit unit offset.
151 void SAL_CALL
CCharacterData::insertData(sal_Int32 offset
, const OUString
& arg
)
153 ::osl::ClearableMutexGuard
guard(m_rMutex
);
155 if (m_aNodePtr
== nullptr)
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) {
165 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
169 OUString tmp2
= tmp
.subView(0, 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
);
176 guard
.clear(); // release mutex before calling event handlers
177 dispatchEvent_Impl(oldValue
, newValue
);
183 Replace the characters starting at the specified 16-bit unit offset
184 with the specified string.
186 void SAL_CALL
CCharacterData::replaceData(sal_Int32 offset
, sal_Int32 count
, const OUString
& arg
)
188 ::osl::ClearableMutexGuard
guard(m_rMutex
);
190 if (m_aNodePtr
== nullptr)
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){
200 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
203 if ((offset
+count
) > tmp
.getLength())
204 count
= tmp
.getLength() - offset
;
206 OUString tmp2
= tmp
.subView(0, offset
) +
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
);
213 guard
.clear(); // release mutex before calling event handlers
214 dispatchEvent_Impl(oldValue
, newValue
);
219 Set the character data of the node that implements this interface.
221 void SAL_CALL
CCharacterData::setData(const OUString
& data
)
223 ::osl::ClearableMutexGuard
guard(m_rMutex
);
225 if (m_aNodePtr
!= nullptr)
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
);
231 guard
.clear(); // release mutex before calling event handlers
232 dispatchEvent_Impl(oldValue
, newValue
);
237 Extracts a range of data from the node.
239 OUString SAL_CALL
CCharacterData::subStringData(sal_Int32 offset
, sal_Int32 count
)
241 ::osl::MutexGuard
const g(m_rMutex
);
244 if (m_aNodePtr
!= nullptr)
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) {
253 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
256 aStr
= tmp
.copy(offset
, count
);
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */