Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xml from file #311

Merged
merged 5 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions examples/c++/setAnnotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,6 @@
using namespace std;
LIBSBML_CPP_NAMESPACE_USE

XMLNode* parseAnnotation(const std::string& annotationFile)
{
// read file into memory
FILE* file = fopen(annotationFile.c_str(), "r");
fseek(file, 0, SEEK_END);
long length = ftell(file);

fseek(file, 0, SEEK_SET);
char* data = new char[length + 1];

// fill data with zeros
memset(data, 0, length + 1);

fread(data, 1, length, file);
fclose(file);
data[length] = '\0';

// parse the string
XMLNode* xmlnode = XMLNode::convertStringToXMLNode(data);
delete[] data;
return xmlnode;
}

bool setAnnotation(const std::string& sbmlFile, const std::string& metaId,
const std::string& annotationFile, const std::string& outputFile)
{
Expand All @@ -52,7 +29,7 @@ bool setAnnotation(const std::string& sbmlFile, const std::string& metaId,
return false;
}

XMLNode* annotation = parseAnnotation(annotationFile);
XMLNode* annotation = XMLNode::readXMLNodeFromFile(annotationFile);
if (!annotation)
{
cerr << "the annotation could not be parsed from file: " << annotationFile << "." << std::endl;
Expand Down
8 changes: 0 additions & 8 deletions src/sbml/xml/ExpatParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,6 @@ ExpatParser::parseNext ()
return false;
}

// catch whether an xml declaration has been found
// Expat does not report a missing xml declaration
if (!mHandler.hasXMLDeclaration())
{
reportError(MissingXMLDecl, "", 1, 1);
return false;
}

if ( !error() && done )
{
mHandler.endDocument();
Expand Down
28 changes: 9 additions & 19 deletions src/sbml/xml/XMLNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <sbml/xml/XMLInputStream.h>
#include <sbml/xml/XMLOutputStream.h>
#include <sbml/xml/XMLConstructorException.h>
#include <sbml/xml/XMLErrorLog.h>
/** @endcond */

#include <sbml/xml/XMLNode.h>
Expand Down Expand Up @@ -675,28 +676,17 @@ XMLNode*
XMLNode::readXMLNodeFromFile(const std::string& filename)
{
XMLNode * xmlnode = NULL;
if (util_file_exists(filename.c_str()))
if (!util_file_exists(filename.c_str()))
{
// read file into memory
FILE* file = fopen(filename.c_str(), "r");
fseek(file, 0, SEEK_END);
long length = ftell(file);

fseek(file, 0, SEEK_SET);
char* data = new char[length + 1];

// fill data with zeros
memset(data, 0, length + 1);
return xmlnode;
}
XMLErrorLog * log = new XMLErrorLog();

fread(data, 1, length, file);
fclose(file);
data[length] = '\0';
XMLInputStream stream(filename.c_str(), true, "", log);
if (!stream.peek().isStart())
return NULL;

// parse the string
xmlnode = XMLNode::convertStringToXMLNode(data);
delete[] data;
}
return xmlnode;
return new XMLNode(stream);
}

/** @cond doxygenLibsbmlInternal */
Expand Down
26 changes: 26 additions & 0 deletions src/sbml/xml/XMLNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,32 @@ class LIBLAX_EXTERN XMLNode : public XMLToken
static XMLNode* convertStringToXMLNode(const std::string& xmlstr,
const XMLNamespaces* xmlns = NULL);


/**
* Returns an XMLNode which is readfrom a file containing XML
* content.
*
* The XML namespace must be defined using argument @p xmlns if the
* corresponding XML namespace attribute is not part of the file of the
* first argument.
*
* @param filename string name of file to be read to a XML node.
* @param xmlns XMLNamespaces the namespaces to set (default value is @c NULL).
*
* @note The caller owns the returned XMLNode and is reponsible for
* deleting it. The returned XMLNode object is a dummy root (container)
* XMLNode if the top-level element in the given XML string is NOT
* <code>&lt;html&gt;</code>, <code>&lt;body&gt;</code>,
* <code>&lt;annotation&gt;</code>, or <code>&lt;notes&gt;</code>. In
* the dummy root node, each top-level element in the given XML string is
* contained as a child XMLNode. XMLToken::isEOF() can be used to
* identify if the returned XMLNode object is a dummy node.
*
* @return a XMLNode which is read from file @p filename. If the
* conversion failed, this method returns @c NULL.
*
* @ifnot hasDefaultArgs @htmlinclude warn-default-args-in-docs.html @endif@~
*/
static XMLNode* readXMLNodeFromFile(const std::string& filename);


Expand Down
6 changes: 0 additions & 6 deletions src/sbml/xml/XercesParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,6 @@ XercesParser::parseNext ()
result = false;
}

