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