New parsing module

This commit is contained in:
Adrien Bourmault 2021-09-16 21:30:43 +02:00
parent f6ad08702f
commit 89a93152d5
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
2 changed files with 168 additions and 0 deletions

31
include/parsing.h Normal file
View File

@ -0,0 +1,31 @@
//=-------------------------------------------------------------------------=//
// XML parsing module //
// //
// Copyright © 2021 The Gem-graph Project //
// //
// This file is part of gem-graph. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program 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 Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#pragma once
#ifndef BASE_H
#include "../include/base.h"
#endif
/* -------------------------------------------------------------------------- */
int ParseModelXML(Model_t*);
/* -------------------------------------------------------------------------- */

137
src/parsing.c Normal file
View File

@ -0,0 +1,137 @@
//=-------------------------------------------------------------------------=//
// XML parsing module //
// //
// Copyright © 2021 The Gem-graph Project //
// //
// This file is part of gem-graph. //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program 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 Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//=-------------------------------------------------------------------------=//
#include "../include/base.h"
#include <string.h>
#include <stdlib.h>
#include <libxml2/libxml/xmlmemory.h>
#include <libxml2/libxml/parser.h>
/* -------------------------------------------------------------------------- */
static int parseIdentityXML(Model_t*, xmlDocPtr, xmlNodePtr);
static int parseIdentityNameXML(Model_t*, xmlDocPtr, xmlNodePtr);
// Table of all parsers
struct parserTableXML_t {
const xmlChar *tag;
int (*execute) (Model_t*, xmlDocPtr, xmlNodePtr);
} typedef ParserTableXML_t;
static ParserTableXML_t toplevelParserTableXML[] =
{
{(const xmlChar *)"identity", parseIdentityXML},
};
static ParserTableXML_t identityParserTableXML[] =
{
{(const xmlChar *)"name", parseIdentityChildren},
{(const xmlChar *)"author", parseIdentityChildren},
{(const xmlChar *)"author_id", parseIdentityChildren},
{(const xmlChar *)"date", parseIdentityChildren},
{(const xmlChar *)"version", parseIdentityChildren},
};
/* -------------------------------------------------------------------------- */
static int parseIdentityNameXML (Model_t *model, xmlDocPtr doc,
xmlNodePtr currentNode)
{
xmlChar *key;
key = xmlNodeListGetString(doc, currentNode->xmlChildrenNode, 1);
printLog("key: %s\n", key);
xmlFree(key);
return 0;
}
static int parseIdentityXML (Model_t *model, xmlDocPtr doc,
xmlNodePtr currentNode)
{
xmlChar *key;
currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) {
for (int i = 0; i < LEN(identityParserTableXML); i++) {
if ((!xmlStrcmp(currentNode->name,
identityParserTableXML[i].tag))) {
identityParserTableXML[i].execute(model,
doc,
currentNode);
}
}
currentNode = currentNode->next;
}
return 0;
}
int ParseModelXML(Model_t *model)
{
printLog("Parsing model %s\n", model->name);
xmlDocPtr xmlDocument;
xmlNodePtr currentNode;
xmlDocument = xmlParseFile(model->filename);
if (xmlDocument == NULL) {
printLog("Can't parse model file at '%s'.\n", model->filename);
return -1;
}
currentNode = xmlDocGetRootElement(xmlDocument);
if (currentNode == NULL) {
printLog("Invalid model file at '%s', document empty !\n",
model->filename);
xmlFreeDoc(xmlDocument);
return -2;
}
if (xmlStrcmp(currentNode->name, (const xmlChar *) "gem-graph-model")) {
printLog("Invalid model file at '%s', "
"root node is not <gem-graph-model> !\n",
model->filename);
xmlFreeDoc(xmlDocument);
return -3;
}
currentNode = currentNode->xmlChildrenNode;
while (currentNode != NULL) {
for (int i = 0; i < LEN(toplevelParserTableXML); i++) {
if ((!xmlStrcmp(currentNode->name,
toplevelParserTableXML[i].tag))) {
toplevelParserTableXML[i].execute(model,
xmlDocument,
currentNode);
}
}
currentNode = currentNode->next;
}
xmlFreeDoc(xmlDocument);
// Interpret what needs to be
//model->date = strtol(date, NULL, 0);
// validate when we're finished
// model->validated = true;
return 0;
}