if (! static_cast <XercesReader *> (mReader) ->hasXMLDeclaration())
{
reportError(MissingXMLDecl, "", 1, 1);
return false;
}

return result;
}

Expand Down
47 changes: 46 additions & 1 deletion src/sbml/xml/test/TestRunner.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@
* and also available online as http://sbml.org/software/libsbml/license.html
* ---------------------------------------------------------------------- -->*/

#include <string.h>
#include <check.h>
#include <stdlib.h>
#include <string.h>

#include <sbml/common/extern.h>
#include <sbml/util/memory.h>

#ifdef LIBSBML_USE_VLD
#include <vld.h>
#endif

#if defined(__cplusplus)
LIBSBML_CPP_NAMESPACE_USE
CK_CPPSTART
#endif

Expand All @@ -66,10 +71,48 @@ Suite *create_suite_XMLOutputStream (void);
Suite *create_suite_XMLAttributes_C (void);
Suite *create_suite_XMLExceptions (void);

/**
* Global.
*
* Declared extern in TestAnnotation suite.
*/
char *TestDataDirectory;


/**
* Sets TestDataDirectory for the the TestAnnotation suite.
*
* For Automake's distcheck target to work properly, TestDataDirectory must
* begin with the value of the environment variable SRCDIR.
*/
void
setTestDataDirectory (void)
{
char *srcdir = getenv("srcdir");
size_t length = (srcdir == NULL) ? 0 : strlen(srcdir);


/**
* strlen("/test-data/") = 11 + 1 (for NULL) = 12
*/
TestDataDirectory = (char *) safe_calloc( length + 12, sizeof(char) );

if (srcdir != NULL)
{
strcpy(TestDataDirectory, srcdir);
strcat(TestDataDirectory, "/");
}

strcat(TestDataDirectory, "test-data/");
}


int
main (int argc, char* argv[])
{
int num_failed = 0;
setTestDataDirectory();
// SRunner *runner = srunner_create(create_suite_XMLNode());
SRunner *runner = srunner_create(create_suite_XMLAttributes());
srunner_add_suite(runner, create_suite_CopyAndClone());

Expand Down Expand Up @@ -97,6 +140,8 @@ main (int argc, char* argv[])

srunner_free(runner);

free(TestDataDirectory);

return num_failed;
}

Expand Down
109 changes: 109 additions & 0 deletions src/sbml/xml/test/TestXMLNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ LIBSBML_CPP_NAMESPACE_USE

CK_CPPSTART

extern char *TestDataDirectory;

static bool
equals(const char* expected, const char* actual)
{
if (!strcmp(expected, actual)) return true;

printf("\nStrings are not equal:\n");
printf("Expected:\n[%s]\n", expected);
printf("Actual:\n[%s]\n", actual);

return false;
}


