source: icXML/icXML-devel/src/xercesc/dom/impl/DOMCharacterDataImpl.cpp @ 2777

Last change on this file since 2777 was 2777, checked in by cameron, 7 years ago

Set up to use xercesc/icxercesc root-relative paths for all includes

File size: 9.1 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/*
19 * $Id: DOMCharacterDataImpl.cpp 678766 2008-07-22 14:00:16Z borisk $
20 */
21
22#include <xercesc/dom/impl/DOMCharacterDataImpl.hpp>
23#include <xercesc/dom/DOMException.hpp>
24#include <xercesc/dom/DOMNode.hpp>
25#include <xercesc/dom/impl/DOMRangeImpl.hpp>
26#include <xercesc/dom/impl/DOMDocumentImpl.hpp>
27#include <xercesc/dom/impl/DOMCasts.hpp>
28#include <xercesc/dom/impl/DOMStringPool.hpp>
29#include <xercesc/util/XMLUniDefs.hpp>
30
31XERCES_CPP_NAMESPACE_BEGIN
32
33DOMCharacterDataImpl::DOMCharacterDataImpl(DOMDocument *doc, const XMLCh *dat)
34{
35    fDoc = (DOMDocumentImpl*)doc;
36
37    XMLSize_t len=XMLString::stringLen(dat);
38    fDataBuf = fDoc->popBuffer(len+1);
39    if (!fDataBuf)
40        fDataBuf = new (fDoc) DOMBuffer(fDoc, len+15);
41    fDataBuf->set(dat, len);
42}
43
44DOMCharacterDataImpl::
45DOMCharacterDataImpl(DOMDocument *doc, const XMLCh* dat, XMLSize_t len)
46{
47    fDoc = (DOMDocumentImpl*)doc;
48
49    fDataBuf = fDoc->popBuffer(len+1);
50
51    if (!fDataBuf)
52        fDataBuf = new (fDoc) DOMBuffer(fDoc, len+15);
53
54    fDataBuf->set(dat, len);
55}
56
57DOMCharacterDataImpl::DOMCharacterDataImpl(const DOMCharacterDataImpl &other)
58{
59    fDoc = (DOMDocumentImpl*)other.fDoc;
60
61    XMLSize_t len=other.getLength();
62    fDataBuf = fDoc->popBuffer(len+1);
63    if (!fDataBuf)
64        fDataBuf = new (fDoc) DOMBuffer(fDoc, len+15);
65    fDataBuf->set(other.fDataBuf->getRawBuffer(), len);
66}
67
68
69DOMCharacterDataImpl::~DOMCharacterDataImpl() {
70}
71
72
73const XMLCh * DOMCharacterDataImpl::getNodeValue() const
74{
75    return fDataBuf->getRawBuffer();
76}
77
78
79void DOMCharacterDataImpl::setNodeValue(const DOMNode *node, const XMLCh *value)
80{
81    if (castToNodeImpl(node)->isReadOnly())
82        throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
83    fDataBuf->set(value);
84
85    DOMDocumentImpl *doc = (DOMDocumentImpl *)node->getOwnerDocument();
86    if (doc != 0) {
87        Ranges* ranges = doc->getRanges();
88        if (ranges != 0) {
89            XMLSize_t sz = ranges->size();
90            if (sz != 0) {
91                for (XMLSize_t i =0; i<sz; i++) {
92                    ranges->elementAt(i)->receiveReplacedText((DOMNode*)node);
93                }
94            }
95        }
96    }
97}
98
99
100void DOMCharacterDataImpl::appendData(const DOMNode *node, const XMLCh *dat)
101{
102    if(castToNodeImpl(node)->isReadOnly())
103        throw DOMException(
104        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
105
106    fDataBuf->append(dat);
107}
108
109void DOMCharacterDataImpl::appendData(const DOMNode *node, const  XMLCh *dat, XMLSize_t n)
110{
111  if(castToNodeImpl(node)->isReadOnly())
112        throw DOMException(
113        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
114
115  fDataBuf->append(dat, n);
116}
117
118void DOMCharacterDataImpl::deleteData(const DOMNode *node, XMLSize_t offset, XMLSize_t count)
119{
120    if (castToNodeImpl(node)->isReadOnly())
121        throw DOMException(DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
122
123    // Note: the C++ XMLCh * operation throws the correct DOMExceptions
124    //       when parameter values are bad.
125    //
126
127    XMLSize_t len = this->fDataBuf->getLen();
128    if (offset > len)
129        throw DOMException(DOMException::INDEX_SIZE_ERR, 0, GetDOMCharacterDataImplMemoryManager);
130
131
132
133    // Cap the value of delLength to avoid trouble with overflows
134    //  in the following length computations.
135    if (count > len)
136        count = len;
137
138    // If the length of data to be deleted would extend off the end
139    //   of the string, cut it back to stop at the end of string.
140    if (offset + count >= len)
141        count = len - offset;
142
143    XMLSize_t newLen = len - count;
144
145    XMLCh* newString;
146    XMLCh temp[4096];
147    if (newLen >= 4095)
148        newString = (XMLCh*) XMLPlatformUtils::fgMemoryManager->allocate
149        (
150            (newLen+1) * sizeof(XMLCh)
151        );//new XMLCh[newLen+1];
152    else
153        newString = temp;
154
155    XMLString::copyNString(newString, fDataBuf->getRawBuffer(), offset);
156    XMLString::copyString(newString+offset, fDataBuf->getRawBuffer()+offset+count);
157
158    fDataBuf->set(newString);
159
160    if (newLen >= 4095)
161        XMLPlatformUtils::fgMemoryManager->deallocate(newString);//delete[] newString;
162
163    // We don't delete the old string (doesn't work), or alter
164    //   the old string (may be shared)
165    //   It just hangs around, possibly orphaned.
166
167    DOMDocumentImpl *doc = (DOMDocumentImpl *)node->getOwnerDocument();
168    if (doc != 0) {
169        Ranges* ranges = doc->getRanges();
170        if (ranges != 0) {
171            XMLSize_t sz = ranges->size();
172            if (sz != 0) {
173                for (XMLSize_t i =0; i<sz; i++) {
174                    ranges->elementAt(i)->updateRangeForDeletedText( (DOMNode*)node, offset, count);
175                }
176            }
177        }
178    }
179}
180
181
182
183const XMLCh *DOMCharacterDataImpl::getData() const
184{
185    return fDataBuf->getRawBuffer();
186}
187
188
189//
190//  getCharDataLength - return the length of the character data string.
191//
192XMLSize_t DOMCharacterDataImpl::getLength() const
193{
194    return fDataBuf->getLen();
195}
196
197
198
199void DOMCharacterDataImpl::insertData(const DOMNode *node, XMLSize_t offset, const XMLCh *dat)
200{
201    if (castToNodeImpl(node)->isReadOnly())
202        throw DOMException(
203        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
204
205    // Note: the C++ XMLCh * operation throws the correct DOMExceptions
206    //       when parameter values are bad.
207    //
208
209    XMLSize_t len = fDataBuf->getLen();
210    if (offset > len)
211        throw DOMException(DOMException::INDEX_SIZE_ERR, 0, GetDOMCharacterDataImplMemoryManager);
212
213    XMLSize_t datLen = XMLString::stringLen(dat);
214
215    XMLSize_t newLen = len + datLen;
216
217    XMLCh* newString;
218    XMLCh temp[4096];
219    if (newLen >= 4095)
220        newString = (XMLCh*) XMLPlatformUtils::fgMemoryManager->allocate
221        (
222            (newLen + 1) * sizeof(XMLCh)
223        );//new XMLCh[newLen+1];
224    else
225        newString = temp;
226
227    XMLString::copyNString(newString, fDataBuf->getRawBuffer(), offset);
228    XMLString::copyNString(newString+offset, dat, datLen);
229    XMLString::copyString(newString+offset+datLen, fDataBuf->getRawBuffer()+offset);
230
231    fDataBuf->set(newString);
232
233    if (newLen >= 4095)
234        XMLPlatformUtils::fgMemoryManager->deallocate(newString);//delete[] newString;
235
236    DOMDocumentImpl *doc = (DOMDocumentImpl *)node->getOwnerDocument();
237    if (doc != 0) {
238        Ranges* ranges = doc->getRanges();
239        if (ranges != 0) {
240            XMLSize_t sz = ranges->size();
241            if (sz != 0) {
242                for (XMLSize_t i =0; i<sz; i++) {
243                    ranges->elementAt(i)->updateRangeForInsertedText( (DOMNode*)node, offset, datLen);
244                }
245            }
246        }
247    }
248}
249
250
251
252void DOMCharacterDataImpl::replaceData(const DOMNode *node, XMLSize_t offset, XMLSize_t count,
253                                    const XMLCh *dat)
254{
255    if (castToNodeImpl(node)->isReadOnly())
256        throw DOMException(
257        DOMException::NO_MODIFICATION_ALLOWED_ERR, 0, GetDOMCharacterDataImplMemoryManager);
258
259    deleteData(node, offset, count);
260    insertData(node, offset, dat);
261}
262
263
264
265
266void DOMCharacterDataImpl::setData(const DOMNode *node, const XMLCh *arg)
267{
268    setNodeValue(node, arg);
269}
270
271
272
273
274
275const XMLCh * DOMCharacterDataImpl::substringData(const DOMNode *node, XMLSize_t offset,
276                                           XMLSize_t count) const
277{
278
279    // Note: the C++ XMLCh * operation throws the correct DOMExceptions
280    //       when parameter values are bad.
281    //
282
283
284    XMLSize_t len = fDataBuf->getLen();
285
286    if (offset > len)
287        throw DOMException(DOMException::INDEX_SIZE_ERR, 0, GetDOMCharacterDataImplMemoryManager);
288
289    DOMDocumentImpl *doc = (DOMDocumentImpl *)node->getOwnerDocument();
290
291    XMLCh* newString;
292    XMLCh temp[4096];
293    if (len >= 4095)
294      newString = (XMLCh*) doc->getMemoryManager()->allocate
295        (
296            (len + 1) * sizeof(XMLCh)
297        );//new XMLCh[len+1];
298    else
299        newString = temp;
300
301    XMLString::copyNString(newString, fDataBuf->getRawBuffer()+offset, count);
302    newString[count] = chNull;
303
304    const XMLCh* retString = doc->getPooledString(newString);
305
306    if (len >= 4095)
307      doc->getMemoryManager()->deallocate(newString);//delete[] newString;
308
309    return retString;
310
311}
312
313
314void DOMCharacterDataImpl::releaseBuffer() {
315    fDoc->releaseBuffer(fDataBuf);
316}
317
318XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.