diff --git a/cyberiadaml.c b/cyberiadaml.c index c9cee56..2f2a765 100644 --- a/cyberiadaml.c +++ b/cyberiadaml.c @@ -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 @@ -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,21 +768,34 @@ 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); - ERROR("no GraphML XML NS href\n"); - return CYBERIADA_XML_ERROR; + 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; } diff --git a/cyberiadaml.h b/cyberiadaml.h index 38c7c4b..23f231e 100644 --- a/cyberiadaml.h +++ b/cyberiadaml.h @@ -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; /* ----------------------------------------------------------------------------- diff --git a/test.c b/test.c index 6f9d982..2f7064a 100644 --- a/test.c +++ b/test.c @@ -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 \n\n", name); + fprintf(stderr, "%s [-t ] \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,29 +51,35 @@ 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; + } + 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]); 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) { fprintf(stderr, "error while reading %s file: %d\n", filename, res);