X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=parse-xml.c;h=82b10a47e741113428f20e7c671ee595e1442885;hb=d7482356fd8feeb2ab2d3fc8106d0a6c2a5ee0cf;hp=ddf93a4b1ad58ff45fdc4790dfe5d5ad005f82dd;hpb=350462949d2dc205355e5c94ccaacf83a0775257;p=ext%2Fsubsurface.git diff --git a/parse-xml.c b/parse-xml.c index ddf93a4..82b10a4 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -3,6 +3,7 @@ #include #include #include +#include #define __USE_XOPEN #include #include @@ -109,7 +110,6 @@ static int cylinder_index; static enum import_source { UNKNOWN, LIBDIVECOMPUTER, - SUUNTO, UEMIS, DIVINGLOG, UDDF, @@ -643,26 +643,6 @@ static void try_to_fill_sample(struct sample *sample, const char *name, char *bu nonmatch("sample", name, buf); } -/* - * Crazy suunto xml. Look at how those o2/he things match up. - */ -static int suunto_dive_match(struct dive **divep, const char *name, int len, char *buf) -{ - struct dive *dive = *divep; - - return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) || - MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) || - MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) || - MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) || - MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) || - MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) || - MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) || - MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) || - MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) || - MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) || - 0; -} - static const char *country, *city; static void divinglog_place(char *place, void *_location) @@ -977,11 +957,6 @@ static void try_to_fill_dive(struct dive **divep, const char *name, char *buf) start_match("dive", name, buf); switch (import_source) { - case SUUNTO: - if (suunto_dive_match(divep, name, len, buf)) - return; - break; - case UEMIS: if (uemis_dive_match(divep, name, len, buf)) return; @@ -1049,7 +1024,8 @@ static void try_to_fill_dive(struct dive **divep, const char *name, char *buf) return; if (MATCH(".buddy", utf8_string, &dive->buddy)) return; - + if (MATCH(".rating", get_index, &dive->rating)) + return; if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size)) return; if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure)) @@ -1179,7 +1155,7 @@ static void sanitize_cylinder_type(cylinder_type_t *type) if (!type->size.mliter) return; - if (input_units.volume == CUFT || import_source == SUUNTO) { + if (input_units.volume == CUFT) { /* confusing - we don't really start from ml but millicuft !*/ volume_of_air = cuft_to_l(type->size.mliter); atm = to_ATM(type->workingpressure); /* working pressure in atm */ @@ -1347,12 +1323,6 @@ static void visit(xmlNode *n) traverse(n->children); } -static void suunto_importer(void) -{ - import_source = SUUNTO; - input_units = SI_units; -} - static void uemis_importer(void) { import_source = UEMIS; @@ -1404,7 +1374,6 @@ static struct nesting { { "P", sample_start, sample_end }, /* Import type recognition */ - { "SUUNTO", suunto_importer }, { "Divinglog", DivingLog_importer }, { "pre_dive", uemis_importer }, { "dives", uemis_importer }, @@ -1484,14 +1453,76 @@ void parse_xml_init(void) } #ifdef XSLT + +/* Maybe we'll want a environment variable that can override this.. */ +static const char *xslt_path = XSLT ":xslt:."; + +static xsltStylesheetPtr try_get_stylesheet(const char *path, int len, const char *name) +{ + xsltStylesheetPtr ret; + int namelen = strlen(name); + char *filename = malloc(len+1+namelen+1); + + if (!filename) + return NULL; + + memcpy(filename, path, len); + filename[len] = G_DIR_SEPARATOR; + memcpy(filename + len + 1, name, namelen+1); + + ret = NULL; + if (!access(filename, R_OK)) + ret = xsltParseStylesheetFile(filename); + free(filename); + + return ret; +} + +static xsltStylesheetPtr get_stylesheet(const char *name) +{ + const char *path = xslt_path, *next; + + do { + int len; + xsltStylesheetPtr ret; + + next = strchr(path, ':'); + len = strlen(path); + if (next) { + len = next - path; + next++; + } + ret = try_get_stylesheet(path, len, name); + if (ret) + return ret; + } while ((path = next) != NULL); + + return NULL; +} + +static struct xslt_files { + const char *root; + const char *file; +} xslt_files[] = { + { "SUUNTO", "SuuntoSDM.xslt" }, + { "JDiveLog", "jdivelog2subsurface.xslt" }, + { NULL, } +}; + xmlDoc *test_xslt_transforms(xmlDoc *doc) { + struct xslt_files *info = xslt_files; xmlDoc *transformed; xsltStylesheetPtr xslt = NULL; xmlNode *root_element = xmlDocGetRootElement(doc); - if (strcasecmp(root_element->name, "JDiveLog") == 0) { + + while ((info->root) && (strcasecmp(root_element->name, info->root) != 0)) { + info++; + } + + if (info->root) { xmlSubstituteEntitiesDefault(1); - xslt = xsltParseStylesheetFile(XSLT G_DIR_SEPARATOR_S "jdivelog2subsurface.xslt"); + xslt = get_stylesheet(info->file); if (xslt == NULL) return doc; transformed = xsltApplyStylesheet(xslt, doc, NULL);