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