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 logancillo <al...@gmail.com> on 2008/02/07 10:34:54 UTC

problem extracting output to screen

hi everybody, i have this code

<code>
#include "../../es_yell_frontlite_mvc_View_RenderizarVistaXalanC.h"
#include <xalanc/Include/PlatformDefinitions.hpp>
#if defined(XALAN_CLASSIC_IOSTREAMS)
#include <iostream.h>
#include <strstream.h>
#include <fstream.h>
#else
#include <iostream>
#include <strstream>
#include <fstream>
#endif
#include <xercesc/util/PlatformUtils.hpp>
#include <xalanc/XalanTransformer/XalanTransformer.hpp>


JNIEXPORT jstring JNICALL
Java_es_yell_frontlite_mvc_View_RenderizarVistaXalanC_renderView
  (JNIEnv *env, jobject obj, jstring xml, jstring xsl) {

    jboolean blnIsCopy;
    jstring salida ;
    
    XALAN_USING_STD(cout)
    XALAN_USING_STD(endl)
    XALAN_USING_STD(cerr)
    XALAN_USING_STD(cout)
    XALAN_USING_STD(endl)
    XALAN_USING_STD(istrstream)
    XALAN_USING_STD(ofstream)
    XALAN_USING_STD(ostrstream)
    
    #if defined(XALAN_STRICT_ANSI_HEADERS)
	using std::strlen;
    #endif
    
    XALAN_USING_XERCES(XMLPlatformUtils)

    XALAN_USING_XALAN(XalanTransformer)

    // Call the static initializer for Xerces.

    XMLPlatformUtils::Initialize();

    // Initialize Xalan.
    XalanTransformer::initialize();
    {

    // Create a XalanTransformer.
        XalanTransformer theXalanTransformer;
        const char* theInputXmlDocument
=env->GetStringUTFChars(xml,&blnIsCopy);
        istrstream	theXMLStream(theInputXmlDocument);
        const char*   theInputXslDocument =
env->GetStringUTFChars(xsl,&blnIsCopy);
        istrstream	theXSLStream(theInputXslDocument);
        
        XALAN_USING_XALAN(XalanDOMString)
        XALAN_USING_XALAN(XSLTInputSource)
        XALAN_USING_XALAN(XSLTResultTarget)
        XALAN_USING_STD(streamsize) 

        XSLTInputSource		inputXSLSource(&theXSLStream);

        inputXSLSource.setSystemId(XalanDOMString("foo").c_str());

        /// Generate the XML output object.
        ostrstream  theOutputStream; 

        
         // Do the transform.
        int theResult = theXalanTransformer.transform(&theXMLStream,
inputXSLSource, theOutputStream);
        const char * cadena = theOutputStream.str();
        cout<<"ANTES---->"<<endl<<cadena<<endl;
        salida= env->NewStringUTF(cadena);
        cout<<"DESPUES---->"<<salida<<endl;
        if(theResult != 0)
        {
            cout << "StreamTransform Error: \n" <<
theXalanTransformer.getLastError()
            << endl
            << endl;
        }
        else{
            //libero memoria
            //delete bufferXML;
            //delete bufferXSL;
            theOutputStream.freeze(false);
        }
        //libero la memoria una vez que tengo en mis buffers el xml y el xsl
        env->ReleaseStringUTFChars(xml,theInputXmlDocument);
        env->ReleaseStringUTFChars(xsl,theInputXslDocument);
    }
    
    
    // Terminate Xalan...
    XalanTransformer::terminate();

// Terminate Xerces...
    XMLPlatformUtils::Terminate();
    cout<<"se acabo!!"<<endl;
    return salida;
}

</code>

This code realizes his work, which is to take the chains of characters of
the xml and the insole xsl and to realize the transformation in html. The
problem is that when the html generated by the screen goes out they go out
strange characters, something like this:



It seems as if the theOutputStream object had the transformation stored in
an internal static buffer and when I return the bulging chain it was not
finished with \n\r or worse, that this treading on memory not reserved
adequately.

I know that xml and xsl templates are correct because is being used in other
app version which uses xalanj as xslt engine.

The java class is like this:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package es.yell.frontlite.mvc.View;

//import java.io.ByteArrayOutputStream;

/**
 *
 * @author alonso
 */
public class RenderizarVistaXalanC {
        
    public native String renderView(String xml, String xsl);
}


pls help!


-- 
View this message in context: http://www.nabble.com/problem-extracting-output-to-screen-tp15330229p15330229.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.


Re: problem extracting output to screen

Posted by David Bertoni <db...@apache.org>.
logancillo wrote:
> hi everybody, i have this code
> 
> <code>
...
>         
>          // Do the transform.
>         int theResult = theXalanTransformer.transform(&theXMLStream,
> inputXSLSource, theOutputStream);
>         const char * cadena = theOutputStream.str();
>         cout<<"ANTES---->"<<endl<<cadena<<endl;
The string return from the stream is not null-terminated, so I suspect you 
will see some garbage characters on the screen after the actual result. 
Make sure you append a null-terminator to the stream, before you call str():
          theOutputStream << '\0';
          const char * cadena = theOutputStream.str();


>         salida= env->NewStringUTF(cadena);
>         cout<<"DESPUES---->"<<salida<<endl;
...
> 
> </code>
> 
> This code realizes his work, which is to take the chains of characters of
> the xml and the insole xsl and to realize the transformation in html. The
> problem is that when the html generated by the screen goes out they go out
> strange characters, something like this:
> 
> 

If you see these characters after the output you expect, the problem is the 
string was not null-terminated, as I mentioned before.

> 
> It seems as if the theOutputStream object had the transformation stored in
> an internal static buffer and when I return the bulging chain it was not
> finished with \n\r or worse, that this treading on memory not reserved
> adequately.
I'm sorry, but I don't really understand what you mean here.  Xalan-C does 
not use any internal static buffers to store the serialized HTML, and I 
doubt very much this is the result of a buffer overrun.
> 
> I know that xml and xsl templates are correct because is being used in other
> app version which uses xalanj as xslt engine.
Then I suspect your code is at fault.

Dave