-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.c
60 lines (51 loc) · 1.92 KB
/
transform.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string.h>
#include <stdio.h>
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include "fileinfo.h"
#include "transform.h"
extern int xmlLoadExtDtdDefaultValue;
xmlDocPtr stylesheetDoc;
xsltStylesheetPtr stylesheet;
int transformLoad(const char *stylesheetData, size_t stylesheetSize) {
xmlLoadExtDtdDefaultValue = 1;
xmlSubstituteEntitiesDefault(1);
stylesheetDoc = xmlReadMemory(stylesheetData, stylesheetSize, NULL, NULL, 0);
Stopif(!stylesheetDoc, return 0, "Unable to read stylesheet !\n");
stylesheet = xsltParseStylesheetDoc(stylesheetDoc);
Stopif(!stylesheet, return 0, "Unable to parse stylesheet !\n");
return 1;
}
int transformXML(const XMLBuff *infile) {
xmlDocPtr doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
Stopif(!doc, return 0, "Unable to read %s!\n", infile->name);
xmlDocPtr result = xsltApplyStylesheet(stylesheet, doc, NULL);
Stopif(!result, return 0, "Transformation failed for %s!\n", infile->name);
struct fileInfo *fi = newFileInfo(infile->name);
Stopif(!fi, return 0, "Failed to establish fileinfo for %s!\n", infile->name);
int toPrint = fi->nameLength+5;
char *outFile = malloc(toPrint);
Stopif(toPrint <= snprintf(outFile, toPrint, "%s.txt", fi->name), return 0, "Failed to create output filename for %s!\n", infile->name);
freeFileInfo(fi);
FILE *fp = fopen(outFile, "w");
Stopif(!fp, return 0, "Can't open %s for writing!\n", outFile);
Stopif(0 > xsltSaveResultToFile(fp, result, stylesheet), return 0, "Nothing saved to %s!\n", outFile);
fclose(fp);
free(outFile);
xmlFreeDoc(result);
xmlFreeDoc(doc);
return 1;
}
void transformCleanup() {
xsltFreeStylesheet(stylesheet);
xsltCleanupGlobals();
xmlCleanupParser();
}