build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / unoxml / source / dom / characterdata.cxx
blobfea48292663ce36664d19da6c8a8250039b19165
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 <characterdata.hxx>
22 #include <string.h>
24 #include <memory>
26 #include <osl/diagnose.h>
28 #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
30 #include "../events/mutationevent.hxx"
32 using namespace css::uno;
33 using namespace css::xml::dom;
34 using namespace css::xml::dom::events;
36 namespace DOM
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 );
56 dispatchEvent(event);
57 dispatchSubtreeModified();
60 /**
61 Append the string to the end of the character data of the node.
63 void SAL_CALL CCharacterData::appendData(const OUString& arg)
64 throw (RuntimeException, DOMException, std::exception)
66 ::osl::ClearableMutexGuard guard(m_rMutex);
68 if (m_aNodePtr != nullptr)
70 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
71 xmlNodeAddContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr()));
72 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
74 guard.clear(); // release mutex before calling event handlers
75 dispatchEvent_Impl(oldValue, newValue);
79 /**
80 Remove a range of 16-bit units from the node.
82 void SAL_CALL CCharacterData::deleteData(sal_Int32 offset, sal_Int32 count)
83 throw (RuntimeException, DOMException, std::exception)
85 ::osl::ClearableMutexGuard guard(m_rMutex);
87 if (m_aNodePtr != nullptr)
89 // get current data
90 std::shared_ptr<xmlChar const> const pContent(
91 xmlNodeGetContent(m_aNodePtr), xmlFree);
92 OString aData(reinterpret_cast<sal_Char const*>(pContent.get()));
93 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
94 if (offset > tmp.getLength() || offset < 0 || count < 0) {
95 DOMException e;
96 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
97 throw e;
99 if ((offset+count) > tmp.getLength())
100 count = tmp.getLength() - offset;
102 OUString tmp2 = tmp.copy(0, offset);
103 tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count));
104 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
105 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
106 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
108 guard.clear(); // release mutex before calling event handlers
109 dispatchEvent_Impl(oldValue, newValue);
115 Return the character data of the node that implements this interface.
117 OUString SAL_CALL CCharacterData::getData() throw (RuntimeException, std::exception)
119 ::osl::MutexGuard const g(m_rMutex);
121 OUString aData;
122 if (m_aNodePtr != nullptr)
124 OSL_ENSURE(m_aNodePtr->content, "character data node with NULL content, please inform lars.oppermann@sun.com!");
125 if (m_aNodePtr->content != nullptr)
127 aData = OUString(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
130 return aData;
134 The number of 16-bit units that are available through data and the
135 substringData method below.
137 sal_Int32 SAL_CALL CCharacterData::getLength() throw (RuntimeException, std::exception)
139 ::osl::MutexGuard const g(m_rMutex);
141 sal_Int32 length = 0;
142 if (m_aNodePtr != nullptr)
144 OUString aData(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
145 length = aData.getLength();
147 return length;
151 Insert a string at the specified 16-bit unit offset.
153 void SAL_CALL CCharacterData::insertData(sal_Int32 offset, const OUString& arg)
154 throw (RuntimeException, DOMException, std::exception)
156 ::osl::ClearableMutexGuard guard(m_rMutex);
158 if (m_aNodePtr != nullptr)
160 // get current data
161 std::shared_ptr<xmlChar const> const pContent(
162 xmlNodeGetContent(m_aNodePtr), xmlFree);
163 OString aData(reinterpret_cast<sal_Char const*>(pContent.get()));
164 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
165 if (offset > tmp.getLength() || offset < 0) {
166 DOMException e;
167 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
168 throw e;
171 OUString tmp2 = tmp.copy(0, offset);
172 tmp2 += arg;
173 tmp2 += tmp.copy(offset, tmp.getLength() - offset);
174 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
175 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
176 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
178 guard.clear(); // release mutex before calling event handlers
179 dispatchEvent_Impl(oldValue, newValue);
185 Replace the characters starting at the specified 16-bit unit offset
186 with the specified string.
188 void SAL_CALL CCharacterData::replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg)
189 throw (RuntimeException, DOMException, std::exception)
191 ::osl::ClearableMutexGuard guard(m_rMutex);
193 if (m_aNodePtr != nullptr)
195 // get current data
196 std::shared_ptr<xmlChar const> const pContent(
197 xmlNodeGetContent(m_aNodePtr), xmlFree);
198 OString aData(reinterpret_cast<sal_Char const*>(pContent.get()));
199 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
200 if (offset > tmp.getLength() || offset < 0 || count < 0){
201 DOMException e;
202 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
203 throw e;
205 if ((offset+count) > tmp.getLength())
206 count = tmp.getLength() - offset;
208 OUString tmp2 = tmp.copy(0, offset);
209 tmp2 += arg;
210 tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count));
211 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
212 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr()));
213 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
215 guard.clear(); // release mutex before calling event handlers
216 dispatchEvent_Impl(oldValue, newValue);
221 Set the character data of the node that implements this interface.
223 void SAL_CALL CCharacterData::setData(const OUString& data)
224 throw (RuntimeException, DOMException, std::exception)
226 ::osl::ClearableMutexGuard guard(m_rMutex);
228 if (m_aNodePtr != nullptr)
230 OUString oldValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
231 xmlNodeSetContent(m_aNodePtr, reinterpret_cast<const xmlChar*>(OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr()));
232 OUString newValue(reinterpret_cast<char*>(m_aNodePtr->content), strlen(reinterpret_cast<char*>(m_aNodePtr->content)), RTL_TEXTENCODING_UTF8);
234 guard.clear(); // release mutex before calling event handlers
235 dispatchEvent_Impl(oldValue, newValue);
240 Extracts a range of data from the node.
242 OUString SAL_CALL CCharacterData::subStringData(sal_Int32 offset, sal_Int32 count)
243 throw (RuntimeException, DOMException, std::exception)
245 ::osl::MutexGuard const g(m_rMutex);
247 OUString aStr;
248 if (m_aNodePtr != nullptr)
250 // get current data
251 std::shared_ptr<xmlChar const> const pContent(
252 xmlNodeGetContent(m_aNodePtr), xmlFree);
253 OString aData(reinterpret_cast<sal_Char const*>(pContent.get()));
254 OUString tmp(OStringToOUString(aData, RTL_TEXTENCODING_UTF8));
255 if (offset > tmp.getLength() || offset < 0 || count < 0) {
256 DOMException e;
257 e.Code = DOMExceptionType_INDEX_SIZE_ERR;
258 throw e;
260 aStr = tmp.copy(offset, count);
262 return aStr;
266 } // namspace DOM
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */