0025621: CAST analysis - Avoid constructors not supplying an initial value for all...
[occt.git] / src / LDOM / LDOM_Element.cxx
CommitLineData
b311480e 1// Created on: 2001-06-27
2// Created by: Alexander GRIGORIEV
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
7fd59977 15
16#include <LDOM_MemManager.hxx>
17#include <LDOM_BasicElement.hxx>
18#include <LDOM_BasicAttribute.hxx>
19
20#include <Standard_ProgramError.hxx>
21
22#include <stdio.h>
23
24//=======================================================================
25//function : LDOM_Element
26//purpose :
27//=======================================================================
28
29LDOM_Element::LDOM_Element (const LDOM_BasicElement& anElem,
30 const Handle(LDOM_MemManager)& aDoc)
31 : LDOM_Node (anElem, aDoc) {}
32
33//=======================================================================
34//function : getAttribute
35//purpose :
36//=======================================================================
37
38LDOMString LDOM_Element::getAttribute (const LDOMString& aName) const
39{
40 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
41 if (anElem.isNull()) return LDOMString ();
42 if (myLastChild == NULL) {
43 const LDOM_BasicNode * aNode = anElem.GetFirstChild();
44 if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
302f96fb 45 for(;;) {
7fd59977 46 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
47 if (aSibling == NULL)
48 return LDOMString ();
49 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
50 (const LDOM_BasicNode *&) myLastChild = aNode;
51 break;
52 }
53 aNode = aSibling;
54 }
55 }
56 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
57 if (anAttr.isNull())
58 return LDOMString ();
59 return LDOMString (anAttr.GetValue(), myDocument -> Self());
60}
61
62//=======================================================================
63//function : getAttributeNode
64//purpose :
65//=======================================================================
66
67LDOM_Attr LDOM_Element::getAttributeNode (const LDOMString& aName) const
68{
69 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
70 if (anElem.isNull()) return LDOM_Attr ();
71 if (myLastChild == NULL) {
72 const LDOM_BasicNode * aNode = anElem.GetFirstChild();
73 if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
302f96fb 74 for(;;) {
7fd59977 75 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
d28abc65
P
76 if (aSibling == NULL)
77 return LDOM_Attr ();
7fd59977 78 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
79 (const LDOM_BasicNode *&) myLastChild = aSibling;
80 break;
81 }
7fd59977 82 aNode = aSibling;
83 }
84 }
85 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
86 return LDOM_Attr (anAttr, myDocument);
87}
88
89//=======================================================================
90//function : getElementsByTagName
91//purpose :
92//=======================================================================
93
94LDOM_NodeList LDOM_Element::getElementsByTagName
95 (const LDOMString& theTagName) const
96{
97 LDOM_NodeList aList (myDocument);
98 if (isNull() == Standard_False) {
99 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
100// if (anElem.GetTagName().equals(theTagName))
101 if (strcmp (anElem.GetTagName(), theTagName.GetString()) == 0)
102 aList.Append (anElem);
103 anElem.AddElementsByTagName (aList, theTagName);
104 }
105 return aList;
106}
107
108//=======================================================================
109//function : setAttribute
110//purpose :
111//=======================================================================
112
113void LDOM_Element::setAttribute (const LDOMString& aName,const LDOMString& aVal)
114{
115 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
116 if (anElem.isNull()) return;
117
118 myLastChild = anElem.AddAttribute (aName, LDOMString (aVal, myDocument),
119 myDocument, myLastChild);
120}
121
122//=======================================================================
123//function : setAttributeNode
124//purpose :
125//=======================================================================
126
127void LDOM_Element::setAttributeNode (const LDOM_Attr& aNewAttr)
128{
129 setAttribute (aNewAttr.getName(), aNewAttr.getValue());
130}
131
132//=======================================================================
133//function : removeAttribute
134//purpose :
135//=======================================================================
136
137void LDOM_Element::removeAttribute (const LDOMString& aName)
138{
139 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
140 if (anElem.isNull()) return;
141 anElem.RemoveAttribute (aName, myLastChild);
142}
143
144//=======================================================================
145//function : GetChildByTagName
146//purpose :
147//=======================================================================
148
149LDOM_Element LDOM_Element::GetChildByTagName (const LDOMString& aTagName) const
150{
151// Verify preconditions
152 LDOM_Element aVoidElement;
153 if (isNull() || aTagName == NULL)
154 return aVoidElement;
155
156// Take the first child. If it doesn't match look for other ones in a loop
157 LDOM_Node aChildNode = getFirstChild();
158 while (aChildNode != NULL)
159 {
160 const LDOM_Node::NodeType aNodeType = aChildNode.getNodeType();
161 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
162 break;
163 if (aNodeType == LDOM_Node::ELEMENT_NODE)
164 {
165 LDOMString
166#ifdef DOM2_MODEL
167 aNodeName = aChildNode.getLocalName(); // try DOM2/namespaces
168 if (aNodeName == NULL)
169#endif
170 aNodeName = aChildNode.getNodeName(); // use DOM1
171 if (aNodeName.equals(aTagName))
172 return (LDOM_Element&) aChildNode; // a match has been found
173 }
174 aChildNode = aChildNode.getNextSibling();
175 }
176 return aVoidElement;
177}
178
179//=======================================================================
180//function : GetSiblingByTagName
181//purpose :
182//=======================================================================
183
184LDOM_Element LDOM_Element::GetSiblingByTagName () const
185{
186// Verify preconditions
187 LDOM_Element aVoidElement;
188 if (isNull()) return aVoidElement;
189
190 LDOMString aTagName = getTagName();
191
192// Take the first child. If it doesn't match look for other ones in a loop
193 LDOM_Node aNextNode = getNextSibling();
194 while (aNextNode != NULL)
195 {
196 const LDOM_Node::NodeType aNodeType = aNextNode.getNodeType();
197 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
198 break;
199 if (aNodeType == LDOM_Node::ELEMENT_NODE)
200 {
201 LDOM_Element aNextElement = (LDOM_Element&) aNextNode;
202 if (aNextElement.getTagName().equals(aTagName))
203 return aNextElement; // a match has been found
204 }
205 aNextNode = aNextNode.getNextSibling();
206 }
207 return aVoidElement;
208}
209
210//=======================================================================
211//function : ReplaceElement
212//purpose : Permanently replace the element erasing the old data though the
213// children are not erased.
214// If anOther belongs to different Document, full copy of all its
215// children is performed.
216//=======================================================================
217
218void LDOM_Element::ReplaceElement (const LDOM_Element& anOther)
219{
220 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
221 const LDOM_BasicElement& anOtherElem =
222 (const LDOM_BasicElement&) anOther.Origin();
223 if (myDocument == anOther.myDocument) {
224 anElem.myTagName = anOtherElem.myTagName;
225 anElem.myAttributeMask = anOtherElem.myAttributeMask;
226 anElem.myFirstChild = anOtherElem.myFirstChild;
227 (const LDOM_BasicNode *&) myLastChild = anOther.myLastChild;
228 } else {
229 anElem.ReplaceElement (anOtherElem, myDocument);
230 (const LDOM_BasicNode *&) myLastChild = NULL;
231 }
232}
233
234//=======================================================================
235//function : GetAttributesList
236//purpose :
237//=======================================================================
238
239LDOM_NodeList LDOM_Element::GetAttributesList () const
240{
241 LDOM_NodeList aList (myDocument);
242 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
243 anElem.AddAttributes (aList, myLastChild);
244 return aList;
245}