forked from alliedtelesis/apteryx-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.c
3878 lines (3580 loc) · 111 KB
/
schema.c
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file schema.c
* Utilities for validating paths against the XML schema.
*
* Copyright 2016, Allied Telesis Labs New Zealand, Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <glib.h>
#include <dirent.h>
#include <fnmatch.h>
#include <syslog.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <jansson.h>
#include <regex.h>
#include <apteryx.h>
#include "apteryx-xml.h"
/* Error handling and debug */
static __thread sch_err tl_error = 0;
static __thread char tl_errmsg[BUFSIZ] = {0};
#define DEBUG(flags, fmt, args...) \
if (flags & SCH_F_DEBUG) \
{ \
syslog (LOG_DEBUG, fmt, ## args); \
printf (fmt, ## args); \
}
#define ERROR(flags, error, fmt, args...) \
{ \
tl_error = error; \
snprintf (tl_errmsg, BUFSIZ - 1, fmt, ## args); \
DEBUG (flags, fmt"\n", ## args); \
}
#define READ_BUF_SIZE 512
typedef struct _sch_instance
{
xmlDoc *doc;
GList *models_list;
GHashTable *map_hash_table;
GHashTable *model_hash_table;
GList *regexes;
} sch_instance;
typedef struct _sch_xml_to_gnode_parms_s
{
sch_instance *in_instance;
int in_flags;
char *in_def_op;
bool in_is_edit;
GNode *out_tree;
nc_error_parms out_error;
GList *out_deletes;
GList *out_removes;
GList *out_creates;
GList *out_replaces;
} _sch_xml_to_gnode_parms;
/* Retrieve the last error code */
sch_err
sch_last_err (void)
{
return tl_error;
}
/* Retrieve the last error message */
const char *
sch_last_errmsg (void)
{
return tl_errmsg;
}
/* List full paths for all schema files in the search path */
static void
list_schema_files (GList ** files, const char *path)
{
DIR *dp;
struct dirent *ep;
char *saveptr = NULL;
char *cpath;
char *dpath;
cpath = g_strdup (path);
dpath = strtok_r (cpath, ":", &saveptr);
while (dpath != NULL)
{
dp = opendir (dpath);
if (dp != NULL)
{
while ((ep = readdir (dp)))
{
char *filename;
if ((fnmatch ("*.xml", ep->d_name, FNM_PATHNAME) != 0) &&
(fnmatch ("*.xml.gz", ep->d_name, FNM_PATHNAME) != 0) &&
(fnmatch ("*.map", ep->d_name, FNM_PATHNAME) != 0))
{
continue;
}
if (dpath[strlen (dpath) - 1] == '/')
filename = g_strdup_printf ("%s%s", dpath, ep->d_name);
else
filename = g_strdup_printf ("%s/%s", dpath, ep->d_name);
*files = g_list_append (*files, filename);
}
(void) closedir (dp);
}
dpath = strtok_r (NULL, ":", &saveptr);
}
free (cpath);
*files = g_list_sort (*files, (GCompareFunc) strcasecmp);
return;
}
static bool
sch_ns_node_equal (xmlNode * a, xmlNode * b)
{
char *a_name = NULL;
char *b_name = NULL;
bool ret = false;
/* Must have matching names */
a_name = (char *) xmlGetProp (a, (xmlChar *) "name");
b_name = (char *) xmlGetProp (b, (xmlChar *) "name");
if (g_strcmp0 (a_name, b_name) != 0)
{
goto exit;
}
/* Must have matching namespaces */
if ((a->ns == b->ns) ||
(a->ns && b->ns && g_strcmp0 ((const char *)a->ns->href, (const char *)b->ns->href) == 0))
{
ret = true;
}
exit:
free (a_name);
free (b_name);
return ret;
}
static void
insert_in_order (xmlNs *ns, xmlNode *parent, xmlNode *child)
{
xmlNode *sibling = NULL;
/* Add nodes for the current model before any augmentations */
if (ns && child->ns && g_strcmp0 ((char *) ns->href, (char *) child->ns->href) == 0)
{
sibling = parent->children;
while (sibling)
{
if (g_strcmp0 ((char *) ns->href, (char *) sibling->ns->href) != 0)
break;
sibling = sibling->next;
}
}
if (sibling)
{
if (child->parent)
xmlUnlinkNode (child);
// printf ("Adding %s after %s\n", xmlGetProp (child, (xmlChar *)"name"), xmlGetProp (sibling, (xmlChar *)"name"));
xmlAddPrevSibling (sibling, child);
}
else if (!child->parent)
{
// printf ("Adding %s to end\n", xmlGetProp (child, (xmlChar *)"name"));
xmlAddChild (parent, child);
}
}
/* Merge nodes from a new tree to the original tree */
static void
merge_nodes (xmlNs * ns, xmlNode * parent, xmlNode * orig, xmlNode * new, int depth)
{
xmlNode *n;
xmlNode *o;
for (n = new; n; n = n->next)
{
xmlAttr* attribute = n->properties;
/* Check if this node is already in the existing tree */
for (o = orig; o; o = o->next)
{
if (sch_ns_node_equal (n, o))
{
/* Check to see if the model names match */
xmlChar *mod_n = xmlGetProp (n, (xmlChar *)"model");
if (mod_n)
{
xmlChar *mod_o = xmlGetProp (o, (xmlChar *)"model");
if (mod_o)
{
if (g_strcmp0 ((char *) mod_o, (char *) mod_n) != 0)
{
xmlChar *name = xmlGetProp (n, (xmlChar *)"name");
syslog (LOG_ERR, "XML: Conflicting model names in same namespace \"%s:%s\" \"%s:%s\"",
(char *) mod_o, (char *) name, (char *) mod_n, (char *) name);
xmlFree (name);
}
xmlFree (mod_o);
}
xmlFree (mod_n);
}
/* Merge into the original node any new attributes from the new node */
while (attribute && attribute->name && attribute->children)
{
if (!xmlHasProp (o, attribute->name))
{
xmlChar* value = xmlNodeListGetString (n->doc, attribute->children, 1);
xmlSetProp (o, attribute->name, value);
xmlFree (value);
}
attribute = attribute->next;
}
break;
}
}
if (o)
{
/* Already exists - merge in the children */
merge_nodes (ns, o, o->children, n->children, depth + 1);
if (depth > 0)
insert_in_order (ns, parent, o);
}
else
{
/* New node */
o = xmlCopyNode (n, 1);
if (depth > 0)
insert_in_order (ns, parent, o);
else
xmlAddChild (parent, o);
}
}
}
/* Remove unwanted nodes and attributes from a parsed tree */
static void
cleanup_nodes (xmlNode * node)
{
xmlNode *n, *next;
n = node;
while (n)
{
next = n->next;
if (n->type == XML_ELEMENT_NODE)
{
cleanup_nodes (n->children);
}
else
{
xmlUnlinkNode (n);
xmlFreeNode (n);
}
n = next;
}
}
/* Add module organisation and revision to the first child(ren) that matches the namespace */
static void
add_module_info_to_children (xmlNode *node, xmlNsPtr ns, xmlChar *mod, xmlChar *org,
xmlChar *ver, xmlChar *feat, xmlChar *devi)
{
xmlNode *n = node;
while (n)
{
if (n->ns && g_strcmp0 ((char *)n->ns->href, (char *)ns->href) == 0)
{
if (!xmlHasProp (n, (const xmlChar *)"model"))
{
xmlNewProp (n, (const xmlChar *)"model", mod);
xmlNewProp (n, (const xmlChar *)"organization", org);
xmlNewProp (n, (const xmlChar *)"version", ver);
xmlNewProp (n, (const xmlChar *)"features", feat);
xmlNewProp (n, (const xmlChar *)"deviations", devi);
}
}
else
{
add_module_info_to_children (n->children, ns, mod, org, ver, feat, devi);
}
n = n->next;
}
}
static void
add_module_info_to_child (sch_instance *instance, xmlNode *module)
{
xmlChar *mod = xmlGetProp (module, (xmlChar *) "model");
if (mod)
{
xmlChar *org = xmlGetProp (module, (xmlChar *) "organization");
xmlChar *ver = xmlGetProp (module, (xmlChar *) "version");
xmlChar *feat = xmlGetProp (module, (xmlChar *) "features");
xmlChar *devi = xmlGetProp (module, (xmlChar *) "deviations");
xmlNsPtr def = xmlSearchNs (module->doc, module, NULL);
add_module_info_to_children (module->children, def, mod, org, ver, feat, devi);
xmlFree (mod);
if (org)
xmlFree (org);
if (ver)
xmlFree (ver);
if (feat)
xmlFree (feat);
if (devi)
xmlFree (devi);
}
}
static bool
save_module_info (sch_instance *instance, xmlNode *module)
{
sch_loaded_model *loaded;
bool add = true;
xmlChar *mod = xmlGetProp (module, (xmlChar *) "model");
xmlChar *org = xmlGetProp (module, (xmlChar *) "organization");
xmlChar *ver = xmlGetProp (module, (xmlChar *) "version");
xmlChar *feat = xmlGetProp (module, (xmlChar *) "features");
xmlChar *devi = xmlGetProp (module, (xmlChar *) "deviations");
if (instance->model_hash_table)
{
if (!mod || strlen ((char *) mod ) == 0 ||
!g_hash_table_lookup (instance->model_hash_table, (const char *) mod))
{
if (mod)
xmlFree (mod);
if (org)
xmlFree (org);
if (ver)
xmlFree (ver);
if (feat)
xmlFree (feat);
if (devi)
xmlFree (devi);
return false;
}
}
if (mod)
{
/* Check for duplicate model information being saved into the list of models */
for (GList *iter = g_list_first (instance->models_list); iter;
iter = g_list_next (iter))
{
loaded = iter->data;
if (g_strcmp0 ((char *) mod, loaded->model) == 0)
{
/* We have a duplicate model */
add = false;
}
}
}
if (add)
{
loaded = g_malloc0 (sizeof (sch_loaded_model));
if (loaded)
{
if (module->ns)
{
if (module->ns->href)
{
loaded->ns_href = g_strdup ((char *) module->ns->href);
}
if (module->ns->prefix)
{
loaded->ns_prefix = g_strdup ((char *) module->ns->prefix);
}
}
else
{
xmlChar *nsp = xmlGetProp (module, (xmlChar *) "namespace");
xmlChar *pre = xmlGetProp (module, (xmlChar *) "prefix");
if (nsp)
{
loaded->ns_href = (char *) nsp;
}
if (pre)
{
loaded->ns_prefix = (char *) pre;
}
}
if (mod)
{
loaded->model = g_strdup ((char *) mod);
}
if (org)
{
loaded->organization = g_strdup ((char *) org);
}
if (ver)
{
loaded->version = g_strdup ((char *) ver);
}
if (feat)
{
loaded->features = g_strdup ((char *) feat);
}
if (devi)
{
loaded->deviations = g_strdup ((char *) devi);
}
instance->models_list = (void *) g_list_append (instance->models_list, loaded);
}
}
if (mod)
xmlFree (mod);
if (org)
xmlFree (org);
if (ver)
xmlFree (ver);
if (feat)
xmlFree (feat);
if (devi)
xmlFree (devi);
return true;
}
static void
copy_nsdef_to_root (xmlDoc *doc, xmlNode *node)
{
xmlNode *n = node;
while (n)
{
/* Copy any NS defintions to the new root */
xmlNsPtr ns = n->nsDef;
while (ns)
{
if (ns->href && !xmlSearchNsByHref (doc, xmlDocGetRootElement (doc), ns->href))
{
char *prefix = (char *) ns->prefix;
if (!prefix)
prefix = (char *) xmlGetProp (n, (xmlChar *)"prefix");
if (prefix)
xmlNewNs (xmlDocGetRootElement (doc), ns->href, (xmlChar *)prefix);
if (!ns->prefix)
free (prefix);
}
ns = ns->next;
}
/* Recurse */
copy_nsdef_to_root (doc, n->children);
n = n->next;
}
}
void
assign_ns_to_root (xmlDoc *doc, xmlNode *node)
{
xmlNode *n = node;
while (n)
{
/* Assign this nodes ns to the new root if needed */
if (n->ns)
n->ns = xmlSearchNsByHref (doc, xmlDocGetRootElement (doc), n->ns->href);
/* Recurse */
assign_ns_to_root (doc, n->children);
/* Chuck away the local NS */
if (n->nsDef) {
xmlFreeNsList (n->nsDef);
n->nsDef = NULL;
}
n = n->next;
}
}
static void
sch_load_namespace_mappings (sch_instance *instance, const char *filename)
{
FILE *fp = NULL;
gchar **ns_names;
char *buf;
if (!instance)
return;
buf = g_malloc0 (READ_BUF_SIZE);
fp = fopen (filename, "r");
if (fp && buf)
{
if (!instance->map_hash_table)
instance->map_hash_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
while (fgets (buf, READ_BUF_SIZE, fp) != NULL)
{
/* Skip comment lines */
if (buf[0] == '#')
continue;
/* Remove any trailing LF */
buf[strcspn(buf, "\n")] = '\0';
ns_names = g_strsplit (buf, " ", 2);
if (ns_names[0] && ns_names[1])
{
void *old_key;
void *old_value;
/* Look up this node name to check for duplicates. */
if (g_hash_table_lookup_extended (instance->map_hash_table, ns_names[0],
&old_key, &old_value))
{
g_hash_table_insert (instance->map_hash_table, g_strdup (ns_names[0]),
g_strdup (ns_names[1]));
g_free (old_key);
g_free (old_value);
}
else
{
g_hash_table_insert (instance->map_hash_table, g_strdup (ns_names[0]),
g_strdup (ns_names[1]));
}
}
g_strfreev (ns_names);
}
fclose (fp);
}
g_free (buf);
}
static void
sch_load_model_list (sch_instance *instance, const char *path, const char *model_list_filename)
{
FILE *fp = NULL;
char *name;
char *buf;
if (!instance)
return;
buf = g_malloc0 (READ_BUF_SIZE);
name = g_strdup_printf ("%s/%s", path, model_list_filename);
fp = fopen (name, "r");
if (fp && buf)
{
if (!instance->model_hash_table)
instance->model_hash_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
while (fgets (buf, READ_BUF_SIZE, fp) != NULL)
{
/* Skip comment lines */
if (buf[0] == '#')
continue;
/* Remove any trailing LF */
buf[strcspn(buf, "\n")] = '\0';
if (strlen (buf) > 0)
{
void *old_key;
void *old_value;
/* Look up this node name to check for duplicates. */
if (g_hash_table_lookup_extended (instance->model_hash_table, buf,
&old_key, &old_value))
{
g_free (old_key);
g_free (old_value);
}
else
{
g_hash_table_insert (instance->model_hash_table, g_strdup (buf),
g_strdup (buf));
}
}
}
fclose (fp);
}
g_free (name);
g_free (buf);
}
/* Parse all XML files in the search path and merge trees */
static sch_instance *
_sch_load (const char *path, const char *model_list_filename)
{
sch_instance *instance;
xmlNode *module;
xmlNs *ns;
GList *files = NULL;
GList *iter;
/* New instance */
instance = g_malloc0 (sizeof (sch_instance));
/* Create a new doc and root node for the merged MODULE */
instance->doc = xmlNewDoc ((xmlChar *) "1.0");
module = xmlNewNode (NULL, (xmlChar *) "MODULE");
ns = xmlNewNs (module, (const xmlChar *) "https://github.com/alliedtelesis/apteryx", NULL);
xmlSetNs (module, ns);
xmlNewNs (module, (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *) "xsi");
xmlNewProp (module, (const xmlChar *) "xsi:schemaLocation",
(const xmlChar *) "https://github.com/alliedtelesis/apteryx-xml https://github.com/alliedtelesis/apteryx-xml/releases/download/v1.2/apteryx.xsd");
xmlDocSetRootElement (instance->doc, module);
if (model_list_filename)
sch_load_model_list (instance, path, model_list_filename);
list_schema_files (&files, path);
for (iter = files; iter; iter = g_list_next (iter))
{
char *filename = (char *) iter->data;
char *ext = strrchr(filename, '.');
if (g_strcmp0 (ext, ".map") == 0)
{
sch_load_namespace_mappings (instance, filename);
continue;
}
xmlDoc *doc_new = xmlParseFile (filename);
if (doc_new == NULL)
{
syslog (LOG_ERR, "XML: failed to parse \"%s\"", filename);
continue;
}
xmlNode *module_new = xmlDocGetRootElement (doc_new);
cleanup_nodes (module_new);
/* Sanity check for empty modules */
if (!module_new || (module_new->children && (module_new->children->name[0] != 'N' && module_new->children->name[0] != 'S')))
{
syslog (LOG_ERR, "XML: ignoring empty schema \"%s\"", filename);
continue;
}
copy_nsdef_to_root (instance->doc, module_new);
if (save_module_info (instance, module_new))
{
add_module_info_to_child (instance, module_new);
merge_nodes (module_new->ns, module, module->children, module_new->children, 0);
xmlFreeDoc (doc_new);
assign_ns_to_root (instance->doc, module->children);
}
else
{
xmlFreeDoc (doc_new);
}
}
g_list_free_full (files, free);
/* Store a link back to the instance in the xmlDoc stucture */
instance->doc->_private = (void *) instance;
return instance;
}
sch_instance *
sch_load (const char *path)
{
return _sch_load (path, NULL);
}
/**
* Only load XML models that are specified in the model list file. If the model list
* filename is NULL, all models are loaded.
*/
sch_instance *
sch_load_with_model_list_filename (const char *path, const char *model_list_filename)
{
return _sch_load (path, model_list_filename);
}
static void
sch_free_loaded_models (GList *loaded_models)
{
GList *list;
sch_loaded_model *loaded;
if (loaded_models)
{
for (list = g_list_first (loaded_models); list; list = g_list_next (list))
{
loaded = list->data;
if (loaded->ns_href)
{
g_free (loaded->ns_href);
}
if (loaded->model)
{
g_free (loaded->model);
}
if (loaded->organization)
{
g_free (loaded->organization);
}
if (loaded->ns_prefix)
{
g_free (loaded->ns_prefix);
}
if (loaded->version)
{
g_free (loaded->version);
}
if (loaded->features)
{
g_free (loaded->features);
}
if (loaded->deviations)
{
g_free (loaded->deviations);
}
g_free (loaded);
}
g_list_free (loaded_models);
loaded_models = NULL;
}
}
static void
free_regex (regex_t *regex_obj)
{
regfree (regex_obj);
g_free (regex_obj);
}
void
sch_free (sch_instance * instance)
{
if (instance)
{
if (instance->models_list)
sch_free_loaded_models (instance->models_list);
if (instance->doc)
xmlFreeDoc (instance->doc);
if (instance->map_hash_table)
g_hash_table_destroy (instance->map_hash_table);
if (instance->model_hash_table)
g_hash_table_destroy (instance->model_hash_table);
if (instance->regexes)
g_list_free_full (instance->regexes, (GDestroyNotify) free_regex);
g_free (instance);
}
}
GList *
sch_get_loaded_models (sch_instance * instance)
{
return instance->models_list;
}
static gboolean
match_name (const char *s1, const char *s2)
{
char c1, c2;
do
{
c1 = *s1;
c2 = *s2;
if (c1 == '\0' && c2 == '\0')
return true;
if (c1 == '-')
c1 = '_';
if (c2 == '-')
c2 = '_';
s1++;
s2++;
}
while (c1 == c2);
return false;
}
static bool
sch_ns_native (sch_instance *instance, xmlNs *ns)
{
/* No namespace means native */
if (!ns)
return true;
/* Root namespace is considered native */
if (instance && ns == xmlDocGetRootElement (instance->doc)->ns)
return true;
/* Check if namespace is in the table of non-native namespaces */
if (instance && instance->map_hash_table)
{
if (g_hash_table_lookup (instance->map_hash_table, (const char *) ns->href))
return false;
}
return true;
}
static bool
remove_hidden_children (xmlNode *node)
{
xmlNode *child;
if (node == NULL)
return false;
/* Keep all the value nodes */
if (node->name[0] == 'V')
return true;
/* Throw away any non schema nodes */
if (node->name[0] != 'M' && node->name[0] != 'N')
return false;
if (sch_is_hidden (node))
return false;
child = node->children;
while (child)
{
if (!remove_hidden_children (child))
{
xmlNode *dchild = child;
child = child->next;
xmlUnlinkNode (dchild);
xmlFreeNode (dchild);
}
else
{
child = child->next;
}
}
return true;
}
static void
format_api_namespaces (sch_instance * instance, xmlNs *ns, xmlNode *node, int depth)
{
xmlNode *child;
if (node == NULL)
return;
child = node->children;
while (child)
{
if (depth == 0 && child->ns && child->ns->prefix && !sch_ns_native (instance, child->ns))
{
/* Replace top-level nodes of non-native models with the namespace prefixed name */
char *old = sch_name (child);
char *name = g_strdup_printf ("%s:%s",child->ns->prefix, old);
xmlSetProp (child, (const xmlChar *)"name", (const xmlChar *)name);
free (name);
free (old);
}
format_api_namespaces (instance, ns, child, depth + 1);
/* Everything in the apteryx namespace */
child->ns = ns;
child = child->next;
}
/* Get rid of all non default namespace definitions on the root node */
if (depth == 0)
{
xmlNs *cur = node->nsDef;
while (cur != NULL)
{
xmlNs *next = cur->next;
if (cur != ns)
xmlFreeNs (cur);
cur = next;
}
node->nsDef = ns;
ns->next = NULL;
xmlNewNs (node, (const xmlChar *) "http://www.w3.org/2001/XMLSchema-instance", (const xmlChar *) "xsi");
}
return;
}
static gint
xmlNodeCmp (gconstpointer a, gconstpointer b)
{
char *aname = (char *) xmlGetProp ((xmlNode *) a, (xmlChar *) "name");
char *bname = (char *) xmlGetProp ((xmlNode *) b, (xmlChar *) "name");
gint result = g_strcmp0 (aname, bname);
free (aname);
free (bname);
return result;
}
static void
sort_root_nodes (xmlNode *module)
{
GList *nodes = NULL;
xmlNode *child = module->children;
while (child)
{
xmlNode *next = child->next;
nodes = g_list_prepend (nodes, child);
xmlUnlinkNode (child);
child = next;
}
nodes = g_list_sort (nodes, xmlNodeCmp);
xmlNode *prev = NULL;
for (GList *iter = nodes; iter; iter = iter->next)
{
child = (xmlNode *) iter->data;
if (prev)
xmlAddNextSibling (prev, child);
else
xmlAddChild (module, child);
prev = child;
}
g_list_free (nodes);
}
char *
sch_dump_xml (sch_instance * instance)
{
xmlNode *xml = xmlDocGetRootElement (instance->doc);
xmlChar *xmlbuf = NULL;
int bufsize;
xmlDoc *copy = xmlCopyDoc (xml->doc, 1);
remove_hidden_children (xmlDocGetRootElement (copy));
format_api_namespaces (instance, xmlDocGetRootElement (copy)->ns, xmlDocGetRootElement (copy), 0);
sort_root_nodes (xmlDocGetRootElement (copy));
xmlDocDumpFormatMemory (copy, &xmlbuf, &bufsize, 1);
xmlFreeDoc (copy);
return (char *) xmlbuf;
}
static bool
sch_ns_match (xmlNode *node, xmlNs *ns)
{
sch_instance *instance = node->doc->_private;
/* Actually the same namespace (object) */
if (node->ns == ns)
return true;
/* NULL == the global namespace */
if (!ns && node->ns == xmlDocGetRootElement (instance->doc)->ns)
return true;
/* Check if both namespaces are part of the global namespace */
if (sch_ns_native (instance, ns) && sch_ns_native (instance, node->ns))
return true;
/* Search up the tree until the root<MODULE> for an exact namespace match */
while (ns && node && node->type == XML_ELEMENT_NODE && node->name[0] == 'N')
{
if (node->ns && node->ns->href &&
g_strcmp0 ((const char *) node->ns->href, (const char *) ns->href) == 0)
return true;
node = node->parent;
}
/* No match */
return false;
}
static xmlNs *
sch_lookup_ns (sch_instance * instance, xmlNode *schema, const char *name, int flags, bool href)
{
xmlNs *ns = NULL;
xmlNode *xml;
if (!schema)
schema = xmlDocGetRootElement (instance->doc);
xml = sch_node_child_first (schema);
while (xml && xml->type == XML_ELEMENT_NODE)
{
if (flags & SCH_F_NS_MODEL_NAME)
{
char *model;
model = (char *) xmlGetProp (xml, (xmlChar *) "model");
if (model)
{
if (g_strcmp0 (model, name) == 0)
{
free (model);
ns = xml->ns;
break;
}
free (model);