File: XdmfElement.cxx

package info (click to toggle)
vtk6 6.1.0+dfsg2-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 165,164 kB
  • ctags: 226,428
  • sloc: cpp: 1,354,490; ansic: 730,748; python: 227,134; tcl: 48,285; xml: 8,290; yacc: 4,832; java: 3,827; perl: 3,108; lex: 1,809; sh: 1,437; asm: 471; makefile: 229
file content (504 lines) | stat: -rw-r--r-- 17,331 bytes parent folder | download | duplicates (6)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*******************************************************************/
/*                               XDMF                              */
/*                   eXtensible Data Model and Format              */
/*                                                                 */
/*  Id : Id  */
/*  Date : $Date$ */
/*  Version : $Revision$ */
/*                                                                 */
/*  Author:                                                        */
/*     Jerry A. Clarke                                             */
/*     clarke@arl.army.mil                                         */
/*     US Army Research Laboratory                                 */
/*     Aberdeen Proving Ground, MD                                 */
/*                                                                 */
/*     Copyright @ 2002 US Army Research Laboratory                */
/*     All Rights Reserved                                         */
/*     See Copyright.txt or http://www.arl.hpc.mil/ice for details */
/*                                                                 */
/*     This software is distributed WITHOUT ANY WARRANTY; without  */
/*     even the implied warranty of MERCHANTABILITY or FITNESS     */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice   */
/*     for more information.                                       */
/*                                                                 */
/*******************************************************************/
#include "XdmfElement.h"
#include "XdmfDOM.h"
#include <libxml/tree.h>
#include <string.h>

#define XDMF_EMPTY_REFERENCE   0x00
#define XDMF_ERROR_REFERENCE    -1

#define XDMF_XML_PRIVATE_DATA(e) e->_private

XdmfElementData::XdmfElementData(){
    this->ReferenceElement = NULL;
    this->CurrentXdmfElement = NULL;
}

XdmfElementData::~XdmfElementData() {
}

XdmfElement::XdmfElement() {
    this->DOM = NULL;
    this->Element = NULL;
    this->ElementName = NULL;
    this->IsReference = 0;
    this->ReferenceElement = NULL;
    this->State = XDMF_ELEMENT_STATE_UNINITIALIZED;
    this->CopyReferenceData = 1;
    this->RootWhenParsed = 0;
    this->DsmBuffer = NULL;
    this->DataXml = 0;
    this->InsertedDataXml = 0;
    this->DeleteOnGridDelete = 0;
}

XdmfElement::~XdmfElement() {
    if(this->DOM && (this->DOM->GetTree() == this->RootWhenParsed)){
        if(this->ReferenceElement){
         if(this->GetReferenceObject(this->ReferenceElement) == this){
             this->SetReferenceObject(this->ReferenceElement, XDMF_ELEMENT_STATE_UNINITIALIZED);
         }
     }
     this->ReferenceElement = NULL;
     if(this->Element){
         if(this->GetReferenceObject(this->Element) == this){
             this->SetReferenceObject(this->Element, XDMF_ELEMENT_STATE_UNINITIALIZED);
         }
     }
    }
    this->Element = NULL;
    if(this->ElementName) delete [] this->ElementName;
    if(this->DataXml) delete [] this->DataXml;
}

void XdmfElement::SetReferenceObject(XdmfXmlNode anElement, void *p){
    XdmfElementData *PrivateData;
    if(!anElement){
        XdmfErrorMessage("Element is NULL");
        return;
    }
    if(XDMF_XML_PRIVATE_DATA(anElement)){
        PrivateData = (XdmfElementData *)XDMF_XML_PRIVATE_DATA(anElement);
    }else{
        PrivateData = new XdmfElementData;
        XDMF_XML_PRIVATE_DATA(anElement) = (void *)PrivateData;
    }
    // XdmfDebug("Old Ref = " << ElementPrivateData->GetReferenceElement());
    // XdmfDebug("New Ref = " << p);
    PrivateData->SetReferenceElement((XdmfElement *)p);
}

