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