1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include <characterdata.hxx> 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir #include <string.h> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <boost/shared_ptr.hpp> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <com/sun/star/xml/dom/events/XDocumentEvent.hpp> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include "../events/mutationevent.hxx" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir namespace DOM 40*cdf0e10cSrcweir { 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir CCharacterData::CCharacterData( 43*cdf0e10cSrcweir CDocument const& rDocument, ::osl::Mutex const& rMutex, 44*cdf0e10cSrcweir NodeType const& reNodeType, xmlNodePtr const& rpNode) 45*cdf0e10cSrcweir : CCharacterData_Base(rDocument, rMutex, reNodeType, rpNode) 46*cdf0e10cSrcweir { 47*cdf0e10cSrcweir } 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir void CCharacterData::dispatchEvent_Impl( 50*cdf0e10cSrcweir OUString const& prevValue, OUString const& newValue) 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir Reference< XDocumentEvent > docevent(getOwnerDocument(), UNO_QUERY); 53*cdf0e10cSrcweir Reference< XMutationEvent > event(docevent->createEvent( 54*cdf0e10cSrcweir OUString::createFromAscii("DOMCharacterDataModified")), UNO_QUERY); 55*cdf0e10cSrcweir event->initMutationEvent( 56*cdf0e10cSrcweir OUString::createFromAscii("DOMCharacterDataModified"), 57*cdf0e10cSrcweir sal_True, sal_False, Reference< XNode >(), 58*cdf0e10cSrcweir prevValue, newValue, OUString(), (AttrChangeType)0 ); 59*cdf0e10cSrcweir dispatchEvent(Reference< XEvent >(event, UNO_QUERY)); 60*cdf0e10cSrcweir dispatchSubtreeModified(); 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir 63*cdf0e10cSrcweir /** 64*cdf0e10cSrcweir Append the string to the end of the character data of the node. 65*cdf0e10cSrcweir */ 66*cdf0e10cSrcweir void SAL_CALL CCharacterData::appendData(const OUString& arg) 67*cdf0e10cSrcweir throw (RuntimeException, DOMException) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir ::osl::ClearableMutexGuard guard(m_rMutex); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir if (m_aNodePtr != NULL) 72*cdf0e10cSrcweir { 73*cdf0e10cSrcweir OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 74*cdf0e10cSrcweir xmlNodeAddContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr())); 75*cdf0e10cSrcweir OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir guard.clear(); // release mutex before calling event handlers 78*cdf0e10cSrcweir dispatchEvent_Impl(oldValue, newValue); 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir /** 83*cdf0e10cSrcweir Remove a range of 16-bit units from the node. 84*cdf0e10cSrcweir */ 85*cdf0e10cSrcweir void SAL_CALL CCharacterData::deleteData(sal_Int32 offset, sal_Int32 count) 86*cdf0e10cSrcweir throw (RuntimeException, DOMException) 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir ::osl::ClearableMutexGuard guard(m_rMutex); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir if (m_aNodePtr != NULL) 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir // get current data 93*cdf0e10cSrcweir ::boost::shared_ptr<xmlChar const> const pContent( 94*cdf0e10cSrcweir xmlNodeGetContent(m_aNodePtr), xmlFree); 95*cdf0e10cSrcweir OString aData(reinterpret_cast<sal_Char const*>(pContent.get())); 96*cdf0e10cSrcweir OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); 97*cdf0e10cSrcweir if (offset > tmp.getLength() || offset < 0 || count < 0) { 98*cdf0e10cSrcweir DOMException e; 99*cdf0e10cSrcweir e.Code = DOMExceptionType_INDEX_SIZE_ERR; 100*cdf0e10cSrcweir throw e; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir if ((offset+count) > tmp.getLength()) 103*cdf0e10cSrcweir count = tmp.getLength() - offset; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir OUString tmp2 = tmp.copy(0, offset); 106*cdf0e10cSrcweir tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count)); 107*cdf0e10cSrcweir OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 108*cdf0e10cSrcweir xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); 109*cdf0e10cSrcweir OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir guard.clear(); // release mutex before calling event handlers 112*cdf0e10cSrcweir dispatchEvent_Impl(oldValue, newValue); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** 118*cdf0e10cSrcweir Return the character data of the node that implements this interface. 119*cdf0e10cSrcweir */ 120*cdf0e10cSrcweir OUString SAL_CALL CCharacterData::getData() throw (RuntimeException) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir OUString aData; 125*cdf0e10cSrcweir if (m_aNodePtr != NULL) 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir OSL_ENSURE(m_aNodePtr->content, "character data node with NULL content, please inform lars.oppermann@sun.com!"); 128*cdf0e10cSrcweir if (m_aNodePtr->content != NULL) 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir aData = OUString((const sal_Char*)m_aNodePtr->content, strlen((const sal_Char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir return aData; 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir /** 137*cdf0e10cSrcweir The number of 16-bit units that are available through data and the 138*cdf0e10cSrcweir substringData method below. 139*cdf0e10cSrcweir */ 140*cdf0e10cSrcweir sal_Int32 SAL_CALL CCharacterData::getLength() throw (RuntimeException) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir sal_Int32 length = 0; 145*cdf0e10cSrcweir if (m_aNodePtr != NULL) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir OUString aData((const sal_Char*)m_aNodePtr->content, strlen((const sal_Char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 148*cdf0e10cSrcweir length = aData.getLength(); 149*cdf0e10cSrcweir } 150*cdf0e10cSrcweir return length; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir /** 154*cdf0e10cSrcweir Insert a string at the specified 16-bit unit offset. 155*cdf0e10cSrcweir */ 156*cdf0e10cSrcweir void SAL_CALL CCharacterData::insertData(sal_Int32 offset, const OUString& arg) 157*cdf0e10cSrcweir throw (RuntimeException, DOMException) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir ::osl::ClearableMutexGuard guard(m_rMutex); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir if (m_aNodePtr != NULL) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir // get current data 164*cdf0e10cSrcweir ::boost::shared_ptr<xmlChar const> const pContent( 165*cdf0e10cSrcweir xmlNodeGetContent(m_aNodePtr), xmlFree); 166*cdf0e10cSrcweir OString aData(reinterpret_cast<sal_Char const*>(pContent.get())); 167*cdf0e10cSrcweir OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); 168*cdf0e10cSrcweir if (offset > tmp.getLength() || offset < 0) { 169*cdf0e10cSrcweir DOMException e; 170*cdf0e10cSrcweir e.Code = DOMExceptionType_INDEX_SIZE_ERR; 171*cdf0e10cSrcweir throw e; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir OUString tmp2 = tmp.copy(0, offset); 175*cdf0e10cSrcweir tmp2 += arg; 176*cdf0e10cSrcweir tmp2 += tmp.copy(offset, tmp.getLength() - offset); 177*cdf0e10cSrcweir OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 178*cdf0e10cSrcweir xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); 179*cdf0e10cSrcweir OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir guard.clear(); // release mutex before calling event handlers 182*cdf0e10cSrcweir dispatchEvent_Impl(oldValue, newValue); 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir /** 188*cdf0e10cSrcweir Replace the characters starting at the specified 16-bit unit offset 189*cdf0e10cSrcweir with the specified string. 190*cdf0e10cSrcweir */ 191*cdf0e10cSrcweir void SAL_CALL CCharacterData::replaceData(sal_Int32 offset, sal_Int32 count, const OUString& arg) 192*cdf0e10cSrcweir throw (RuntimeException, DOMException) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir ::osl::ClearableMutexGuard guard(m_rMutex); 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir if (m_aNodePtr != NULL) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir // get current data 199*cdf0e10cSrcweir ::boost::shared_ptr<xmlChar const> const pContent( 200*cdf0e10cSrcweir xmlNodeGetContent(m_aNodePtr), xmlFree); 201*cdf0e10cSrcweir OString aData(reinterpret_cast<sal_Char const*>(pContent.get())); 202*cdf0e10cSrcweir OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); 203*cdf0e10cSrcweir if (offset > tmp.getLength() || offset < 0 || count < 0){ 204*cdf0e10cSrcweir DOMException e; 205*cdf0e10cSrcweir e.Code = DOMExceptionType_INDEX_SIZE_ERR; 206*cdf0e10cSrcweir throw e; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir if ((offset+count) > tmp.getLength()) 209*cdf0e10cSrcweir count = tmp.getLength() - offset; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir OUString tmp2 = tmp.copy(0, offset); 212*cdf0e10cSrcweir tmp2 += arg; 213*cdf0e10cSrcweir tmp2 += tmp.copy(offset+count, tmp.getLength() - (offset+count)); 214*cdf0e10cSrcweir OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 215*cdf0e10cSrcweir xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(tmp2, RTL_TEXTENCODING_UTF8).getStr())); 216*cdf0e10cSrcweir OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir guard.clear(); // release mutex before calling event handlers 219*cdf0e10cSrcweir dispatchEvent_Impl(oldValue, newValue); 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir /** 224*cdf0e10cSrcweir Set the character data of the node that implements this interface. 225*cdf0e10cSrcweir */ 226*cdf0e10cSrcweir void SAL_CALL CCharacterData::setData(const OUString& data) 227*cdf0e10cSrcweir throw (RuntimeException, DOMException) 228*cdf0e10cSrcweir { 229*cdf0e10cSrcweir ::osl::ClearableMutexGuard guard(m_rMutex); 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir if (m_aNodePtr != NULL) 232*cdf0e10cSrcweir { 233*cdf0e10cSrcweir OUString oldValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 234*cdf0e10cSrcweir xmlNodeSetContent(m_aNodePtr, (const xmlChar*)(OUStringToOString(data, RTL_TEXTENCODING_UTF8).getStr())); 235*cdf0e10cSrcweir OUString newValue((char*)m_aNodePtr->content, strlen((char*)m_aNodePtr->content), RTL_TEXTENCODING_UTF8); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir guard.clear(); // release mutex before calling event handlers 238*cdf0e10cSrcweir dispatchEvent_Impl(oldValue, newValue); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir } 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir /** 243*cdf0e10cSrcweir Extracts a range of data from the node. 244*cdf0e10cSrcweir */ 245*cdf0e10cSrcweir OUString SAL_CALL CCharacterData::subStringData(sal_Int32 offset, sal_Int32 count) 246*cdf0e10cSrcweir throw (RuntimeException, DOMException) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir ::osl::MutexGuard const g(m_rMutex); 249*cdf0e10cSrcweir 250*cdf0e10cSrcweir OUString aStr; 251*cdf0e10cSrcweir if (m_aNodePtr != NULL) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir // get current data 254*cdf0e10cSrcweir ::boost::shared_ptr<xmlChar const> const pContent( 255*cdf0e10cSrcweir xmlNodeGetContent(m_aNodePtr), xmlFree); 256*cdf0e10cSrcweir OString aData(reinterpret_cast<sal_Char const*>(pContent.get())); 257*cdf0e10cSrcweir OUString tmp(aData, aData.getLength(), RTL_TEXTENCODING_UTF8); 258*cdf0e10cSrcweir if (offset > tmp.getLength() || offset < 0 || count < 0) { 259*cdf0e10cSrcweir DOMException e; 260*cdf0e10cSrcweir e.Code = DOMExceptionType_INDEX_SIZE_ERR; 261*cdf0e10cSrcweir throw e; 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir aStr = tmp.copy(offset, count); 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir return aStr; 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir } // namspace DOM 270*cdf0e10cSrcweir 271