transform.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #define _GNU_SOURCE //asks stdio.h to include asprintf
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <libxml/xmlmemory.h>
  5. #include <libxml/debugXML.h>
  6. #include <libxml/HTMLtree.h>
  7. #include <libxml/xmlIO.h>
  8. #include <libxml/xinclude.h>
  9. #include <libxml/catalog.h>
  10. #include <libxslt/xslt.h>
  11. #include <libxslt/xsltInternals.h>
  12. #include <libxslt/transform.h>
  13. #include <libxslt/xsltutils.h>
  14. #include "transform.h"
  15. extern int xmlLoadExtDtdDefaultValue;
  16. xmlDocPtr stylesheetDoc;
  17. xsltStylesheetPtr stylesheet;
  18. int transformLoad(const char *stylesheetData, size_t stylesheetSize) {
  19. xmlLoadExtDtdDefaultValue = 1;
  20. xmlSubstituteEntitiesDefault(1);
  21. stylesheetDoc = xmlReadMemory(stylesheetData, stylesheetSize, NULL, NULL, 0);
  22. Stopif(!stylesheetDoc, return 0, "Unable to read stylesheet !\n");
  23. stylesheet = xsltParseStylesheetDoc(stylesheetDoc);
  24. Stopif(!stylesheet, return 0, "Unable to parse stylesheet !\n");
  25. return 1;
  26. }
  27. int transformXML(XMLBuff *infile) {
  28. xmlDocPtr doc, result;
  29. doc = xmlReadMemory(infile->data, infile->size, infile->name, NULL, 0);
  30. Stopif(!doc, return 0, "Unable to read %s!\n", infile->name);
  31. result = xsltApplyStylesheet(stylesheet, doc, NULL);
  32. Stopif(!result, return 0, "Transformation failed for %s!\n", infile->name);
  33. char *outFile;
  34. struct fileInfo *fi = newFileInfo(infile->name);
  35. asprintf(&outFile, "%s.html", fi->name);
  36. freeFileInfo(fi);
  37. FILE *fp;
  38. fp = fopen(outFile, "w");
  39. Stopif(xsltSaveResultToFile(fp, result, stylesheet) < 0, return 0, "Nothing saved to %s!\n", outFile);
  40. fclose(fp);
  41. free(outFile);
  42. xmlFreeDoc(result);
  43. xmlFreeDoc(doc);
  44. return 1;
  45. }
  46. void transformCleanup() {
  47. xsltFreeStylesheet(stylesheet);
  48. xsltCleanupGlobals();
  49. xmlCleanupParser();
  50. }