START_TEST (test_XMLNode_getIndex)
{
const char* xmlstr = "<annotation>\n"
Expand Down Expand Up @@ -1640,6 +1655,97 @@ END_TEST
//END_TEST
//


START_TEST(test_read_from_file)
{
char *filename = safe_strcat(TestDataDirectory, "just_annotation.xml");
XMLNode *node = XMLNode::readXMLNodeFromFile(filename);

fail_unless(node != NULL);

fail_unless(node->getNumChildren() == 1);

const XMLNode_t* rdf = XMLNode_getChild(node, 0);

fail_unless(!strcmp(XMLNode_getName(rdf), "RDF"));
fail_unless(!strcmp(XMLNode_getPrefix(rdf), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(rdf), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(rdf) == 1);

const XMLNode_t* desc = XMLNode_getChild(rdf, 0);

fail_unless(!strcmp(XMLNode_getName(desc), "Description"));
fail_unless(!strcmp(XMLNode_getPrefix(desc), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(desc), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(desc) == 1);

const XMLNode_t * is1 = XMLNode_getChild(desc, 0);
fail_unless(!strcmp(XMLNode_getName(is1), "is"));
fail_unless(!strcmp(XMLNode_getPrefix(is1), "bqbiol"));
fail_unless(XMLNode_getNumChildren(is1) == 1);

const XMLNode_t * Bag = XMLNode_getChild(is1, 0);
fail_unless(!strcmp(XMLNode_getName(Bag), "Bag"));
fail_unless(!strcmp(XMLNode_getPrefix(Bag), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(Bag), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(Bag) == 4);

const XMLNode_t * li = XMLNode_getChild(Bag, 0);
fail_unless(!strcmp(XMLNode_getName(li), "li"));
fail_unless(!strcmp(XMLNode_getPrefix(li), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(li), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(li->getAttrValue(0) == "http://identifiers.org/chebi/CHEBI:59789");
fail_unless(XMLNode_getNumChildren(li) == 0);

const XMLNode_t * hasProp1 = XMLNode_getChild(Bag, 1);
fail_unless(!strcmp(XMLNode_getName(hasProp1), "hasProperty"));
fail_unless(!strcmp(XMLNode_getPrefix(hasProp1), "bqbiol"));
fail_unless(XMLNode_getNumChildren(hasProp1) == 1);

const XMLNode_t * Bag1 = XMLNode_getChild(hasProp1, 0);
fail_unless(!strcmp(XMLNode_getName(Bag1), "Bag"));
fail_unless(!strcmp(XMLNode_getPrefix(Bag1), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(Bag1), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(Bag1) == 1);

const XMLNode_t * li_Bag1 = XMLNode_getChild(Bag1, 0);
fail_unless(!strcmp(XMLNode_getName(li_Bag1), "li"));
fail_unless(!strcmp(XMLNode_getPrefix(li_Bag1), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(li_Bag1), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(li_Bag1) == 0);


const XMLNode_t * li2 = XMLNode_getChild(Bag, 2);
fail_unless(!strcmp(XMLNode_getName(li2), "li"));
fail_unless(!strcmp(XMLNode_getPrefix(li2), "rdf"));
fail_unless(!strcmp(XMLNode_getURI(li2), "http://www.w3.org/1999/02/22-rdf-syntax-ns#"));
fail_unless(XMLNode_getNumChildren(li2) == 0);

free(filename);
delete(node);
}
END_TEST

START_TEST(test_read_from_file2)
{
const char * expected = "<thing>\n"
" <child att=\"yes\"/>\n"
"</thing>";

char *filename = safe_strcat(TestDataDirectory, "random.xml");
XMLNode *node = XMLNode::readXMLNodeFromFile(filename);

fail_unless(node != NULL);

std::string random = XMLNode::convertXMLNodeToString(node);

fail_unless(equals(expected, random.c_str()));

safe_free(filename);
delete(node);
}
END_TEST

Suite *
create_suite_XMLNode (void)
{
Expand All @@ -1665,6 +1771,9 @@ create_suite_XMLNode (void)
tcase_add_test( tcase, test_XMLNode_namespace_set_clear );
tcase_add_test( tcase, test_XMLNode_attribute_add_remove);
tcase_add_test( tcase, test_XMLNode_attribute_set_clear);
tcase_add_test( tcase, test_read_from_file);
tcase_add_test(tcase, test_read_from_file2);

suite_add_tcase(suite, tcase);

return suite;
Expand Down
23 changes: 23 additions & 0 deletions src/sbml/xml/test/test-data/just_annotation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<annotation>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:vcard4="http://www.w3.org/2006/vcard/ns#" xmlns:bqbiol="http://biomodels.net/biology-qualifiers/" xmlns:bqmodel="http://biomodels.net/model-qualifiers/">
<rdf:Description rdf:about="#metaid_0000036">
<bqbiol:is>
<rdf:Bag>
<rdf:li rdf:resource="http://identifiers.org/chebi/CHEBI:59789"/>
<bqbiol:hasProperty>
<rdf:Bag>
<rdf:li rdf:resource="http://amas/match_score/by_name/1.0"/>
</rdf:Bag>
</bqbiol:hasProperty>
<rdf:li rdf:resource="http://identifiers.org/chebi/CHEBI:15414"/>
<bqbiol:hasProperty>
<rdf:Bag>
<rdf:li rdf:resource="http://amas/match_score/by_name/1.0"/>
</rdf:Bag>
</bqbiol:hasProperty>
</rdf:Bag>
</bqbiol:is>
</rdf:Description>
</rdf:RDF>
</annotation>
4 changes: 4 additions & 0 deletions src/sbml/xml/test/test-data/random.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0"?>
<thing>
<child att="yes"/>
</thing>