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