From ac3d2436ec8a644abcd0ad76d3934bc6f112bac8 Mon Sep 17 00:00:00 2001 From: "Frank T. Bergmann" Date: Thu, 22 Aug 2024 10:57:17 +0200 Subject: [PATCH] #390: add hasPrefix to xmlnode / xmlattributes --- src/sbml/xml/XMLAttributes.cpp | 7 +++++++ src/sbml/xml/XMLAttributes.h | 19 +++++++++++++++++++ src/sbml/xml/XMLNode.cpp | 7 +++++++ src/sbml/xml/XMLNode.h | 16 ++++++++++++++-- src/sbml/xml/test/TestXMLNode.cpp | 5 +++++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/sbml/xml/XMLAttributes.cpp b/src/sbml/xml/XMLAttributes.cpp index 067586f19b..ebf934d298 100644 --- a/src/sbml/xml/XMLAttributes.cpp +++ b/src/sbml/xml/XMLAttributes.cpp @@ -1448,6 +1448,13 @@ XMLAttributes_getPrefix (const XMLAttributes_t *xa, int index) return xa->getPrefix(index).empty() ? NULL : safe_strdup(xa->getPrefix(index).c_str()); } +LIBLAX_EXTERN +int +XMLAttributes_hasPrefix (const XMLAttributes_t *xa, int index) +{ + if (xa == NULL) return 0; + return xa->getPrefix(index).empty() ? 0 : 1; +} LIBLAX_EXTERN char * diff --git a/src/sbml/xml/XMLAttributes.h b/src/sbml/xml/XMLAttributes.h index 7a434e4ca6..79088a313d 100644 --- a/src/sbml/xml/XMLAttributes.h +++ b/src/sbml/xml/XMLAttributes.h @@ -2165,6 +2165,25 @@ LIBLAX_EXTERN char * XMLAttributes_getPrefix (const XMLAttributes_t *xa, int index); +/** + * Return whether an attribute in this XMLAttributes_t structure (by position) has a prefix. + * + * @param xa the XMLAttributes_t structure. + * @param index an integer, the position of the attribute whose value is + * required. + * + * @return 1 if the attribute has a prefix, 0 otherwise. + * + * @note If index + * is out of range, 0 will be returned. + * Use XMLAttributes_hasAttribute() != 0 to test for attribute existence. + * + * @memberof XMLAttributes_t + */ +LIBLAX_EXTERN +int +XMLAttributes_hasPrefix (const XMLAttributes_t *xa, int index); + /** * Return the namespace URI of an attribute in this XMLAttributes_t structure (by position). diff --git a/src/sbml/xml/XMLNode.cpp b/src/sbml/xml/XMLNode.cpp index 9d0b95e1ea..828ad4bfe8 100644 --- a/src/sbml/xml/XMLNode.cpp +++ b/src/sbml/xml/XMLNode.cpp @@ -874,6 +874,13 @@ XMLNode_getPrefix (const XMLNode_t *node) return node->getPrefix().empty() ? NULL : node->getPrefix().c_str(); } +LIBLAX_EXTERN +int +XMLNode_hasPrefix (const XMLNode_t *node) +{ + if (node == NULL) return 0; + return node->getPrefix().empty() ? 0 : 1; +} LIBLAX_EXTERN const char * diff --git a/src/sbml/xml/XMLNode.h b/src/sbml/xml/XMLNode.h index 92ae443f5a..b190a31744 100644 --- a/src/sbml/xml/XMLNode.h +++ b/src/sbml/xml/XMLNode.h @@ -833,8 +833,7 @@ XMLNode_getName (const XMLNode_t *node); * * @return the namespace prefix of this XML element. * - * @note If no prefix - * exists, an empty string will be return. + * @note If no prefix exists, NULL will be returned. * * @memberof XMLNode_t */ @@ -842,6 +841,19 @@ LIBLAX_EXTERN const char * XMLNode_getPrefix (const XMLNode_t *node); +/** + * Returns a flag, whether this XML element has a namespace prefix. + * + * @param node XMLNode_t structure to be queried. + * + * @return 1 (true) if this XML element has a namespace prefix, + * 0 (false) otherwise. + * + * @memberof XMLNode_t + */ +LIBLAX_EXTERN +int +XMLNode_hasPrefix (const XMLNode_t *node); /** * Returns the namespace URI of this XML element. diff --git a/src/sbml/xml/test/TestXMLNode.cpp b/src/sbml/xml/test/TestXMLNode.cpp index e861c4d599..595edc758c 100644 --- a/src/sbml/xml/test/TestXMLNode.cpp +++ b/src/sbml/xml/test/TestXMLNode.cpp @@ -180,6 +180,7 @@ START_TEST (test_XMLNode_createFromToken) fail_unless(strcmp(XMLNode_getName(node), "attr") == 0); fail_unless(strcmp(XMLNode_getPrefix(node), "prefix") == 0); + fail_unless(XMLNode_hasPrefix(node) == 1); fail_unless(strcmp(XMLNode_getURI(node), "uri") == 0); fail_unless (XMLNode_getChild(node, 1) != NULL); @@ -220,6 +221,7 @@ START_TEST (test_XMLNode_createElement) fail_unless(XMLNode_getNumChildren(snode) == 0); fail_unless(strcmp(XMLNode_getName (snode), name ) == 0); fail_unless(strcmp(XMLNode_getPrefix(snode), prefix) == 0); + fail_unless(XMLNode_hasPrefix(snode) == 1); fail_unless(strcmp(XMLNode_getURI (snode), uri ) == 0); fail_unless(XMLNode_isElement(snode) == 1); fail_unless(XMLNode_isStart (snode) == 1); @@ -266,6 +268,7 @@ START_TEST (test_XMLNode_createElement) fail_unless(XMLNode_getNumChildren(snode) == 0); fail_unless(strcmp(XMLNode_getName (snode), "test") == 0); fail_unless(XMLNode_getPrefix(snode) == NULL ); + fail_unless(XMLNode_hasPrefix(snode) == 0 ); fail_unless(XMLNode_getURI (snode) == NULL ); fail_unless(XMLNode_isElement(snode) == 1); fail_unless(XMLNode_isStart (snode) == 1); @@ -284,6 +287,7 @@ START_TEST (test_XMLNode_createElement) free(test); fail_unless(XMLAttributes_getPrefix(cattr, 0) == NULL); + fail_unless(XMLAttributes_hasPrefix(cattr, 0) == 0); fail_unless(XMLAttributes_getURI (cattr, 0) == NULL); /* end element */ @@ -293,6 +297,7 @@ START_TEST (test_XMLNode_createElement) fail_unless(XMLNode_getNumChildren(enode) == 0); fail_unless(strcmp(XMLNode_getName(enode), "test") == 0); fail_unless(XMLNode_getPrefix(enode) == NULL ); + fail_unless(XMLNode_hasPrefix(enode) == 0 ); fail_unless(XMLNode_getURI (enode) == NULL ); fail_unless(XMLNode_isElement(enode) == 1); fail_unless(XMLNode_isStart (enode) == 0);