-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXdmfHDFDOM.cxx
535 lines (505 loc) · 15.1 KB
/
XdmfHDFDOM.cxx
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*=========================================================================
Project : XdmfGenerator
Module : XdmfHDFDOM.cxx
Authors:
John Biddiscombe Jerome Soumagne
biddisco@cscs.ch soumagne@cscs.ch
Copyright (C) CSCS - Swiss National Supercomputing Centre.
You may use modify and and distribute this code freely providing
1) This copyright notice appears on all copies of source code
2) An acknowledgment appears with any substantial usage of the code
3) If this code is contributed to any other open source project, it
must not be reformatted such that the indentation, bracketing or
overall style is modified significantly.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This work has received funding from the European Community's Seventh
Framework Programme (FP7/2007-2013) under grant agreement 225967 “NextMuSE”
=========================================================================*/
/*=========================================================================
This code is derived from an earlier work and is distributed
with permission from, and thanks to ...
=========================================================================*/
/*******************************************************************/
/* XDMF */
/* eXtensible Data Model and Format */
/* */
/* */
/* Author: */
/* Jerry A. Clarke */
/* clarke@arl.army.mil */
/* US Army Research Laboratory */
/* Aberdeen Proving Ground, MD */
/* */
/* Copyright @ 2007 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 "XdmfHDFDOM.h"
#include <libxml/globals.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xinclude.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
//----------------------------------------------------------------------------
static XdmfXmlNode
XdmfGetNextElement(XdmfXmlNode Node) {
XdmfXmlNode NextElement = Node->next;
while (NextElement && (NextElement->type != XML_ELEMENT_NODE)) {
NextElement = NextElement->next;
}
return(NextElement);
}
//----------------------------------------------------------------------------
XdmfHDFDOM::XdmfHDFDOM()
{
this->Tree = NULL;
this->Input = &cin;
this->Doc = NULL;
this->SetFileName("stdin");
this->SetWorkingDirectory("");
// Allow Indenting on Serialization
xmlIndentTreeOutput = 1;
xmlKeepBlanksDefault(0);
// Set Default Options
this->ParserOptions = XML_PARSE_NOENT | XML_PARSE_XINCLUDE | XML_PARSE_NONET;
}
//----------------------------------------------------------------------------
XdmfHDFDOM::~XdmfHDFDOM()
{
XdmfDebug("Destroying DOM");
if (this->Input != &cin) {
XdmfDebug("Deleting Input");
ifstream *OldInput = (ifstream *)this->Input;
OldInput->close();
delete this->Input;
this->Input = &cin;
}
if (this->Doc) xmlFreeDoc((xmlDoc *)this->Doc);
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::GetNumberOfAttributes(XdmfXmlNode Node)
{
XdmfInt32 NumberOfAttributes = 0;
xmlAttr *attr;
if (!Node) return(0);
attr = Node->properties;
while (attr) {
attr = attr->next;
NumberOfAttributes++;
}
return(NumberOfAttributes);
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::GetAttributeName(XdmfXmlNode Node, XdmfInt32 Index)
{
XdmfInt32 EIndex = 0;
xmlAttr *attr;
if (!Node) return(0);
attr = Node->properties;
while (attr && (EIndex < Index)) {
attr = attr->next;
EIndex++;
}
if (attr) {
return((XdmfConstString)attr->name);
}
return(NULL);
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::IsChild(XdmfXmlNode ChildToCheck, XdmfXmlNode Node)
{
XdmfXmlNode child;
// Check All Children
for(child=Node->xmlChildrenNode; child ; child=child->next) {
if (child->type == XML_ELEMENT_NODE) {
// Is this it?
if (child == ChildToCheck) {
return(XDMF_SUCCESS);
}
// Check Its children
if (this->IsChild(ChildToCheck, child) == XDMF_SUCCESS) {
return(XDMF_SUCCESS);
}
}
}
return(XDMF_FAIL);
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::SetInputFileName(XdmfConstString Filename)
{
if (this->Input != &cin) {
ifstream *OldInput = (ifstream *)this->Input;
OldInput->close();
delete this->Input;
this->Input = &cin;
}
if (XDMF_WORD_CMP(Filename, "stdin")) {
this->Input = &cin;
} else {
ifstream *NewInput = new ifstream(Filename);
if (!NewInput) {
XdmfErrorMessage("Can't Open Input File " << Filename);
return(XDMF_FAIL);
}
this->Input = NewInput;
}
this->SetFileName(Filename);
return(XDMF_SUCCESS);
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::Serialize(XdmfXmlNode Node)
{
int buflen;
xmlBufferPtr bufp;
if (!Node) Node = this->Tree;
if (!Node) return(NULL);
bufp = xmlBufferCreate();
buflen = xmlNodeDump(bufp, this->Doc, Node, 0, 1);
return(this->DupBuffer(bufp));
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::__Parse(XdmfConstString inxml, XdmfXmlDoc *DocPtr)
{
XdmfXmlNode Root = NULL;
XdmfXmlDoc pDoc;
int parserOptions;
parserOptions = this->ParserOptions;
if (inxml) {
// Is this XML or a File Name
if (inxml[0] == '<') {
// It's XML
pDoc = xmlReadMemory(inxml, (int)strlen(inxml), NULL, NULL, parserOptions);
} else {
// It's a File Name
this->SetInputFileName(inxml);
pDoc = xmlReadFile(this->GetInputFileName(), NULL, parserOptions);
}
} else {
pDoc = xmlReadFile(this->GetInputFileName(), NULL, parserOptions);
}
if (pDoc) {
if (parserOptions & XML_PARSE_XINCLUDE) {
if (xmlXIncludeProcess(pDoc) < 0) {
xmlFreeDoc(pDoc);
pDoc = NULL;
}
}
Root = xmlDocGetRootElement(pDoc);
}
if (DocPtr) *DocPtr = pDoc;
return(Root);
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::Parse(XdmfConstString inxml)
{
XdmfXmlNode Root;
// Remove Previous Data
if (this->Doc) xmlFreeDoc((xmlDoc *)this->Doc);
this->Tree = NULL;
Root = this->__Parse(inxml, &this->Doc);
if (Root) {
this->Tree = Root;
} else {
return(XDMF_FAIL);
}
return(XDMF_SUCCESS);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::GetChild(XdmfInt64 Index, XdmfXmlNode Node)
{
XdmfXmlNode child;
if (!Node) {
Node = this->Tree;
}
if (!Node) return(0);
child = Node->children;
if (Index == 0) {
if (child->type != XML_ELEMENT_NODE) {
child = XdmfGetNextElement(child);
}
}
while (child && Index) {
child = XdmfGetNextElement(child);
Index--;
}
return(child);
}
//----------------------------------------------------------------------------
XdmfInt64
XdmfHDFDOM::GetNumberOfChildren(XdmfXmlNode Node)
{
XdmfInt64 Index = 0;
XdmfXmlNode child;
if (!Node) {
Node = this->Tree;
}
if (!Node) return(0);
child = Node->children;
while (child) {
if (child->type == XML_ELEMENT_NODE) Index++;
child = XdmfGetNextElement(child);
}
return(Index);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::GetRoot(void)
{
return(this->Tree);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::FindElement(XdmfConstString TagName, XdmfInt32 Index, XdmfXmlNode Node, XdmfInt32 IgnoreInfo)
{
XdmfString type = (XdmfString)TagName;
XdmfXmlNode child;
// this->SetDebug(1);
if (TagName) {
XdmfDebug("FindElement " << TagName << " Index = " << Index);
} else {
XdmfDebug("FindElement NULL Index = " << Index);
}
if (!Node) {
if (!this->Tree) return(NULL);
Node = this->Tree;
}
child = Node->children;
if (!child) return(NULL);
if (type) {
if (STRNCASECMP(type, "NULL", 4) == 0) type = NULL;
}
if (!type) {
if (IgnoreInfo) {
while (child) {
if (XDMF_WORD_CMP("Information", (const char *)(child)->name) == 0) {
if (Index <= 0) {
return(child);
}
Index--;
}
child = XdmfGetNextElement(child);
}
} else {
return(this->GetChild(Index, Node));
}
} else {
while (child) {
if (IgnoreInfo && XDMF_WORD_CMP("Information", (const char *)(child)->name)) {
child = XdmfGetNextElement(child);
} else {
if (XDMF_WORD_CMP((const char *)type, (const char *)(child)->name)) {
if (Index <= 0) {
return(child);
}
Index--;
}
child = XdmfGetNextElement(child);
}
}
}
return(NULL);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::FindNextElement(XdmfConstString TagName, XdmfXmlNode Node, XdmfInt32 IgnoreInfo)
{
XdmfString type = (XdmfString)TagName;
XdmfXmlNode child;
// this->SetDebug(1);
if (TagName) {
XdmfDebug("FindNextElement" << TagName);
} else {
XdmfDebug("FindNextElement NULL");
}
if (!Node) {
if (!this->Tree) return(NULL);
Node = this->Tree->children;
}
if (!Node) return(NULL);
if (type) {
if (STRNCASECMP(type, "NULL", 4) == 0) type = NULL;
}
child = XdmfGetNextElement(Node);
while (child) {
if (IgnoreInfo && XDMF_WORD_CMP("Information", (const char *)(child)->name)) {
// skip Information elements.
} else {
if (!type ||
(XDMF_WORD_CMP((const char *)type, (const char *)(child)->name))) {
return child;
}
}
child = XdmfGetNextElement(child);
}
return (NULL);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::FindElementByAttribute(XdmfConstString Attribute,
XdmfConstString Value, XdmfInt32 Index, XdmfXmlNode Node)
{
XdmfXmlNode child;
if (!Node) {
Node = this->Tree;
}
if (!Node) return(NULL);
child = Node->children;
while (child) {
xmlChar *txt = xmlGetProp(child, (xmlChar *)Attribute);
if (XDMF_WORD_CMP((const char *)txt, (const char *)Value)) {
if (Index <= 0) {
xmlFree(txt);
return(child);
}
xmlFree(txt);
Index--;
}
child = XdmfGetNextElement(child);
}
return(NULL);
}
//----------------------------------------------------------------------------
XdmfXmlNode
XdmfHDFDOM::FindElementByPath(XdmfConstString Path)
{
// Use an XPath expression to return a Node
xmlXPathContextPtr xpathCtx;
xmlXPathObjectPtr xpathObj;
xmlNodeSetPtr nodes;
XdmfXmlNode child = NULL;
int i;
if (!this->Doc) {
XdmfErrorMessage("XML must be parsed before XPath is available");
return(NULL);
}
// Create the context
xpathCtx = xmlXPathNewContext(this->Doc);
if (xpathCtx == NULL) {
XdmfErrorMessage("Can't Create XPath Context");
return(NULL);
}
xpathObj = xmlXPathEvalExpression((const xmlChar *)Path, xpathCtx);
if (xpathObj == NULL) {
XdmfErrorMessage("Can't evaluate XPath : " << Path);
return(NULL);
}
// Return the first XML_ELEMENT_NODE
nodes = xpathObj->nodesetval;
if (!nodes) {
XdmfErrorMessage("No Elements Match XPath Expression : " << Path);
return(NULL);
}
XdmfDebug("Found " << nodes->nodeNr << " Element that match XPath expression " << Path);
for(i=0 ; i < nodes->nodeNr ; i++) {
child = nodes->nodeTab[i];
if (child->type == XML_ELEMENT_NODE) {
// this is it
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return(child);
}
}
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return(NULL);
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::GetPath(XdmfXmlNode Node)
{
char *txt;
if (!Node) {
XdmfErrorMessage("Node == NULL");
return((XdmfConstString)NULL);
}
txt = (char *)xmlGetNodePath(Node);
return(this->DupChars(txt));
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::FindNumberOfElements(XdmfConstString TagName, XdmfXmlNode Node)
{
XdmfXmlNode child;
XdmfInt32 Index = 0;
if (!Node) {
if (!this->Tree) return(XDMF_FAIL);
Node = this->Tree;
}
child = Node->children;
if (!child) return(0);
while (child) {
if (XDMF_WORD_CMP(TagName, (const char *)child->name)) {
Index++;
}
child = XdmfGetNextElement(child);
}
return(Index);
}
//----------------------------------------------------------------------------
XdmfInt32
XdmfHDFDOM::FindNumberOfElementsByAttribute(XdmfConstString Attribute,
XdmfConstString Value, XdmfXmlNode Node)
{
XdmfInt32 NElements = 0;
XdmfXmlNode child;
if (!Node) {
Node = this->Tree;
}
if (!Node) return(0);
child = Node->children;
while (child) {
xmlChar *txt;
txt = xmlGetProp(child, (xmlChar *)Attribute);
if (XDMF_WORD_CMP((const char *)txt, (const char *)Value)) {
NElements++;
}
xmlFree(txt);
child = XdmfGetNextElement(child);
}
return(0);
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::GetElementName(XdmfXmlNode Node)
{
return (XdmfConstString)Node->name;
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::GetAttribute(XdmfXmlNode Node, XdmfConstString Attribute)
{
if (!Node) {
Node = this->Tree;
}
if (!Node) return(0);
return((XdmfConstString)xmlGetProp(Node, (xmlChar *)Attribute));
}
//----------------------------------------------------------------------------
XdmfConstString
XdmfHDFDOM::GetCData(XdmfXmlNode Node)
{
char *txt;
if (!Node) {
Node = this->Tree;
}
if (!Node) return(0);
txt = (char *)xmlNodeListGetString(this->Doc, Node->xmlChildrenNode, 1);
return(this->DupChars(txt));
}
//----------------------------------------------------------------------------