Project

Profile

Help

Possible memory leaks » SaxonCMemoryTest.cpp

Paul Greve, 2025-01-23 13:25

 
#include <sys/types.h>
#include <sys/stat.h>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

#include "SaxonProcessor.h"
#include "XdmValue.h"
#include "XdmItem.h"
#include "XdmNode.h"

int main(int argc, char* argv[])
{
const char *xml =
"<family>\n"
"<firstChild name=\"0\">\n"
"<firstGrandChild name=\"0.1\"/>\n"
"</firstChild>\n"
"<secondChild name=\"1\">\n"
"<secondGrandChild name=\"1.0\">\n"
"<firstGreatGrandChild name=\"1.0.0\">\n"
"</firstGreatGrandChild>\n"
"<secondGreatGrandChild name=\"1.0.1\">\n"
"</secondGreatGrandChild>\n"
"<thirdGreatGrandChild name=\"1.0.2\">\n"
"</thirdGreatGrandChild>\n"
"</secondGrandChild>\n"
"<thirdGrandChild name=\"1.1\">\n"
"</thirdGrandChild>\n"
"</secondChild>\n"
"<thirdChild name=\"2\"/>\n"
"</family>";

printf("Original XML:\n");
printf("%s\n\n", xml);

SaxonProcessor *processor = new SaxonProcessor(true);
processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation",
"/usr/lib/saxon-j/saxon-license.lic");
processor->setConfigurationProperty("http://saxon.sf.net/feature/multipleSchemaImports", "on");
processor->setConfigurationProperty("xsdversion", "1.0");

if (processor == nullptr)
{
fprintf(stderr, "Saxon processor creation failed");
return -1;
}

XdmNode *dom = nullptr;

#if 1
// parse the xml doc
try
{
dom = processor->parseXmlFromString(xml);

if (dom == nullptr)
{
fprintf(stderr, "Document parse failed");
delete processor;
return -1;
}
}
catch(SaxonApiException &e)
{
fprintf(stderr, "Document parse failed: %s\n", e.what());
delete processor;
return -1;
}
#endif

#if 1
// Run an XLST transform to serialize the DOM back out to memory
Xslt30Processor *xsltProcessor = processor->newXslt30Processor();

if (xsltProcessor == nullptr)
{
fprintf(stderr, "XSLT processor creation failed");
delete dom;
delete processor;
return -1;
}

// compile the stylesheet
const char *styleSheet =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
"<xsl:output method=\"xml\" version=\"1.0\" encoding=\"UTF-8\" indent=\"no\"/>"
"<xsl:template match=\"@*|node()\">"
"<xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy>"
"</xsl:template></xsl:stylesheet>";

XsltExecutable *xslt = nullptr;
try
{
xslt = xsltProcessor->compileFromString(styleSheet);

if (xslt == nullptr)
{
fprintf(stderr, "Stylesheet compilation failed");
delete xsltProcessor;
delete dom;
delete processor;
return -1;
}
}
catch(SaxonApiException &e)
{
fprintf(stderr, "Stylesheet compilation failed: %s\n", e.what());
delete xsltProcessor;
delete dom;
delete processor;
return -1;
}

// Run the transform
try
{
const char *transformedXml = xslt->transformToString(dom);

if (transformedXml != nullptr)
{
printf("Transformed XML:\n");
printf("%s\n", transformedXml);

operator delete((char *)transformedXml);
}
}
catch(SaxonApiException &e)
{
fprintf(stderr, "\nXML transformation failed: %s\n", e.what());
}

delete xslt;
delete xsltProcessor;
#endif

if (dom != nullptr)
{
delete dom;
}
delete processor;

SaxonProcessor::release();

return 0;
}
(1-1/2)