autodetect graphml format

This commit is contained in:
Alexey Fedoseev
2024-01-06 15:34:51 +03:00
parent e8e6c7229c
commit f0a0e416ae
3 changed files with 50 additions and 27 deletions

View File

@@ -17,6 +17,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#include <stdlib.h>
@@ -27,6 +28,7 @@
#include "cyberiadaml.h"
#define GRAPHML_NAMESPACE_URI "http://graphml.graphdrawing.org/xmlns"
#define GRAPHML_NAMESPACE_URI_YED "http://www.yworks.com/xml/graphml"
#define GRAPHML_GRAPHML_ELEMENT "graphml"
#define GRAPHML_BERLOGA_SCHEMENAME_ATTR "SchemeName"
#define GRAPHML_GRAPH_ELEMENT "graph"
@@ -766,22 +768,35 @@ static int cyberiada_decode_cyberiada_xml(xmlNode* root, CyberiadaSM* sm)
return CYBERIADA_FORMAT_ERROR;
}
static int cyberiada_check_graphml_ns(xmlNode* root)
static int cyberiada_check_graphml_ns(xmlNode* root, CyberiadaXMLFormat* format)
{
xmlNs* ns;
int graphml_found = 0;
int yed_found = 0;
if (!root || !(ns = root->nsDef)) {
ERROR("bad GraphML XML NS: null ns ptr\n");
return CYBERIADA_XML_ERROR;
}
do {
if (strcmp((const char*)ns->href, GRAPHML_NAMESPACE_URI) == 0) {
return CYBERIADA_NO_ERROR;
graphml_found = 1;
} else if (strcmp((const char*)ns->href, GRAPHML_NAMESPACE_URI_YED) == 0) {
yed_found = 1;
}
ns = ns->next;
} while (ns);
if (!graphml_found) {
ERROR("no GraphML XML NS href\n");
return CYBERIADA_XML_ERROR;
}
if (*format == cybxmlUnknown && yed_found) {
*format = cybxmlYED;
} else if (*format == cybxmlYED && !yed_found) {
ERROR("no GraphML YED NS href\n");
return CYBERIADA_XML_ERROR;
}
return CYBERIADA_NO_ERROR;
}
int cyberiada_read_sm(CyberiadaSM* sm, const char* filename, CyberiadaXMLFormat format)
{
@@ -806,8 +821,8 @@ int cyberiada_read_sm(CyberiadaSM* sm, const char* filename, CyberiadaXMLFormat
}
/* check whether the xml is graphml */
if (cyberiada_check_graphml_ns(root)) {
ERROR("error: no graphml namespace in %s\n", filename);
if (cyberiada_check_graphml_ns(root, &format)) {
ERROR("error: no valid graphml namespace in %s\n", filename);
return CYBERIADA_XML_ERROR;
}
@@ -816,8 +831,8 @@ int cyberiada_read_sm(CyberiadaSM* sm, const char* filename, CyberiadaXMLFormat
} else if (format == cybxmlCyberiada) {
res = cyberiada_decode_cyberiada_xml(root, sm);
} else {
ERROR("error: unsupported GraphML format %d of file %s\n",
format, filename);
ERROR("error: unsupported GraphML format of file %s\n",
filename);
return CYBERIADA_XML_ERROR;
}

View File

@@ -17,6 +17,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/
*
* ----------------------------------------------------------------------------- */
#ifndef __CYBERIADA_ML_H
@@ -98,7 +99,8 @@ typedef struct {
/* SM GraphML supported formats */
typedef enum {
cybxmlYED = 0,
cybxmlCyberiada
cybxmlCyberiada,
cybxmlUnknown
} CyberiadaXMLFormat;
/* -----------------------------------------------------------------------------

14
test.c
View File

@@ -39,7 +39,7 @@ unsigned int format_count = sizeof(formats) / sizeof(char*);
static void print_usage(const char* name)
{
unsigned int i;
fprintf(stderr, "%s -t <format> <path-to-graphml-file>\n\n", name);
fprintf(stderr, "%s [-t <format>] <path-to-graphml-file>\n\n", name);
fprintf(stderr, "Supported formats:\n");
for (i = 0; i < format_count; i++) {
fprintf(stderr, " %-20s %s\n", formats[i], format_names[i]);
@@ -51,13 +51,13 @@ int main(int argc, char** argv)
{
char *filename;
char *format_str = "";
CyberiadaXMLFormat format;
CyberiadaXMLFormat format = cybxmlUnknown;
unsigned int i;
int res;
CyberiadaSM sm;
if (argc != 4 ||
strcmp(argv[1], "-t") != 0) {
if (argc == 4) {
if (strcmp(argv[1], "-t") != 0) {
print_usage(argv[0]);
return 1;
}
@@ -74,6 +74,12 @@ int main(int argc, char** argv)
return 2;
}
filename = argv[3];
} else if (argc == 2) {
filename = argv[1];
} else {
print_usage(argv[0]);
return 1;
}
if ((res = cyberiada_read_sm(&sm, filename, format)) != CYBERIADA_NO_ERROR) {
fprintf(stderr, "error while reading %s file: %d\n",
filename, res);