-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.c
57 lines (49 loc) · 1.67 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
#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 "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(XMLBuff *infile) {
xmlDocPtr doc, result;
doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
Stopif(!doc, return 0, "Unable to read %s!\n", infile->name);
result = xsltApplyStylesheet(stylesheet, doc, NULL);
Stopif(!result, return 0, "Transformation failed for %s!\n", infile->name);
struct fileInfo *fi = newFileInfo(infile->name);
char *outFile = malloc(fi->nameLength+6);
snprintf(outFile, fi->nameLength+6, "%s.html", fi->name);
freeFileInfo(fi);
FILE *fp;
fp = fopen(outFile, "w");
Stopif(xsltSaveResultToFile(fp, result, stylesheet) < 0, return 0, "Nothing saved to %s!\n", outFile);
fclose(fp);
free(outFile);
xmlFreeDoc(result);
xmlFreeDoc(doc);
return 1;
}
void transformCleanup() {
xsltFreeStylesheet(stylesheet);
xsltCleanupGlobals();
xmlCleanupParser();
}