0024023: Revamp the OCCT Handle - gcc and clang
[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>
e8862cf4 19#include <LDOM_Element.hxx>
7fd59977 20
21#include <Standard_ProgramError.hxx>
22
23#include <stdio.h>
24
25//=======================================================================
26//function : LDOM_Element
27//purpose :
28//=======================================================================
29
30LDOM_Element::LDOM_Element (const LDOM_BasicElement& anElem,
31 const Handle(LDOM_MemManager)& aDoc)
32 : LDOM_Node (anElem, aDoc) {}
33
34//=======================================================================
35//function : getAttribute
36//purpose :
37//=======================================================================
38
39LDOMString LDOM_Element::getAttribute (const LDOMString& aName) const
40{
41 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
42 if (anElem.isNull()) return LDOMString ();
43 if (myLastChild == NULL) {
44 const LDOM_BasicNode * aNode = anElem.GetFirstChild();
45 if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
302f96fb 46 for(;;) {
7fd59977 47 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
48 if (aSibling == NULL)
49 return LDOMString ();
50 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
51 (const LDOM_BasicNode *&) myLastChild = aNode;
52 break;
53 }
54 aNode = aSibling;
55 }
56 }
57 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
58 if (anAttr.isNull())
59 return LDOMString ();
60 return LDOMString (anAttr.GetValue(), myDocument -> Self());
61}
62
63//=======================================================================
64//function : getAttributeNode
65//purpose :
66//=======================================================================
67
68LDOM_Attr LDOM_Element::getAttributeNode (const LDOMString& aName) const
69{
70 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
71 if (anElem.isNull()) return LDOM_Attr ();
72 if (myLastChild == NULL) {
73 const LDOM_BasicNode * aNode = anElem.GetFirstChild();
74 if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
302f96fb 75 for(;;) {
7fd59977 76 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
d28abc65
P
77 if (aSibling == NULL)
78 return LDOM_Attr ();
7fd59977 79 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
80 (const LDOM_BasicNode *&) myLastChild = aSibling;
81 break;
82 }
7fd59977 83 aNode = aSibling;
84 }
85 }
86 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
87 return LDOM_Attr (anAttr, myDocument);
88}
89
90//=======================================================================
91//function : getElementsByTagName
92//purpose :
93//=======================================================================
94
95LDOM_NodeList LDOM_Element::getElementsByTagName
96 (const LDOMString& theTagName) const
97{
98 LDOM_NodeList aList (myDocument);
99 if (isNull() == Standard_False) {
100 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
101// if (anElem.GetTagName().equals(theTagName))
102 if (strcmp (anElem.GetTagName(), theTagName.GetString()) == 0)
103 aList.Append (anElem);
104 anElem.AddElementsByTagName (aList, theTagName);
105 }
106 return aList;
107}
108
109//=======================================================================
110//function : setAttribute
111//purpose :
112//=======================================================================
113
114void LDOM_Element::setAttribute (const LDOMString& aName,const LDOMString& aVal)
115{
116 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
117 if (anElem.isNull()) return;
118
119 myLastChild = anElem.AddAttribute (aName, LDOMString (aVal, myDocument),
120 myDocument, myLastChild);
121}
122
123//=======================================================================
124//function : setAttributeNode
125//purpose :
126//=======================================================================
127
128void LDOM_Element::setAttributeNode (const LDOM_Attr& aNewAttr)
129{
130 setAttribute (aNewAttr.getName(), aNewAttr.getValue());
131}
132
133//=======================================================================
134//function : removeAttribute
135//purpose :
136//=======================================================================
137
138void LDOM_Element::removeAttribute (const LDOMString& aName)
139{
140 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
141 if (anElem.isNull()) return;
142 anElem.RemoveAttribute (aName, myLastChild);
143}
144
145//=======================================================================
146//function : GetChildByTagName
147//purpose :
148//=======================================================================
149
150LDOM_Element LDOM_Element::GetChildByTagName (const LDOMString& aTagName) const
151{
152// Verify preconditions
153 LDOM_Element aVoidElement;
154 if (isNull() || aTagName == NULL)
155 return aVoidElement;
156
157// Take the first child. If it doesn't match look for other ones in a loop
158 LDOM_Node aChildNode = getFirstChild();
159 while (aChildNode != NULL)
160 {
161 const LDOM_Node::NodeType aNodeType = aChildNode.getNodeType();
162 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
163 break;
164 if (aNodeType == LDOM_Node::ELEMENT_NODE)
165 {
166 LDOMString
167#ifdef DOM2_MODEL
168 aNodeName = aChildNode.getLocalName(); // try DOM2/namespaces
169 if (aNodeName == NULL)
170#endif
171 aNodeName = aChildNode.getNodeName(); // use DOM1
172 if (aNodeName.equals(aTagName))
173 return (LDOM_Element&) aChildNode; // a match has been found
174 }
175 aChildNode = aChildNode.getNextSibling();
176 }
177 return aVoidElement;
178}
179
180//=======================================================================
181//function : GetSiblingByTagName
182//purpose :
183//=======================================================================
184
185LDOM_Element LDOM_Element::GetSiblingByTagName () const
186{
187// Verify preconditions
188 LDOM_Element aVoidElement;
189 if (isNull()) return aVoidElement;
190
191 LDOMString aTagName = getTagName();
192
193// Take the first child. If it doesn't match look for other ones in a loop
194 LDOM_Node aNextNode = getNextSibling();
195 while (aNextNode != NULL)
196 {
197 const LDOM_Node::NodeType aNodeType = aNextNode.getNodeType();
198 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
199 break;
200 if (aNodeType == LDOM_Node::ELEMENT_NODE)
201 {
202 LDOM_Element aNextElement = (LDOM_Element&) aNextNode;
203 if (aNextElement.getTagName().equals(aTagName))
204 return aNextElement; // a match has been found
205 }
206 aNextNode = aNextNode.getNextSibling();
207 }
208 return aVoidElement;
209}
210
211//=======================================================================
212//function : ReplaceElement
213//purpose : Permanently replace the element erasing the old data though the
214// children are not erased.
215// If anOther belongs to different Document, full copy of all its
216// children is performed.
217//=======================================================================
218
219void LDOM_Element::ReplaceElement (const LDOM_Element& anOther)
220{
221 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
222 const LDOM_BasicElement& anOtherElem =
223 (const LDOM_BasicElement&) anOther.Origin();
224 if (myDocument == anOther.myDocument) {
225 anElem.myTagName = anOtherElem.myTagName;
226 anElem.myAttributeMask = anOtherElem.myAttributeMask;
227 anElem.myFirstChild = anOtherElem.myFirstChild;
228 (const LDOM_BasicNode *&) myLastChild = anOther.myLastChild;
229 } else {
230 anElem.ReplaceElement (anOtherElem, myDocument);
231 (const LDOM_BasicNode *&) myLastChild = NULL;
232 }
233}
234
235//=======================================================================
236//function : GetAttributesList
237//purpose :
238//=======================================================================
239
240LDOM_NodeList LDOM_Element::GetAttributesList () const
241{
242 LDOM_NodeList aList (myDocument);
243 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
244 anElem.AddAttributes (aList, myLastChild);
245 return aList;
246}