开发者

Improving XSL translation using Apache XalanC++ libaray

开发者 https://www.devze.com 2023-03-01 12:24 出处:网络
Function converter() accepts parsed xsl stream as input and returns the output string. Can I improve this function ?

Function converter() accepts parsed xsl stream as input and returns the output string. Can I improve this function ? Can I cache incoming strings ? Any suggestion will be appreciated.

 int  converter( char *xslInput,char *htmlOutput)
   {
    int theResult=-1;
    int i=0;
    char c;
    char xmlbuf[100] = {'\0'};
    char xmlbuf1[]="<?xml version=\"1.0\" encoding=\"";
    char xmlbuf2[]="\"?><a></a>";
    char default_encoding[]="iso-8859-1";
    char final_encoding[15];
    char *encoding = NULL;
    strcpy(final_encoding,default_encoding);
    strcpy(xmlbuf,xmlbuf1);
    strcat(xmlbuf,final_encoding);
    strcat(xmlbuf,xmlbuf2);

    XALAN_USING_STD(cerr)
    XALAN_USING_STD(cout)
    XALAN_USING_STD(endl)
    XALAN_USING_STD(ofstream)
    XALAN_USING_STD(ostrstream)
    XALAN_USING_STD(istrstream)

    XALAN_USING_STD(ostrstream)

    XALAN_USING_XERCES(XMLPlatformUtils)
    XALAN_USING_XALAN(XalanTransformer)

    ostrstream      theOutput;

    // 2. Initialize Xerces and Xalan
    XMLPlatformUtils::Initialize();
    XalanTransformer::initialize();

    {
            // 3. Create a XalanTransformer
            XalanTransformer theXalanTransformer;

            // Our input streams...
            istrstream      theXMLStream(xmlbuf, strlen(xmlbuf));
            istrstream      theXSLStream(xslInput, strlen(xslInput));
            ostrstream      theOutput;

            // 4. Prepare the input and output sources
            XALAN_USING_XALAN(XSLTInputSource)
            XALAN_USING_XALAN(XSLTResultTarget)
           // 5. Perform the transformation
            theResult = theXalanTransformer.transform(&theXMLStream, &theXSLStream, theOutput);
            if(theResult != 0)
            {
                    cerr << "StreamTransform Error: \n" << theXalanTransformer.getLastError()
                     << endl
                     << endl;
            }
            else
            {
                    theOutput << '\0';
                    strcpy(htmlOutput, theOutput.str());
                    cout << "Result of Transformation is SUCCESS\n" ;

            }

    }

            // 5. Shutdown the transformation thingy...
            XalanTransformer::te开发者_JAVA百科rminate();
            XalanTransformer::ICUCleanUp();
            XMLPlatformUtils::Terminate();

    return theResult;
    }


One thing is that too much local variables are used,e.g., char xmlbuf1, char xmlbuf2, etc. Furthermore, can you use std::string instead of those raw c-style arrays?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号