1 // Created on: 2001-06-27
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <LDOM_MemManager.hxx>
17 #include <LDOM_BasicElement.hxx>
18 #include <LDOM_BasicAttribute.hxx>
20 #include <Standard_ProgramError.hxx>
24 //=======================================================================
25 //function : LDOM_Element
27 //=======================================================================
29 LDOM_Element::LDOM_Element (const LDOM_BasicElement& anElem,
30 const Handle(LDOM_MemManager)& aDoc)
31 : LDOM_Node (anElem, aDoc) {}
33 //=======================================================================
34 //function : getAttribute
36 //=======================================================================
38 LDOMString LDOM_Element::getAttribute (const LDOMString& aName) const
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)
46 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
49 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
50 (const LDOM_BasicNode *&) myLastChild = aNode;
56 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
59 return LDOMString (anAttr.GetValue(), myDocument -> Self());
62 //=======================================================================
63 //function : getAttributeNode
65 //=======================================================================
67 LDOM_Attr LDOM_Element::getAttributeNode (const LDOMString& aName) const
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)
75 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
78 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
79 (const LDOM_BasicNode *&) myLastChild = aSibling;
85 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
86 return LDOM_Attr (anAttr, myDocument);
89 //=======================================================================
90 //function : getElementsByTagName
92 //=======================================================================
94 LDOM_NodeList LDOM_Element::getElementsByTagName
95 (const LDOMString& theTagName) const
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);
108 //=======================================================================
109 //function : setAttribute
111 //=======================================================================
113 void LDOM_Element::setAttribute (const LDOMString& aName,const LDOMString& aVal)
115 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
116 if (anElem.isNull()) return;
118 myLastChild = anElem.AddAttribute (aName, LDOMString (aVal, myDocument),
119 myDocument, myLastChild);
122 //=======================================================================
123 //function : setAttributeNode
125 //=======================================================================
127 void LDOM_Element::setAttributeNode (const LDOM_Attr& aNewAttr)
129 setAttribute (aNewAttr.getName(), aNewAttr.getValue());
132 //=======================================================================
133 //function : removeAttribute
135 //=======================================================================
137 void LDOM_Element::removeAttribute (const LDOMString& aName)
139 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
140 if (anElem.isNull()) return;
141 anElem.RemoveAttribute (aName, myLastChild);
144 //=======================================================================
145 //function : GetChildByTagName
147 //=======================================================================
149 LDOM_Element LDOM_Element::GetChildByTagName (const LDOMString& aTagName) const
151 // Verify preconditions
152 LDOM_Element aVoidElement;
153 if (isNull() || aTagName == NULL)
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)
160 const LDOM_Node::NodeType aNodeType = aChildNode.getNodeType();
161 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
163 if (aNodeType == LDOM_Node::ELEMENT_NODE)
167 aNodeName = aChildNode.getLocalName(); // try DOM2/namespaces
168 if (aNodeName == NULL)
170 aNodeName = aChildNode.getNodeName(); // use DOM1
171 if (aNodeName.equals(aTagName))
172 return (LDOM_Element&) aChildNode; // a match has been found
174 aChildNode = aChildNode.getNextSibling();
179 //=======================================================================
180 //function : GetSiblingByTagName
182 //=======================================================================
184 LDOM_Element LDOM_Element::GetSiblingByTagName () const
186 // Verify preconditions
187 LDOM_Element aVoidElement;
188 if (isNull()) return aVoidElement;
190 LDOMString aTagName = getTagName();
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)
196 const LDOM_Node::NodeType aNodeType = aNextNode.getNodeType();
197 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
199 if (aNodeType == LDOM_Node::ELEMENT_NODE)
201 LDOM_Element aNextElement = (LDOM_Element&) aNextNode;
202 if (aNextElement.getTagName().equals(aTagName))
203 return aNextElement; // a match has been found
205 aNextNode = aNextNode.getNextSibling();
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 //=======================================================================
218 void LDOM_Element::ReplaceElement (const LDOM_Element& anOther)
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;
229 anElem.ReplaceElement (anOtherElem, myDocument);
230 (const LDOM_BasicNode *&) myLastChild = NULL;
234 //=======================================================================
235 //function : GetAttributesList
237 //=======================================================================
239 LDOM_NodeList LDOM_Element::GetAttributesList () const
241 LDOM_NodeList aList (myDocument);
242 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
243 anElem.AddAttributes (aList, myLastChild);