yumapro  23.10T-12
YumaPro SDK
Loading...
Searching...
No Matches
xml_util.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 - 2012, Andy Bierman, All Rights Reserved.
3 * Copyright (c) 2012 - 2021, YumaWorks, Inc., All Rights Reserved.
4 *
5 * Unless required by applicable law or agreed to in writing,
6 * software distributed under the License is distributed on an
7 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the
9 * specific language governing permissions and limitations
10 * under the License.
11 */
12#ifndef _H_xml_util
13#define _H_xml_util
14/* FILE: xml_util.h
15*********************************************************************
16* *
17* P U R P O S E *
18* *
19*********************************************************************/
20
26/*********************************************************************
27* *
28* C H A N G E H I S T O R Y *
29* *
30*********************************************************************
31
32date init comment
33----------------------------------------------------------------------
3414-oct-05 abb begun
352-jan-06 abb rewrite xml_consume_* API to use simpler
36 xml_node_t
37*/
38
39
40/* From /usr/include/libxml2/libxml/ */
41#include <xmlreader.h>
42#include <xmlstring.h>
43
44#ifndef _H_ncxconst
45#include "ncxconst.h"
46#endif
47
48#ifndef _H_xmlns
49#include "xmlns.h"
50#endif
51
52#ifndef _H_log
53#include "log.h"
54#endif
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/********************************************************************
61* *
62* C O N S T A N T S *
63* *
64*********************************************************************/
65
66#define MAX_CHAR_ENT 8
67
68#define XML_START_MSG ((const xmlChar *)\
69 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
70
71#define XML_START_MSG_SIZE 38
72
73
74/* XML_PARSE_NOCDATA option merge CDATA as text nodes, used to
75 * parse Confd get-schema reply properly
76 * YPW-2127: add XML_PARSE_HUGE to avoid arbitrary 10M string limit
77 * - No artificial string node limit used!
78 */
79#define XML_READER_OPTIONS \
80 (XML_PARSE_RECOVER+XML_PARSE_NOERROR+ \
81 XML_PARSE_NOWARNING+XML_PARSE_NOBLANKS+XML_PARSE_NONET+ \
82 XML_PARSE_XINCLUDE+XML_PARSE_NOCDATA+XML_PARSE_HUGE)
83
84#define XML_SES_URL "netconf://pdu"
85
86/********************************************************************
87* *
88* T Y P E S *
89* *
90*********************************************************************/
91
155typedef dlq_hdr_t xml_attrs_t;
156
157
159typedef struct xml_attr_t_ {
161 dlq_hdr_t attr_qhdr;
162
165
168
170 const xmlChar *attr_qname;
171
173 const xmlChar *attr_name;
174
178 xmlChar *attr_dname;
179
181 xmlChar *attr_val;
182
188 struct xpath_pcb_t_ *attr_xpcb;
189} xml_attr_t;
190
191
193typedef enum xml_nodetyp_t_ {
200
201
207typedef struct xml_node_t_ {
210
213
216
218 const xmlChar *module;
219
221 const xmlChar *badns;
222
224 xmlChar *qname;
225
227 const xmlChar *elname;
228
230 const xmlChar *simval;
231
233 uint32 simlen;
234
236 xmlChar *simfree;
237
239 int depth;
240
243} xml_node_t;
244
245
246/********************************************************************
247* *
248* F U N C T I O N S *
249* *
250*********************************************************************/
251
252
258extern xml_node_t *
259 xml_new_node (void);
260
261
267extern void
269
270
276extern void
278
279
285extern void
287
288
297extern status_t
298 xml_get_reader_from_buff (const xmlChar *buff,
299 xmlTextReaderPtr *reader);
300
301
311extern status_t
312 xml_get_reader_from_filespec (const char *filespec,
313 xmlTextReaderPtr *reader);
314
315
327extern status_t
328 xml_get_reader_for_session (xmlInputReadCallback readfn,
329 xmlInputCloseCallback closefn,
330 void *context,
331 xmlTextReaderPtr *reader);
332
333
345extern status_t
346 xml_reset_reader_for_session (xmlInputReadCallback readfn,
347 xmlInputCloseCallback closefn,
348 void *context,
349 xmlTextReaderPtr reader);
350
351
357extern void
358 xml_free_reader (xmlTextReaderPtr reader);
359
360
368extern const char *
369 xml_get_node_name (int nodeval);
370
371
379extern boolean
380 xml_advance_reader (xmlTextReaderPtr reader);
381
382
392extern status_t
393 xml_node_match (const xml_node_t *node,
394 xmlns_id_t nsid,
395 const xmlChar *elname,
396 xml_nodetyp_t nodetyp);
397
398
407extern status_t
408 xml_endnode_match (const xml_node_t *startnode,
409 const xml_node_t *endnode);
410
411
420extern boolean
421 xml_docdone (xmlTextReaderPtr reader);
422
423
430extern void
431 xml_dump_node (const xml_node_t *node,
432 log_debug_t lvl);
433
434
440extern void
442
443
449extern xml_attr_t *
450 xml_new_attr (void);
451
452
458extern void
460
461
471extern status_t
473 xmlns_id_t ns_id,
474 const xmlChar *attr_name,
475 const xmlChar *attr_val);
476
477
491extern xml_attr_t *
493 xmlns_id_t ns_id,
494 const xmlChar *attr_qname,
495 uint32 plen,
496 const xmlChar *attr_val,
497 status_t *res);
498
499
509extern status_t
511 xmlns_id_t ns_id,
512 const xmlChar *pfix);
513
514
524extern status_t
526 const xmlChar *ns,
527 const xmlChar *pfix);
528
529
542extern status_t
544 xmlns_id_t ns_id,
545 const xmlChar *pfix,
546 const xmlChar *nsval);
547
548
555extern xml_attr_t *
557
558
565extern xml_attr_t *
566 xml_get_first_attr (const xml_node_t *node);
567
568
575extern xml_attr_t *
577
578
584extern void
586
587
594extern void
595 xml_move_attrs (xml_attrs_t *src_attrs,
596 xml_attrs_t *dest_attrs);
597
598
599
611extern xml_attr_t *
613 xmlns_id_t nsid,
614 const xmlChar *attrname);
615
616
628extern xml_attr_t *
630 xmlns_id_t nsid,
631 const xmlChar *attrname);
632
633
645extern const xml_attr_t *
646 xml_find_ro_attr (const xml_node_t *node,
647 xmlns_id_t nsid,
648 const xmlChar *attrname);
649
650
659extern uint32
660 xml_strlen (const xmlChar *str);
661
662
674extern uint32
675 xml_strlen_sp (const xmlChar *str,
676 boolean *sp);
677
678
689extern uint32
690 xml_strcpy (xmlChar *copyTo,
691 const xmlChar *copyFrom);
692
693
703extern uint32
704 xml_strncpy (xmlChar *copyTo,
705 const xmlChar *copyFrom,
706 uint32 maxlen);
707
708
715extern xmlChar *
716 xml_strdup (const xmlChar *copyFrom);
717
718
726extern xmlChar *
727 xml_strcat (xmlChar *appendTo,
728 const xmlChar *appendFrom);
729
730
739extern xmlChar *
740 xml_strncat (xmlChar *appendTo,
741 const xmlChar *appendFrom,
742 uint32 maxlen);
743
744
752extern xmlChar *
753 xml_strndup (const xmlChar *copyFrom,
754 uint32 maxlen);
755
756
764extern char *
765 xml_ch_strndup (const char *copyFrom,
766 uint32 maxlen);
767
768
782extern int
783 xml_strcmp (const xmlChar *s1,
784 const xmlChar *s2);
785
786
800extern int
801 xml_stricmp (const xmlChar *s1,
802 const xmlChar *s2);
803
804
818extern int
819 xml_strncmp (const xmlChar *s1,
820 const xmlChar *s2,
821 uint32 maxlen);
822
823
838extern int
839 xml_strnicmp (const xmlChar *s1,
840 const xmlChar *s2,
841 uint32 maxlen);
842
843
850extern boolean
851 xml_isspace (uint32 ch);
852
853
860extern boolean
861 xml_isspace_str (const xmlChar *str);
862
863
881extern int
882 xml_strcmp_nosp (const xmlChar *s1,
883 const xmlChar *s2);
884
885
906int
907 xml_strcmp_nosp2 (const xmlChar *s1,
908 const xmlChar *s2);
909
910
922extern xmlChar *
923 xml_copy_clean_string (const xmlChar *str);
924
925
934extern xmlChar
935 xml_convert_char_entity (const xmlChar *str,
936 uint32 *used);
937
938
958extern status_t
959 xml_check_ns (xmlTextReaderPtr reader,
960 const xmlChar *elname,
961 xmlns_id_t *id,
962 uint32 *pfix_len,
963 const xmlChar **badns);
964
965
975extern void
976 xml_check_qname_content (xmlTextReaderPtr reader,
977 xml_node_t *node);
978
979
995extern status_t
996 xml_get_namespace_id (xmlTextReaderPtr reader,
997 const xmlChar *prefix,
998 uint32 prefixlen,
999 boolean register_missing,
1000 xmlns_id_t *retnsid);
1001
1002
1017extern status_t
1018 xml_consume_node (xmlTextReaderPtr reader,
1019 xml_node_t *xmlnode,
1020 boolean nserr,
1021 boolean adv);
1022
1023
1036extern xmlChar * xml_trim_string (xmlChar *str);
1037
1038
1042#ifdef __cplusplus
1043} /* end extern 'C' */
1044#endif
1045
1046#endif /* _H_xml_util */
log_debug_t
The debug level enumerations used in util/log.c.
Definition: log.h:345
status_t
global error return code
Definition: status_enum.h:210
uint16 xmlns_id_t
integer handle for registered namespaces
Definition: xmlns.h:89
status_t xml_consume_node(xmlTextReaderPtr reader, xml_node_t *xmlnode, boolean nserr, boolean adv)
parse function for YIN input
Definition: xml_util.c:2410
char * xml_ch_strndup(const char *copyFrom, uint32 maxlen)
String duplicate for max N chars.
Definition: xml_util.c:1676
status_t xml_reset_reader_for_session(xmlInputReadCallback readfn, xmlInputCloseCallback closefn, void *context, xmlTextReaderPtr reader)
Reset the xmlTextReader for parsing the input of a NETCONF session.
Definition: xml_util.c:423
status_t xml_add_inv_xmlns_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *pfix, const xmlChar *nsval)
add an xmlns decl to the attribute Queue for an INVALID namespace.
Definition: xml_util.c:1096
xml_attr_t * xml_find_attr(xml_node_t *node, xmlns_id_t nsid, const xmlChar *attrname)
Find an attribute.
Definition: xml_util.c:1290
status_t xml_add_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *attr_name, const xmlChar *attr_val)
add an attribute to an attribute list
Definition: xml_util.c:830
int xml_strcmp_nosp2(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar for 2 strings, but completely ignoring whitespace differences.
Definition: xml_util.c:2014
uint32 xml_strcpy(xmlChar *copyTo, const xmlChar *copyFrom)
String copy for xmlChar.
Definition: xml_util.c:1471
xmlChar * xml_strncat(xmlChar *appendTo, const xmlChar *appendFrom, uint32 maxlen)
String concatenate for at most maxlen xmlChars.
Definition: xml_util.c:1607
int xml_stricmp(const xmlChar *s1, const xmlChar *s2)
Case insensitive string compare for xmlChar.
Definition: xml_util.c:1754
const xml_attr_t * xml_find_ro_attr(const xml_node_t *node, xmlns_id_t nsid, const xmlChar *attrname)
Find a read-only attribute.
Definition: xml_util.c:1358
status_t xml_get_reader_from_buff(const xmlChar *buff, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing a debug test file.
Definition: xml_util.c:307
void xml_free_node(xml_node_t *node)
Free an xml_node_t struct.
Definition: xml_util.c:246
xmlChar * xml_strcat(xmlChar *appendTo, const xmlChar *appendFrom)
String concatenate for xmlChar.
Definition: xml_util.c:1576
int xml_strcmp(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar.
Definition: xml_util.c:1717
boolean xml_isspace(uint32 ch)
Check if an xmlChar is a space char.
Definition: xml_util.c:1876
xml_nodetyp_t
only 4 types of nodes returned
Definition: xml_util.h:193
xml_attr_t * xml_find_attr_q(xml_attrs_t *attrs, xmlns_id_t nsid, const xmlChar *attrname)
Find an attribute in a Q.
Definition: xml_util.c:1317
int xml_strcmp_nosp(const xmlChar *s1, const xmlChar *s2)
String compare for xmlChar for 2 strings, but ignoring whitespace differences.
Definition: xml_util.c:1937
boolean xml_docdone(xmlTextReaderPtr reader)
check if the input is completed for a given PDU
Definition: xml_util.c:646
void xml_free_attr(xml_attr_t *attr)
free an attribute
Definition: xml_util.c:795
uint32 xml_strlen(const xmlChar *str)
Get the String len for xmlChar.
Definition: xml_util.c:1395
status_t xml_endnode_match(const xml_node_t *startnode, const xml_node_t *endnode)
check if a specific node is the proper endnode match for a given startnode
Definition: xml_util.c:601
xml_attr_t * xml_get_first_attr(const xml_node_t *node)
get the first attribute in the attrs list, from an xml_node_t param
Definition: xml_util.c:1194
const char * xml_get_node_name(int nodeval)
get the node type according to the xmlElementType enum list in /usr/include/libxml/libxml/tree....
Definition: xml_util.c:480
boolean xml_isspace_str(const xmlChar *str)
Check if an xmlChar string is all whitespace chars.
Definition: xml_util.c:1898
status_t xml_check_ns(xmlTextReaderPtr reader, const xmlChar *elname, xmlns_id_t *id, uint32 *pfix_len, const xmlChar **badns)
Check a namespace during parsing.
Definition: xml_util.c:2212
status_t xml_node_match(const xml_node_t *node, xmlns_id_t nsid, const xmlChar *elname, xml_nodetyp_t nodetyp)
check if a specific node is the proper owner, name, and type
Definition: xml_util.c:552
status_t xml_get_reader_from_filespec(const char *filespec, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing a debug test file.
Definition: xml_util.c:344
uint32 xml_strncpy(xmlChar *copyTo, const xmlChar *copyFrom, uint32 maxlen)
String copy for xmlChar – checks for buffer overflow.
Definition: xml_util.c:1501
xmlChar * xml_copy_clean_string(const xmlChar *str)
Get a malloced string contained the converted string from the input.
Definition: xml_util.c:2088
void xml_clean_attrs(xml_attrs_t *attrs)
clean an xml_attrs_t variable
Definition: xml_util.c:1241
xml_attr_t * xml_add_qattr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *attr_qname, uint32 plen, const xmlChar *attr_val, status_t *res)
add a qualified attribute to an attribute list with a prefix
Definition: xml_util.c:882
void xml_clean_node(xml_node_t *node)
Clean an xml_node_t struct.
Definition: xml_util.c:266
void xml_free_reader(xmlTextReaderPtr reader)
Free the previously allocated xmlTextReader.
Definition: xml_util.c:458
xml_attr_t * xml_first_attr(xml_attrs_t *attrs)
get the first attribute in the list
Definition: xml_util.c:1171
status_t xml_get_namespace_id(xmlTextReaderPtr reader, const xmlChar *prefix, uint32 prefixlen, boolean register_missing, xmlns_id_t *retnsid)
Get the namespace for the specified prefix (may be NULL) Use the current XML reader context to resolv...
Definition: xml_util.c:2325
void xml_move_attrs(xml_attrs_t *src_attrs, xml_attrs_t *dest_attrs)
Move all the attrs from the src to the dest.
Definition: xml_util.c:1267
int xml_strncmp(const xmlChar *s1, const xmlChar *s2, uint32 maxlen)
String compare for xmlChar for at most 'maxlen' xmlChars.
Definition: xml_util.c:1795
xml_attr_t * xml_new_attr(void)
malloc and init an attribute struct
Definition: xml_util.c:774
status_t xml_get_reader_for_session(xmlInputReadCallback readfn, xmlInputCloseCallback closefn, void *context, xmlTextReaderPtr *reader)
Get a new xmlTextReader for parsing the input of a NETCONF session.
Definition: xml_util.c:382
void xml_check_qname_content(xmlTextReaderPtr reader, xml_node_t *node)
Check if the string node content is a likely QName.
Definition: xml_util.c:2269
xml_node_t * xml_new_node(void)
Malloc and init a new xml_node_t struct.
Definition: xml_util.c:205
void xml_init_node(xml_node_t *node)
Init an xml_node_t struct.
Definition: xml_util.c:228
xmlChar xml_convert_char_entity(const xmlChar *str, uint32 *used)
Convert an XML character entity into a single xmlChar.
Definition: xml_util.c:2151
int xml_strnicmp(const xmlChar *s1, const xmlChar *s2, uint32 maxlen)
Case insensitive string compare for xmlChar for at most 'maxlen' xmlChars.
Definition: xml_util.c:1838
uint32 xml_strlen_sp(const xmlChar *str, boolean *sp)
get length and check if any whitespace at the same time
Definition: xml_util.c:1430
void xml_init_attrs(xml_attrs_t *attrs)
initialize an xml_attrs_t variable
Definition: xml_util.c:754
xml_attr_t * xml_next_attr(xml_attr_t *attr)
get the next attribute in the list
Definition: xml_util.c:1218
xmlChar * xml_strndup(const xmlChar *copyFrom, uint32 maxlen)
String duplicate for max N xmlChars.
Definition: xml_util.c:1638
void xml_dump_node(const xml_node_t *node, log_debug_t lvl)
Debug function to printf xml_node_t contents.
Definition: xml_util.c:673
xmlChar * xml_trim_string(xmlChar *str)
trim leading and trailing whitespace inline in the same buffer
Definition: xml_util.c:2606
status_t xml_add_xmlns_attr_string(xml_attrs_t *attrs, const xmlChar *ns, const xmlChar *pfix)
add an xmlns decl to the attribute Queue
Definition: xml_util.c:1019
dlq_hdr_t xml_attrs_t
queue of xml_attr_t
Definition: xml_util.h:155
status_t xml_add_xmlns_attr(xml_attrs_t *attrs, xmlns_id_t ns_id, const xmlChar *pfix)
add an xmlns decl to the attribute Queue
Definition: xml_util.c:938
xmlChar * xml_strdup(const xmlChar *copyFrom)
String duplicate for xmlChar.
Definition: xml_util.c:1534
boolean xml_advance_reader(xmlTextReaderPtr reader)
Advance to the next node in the specified reader.
Definition: xml_util.c:522
@ XML_NT_NONE
not set
Definition: xml_util.h:194
@ XML_NT_END
end-tag of an element
Definition: xml_util.h:197
@ XML_NT_STRING
string content node
Definition: xml_util.h:198
@ XML_NT_START
start-tag of an element
Definition: xml_util.h:196
@ XML_NT_EMPTY
standalone empty node
Definition: xml_util.h:195
NCX System Logging Manager.
Contains NCX constants.
represents one attribute
Definition: xml_util.h:159
const xmlChar * attr_name
attribute name without any prefix
Definition: xml_util.h:173
struct xpath_pcb_t_ * attr_xpcb
XPath PCB in case XPath was parsed.
Definition: xml_util.h:188
xmlns_id_t attr_ns
attribute namespace ID
Definition: xml_util.h:164
xmlns_id_t attr_xmlns_ns
if xmlns, then namespace ID getting assigned
Definition: xml_util.h:167
xmlChar * attr_val
attribute value string
Definition: xml_util.h:181
const xmlChar * attr_qname
attribute qname with prefix (if any)
Definition: xml_util.h:170
xmlChar * attr_dname
malloced attribute name full qualified name if any
Definition: xml_util.h:178
dlq_hdr_t attr_qhdr
queue header
Definition: xml_util.h:161
gather node data into a simple struct.
Definition: xml_util.h:207
const xmlChar * elname
element name without any prefix
Definition: xml_util.h:227
const xmlChar * module
YANG module found for this node.
Definition: xml_util.h:218
xmlChar * qname
qualified name of element
Definition: xml_util.h:224
const xmlChar * simval
simple value if this is an XML_NT_STRING node
Definition: xml_util.h:230
xmlChar * simfree
non-NULL if simval is freed
Definition: xml_util.h:236
xmlns_id_t contentnsid
namespace of content
Definition: xml_util.h:215
uint32 simlen
length of the simple value string
Definition: xml_util.h:233
const xmlChar * badns
set if nsid == xmlns_inv_id()
Definition: xml_util.h:221
int depth
XML node depth.
Definition: xml_util.h:239
xml_nodetyp_t nodetyp
XML tag type.
Definition: xml_util.h:209
xml_attrs_t attrs
attributes parsed for this node if XML_NT_EMPTY or XML_NT_START
Definition: xml_util.h:242
xmlns_id_t nsid
namespace ID
Definition: xml_util.h:212
XML namespace support.