void *
XdmfElement::GetReferenceObject(XdmfXmlNode anElement){
    XdmfElementData *ElementPrivateData;
    if(!anElement){
        XdmfErrorMessage("NULL Reference Element");
        return(NULL);
    }
    if(XDMF_XML_PRIVATE_DATA(anElement) == XDMF_EMPTY_REFERENCE){
        XdmfDebug("XML Node contains no initialized object");
        return(NULL);
    }
    ElementPrivateData = (XdmfElementData *)XDMF_XML_PRIVATE_DATA(anElement);
    if(ElementPrivateData->GetReferenceElement() == XDMF_EMPTY_REFERENCE){
        XdmfDebug("XML Node contains no initialized object");
        return(NULL);
    }
    return(ElementPrivateData->GetReferenceElement());
}

void XdmfElement::SetCurrentXdmfElement(XdmfXmlNode anElement, void *p){
    XdmfElementData *PrivateData;
    if(!anElement){
        XdmfErrorMessage("Element is NULL");
        return;
    }
    if(XDMF_XML_PRIVATE_DATA(anElement)){
        PrivateData = (XdmfElementData *)XDMF_XML_PRIVATE_DATA(anElement);
    }else{
        PrivateData = new XdmfElementData;
        XDMF_XML_PRIVATE_DATA(anElement) = (void *)PrivateData;
    }
    PrivateData->SetCurrentXdmfElement((XdmfElement *)p);
}

void *
XdmfElement::GetCurrentXdmfElement(XdmfXmlNode anElement){
    XdmfElementData *ElementPrivateData;
    if(!anElement){
        XdmfErrorMessage("NULL Reference Element");
        return(NULL);
    }
    ElementPrivateData = (XdmfElementData *)XDMF_XML_PRIVATE_DATA(anElement);
    if(!ElementPrivateData){
        return(NULL);
    }
    if(ElementPrivateData->GetCurrentXdmfElement() == XDMF_EMPTY_REFERENCE){
        XdmfDebug("XML Node contains no initialized object");
        return(NULL);
    }
    return(ElementPrivateData->GetCurrentXdmfElement());
}

XdmfInt32 XdmfElement::SetElement(XdmfXmlNode anElement, XdmfInt32 AssociateElement){
    if(!anElement) {
        XdmfErrorMessage("Element is NULL");
        return(XDMF_FAIL);
    }
    // Clear the ReferenceObject underlying node. This will also create ElementPrivateData if necessary
    XdmfDebug("Clearing ReferenceObject of XML node");
    this->SetReferenceObject(anElement, XDMF_EMPTY_REFERENCE);
    if(AssociateElement) this->SetCurrentXdmfElement(anElement, this);
    this->Element = anElement;
    if(this->DOM){
        this->RootWhenParsed = this->DOM->GetTree();
    }
    return(XDMF_SUCCESS);
}

XdmfInt32 XdmfElement::InsertChildElement(XdmfXmlNode Child){
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(XDMF_FAIL);
    }
    if(!this->Element) {
        XdmfErrorMessage("Current Element is empty");
        return(XDMF_FAIL);
    }
    if(this->DOM->Insert(this->Element, Child)){
        return(XDMF_SUCCESS);
    }
    return(XDMF_FAIL);
}


XdmfInt32
XdmfElement::Insert(XdmfElement *Child){
    XdmfXmlNode element;

    if(!this->DOM) {
        XdmfErrorMessage("No DOM has not been set : Parent must have a DOM and Element before insertion makes sense");
        return(XDMF_FAIL);
    }
    if(!this->GetElement()) {
        XdmfErrorMessage("No Element has not been set : Parent must have a DOM and Element before insertion makes sense");
        return(XDMF_FAIL);
    }
    if(!Child){
        XdmfErrorMessage("Child Element is NULL");
        return(XDMF_FAIL);
    }
    if(!Child->GetElementName()) {
        XdmfErrorMessage("Child Element has no ElementName");
        return(XDMF_FAIL);
    }
    Child->SetDOM(this->DOM);
    element = this->GetDOM()->InsertNew(this->Element, Child->GetElementName());
    if(!element){
        XdmfErrorMessage("Failed to Insert New Child XML Node");
        return(XDMF_FAIL);
    }
    if(Child->SetElement(element) != XDMF_SUCCESS){
        XdmfErrorMessage("Failed to set child XML node");
        return(XDMF_FAIL);
    }
    return(XDMF_SUCCESS);
}

XdmfInt32
XdmfElement::Copy(XdmfElement *){
    return(XDMF_SUCCESS);
}

