Skip to content

Commit

Permalink
Merge pull request #391 from sbmlteam/add-xmlnode_hasPrefix
Browse files Browse the repository at this point in the history
 #390: add hasPrefix to xmlnode / xmlattributes
  • Loading branch information
skeating authored Aug 22, 2024
2 parents 70d2890 + ac3d243 commit bc7264b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/sbml/xml/XMLAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
19 changes: 19 additions & 0 deletions src/sbml/xml/XMLAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions src/sbml/xml/XMLNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
16 changes: 14 additions & 2 deletions src/sbml/xml/XMLNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,27 @@ 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
*/
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.
Expand Down
5 changes: 5 additions & 0 deletions src/sbml/xml/test/TestXMLNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand All @@ -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);
Expand Down

0 comments on commit bc7264b

Please sign in to comment.