1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <characterdata.hxx>
24 #include <boost/shared_ptr.hpp>
26 #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
28 #include "../events/mutationevent.hxx"
34 CCharacterData::CCharacterData(
35 CDocument
const& rDocument
, ::osl::Mutex
const& rMutex
,
36 NodeType
const& reNodeType
, xmlNodePtr
const& rpNode
)
37 : CCharacterData_Base(rDocument
, rMutex
, reNodeType
, rpNode
)
41 void CCharacterData::dispatchEvent_Impl(
42 OUString
const& prevValue
, OUString
const& newValue
)
44 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
45 Reference
< XMutationEvent
> event(docevent
->createEvent(
46 "DOMCharacterDataModified"), UNO_QUERY
);
47 event
->initMutationEvent(
48 "DOMCharacterDataModified",
49 sal_True
, sal_False
, Reference
< XNode
>(),
50 prevValue
, newValue
, OUString(), (AttrChangeType
)0 );
51 dispatchEvent(Reference
< XEvent
>(event
, UNO_QUERY
));
52 dispatchSubtreeModified();
56 Append the string to the end of the character data of the node.
58 void SAL_CALL
CCharacterData::appendData(const OUString
& arg
)
59 throw (RuntimeException
, DOMException
)
61 ::osl::ClearableMutexGuard
guard(m_rMutex
);
63 if (m_aNodePtr
!= NULL
)
65 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
66 xmlNodeAddContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(arg
, RTL_TEXTENCODING_UTF8
).getStr()));
67 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
69 guard
.clear(); // release mutex before calling event handlers
70 dispatchEvent_Impl(oldValue
, newValue
);
75 Remove a range of 16-bit units from the node.
77 void SAL_CALL
CCharacterData::deleteData(sal_Int32 offset
, sal_Int32 count
)
78 throw (RuntimeException
, DOMException
)
80 ::osl::ClearableMutexGuard
guard(m_rMutex
);
82 if (m_aNodePtr
!= NULL
)
85 ::boost::shared_ptr
<xmlChar
const> const pContent(
86 xmlNodeGetContent(m_aNodePtr
), xmlFree
);
87 OString
aData(reinterpret_cast<sal_Char
const*>(pContent
.get()));
88 OUString
tmp(OStringToOUString(aData
, RTL_TEXTENCODING_UTF8
));
89 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0) {
91 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
94 if ((offset
+count
) > tmp
.getLength())
95 count
= tmp
.getLength() - offset
;
97 OUString tmp2
= tmp
.copy(0, offset
);
98 tmp2
+= tmp
.copy(offset
+count
, tmp
.getLength() - (offset
+count
));
99 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
100 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
101 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
103 guard
.clear(); // release mutex before calling event handlers
104 dispatchEvent_Impl(oldValue
, newValue
);
110 Return the character data of the node that implements this interface.
112 OUString SAL_CALL
CCharacterData::getData() throw (RuntimeException
)
114 ::osl::MutexGuard
const g(m_rMutex
);
117 if (m_aNodePtr
!= NULL
)
119 OSL_ENSURE(m_aNodePtr
->content
, "character data node with NULL content, please inform lars.oppermann@sun.com!");
120 if (m_aNodePtr
->content
!= NULL
)
122 aData
= OUString((const sal_Char
*)m_aNodePtr
->content
, strlen((const sal_Char
*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
129 The number of 16-bit units that are available through data and the
130 substringData method below.
132 sal_Int32 SAL_CALL
CCharacterData::getLength() throw (RuntimeException
)
134 ::osl::MutexGuard
const g(m_rMutex
);
136 sal_Int32 length
= 0;
137 if (m_aNodePtr
!= NULL
)
139 OUString
aData((const sal_Char
*)m_aNodePtr
->content
, strlen((const sal_Char
*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
140 length
= aData
.getLength();
146 Insert a string at the specified 16-bit unit offset.
148 void SAL_CALL
CCharacterData::insertData(sal_Int32 offset
, const OUString
& arg
)
149 throw (RuntimeException
, DOMException
)
151 ::osl::ClearableMutexGuard
guard(m_rMutex
);
153 if (m_aNodePtr
!= NULL
)
156 ::boost::shared_ptr
<xmlChar
const> const pContent(
157 xmlNodeGetContent(m_aNodePtr
), xmlFree
);
158 OString
aData(reinterpret_cast<sal_Char
const*>(pContent
.get()));
159 OUString
tmp(OStringToOUString(aData
, RTL_TEXTENCODING_UTF8
));
160 if (offset
> tmp
.getLength() || offset
< 0) {
162 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
166 OUString tmp2
= tmp
.copy(0, offset
);
168 tmp2
+= tmp
.copy(offset
, tmp
.getLength() - offset
);
169 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
170 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
171 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
173 guard
.clear(); // release mutex before calling event handlers
174 dispatchEvent_Impl(oldValue
, newValue
);
180 Replace the characters starting at the specified 16-bit unit offset
181 with the specified string.
183 void SAL_CALL
CCharacterData::replaceData(sal_Int32 offset
, sal_Int32 count
, const OUString
& arg
)
184 throw (RuntimeException
, DOMException
)
186 ::osl::ClearableMutexGuard
guard(m_rMutex
);
188 if (m_aNodePtr
!= NULL
)
191 ::boost::shared_ptr
<xmlChar
const> const pContent(
192 xmlNodeGetContent(m_aNodePtr
), xmlFree
);
193 OString
aData(reinterpret_cast<sal_Char
const*>(pContent
.get()));
194 OUString
tmp(OStringToOUString(aData
, RTL_TEXTENCODING_UTF8
));
195 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0){
197 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
200 if ((offset
+count
) > tmp
.getLength())
201 count
= tmp
.getLength() - offset
;
203 OUString tmp2
= tmp
.copy(0, offset
);
205 tmp2
+= tmp
.copy(offset
+count
, tmp
.getLength() - (offset
+count
));
206 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
207 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
208 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
210 guard
.clear(); // release mutex before calling event handlers
211 dispatchEvent_Impl(oldValue
, newValue
);
216 Set the character data of the node that implements this interface.
218 void SAL_CALL
CCharacterData::setData(const OUString
& data
)
219 throw (RuntimeException
, DOMException
)
221 ::osl::ClearableMutexGuard
guard(m_rMutex
);
223 if (m_aNodePtr
!= NULL
)
225 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
226 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(data
, RTL_TEXTENCODING_UTF8
).getStr()));
227 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
229 guard
.clear(); // release mutex before calling event handlers
230 dispatchEvent_Impl(oldValue
, newValue
);
235 Extracts a range of data from the node.
237 OUString SAL_CALL
CCharacterData::subStringData(sal_Int32 offset
, sal_Int32 count
)
238 throw (RuntimeException
, DOMException
)
240 ::osl::MutexGuard
const g(m_rMutex
);
243 if (m_aNodePtr
!= NULL
)
246 ::boost::shared_ptr
<xmlChar
const> const pContent(
247 xmlNodeGetContent(m_aNodePtr
), xmlFree
);
248 OString
aData(reinterpret_cast<sal_Char
const*>(pContent
.get()));
249 OUString
tmp(OStringToOUString(aData
, RTL_TEXTENCODING_UTF8
));
250 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0) {
252 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
255 aStr
= tmp
.copy(offset
, count
);
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */