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

View File

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

40
test.c
View File

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