Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id$
*/
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMException.hpp>
#include "DOMAttrImpl.hpp"
#include "DOMStringPool.hpp"
#include "DOMDocumentImpl.hpp"
#include "DOMCasts.hpp"
#include "DOMNodeIDMap.hpp"
DOMAttrImpl::DOMAttrImpl(DOMDocument *ownerDoc, const XMLCh *aName)
: fNode(ownerDoc), fParent (ownerDoc)
{
DOMDocumentImpl *docImpl = (DOMDocumentImpl *)ownerDoc;
fName = docImpl->getPooledString(aName);
fNode.isSpecified(true);
};
DOMAttrImpl::DOMAttrImpl(const DOMAttrImpl &other, bool deep)
: fNode(other.fNode), fParent (other.fParent)
{
fName = other.fName;
if (other.fNode.isSpecified())
fNode.isSpecified(true);
else
fNode.isSpecified(false);
if (other.fNode.isIdAttr())
{
fNode.isIdAttr(true);
DOMDocumentImpl *doc = (DOMDocumentImpl *)this->getOwnerDocument();
doc->getNodeIDMap()->add(this);
}
};
DOMAttrImpl::~DOMAttrImpl() {
};
DOMNode * DOMAttrImpl::cloneNode(bool deep) const
{
DOMNode* newNode = new (this->getOwnerDocument(), DOMDocumentImpl::ATTR_OBJECT) DOMAttrImpl(*this, deep);
fNode.callUserDataHandlers(DOMUserDataHandler::NODE_CLONED, this, newNode);
return newNode;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
};
const XMLCh * DOMAttrImpl::getNodeName() const{
return fName;
};
short DOMAttrImpl::getNodeType() const {
return DOMNode::ATTRIBUTE_NODE;
};
const XMLCh * DOMAttrImpl::getName() const {
return fName;
};
const XMLCh * DOMAttrImpl::getNodeValue() const
{
return getValue();
};
bool DOMAttrImpl::getSpecified() const
{
return fNode.isSpecified();
};
const XMLCh * DOMAttrImpl::getValue() const
{
if (fParent.fFirstChild == 0) {
return XMLUni::fgZeroLenString; // return "";
}
DOMNode *node = castToChildImpl(fParent.fFirstChild)->nextSibling;
if (node == 0) {
return fParent.fFirstChild->getNodeValue();
}
int length = 0;
for (node = fParent.fFirstChild; node != 0; node = castToChildImpl(node)->nextSibling)
length += XMLString::stringLen(node->getNodeValue());
// revisit - Come up with some way of not allocating a new string each
// time we do this. But it's not an immediate pressing problem,
// becuase we only allocate a new string when we have attribute
// values that contain entity reference nodes. And the parser
// does not ever produce such a thing.
//XMLCh * retString = new (this->getOwnerDocument()) XMLCh[length+1];
length = sizeof(XMLCh) * (length+1);
length = (length % 4) + length;
XMLCh * retString = (XMLCh*) ((DOMDocumentImpl *)this->getOwnerDocument())->allocate(length);
retString[0] = 0;
for (node = fParent.fFirstChild; node != 0; node = castToChildImpl(node)->nextSibling)
{
XMLString::catString(retString, node->getNodeValue());
};
return retString;
};
void DOMAttrImpl::setNodeValue(const XMLCh *val)
{
setValue(val);
};
void DOMAttrImpl::setSpecified(bool arg)
{
fNode.isSpecified(arg);
};
void DOMAttrImpl::setValue(const XMLCh *val)
{
if (fNode.isReadOnly())
{
throw DOMException (
DOMException::NO_MODIFICATION_ALLOWED_ERR, 0);
}
// If this attribute was of type ID and in the map, take it out,
// then put it back in with the new name. For now, we don't worry
// about what happens if the new name conflicts
//
DOMDocumentImpl *doc = (DOMDocumentImpl *)getOwnerDocument();
if (fNode.isIdAttr())
doc->getNodeIDMap()->remove(this);
DOMNode *kid;
while ((kid = fParent.fFirstChild) != 0) // Remove existing kids
{
DOMNode* node = removeChild(kid);
if (node)
node->release();
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
}
if (val != 0) // Create and add the new one
appendChild(doc->createTextNode(val));
fNode.isSpecified(true);
fParent.changed();
if (fNode.isIdAttr())
doc->getNodeIDMap()->add(this);
};
//Introduced in DOM Level 2
DOMElement *DOMAttrImpl::getOwnerElement() const
{
// if we have an owner, ownerNode is our ownerElement, otherwise it's
// our ownerDocument and we don't have an ownerElement
return (DOMElement *) (fNode.isOwned() ? fNode.fOwnerNode : 0);
}
//internal use by parser only
void DOMAttrImpl::setOwnerElement(DOMElement *ownerElem)
{
fNode.fOwnerNode = ownerElem;
// revisit. Is this backwards? isOwned(true)?
fNode.isOwned(false);
}
void DOMAttrImpl::release()
{
if (fNode.isOwned() && !fNode.isToBeReleased())
throw DOMException(DOMException::INVALID_ACCESS_ERR,0);
DOMDocumentImpl* doc = (DOMDocumentImpl*) getOwnerDocument();
if (doc) {
fNode.callUserDataHandlers(DOMUserDataHandler::NODE_DELETED, 0, 0);
fParent.release();
doc->release(this, DOMDocumentImpl::ATTR_OBJECT);
}
else {
// shouldn't reach here
throw DOMException(DOMException::INVALID_ACCESS_ERR,0);
}
}
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
DOMNode* DOMAttrImpl::rename(const XMLCh* namespaceURI, const XMLCh* name)
{
DOMElement* el = getOwnerElement();
DOMDocumentImpl* doc = (DOMDocumentImpl*) getOwnerDocument();
if (el)
el->removeAttributeNode(this);
if (!namespaceURI || !*namespaceURI) {
fName = doc->getPooledString(name);
if (el)
el->setAttributeNode(this);
return this;
}
else {
// create a new AttrNS
DOMAttr* newAttr = doc->createAttributeNS(namespaceURI, name);
// transfer the userData
doc->transferUserData(castToNodeImpl(this), castToNodeImpl(newAttr));
// move children to new node
DOMNode* child = getFirstChild();
while (child) {
removeChild(child);
newAttr->appendChild(child);
child = getFirstChild();
}
// and fire user data NODE_RENAMED event
castToNodeImpl(newAttr)->callUserDataHandlers(DOMUserDataHandler::NODE_RENAMED, this, newAttr);
// reattach attr to element
if (el)
el->setAttributeNodeNS(newAttr);
return newAttr;
}
}
DOMNode* DOMAttrImpl::appendChild(DOMNode *newChild) {return fParent.appendChild (newChild); };
DOMNamedNodeMap* DOMAttrImpl::getAttributes() const {return fNode.getAttributes (); };
DOMNodeList* DOMAttrImpl::getChildNodes() const {return fParent.getChildNodes (); };
DOMNode* DOMAttrImpl::getFirstChild() const {return fParent.getFirstChild (); };
DOMNode* DOMAttrImpl::getLastChild() const {return fParent.getLastChild (); };
const XMLCh* DOMAttrImpl::getLocalName() const {return fNode.getLocalName (); };
const XMLCh* DOMAttrImpl::getNamespaceURI() const {return fNode.getNamespaceURI (); };
DOMNode* DOMAttrImpl::getNextSibling() const {return fNode.getNextSibling (); };
DOMDocument* DOMAttrImpl::getOwnerDocument() const {return fNode.getOwnerDocument (); };
const XMLCh* DOMAttrImpl::getPrefix() const {return fNode.getPrefix (); };
DOMNode* DOMAttrImpl::getParentNode() const {return fNode.getParentNode (); };
DOMNode* DOMAttrImpl::getPreviousSibling() const {return fNode.getPreviousSibling (); };
bool DOMAttrImpl::hasChildNodes() const {return fParent.hasChildNodes (); };
DOMNode* DOMAttrImpl::insertBefore(DOMNode *newChild, DOMNode *refChild)
{return fParent.insertBefore (newChild, refChild); };
void DOMAttrImpl::normalize() {fParent.normalize (); };
DOMNode* DOMAttrImpl::removeChild(DOMNode *oldChild) {return fParent.removeChild (oldChild); };
DOMNode* DOMAttrImpl::replaceChild(DOMNode *newChild, DOMNode *oldChild)
{return fParent.replaceChild (newChild, oldChild); };
bool DOMAttrImpl::isSupported(const XMLCh *feature, const XMLCh *version) const
{return fNode.isSupported (feature, version); };
void DOMAttrImpl::setPrefix(const XMLCh *prefix) {fNode.setPrefix(prefix); };
bool DOMAttrImpl::hasAttributes() const {return fNode.hasAttributes(); };
bool DOMAttrImpl::isSameNode(const DOMNode* other) {return fNode.isSameNode(other); };
bool DOMAttrImpl::isEqualNode(const DOMNode* arg) {return fParent.isEqualNode(arg); };
void* DOMAttrImpl::setUserData(const XMLCh* key, void* data, DOMUserDataHandler* handler)
{return fNode.setUserData(key, data, handler); };
void* DOMAttrImpl::getUserData(const XMLCh* key) const {return fNode.getUserData(key); };
const XMLCh* DOMAttrImpl::getBaseURI() const {return fNode.getBaseURI(); };
short DOMAttrImpl::compareTreePosition(DOMNode* other) {return fNode.compareTreePosition(other); };
const XMLCh* DOMAttrImpl::getTextContent() const {return fNode.getTextContent(); };
void DOMAttrImpl::setTextContent(const XMLCh* textContent){fNode.setTextContent(textContent); };
const XMLCh* DOMAttrImpl::lookupNamespacePrefix(const XMLCh* namespaceURI, bool useDefault) const {return fNode.lookupNamespacePrefix(namespaceURI, useDefault); };
bool DOMAttrImpl::isDefaultNamespace(const XMLCh* namespaceURI) const {return fNode.isDefaultNamespace(namespaceURI); };
const XMLCh* DOMAttrImpl::lookupNamespaceURI(const XMLCh* prefix) const {return fNode.lookupNamespaceURI(prefix); };
DOMNode* DOMAttrImpl::getInterface(const XMLCh* feature) {return fNode.getInterface(feature); };