transform.c 1.7 KB

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