You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Nithya Nirmal Vijayakumar <nv...@cs.indiana.edu> on 2006/03/29 21:46:34 UTC
Write transform output to a buffer
Hi,
I am a new user to Xalan-C++. I want to apply xsl transformation to xml
stored in a buffer and get the output into a buffer. The
StreamTransform.cpp example reads from a buffer and writes output to a
stream. I want to make this write to a buffer. I saw some discussion about
this in this group and tried following the advice of extending
Callbackhandler. But I am getting many compilation errors. Has anyone
already done this? Could you please post your code? Any help will be
greatly appreciated.
This is what I tried:
//Declaration of wrapper class
XALAN_USING_XALAN(XalanOutputStream)
class xalan_string_stream:public XalanOutputStream{
private:
char *output;
char *buffer;
unsigned cursor;
public:
xalan_string_stream(MemoryManagerType
XALAN_DEFAULT_CONSTRUCTOR_MEMORY_MGR,
size_type theBufferSize = eDefaultBufferSize,
size_type theTranscoderBlockSize = eDefaultTranscoderBlockSize,
bool fThrowTranscodeException = true){
buffer=new char[1024];
}
~xalan_string_stream(){}
void writeData (const char *input, size_type length){
bcopy(input,buffer,length);
}
void doFlush (){
}
char *get_result(){
return buffer;
}
};
//usage inside main
istrstream theXMLStream(theInputDocument,
strlen(theInputDocument));
istrstream theXSLStream(theStyleSheet, strlen(theStyleSheet));
XALAN_USING_XALAN(XalanDOMString)
XALAN_USING_XALAN(XalanOutputStreamPrintWriter)
XALAN_USING_XALAN(XSLTResultTarget)
XALAN_USING_XALAN(XSLTInputSource)
XSLTInputSource inputSource(&theXSLStream);
inputSource.setSystemId(XalanDOMString("foo").c_str());
const xalan_string_stream *outputStream = new xalan_string_stream();
XalanOutputStreamPrintWriter *printWriter=new
XalanOutputStreamPrintWriter((XalanOutputStream &)outputStream, false);
XSLTResultTarget target(printWriter);
theResult = theXalanTransformer.transform(&theXMLStream,
inputSource,target);
//compilation error obtained
BufferTransform.cpp:40: base `xalanc_1_10::XalanOutputStream' with only
non-default constructor in class without a constructor
BufferTransform.cpp:54: ISO C++ forbids defining types within return type
BufferTransform.cpp:54: destructors must be member functions
BufferTransform.cpp:54: return type specification for destructor invalid
BufferTransform.cpp:56: type specifier omitted for parameter `size_type'
BufferTransform.cpp:56: syntax error before `)' token
BufferTransform.cpp: In function `void writeData(...)':
BufferTransform.cpp:57: `input' undeclared (first use this function)
BufferTransform.cpp:57: (Each undeclared identifier is reported only once
for each function it appears in.)
BufferTransform.cpp:57: `buffer' undeclared (first use this function)
BufferTransform.cpp:57: `length' undeclared (first use this function)
BufferTransform.cpp: At global scope:
BufferTransform.cpp:66: syntax error before `}' token
BufferTransform.cpp: In function `int transform(const char*, char*)':
BufferTransform.cpp:155: ISO C++ forbids declaration of
`xalan_string_stream'
with no type
BufferTransform.cpp:155: uninitialized const `xalan_string_stream'
BufferTransform.cpp:155: syntax error before `*' token
BufferTransform.cpp:157: `outputStream' undeclared (first use this
function)
make: *** [all] Error 1
thanks,
Nithya
Re: Write transform output to a buffer
Posted by David Bertoni <db...@apache.org>.
Nithya Nirmal Vijayakumar wrote:
> Hi,
>
> I am a new user to Xalan-C++. I want to apply xsl transformation to xml
> stored in a buffer and get the output into a buffer. The
> StreamTransform.cpp example reads from a buffer and writes output to a
> stream. I want to make this write to a buffer. I saw some discussion about
> this in this group and tried following the advice of extending
> Callbackhandler. But I am getting many compilation errors. Has anyone
> already done this? Could you please post your code? Any help will be
> greatly appreciated.
The simplest thing is to use the class ostrstream. I've attached a diff
for the StreamTransform sample that does that.
>
> This is what I tried:
>
> //Declaration of wrapper class
> XALAN_USING_XALAN(XalanOutputStream)
> class xalan_string_stream:public XalanOutputStream{
> private:
> char *output;
> char *buffer;
> unsigned cursor;
> public:
> xalan_string_stream(MemoryManagerType
> XALAN_DEFAULT_CONSTRUCTOR_MEMORY_MGR,
> size_type theBufferSize = eDefaultBufferSize,
> size_type theTranscoderBlockSize = eDefaultTranscoderBlockSize,
> bool fThrowTranscodeException = true){
> buffer=new char[1024];
> }
>
> ~xalan_string_stream(){}
>
In the destructor, you need:
delete [] buffer;
or you'll have a memory leak;
> void writeData (const char *input, size_type length){
> bcopy(input,buffer,length);
> }
>
This is extremely dangerous, because you have no guarantee the data will
fit in the buffer you've allocated.
> void doFlush (){
> }
>
> char *get_result(){
> return buffer;
> }
> };
>
> //usage inside main
> istrstream theXMLStream(theInputDocument,
> strlen(theInputDocument));
>
> istrstream theXSLStream(theStyleSheet, strlen(theStyleSheet));
>
> XALAN_USING_XALAN(XalanDOMString)
> XALAN_USING_XALAN(XalanOutputStreamPrintWriter)
> XALAN_USING_XALAN(XSLTResultTarget)
> XALAN_USING_XALAN(XSLTInputSource)
>
> XSLTInputSource inputSource(&theXSLStream);
>
> inputSource.setSystemId(XalanDOMString("foo").c_str());
>
> const xalan_string_stream *outputStream = new xalan_string_stream();
>
> XalanOutputStreamPrintWriter *printWriter=new
> XalanOutputStreamPrintWriter((XalanOutputStream &)outputStream, false);
>
Where did you get this cast from? Are you trying to remove const from
the xalan_string_stream instance? Why would you define it const in the
first place?
This is much simpler:
xalan_string_stream outputStream;
XalanOutputStreamPrintWriter printWriter(&outputStream);
XSLTResultTarget target(&printWriter);
> XSLTResultTarget target(printWriter);
>
> theResult = theXalanTransformer.transform(&theXMLStream,
> inputSource,target);
>
> //compilation error obtained
> BufferTransform.cpp:40: base `xalanc_1_10::XalanOutputStream' with only
> non-default constructor in class without a constructor
Well, you have to call the base class constructor, and there's no
default for the base class. Perhaps you meant something like this:
XALAN_USING_XALAN(XalanOutputStream)
XALAN_USING_XALAN(XalanMemMgrs)
XALAN_USING_XALAN(MemoryManagerType)
class xalan_string_stream:public XalanOutputStream{
private:
char *output;
char *buffer;
unsigned cursor;
public:
xalan_string_stream(MemoryManagerType& theMgr
XALAN_DEFAULT_CONSTRUCTOR_MEMORY_MGR,
size_type theBufferSize = eDefaultBufferSize,
size_type theTranscoderBlockSize = eDefaultTranscoderBlockSize,
bool fThrowTranscodeException = true):
XalanOutputStream(
theMgr,
theBufferSize,
theTranscoderBlockSize,
fThrowTranscodeException) {
buffer=new char[1024];
}
...
> BufferTransform.cpp:54: ISO C++ forbids defining types within return type
> BufferTransform.cpp:54: destructors must be member functions
> BufferTransform.cpp:54: return type specification for destructor invalid
> BufferTransform.cpp:56: type specifier omitted for parameter `size_type'
> BufferTransform.cpp:56: syntax error before `)' token
> BufferTransform.cpp: In function `void writeData(...)':
> BufferTransform.cpp:57: `input' undeclared (first use this function)
> BufferTransform.cpp:57: (Each undeclared identifier is reported only once
> for each function it appears in.)
> BufferTransform.cpp:57: `buffer' undeclared (first use this function)
> BufferTransform.cpp:57: `length' undeclared (first use this function)
> BufferTransform.cpp: At global scope:
> BufferTransform.cpp:66: syntax error before `}' token
> BufferTransform.cpp: In function `int transform(const char*, char*)':
> BufferTransform.cpp:155: ISO C++ forbids declaration of
> `xalan_string_stream'
> with no type
> BufferTransform.cpp:155: uninitialized const `xalan_string_stream'
> BufferTransform.cpp:155: syntax error before `*' token
> BufferTransform.cpp:157: `outputStream' undeclared (first use this
> function)
> make: *** [all] Error 1
>
You should not be relying on this list to help you correct C++ syntax
errors. Please make sure your code is reasonably correct before you
post a question.
Dave