1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: characterdata.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp>
32 #include "characterdata.hxx"
33 #include "../events/mutationevent.hxx"
39 CCharacterData::CCharacterData()
42 void CCharacterData::_dispatchEvent(const OUString
& prevValue
, const OUString
& newValue
)
44 Reference
< XDocumentEvent
> docevent(getOwnerDocument(), UNO_QUERY
);
45 Reference
< XMutationEvent
> event(docevent
->createEvent(
46 OUString::createFromAscii("DOMCharacterDataModified")), UNO_QUERY
);
47 event
->initMutationEvent(
48 OUString::createFromAscii("DOMCharacterDataModified"),
49 sal_True
, sal_False
, Reference
< XNode
>(),
50 prevValue
, newValue
, OUString(), (AttrChangeType
)0 );
51 dispatchEvent(Reference
< XEvent
>(event
, UNO_QUERY
));
52 dispatchSubtreeModified();
55 void CCharacterData::init_characterdata(const xmlNodePtr aNodePtr
)
61 Append the string to the end of the character data of the node.
63 void SAL_CALL
CCharacterData::appendData(const OUString
& arg
)
64 throw (RuntimeException
, DOMException
)
66 if (m_aNodePtr
!= NULL
)
68 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
69 xmlNodeAddContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(arg
, RTL_TEXTENCODING_UTF8
).getStr()));
70 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
71 _dispatchEvent(oldValue
, newValue
);
76 Remove a range of 16-bit units from the node.
78 void SAL_CALL
CCharacterData::deleteData(sal_Int32 offset
, sal_Int32 count
)
79 throw (RuntimeException
, DOMException
)
81 if (m_aNodePtr
!= NULL
)
84 OString
aData((const sal_Char
*)xmlNodeGetContent(m_aNodePtr
));
85 OUString
tmp(aData
, aData
.getLength(), RTL_TEXTENCODING_UTF8
);
86 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0) {
88 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
91 if ((offset
+count
) > tmp
.getLength())
92 count
= tmp
.getLength() - offset
;
94 OUString tmp2
= tmp
.copy(0, offset
);
95 tmp2
+= tmp
.copy(offset
+count
, tmp
.getLength() - (offset
+count
));
96 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
97 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
98 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
99 _dispatchEvent(oldValue
, newValue
);
106 Return the character data of the node that implements this interface.
108 OUString SAL_CALL
CCharacterData::getData() throw (RuntimeException
)
111 if (m_aNodePtr
!= NULL
)
113 OSL_ENSURE(m_aNodePtr
->content
, "character data node with NULL content, please inform lars.oppermann@sun.com!");
114 if (m_aNodePtr
->content
!= NULL
)
116 aData
= OUString((const sal_Char
*)m_aNodePtr
->content
, strlen((const sal_Char
*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
123 The number of 16-bit units that are available through data and the
124 substringData method below.
126 sal_Int32
CCharacterData::getLength() throw (RuntimeException
)
128 sal_Int32 length
= 0;
129 if (m_aNodePtr
!= NULL
)
131 OUString
aData((const sal_Char
*)m_aNodePtr
->content
, strlen((const sal_Char
*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
132 length
= aData
.getLength();
138 Insert a string at the specified 16-bit unit offset.
140 void SAL_CALL
CCharacterData::insertData(sal_Int32 offset
, const OUString
& arg
)
141 throw (RuntimeException
, DOMException
)
143 if (m_aNodePtr
!= NULL
)
146 OString
aData((const sal_Char
*)xmlNodeGetContent(m_aNodePtr
));
147 OUString
tmp(aData
, aData
.getLength(), RTL_TEXTENCODING_UTF8
);
148 if (offset
> tmp
.getLength() || offset
< 0) {
150 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
154 OUString tmp2
= tmp
.copy(0, offset
);
156 tmp2
+= tmp
.copy(offset
, tmp
.getLength() - offset
);
157 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
158 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
159 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
160 _dispatchEvent(oldValue
, newValue
);
167 Replace the characters starting at the specified 16-bit unit offset
168 with the specified string.
170 void SAL_CALL
CCharacterData::replaceData(sal_Int32 offset
, sal_Int32 count
, const OUString
& arg
)
171 throw (RuntimeException
, DOMException
)
173 if (m_aNodePtr
!= NULL
)
176 OString
aData((const sal_Char
*)xmlNodeGetContent(m_aNodePtr
));
177 OUString
tmp(aData
, aData
.getLength(), RTL_TEXTENCODING_UTF8
);
178 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0){
180 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
183 if ((offset
+count
) > tmp
.getLength())
184 count
= tmp
.getLength() - offset
;
186 OUString tmp2
= tmp
.copy(0, offset
);
188 tmp2
+= tmp
.copy(offset
+count
, tmp
.getLength() - (offset
+count
));
189 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
190 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(tmp2
, RTL_TEXTENCODING_UTF8
).getStr()));
191 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
192 _dispatchEvent(oldValue
, newValue
);
197 Set the character data of the node that implements this interface.
199 void SAL_CALL
CCharacterData::setData(const OUString
& data
)
200 throw (RuntimeException
, DOMException
)
202 if (m_aNodePtr
!= NULL
)
204 OUString
oldValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
205 xmlNodeSetContent(m_aNodePtr
, (const xmlChar
*)(OUStringToOString(data
, RTL_TEXTENCODING_UTF8
).getStr()));
206 OUString
newValue((char*)m_aNodePtr
->content
, strlen((char*)m_aNodePtr
->content
), RTL_TEXTENCODING_UTF8
);
207 _dispatchEvent(oldValue
, newValue
);
213 Extracts a range of data from the node.
215 OUString SAL_CALL
CCharacterData::subStringData(sal_Int32 offset
, sal_Int32 count
)
216 throw (RuntimeException
, DOMException
)
219 if (m_aNodePtr
!= NULL
)
222 OString
aData((const sal_Char
*)xmlNodeGetContent(m_aNodePtr
));
223 OUString
tmp(aData
, aData
.getLength(), RTL_TEXTENCODING_UTF8
);
224 if (offset
> tmp
.getLength() || offset
< 0 || count
< 0) {
226 e
.Code
= DOMExceptionType_INDEX_SIZE_ERR
;
229 aStr
= tmp
.copy(offset
, count
);