XdmfInt32 XdmfElement::UpdateInformation(){
    XdmfConstString Value;

    XdmfElement *e;
    XdmfXmlNode ref;

    XdmfDebug("XdmfElement::UpdateInformation()");
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(XDMF_FAIL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(XDMF_FAIL);
    }
    Value = this->Get("Name");
    if(Value) this->SetName(Value);
    ref = this->CheckForReference(this->Element);
    if(ref == (XdmfXmlNode)XDMF_ERROR_REFERENCE){
        XdmfErrorMessage("Error Checking Reference");
        free((void*)Value);
        return(XDMF_FAIL);
    }
    if(ref){
        XdmfXmlNode node;

        // "this" is now the ReferenceObject for this->ReferenceElement
        XdmfDebug("Setting ReferenceObject and Following Chain");
        this->SetReferenceObject(this->ReferenceElement, this);
        while(ref){
            e = (XdmfElement *)this->GetReferenceObject(ref);
            if(e && (e != this)){
                XdmfDebug("Updating Information from another Object");
                // XdmfDebug(e->Serialize());
                // There if an Object associated with this node. UpdateInformation()?
                if(e->GetState() < XDMF_ELEMENT_STATE_LIGHT_PARSED){
                    // Could cause a chain of UpdateInformation() 
                    XdmfDebug("Call UpdateInformation on ReferenceObject");
                    e->UpdateInformation();
                }
                // Copy out appropriate information and return
                XdmfDebug("Copying Information from Reference Object");
                this->Element = e->Element;
                free((void*)Value);
                return(this->Copy(e));
            }
            // No ReferenceObject Set. Is this a Reference as well?
            node = this->FollowReference(ref);
            if(node){
                ref = node;
            }else{
                // Not a Reference. Is it the right Type ?
                if(STRCMP((const char *)ref->name, (const char *)this->ReferenceElement->name) != 0){
                    XdmfErrorMessage("Reference node " << Value << " is a " << ref->name << " not " << ReferenceElement->name);
                    delete [] Value;
                    return(XDMF_FAIL);
                }
                // If this is a derived Class, UpdateInformation will act on this target.
                this->SetElement(ref);
                // This is the end of the chain and there is no ReferenceObject for the XML node.
                XdmfDebug("Setting Reference Object");
                this->SetReferenceObject(ref, this);
                ref = NULL;
            }
        }
    }else{
        XdmfDebug("Setting Reference Object");
        this->SetReferenceObject(this->Element, this);
    }
    free((void*)Value);
    this->State = XDMF_ELEMENT_STATE_LIGHT_PARSED;
    return(XDMF_SUCCESS);
}

XdmfInt32 XdmfElement::Update(){
    XdmfXmlNode node, ref;
    XdmfElement *e;

    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(XDMF_FAIL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(XDMF_FAIL);
    }
    if(this->GetIsReference()){
        // Don't assume that state has not changed since last UpdateInformation()
        ref = this->FollowReference(this->ReferenceElement);
        while(ref){
            e = (XdmfElement *)this->GetReferenceObject(ref);
            if(e && (e != this)){
                // Does it need Updating ?
                if(e->GetState() < XDMF_ELEMENT_STATE_LIGHT_PARSED) e->UpdateInformation();
                if(e->GetState() < XDMF_ELEMENT_STATE_HEAVY_READ) e->Update();
                this->Element = e->Element;
                return(this->Copy(e));
            }
            // No External Reference Object
            node = this->FollowReference(ref);
            if(node){
                ref = node;
            }else{
                // No Reference Object and this is the end of the chain
                // If this is a derived Class, Update will act on this target.
                this->SetElement(ref);
                // This is the end of the chain and there is no ReferenceObject for the XML node.
                XdmfDebug("Setting Reference Object");
                this->SetReferenceObject(ref, this);
                ref = NULL;
            }
        }
    }
    this->State = XDMF_ELEMENT_STATE_HEAVY_READ;
    return(XDMF_SUCCESS);
}

XdmfXmlNode
XdmfElement::FollowReference(XdmfXmlNode anElement){
    XdmfConstString Value;
    XdmfXmlNode     ref = NULL;

    if(!anElement){
        XdmfErrorMessage("Element is NULL");
        return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
    }
    Value = this->DOM->Get(anElement, "Reference");
    if(Value){
        if(STRCASECMP(Value, "XML") == 0){
            Value = this->DOM->GetCData(anElement);
            if(!Value){
                XdmfErrorMessage("Reference to CDATA is NULL");
                return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
            }
        }
        XdmfDebug("Following Reference to " << Value);
        ref = this->DOM->FindElementByPath(Value);
        if(!ref){
            XdmfErrorMessage("Can't Find Node of Path " << Value);
            return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
        }
    }
    return(ref);
}

