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 under
8 // the terms of the GNU Lesser General Public License 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>
19 #include <LDOM_Element.hxx>
21 #include <Standard_ProgramError.hxx>
25 //=======================================================================
26 //function : LDOM_Element
28 //=======================================================================
30 LDOM_Element::LDOM_Element (const LDOM_BasicElement& anElem,
31 const Handle(LDOM_MemManager)& aDoc)
32 : LDOM_Node (anElem, aDoc) {}
34 //=======================================================================
35 //function : getAttribute
37 //=======================================================================
39 LDOMString LDOM_Element::getAttribute (const LDOMString& aName) const
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)
47 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
50 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
51 (const LDOM_BasicNode *&) myLastChild = aNode;
57 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
60 return LDOMString (anAttr.GetValue(), myDocument -> Self());
63 //=======================================================================
64 //function : getAttributeNode
66 //=======================================================================
68 LDOM_Attr LDOM_Element::getAttributeNode (const LDOMString& aName) const
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)
76 const LDOM_BasicNode * aSibling = aNode -> GetSibling();
79 if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
80 (const LDOM_BasicNode *&) myLastChild = aSibling;
86 const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
87 return LDOM_Attr (anAttr, myDocument);
90 //=======================================================================
91 //function : getElementsByTagName
93 //=======================================================================
95 LDOM_NodeList LDOM_Element::getElementsByTagName
96 (const LDOMString& theTagName) const
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);
109 //=======================================================================
110 //function : setAttribute
112 //=======================================================================
114 void LDOM_Element::setAttribute (const LDOMString& aName,const LDOMString& aVal)
116 LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
117 if (anElem.isNull()) return;
119 myLastChild = anElem.AddAttribute (aName, LDOMString (aVal, myDocument),
120 myDocument, myLastChild);
123 //=======================================================================
124 //function : setAttributeNode
126 //=======================================================================
128 void LDOM_Element::setAttributeNode (const LDOM_Attr& aNewAttr)
130 setAttribute (aNewAttr.getName(), aNewAttr.getValue());
133 //=======================================================================
134 //function : removeAttribute
136 //=======================================================================
138 void LDOM_Element::removeAttribute (const LDOMString& aName)
140 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
141 if (anElem.isNull()) return;
142 anElem.RemoveAttribute (aName, myLastChild);
145 //=======================================================================
146 //function : GetChildByTagName
148 //=======================================================================
150 LDOM_Element LDOM_Element::GetChildByTagName (const LDOMString& aTagName) const
152 // Verify preconditions
153 LDOM_Element aVoidElement;
154 if (isNull() || aTagName == NULL)
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)
161 const LDOM_Node::NodeType aNodeType = aChildNode.getNodeType();
162 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
164 if (aNodeType == LDOM_Node::ELEMENT_NODE)
168 aNodeName = aChildNode.getLocalName(); // try DOM2/namespaces
169 if (aNodeName == NULL)
171 aNodeName = aChildNode.getNodeName(); // use DOM1
172 if (aNodeName.equals(aTagName))
173 return (LDOM_Element&) aChildNode; // a match has been found
175 aChildNode = aChildNode.getNextSibling();
180 //=======================================================================
181 //function : GetSiblingByTagName
183 //=======================================================================
185 LDOM_Element LDOM_Element::GetSiblingByTagName () const
187 // Verify preconditions
188 LDOM_Element aVoidElement;
189 if (isNull()) return aVoidElement;
191 LDOMString aTagName = getTagName();
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)
197 const LDOM_Node::NodeType aNodeType = aNextNode.getNodeType();
198 if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
200 if (aNodeType == LDOM_Node::ELEMENT_NODE)
202 LDOM_Element aNextElement = (LDOM_Element&) aNextNode;
203 if (aNextElement.getTagName().equals(aTagName))
204 return aNextElement; // a match has been found
206 aNextNode = aNextNode.getNextSibling();
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 //=======================================================================
219 void LDOM_Element::ReplaceElement (const LDOM_Element& anOther)
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;
230 anElem.ReplaceElement (anOtherElem, myDocument);
231 (const LDOM_BasicNode *&) myLastChild = NULL;
235 //=======================================================================
236 //function : GetAttributesList
238 //=======================================================================
240 LDOM_NodeList LDOM_Element::GetAttributesList () const
242 LDOM_NodeList aList (myDocument);
243 const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
244 anElem.AddAttributes (aList, myLastChild);