summaryrefslogtreecommitdiff
path: root/src/LDOM/LDOM_Element.cxx
blob: 3eca84ea90ef60f3616efe048fbed091613f26fc (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
208
209
210
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
// File:      LDOM_Element.cxx
// Created:   27.06.01 14:22:45
// Author:    Alexander GRIGORIEV
// Copyright: OpenCascade 2001
// History:


#include <LDOM_MemManager.hxx>
#include <LDOM_BasicElement.hxx>
#include <LDOM_BasicAttribute.hxx>

#include <Standard_ProgramError.hxx>

#include <stdio.h>

//=======================================================================
//function : LDOM_Element
//purpose  : 
//=======================================================================

LDOM_Element::LDOM_Element (const LDOM_BasicElement&       anElem,
                            const Handle(LDOM_MemManager)& aDoc)
     : LDOM_Node (anElem, aDoc) {}

//=======================================================================
//function : getAttribute
//purpose  : 
//=======================================================================

LDOMString LDOM_Element::getAttribute (const LDOMString& aName) const
{
  const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
  if (anElem.isNull()) return LDOMString ();
  if (myLastChild == NULL) {
    const LDOM_BasicNode * aNode = anElem.GetFirstChild();
    if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
      while (1) {
        const LDOM_BasicNode * aSibling = aNode -> GetSibling();
        if (aSibling == NULL)
          return LDOMString ();
        if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
          (const LDOM_BasicNode *&) myLastChild = aNode;
          break;
        }
        aNode = aSibling;
      }
  }
  const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
  if (anAttr.isNull())
    return LDOMString ();
  return LDOMString (anAttr.GetValue(), myDocument -> Self());
}

//=======================================================================
//function : getAttributeNode
//purpose  : 
//=======================================================================

LDOM_Attr LDOM_Element::getAttributeNode (const LDOMString& aName) const
{
  const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
  if (anElem.isNull()) return LDOM_Attr ();
  if (myLastChild == NULL) {
    const LDOM_BasicNode * aNode = anElem.GetFirstChild();
    if (aNode && aNode -> getNodeType () != LDOM_Node::ATTRIBUTE_NODE)
      while (1) {
        const LDOM_BasicNode * aSibling = aNode -> GetSibling();
        if (aSibling -> getNodeType () == LDOM_Node::ATTRIBUTE_NODE) {
          (const LDOM_BasicNode *&) myLastChild = aSibling;
          break;
        }
        if (aSibling == NULL)
          return LDOM_Attr ();
        aNode = aSibling;
      }
  }
  const LDOM_BasicAttribute& anAttr = anElem.GetAttribute (aName, myLastChild);
  return LDOM_Attr (anAttr, myDocument);
}

//=======================================================================
//function : getElementsByTagName
//purpose  : 
//=======================================================================

LDOM_NodeList LDOM_Element::getElementsByTagName
                                        (const LDOMString& theTagName) const
{
  LDOM_NodeList aList (myDocument);
  if (isNull() == Standard_False) {
    const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
//    if (anElem.GetTagName().equals(theTagName))
    if (strcmp (anElem.GetTagName(), theTagName.GetString()) == 0)
      aList.Append (anElem);
    anElem.AddElementsByTagName (aList, theTagName);
  }
  return aList;
}

//=======================================================================
//function : setAttribute
//purpose  : 
//=======================================================================

void LDOM_Element::setAttribute (const LDOMString& aName,const LDOMString& aVal)
{
  LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
  if (anElem.isNull()) return;

  myLastChild = anElem.AddAttribute (aName, LDOMString (aVal, myDocument),
                                     myDocument, myLastChild);
}

//=======================================================================
//function : setAttributeNode
//purpose  : 
//=======================================================================

void LDOM_Element::setAttributeNode (const LDOM_Attr& aNewAttr)
{
  setAttribute (aNewAttr.getName(), aNewAttr.getValue());
}

//=======================================================================
//function : removeAttribute
//purpose  : 
//=======================================================================

void LDOM_Element::removeAttribute (const LDOMString& aName)
{
  const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
  if (anElem.isNull()) return;
  anElem.RemoveAttribute (aName, myLastChild);
}

//=======================================================================
//function : GetChildByTagName
//purpose  : 
//=======================================================================

LDOM_Element LDOM_Element::GetChildByTagName (const LDOMString& aTagName) const
{
// Verify preconditions
  LDOM_Element aVoidElement;
  if (isNull() || aTagName == NULL)
    return aVoidElement;
    
// Take the first child. If it doesn't match look for other ones in a loop
  LDOM_Node aChildNode = getFirstChild();
  while (aChildNode != NULL)
  {
    const LDOM_Node::NodeType aNodeType = aChildNode.getNodeType();
    if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
      break;
    if (aNodeType == LDOM_Node::ELEMENT_NODE)
    {
      LDOMString
#ifdef DOM2_MODEL
        aNodeName = aChildNode.getLocalName(); // try DOM2/namespaces
      if (aNodeName == NULL)
#endif
        aNodeName = aChildNode.getNodeName();          // use DOM1
      if (aNodeName.equals(aTagName))
        return (LDOM_Element&) aChildNode;           // a match has been found
    }
    aChildNode = aChildNode.getNextSibling();
  }
  return aVoidElement;
}

//=======================================================================
//function : GetSiblingByTagName
//purpose  : 
//=======================================================================

LDOM_Element LDOM_Element::GetSiblingByTagName () const
{
// Verify preconditions
  LDOM_Element aVoidElement;
  if (isNull()) return aVoidElement;

  LDOMString aTagName = getTagName();
    
// Take the first child. If it doesn't match look for other ones in a loop
  LDOM_Node aNextNode = getNextSibling();
  while (aNextNode != NULL)
  {
    const LDOM_Node::NodeType aNodeType = aNextNode.getNodeType();
    if (aNodeType == LDOM_Node::ATTRIBUTE_NODE)
      break;
    if (aNodeType == LDOM_Node::ELEMENT_NODE)
    {
      LDOM_Element aNextElement = (LDOM_Element&) aNextNode;
      if (aNextElement.getTagName().equals(aTagName))
        return aNextElement;           // a match has been found
    }
    aNextNode = aNextNode.getNextSibling();
  }
  return aVoidElement;
}

//=======================================================================
//function : ReplaceElement
//purpose  : Permanently replace the element erasing the old data though the
//           children are not erased.
//           If anOther belongs to different Document, full copy of all its
//           children is performed.
//=======================================================================

void LDOM_Element::ReplaceElement (const LDOM_Element& anOther)
{
  LDOM_BasicElement& anElem = (LDOM_BasicElement&) Origin();
  const LDOM_BasicElement& anOtherElem =
      (const LDOM_BasicElement&) anOther.Origin();
  if (myDocument == anOther.myDocument) {
    anElem.myTagName       = anOtherElem.myTagName;
    anElem.myAttributeMask = anOtherElem.myAttributeMask;
    anElem.myFirstChild    = anOtherElem.myFirstChild;
    (const LDOM_BasicNode *&) myLastChild = anOther.myLastChild;
  } else {
    anElem.ReplaceElement (anOtherElem, myDocument);
    (const LDOM_BasicNode *&) myLastChild = NULL;
  }
}

//=======================================================================
//function : GetAttributesList
//purpose  : 
//=======================================================================

LDOM_NodeList LDOM_Element::GetAttributesList () const
{
  LDOM_NodeList aList (myDocument);
  const LDOM_BasicElement& anElem = (const LDOM_BasicElement&) Origin();
  anElem.AddAttributes (aList, myLastChild);
  return aList;
}