XdmfXmlNode
XdmfElement::CheckForReference(XdmfXmlNode anElement){

    XdmfXmlNode node;

    XdmfDebug("XdmfElement::CheckForReference(XdmfXmlNode Element)");
    if(!anElement) return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
    // Does the Referenced Node Exist and is it of the Same Type
    node = this->FollowReference(anElement);
    if(node == (XdmfXmlNode)XDMF_ERROR_REFERENCE){
        XdmfErrorMessage("Error Following Reference");
        return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
    }
    if(node){
        XdmfDebug("Element is a Reference");
        // Check Type (low level XML "name") against this->Element
        if(STRCMP((const char *)node->name, (const char *)anElement->name) != 0){
            XdmfErrorMessage("Reference node is a " << node->name << " not " << anElement->name);
            return((XdmfXmlNode)XDMF_ERROR_REFERENCE);
        }
    }else{
        // This is not a Reference Node
        return((XdmfXmlNode)XDMF_EMPTY_REFERENCE);
    }
    XdmfDebug("Setting ReferenceElement");
    this->ReferenceElement = anElement;
    this->SetIsReference(1);
    return(node);
}

XdmfConstString XdmfElement::Serialize(){
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(NULL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(NULL);
    }
    if(this->GetIsReference()){
        return(this->DOM->Serialize(this->ReferenceElement));
    }
    return(this->DOM->Serialize(this->Element));
}
XdmfConstString XdmfElement::GetElementType(){
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(NULL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(NULL);
    }
    return((XdmfConstString)this->Element->name);
}


XdmfInt32
XdmfElement::BuildFromDataXml(XdmfInt32 AllowCData){
    if(this->DataXml){
        if(AllowCData){
            char        first = 0;
            XdmfInt64   i = 0;

            while((first <= ' ') && (i < strlen(this->DataXml))){
                first = this->DataXml[i++];
                if((first > ' ') && (first != '<')){
                    this->Set("CData", this->DataXml);
                    return(XDMF_SUCCESS);
                }
            }
        }
        if(this->DOM){
            if(this->InsertedDataXml == this->DataXml){
                // Already done
                return(XDMF_SUCCESS);
            }
            if(this->DOM->InsertFromString(this->GetElement(), this->DataXml)){
                this->SetInsertedDataXml(this->DataXml);
                return(XDMF_SUCCESS);
            }else{
                XdmfErrorMessage("Error Inserting Raw XML : " << endl << this->DataXml);
                return(XDMF_FAIL);
            }
        }else{
            XdmfErrorMessage("Can't insert raw XML sine DOM is not set");
            return(XDMF_FAIL);
        }
    }
    return(XDMF_FAIL);
}

XdmfInt32 XdmfElement::Build(){
    XdmfConstString  name;

    XdmfInt32        i;

    name = this->GetName();
    if(name && (strlen(name) > 0)){
        this->Set("Name", name);
    }
    if(this->DOM){
        XdmfXmlNode myelement = this->GetElement();
        if(myelement){
            XdmfXmlNode childnode;
            XdmfElement *childelement;
            for(i=0;i<this->DOM->GetNumberOfChildren(myelement);i++){
                childnode = this->DOM->GetChild(i, myelement);
                childelement = (XdmfElement *)this->GetCurrentXdmfElement(childnode);
                if(childelement){
                    // cout << "Child Element Type of " << childelement << " = " << childelement->GetElementType() << endl;
                    childelement->Build();
                }
            }
        }
    }
    return(XDMF_SUCCESS);
}

XdmfInt32 XdmfElement::Set(XdmfConstString aName, XdmfConstString Value){
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(XDMF_FAIL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(XDMF_FAIL);
    }
    this->DOM->Set(this->Element, aName, Value);
    return(XDMF_SUCCESS);
}


XdmfConstString XdmfElement::Get(XdmfConstString aName){
    if(!this->DOM) {
        XdmfErrorMessage("No DOM has been set");
        return(NULL);
    }
    if(!this->Element){
        XdmfErrorMessage("No XML Node has been set");
        return(NULL);
    }
    return(this->DOM->Get(this->Element, aName));
}