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