Here is a modified version that defines and installs an EntityResolver. You
will need to modify the resolver to suit; this one just returns a
URLInputSource for the URL:
class MyResolver : public EntityResolver {
public:
InputSource* resolveEntity (const XMLCh* const publicId,
const XMLCh* const systemId)
{
XMLURL URL(systemId);
return new URLInputSource(URL);
}
};
Any rewriting that you want to take place can be performed on the URL
object.
> -----Original Message-----
> From: David Fishburn [mailto:[EMAIL PROTECTED]
> Sent: 29 July 2003 16:05
> To: 'Mark Weaver'; [email protected]
> Subject: RE: Xsl:include support - how to enable it?
>
>
>
> Right, but where (what directory), do I create this class in:
> entity resolver class.
>
> Then what do I need to recompile to enable it?
>
> Thanks,
> Dave
>
> -----Original Message-----
> From: Mark Weaver [mailto:[EMAIL PROTECTED]
> Sent: Monday, July 28, 2003 8:52 AM
> To: [email protected]
> Subject: RE: Xsl:include support - how to enable it?
>
>
>
> > After that, I am not sure how to enable the EntityResolver.
> > I am using the following sample that I have based everything on:
> > C:\OpenSrc\xalan\C\xml-xalan\c\samples\XalanTransformerCallback
> >
> > So I am not sure where I need to put the code to enable this. I am
> > putting it in my XalanTransformaterCallback.cpp file?
> >
> >
> You just need to call XalanTransformer::setEntityResolver with a pointer
> to your entity resolver class.
>
> Mark
>
>
>
>
#include <xercesc/util/PlatformUtils.hpp>
#include <XalanTransformer/XalanTransformer.hpp>
#include <cstdio>
#if defined(XALAN_CLASSIC_IOSTREAMS)
#include <iostream.h>
#else
#include <iostream>
#endif
#include <xercesc/sax/EntityResolver.hpp>
#include <xercesc/sax/InputSource.hpp>
#include <xercesc/framework/URLInputSource.hpp>
using namespace xercesc;
class MyResolver : public EntityResolver {
public:
InputSource* resolveEntity (const XMLCh* const publicId,
const XMLCh* const systemId)
{
XMLURL URL(systemId);
return new URLInputSource(URL);
}
};
// This is a simple class that illustrates how XalanTransformer's "callback" API
// is used. This example just abstracts writing data to a FILE*, but other
// actions are possible.
class CallbackHandler
{
public:
CallbackHandler(FILE* theFile) :
m_file(theFile)
{
assert(m_file != 0);
}
CallbackSizeType
write(
const char* theData,
CallbackSizeType theLength)
{
return fwrite(theData, sizeof(char), theLength, m_file);
}
void
flush()
{
fflush(m_file);
}
private:
FILE* const m_file;
};
// These functions need to have C linkage, so surround them with an extern C
block...
extern "C"
{
// This is the write callback function, which casts the handle
// to the appropriate type, then calls the write() member function
// on the CallbackHandler class.
CallbackSizeType
writeCallback(
const char* theData,
CallbackSizeType theLength,
void* theHandle)
{
#if defined(XALAN_OLD_STYLE_CASTS)
return ((CallbackHandler*)theHandle)->write(theData, theLength);
#else
return reinterpret_cast<CallbackHandler*>(theHandle)->write(theData,
theLength);
#endif
}
// This is the flush callback function, which casts the handle
// to the appropriate type, then calls the flush() member function
// on the CallbackHandler class.
void
flushCallback(void* theHandle)
{
#if defined(XALAN_OLD_STYLE_CASTS)
((CallbackHandler*)theHandle)->flush();
#else
reinterpret_cast<CallbackHandler*>(theHandle)->flush();
#endif
}
}
int
doTransform(
const char* theXMLFile,
const char* theXSLFile,
FILE* theOutputFile)
{
XALAN_USING_STD(cerr)
XALAN_USING_STD(endl)
XALAN_USING_XALAN(XalanTransformer)
// Create a XalanTransformer...
std::auto_ptr<EntityResolver> entityResolver(new MyResolver);
XalanTransformer theXalanTransformer;
theXalanTransformer.setEntityResolver(entityResolver.get());
// Create an instance of the class we wrote to handle
// the callbacks...
CallbackHandler theHandler(theOutputFile);
// Do the transform...
const int theResult = theXalanTransformer.transform(
theXMLFile,
theXSLFile,
&theHandler,
writeCallback,
flushCallback);
if(theResult != 0)
{
cerr << "XalanError: " << theXalanTransformer.getLastError() <<
endl;
}
return theResult;
}
int
main(
int argc,
const char* argv[])
{
XALAN_USING_STD(cerr)
XALAN_USING_STD(cout)
XALAN_USING_STD(endl)
if (argc < 3 || argc > 4)
{
cerr << "Usage: XalanTransformerCallback XMLFileName
XSLFileName [OutFileName]" << endl;
return -1;
}
int theResult = -1;
try
{
XALAN_USING_XERCES(XMLPlatformUtils)
XALAN_USING_XALAN(XalanTransformer)
// Call the static initializer for Xerces.
XMLPlatformUtils::Initialize();
// Initialize Xalan.
XalanTransformer::initialize();
if (argc == 3)
{
// No output file, so use stdout...
theResult = doTransform(argv[1], argv[2], stdout);
}
else
{
// Output file specified, so try to open it...
FILE* const theOutputFile = fopen(argv[3], "w");
if (theOutputFile == 0)
{
cerr << "Error: " << "Unable to open output
file " << argv[3] << endl;
}
else
{
theResult = doTransform(argv[1], argv[2],
theOutputFile);
fclose(theOutputFile);
}
}
// Terminate Xalan...
XalanTransformer::terminate();
// Terminate Xerces...
XMLPlatformUtils::Terminate();
// Clean up the ICU, if it's integrated...
XalanTransformer::ICUCleanUp();
}
catch(...)
{
cerr << "An unknown error occurred!" << endl;
}
return theResult;
}