Annotation of parser3/src/classes/xalan-patched/XalanTransformer2.cpp, revision 1.2

1.2     ! parser      1: /** @file
        !             2:        Parser: patched transform method to 
        !             3:                1. return exceptions 
        !             4:                2. pass string params
        !             5:        decls.
        !             6: 
        !             7:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
        !             8:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
        !             9: 
        !            10:        $Id: pa_config_includes.h,v 1.11 2001/09/26 10:32:25 parser Exp $
        !            11: 
        !            12:        based on:
        !            13: */
1.1       parser     14: /*
                     15:  * The Apache Software License, Version 1.1
                     16:  *
                     17:  *
                     18:  * Copyright (c) 2001 The Apache Software Foundation.  All rights 
                     19:  * reserved.
                     20:  *
                     21:  * Redistribution and use in source and binary forms, with or without
                     22:  * modification, are permitted provided that the following conditions
                     23:  * are met:
                     24:  *
                     25:  * 1. Redistributions of source code must retain the above copyright
                     26:  *       notice, this list of conditions and the following disclaimer. 
                     27:  *
                     28:  * 2. Redistributions in binary form must reproduce the above copyright
                     29:  *       notice, this list of conditions and the following disclaimer in
                     30:  *       the documentation and/or other materials provided with the
                     31:  *       distribution.
                     32:  *
                     33:  * 3. The end-user documentation included with the redistribution,
                     34:  *       if any, must include the following acknowledgment:  
                     35:  *              "This product includes software developed by the
                     36:  *               Apache Software Foundation (http://www.apache.org/)."
                     37:  *       Alternately, this acknowledgment may appear in the software itself,
                     38:  *       if and wherever such third-party acknowledgments normally appear.
                     39:  *
                     40:  * 4. The names "Xalan" and "Apache Software Foundation" must
                     41:  *       not be used to endorse or promote products derived from this
                     42:  *       software without prior written permission. For written 
                     43:  *       permission, please contact apache@apache.org.
                     44:  *
                     45:  * 5. Products derived from this software may not be called "Apache",
                     46:  *       nor may "Apache" appear in their name, without prior written
                     47:  *       permission of the Apache Software Foundation.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
                     50:  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     51:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     52:  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
                     53:  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
                     54:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
                     55:  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
                     56:  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
                     57:  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
                     58:  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
                     59:  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     60:  * SUCH DAMAGE.
                     61:  * ====================================================================
                     62:  *
                     63:  * This software consists of voluntary contributions made by many
                     64:  * individuals on behalf of the Apache Software Foundation and was
                     65:  * originally based on software copyright (c) 1999, International
                     66:  * Business Machines, Inc., http://www.ibm.com.  For more
                     67:  * information on the Apache Software Foundation, please see
                     68:  * <http://www.apache.org/>.
                     69:  */
1.2     ! parser     70: 
        !            71: #include "pa_config_includes.h"
        !            72: #ifdef XML
        !            73: 
        !            74: #include "XalanTransformer2.hpp"
1.1       parser     75: 
                     76: 
                     77: 
                     78: #include <algorithm>
                     79: 
                     80: 
                     81: 
                     82: #include <sax/SAXException.hpp>
                     83: 
                     84: 
                     85: 
                     86: #include <XalanDOM/XalanDOMException.hpp>
                     87: 
                     88: 
                     89: 
                     90: #include <PlatformSupport/DOMStringHelper.hpp>
                     91: #include <PlatformSupport/DOMStringPrintWriter.hpp>
                     92: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
                     93: 
                     94: 
                     95: 
                     96: #include <XPath/XObjectFactoryDefault.hpp>
                     97: #include <XPath/XPathFactoryBlock.hpp>
                     98: #include <XPath/XPathFactoryDefault.hpp>
                     99: 
                    100: 
                    101: 
                    102: #include <XSLT/StylesheetConstructionContextDefault.hpp>
                    103: #include <XSLT/StylesheetExecutionContextDefault.hpp>
                    104: #include <XSLT/StylesheetRoot.hpp>
                    105: #include <XSLT/XSLTEngineImpl.hpp>
                    106: #include <XSLT/XSLTInit.hpp>
                    107: #include <XSLT/XSLTProcessorEnvSupportDefault.hpp>
                    108: 
                    109: 
                    110: 
                    111: #include <XalanSourceTree/XalanSourceTreeDOMSupport.hpp>
                    112: #include <XalanSourceTree/XalanSourceTreeParserLiaison.hpp>
                    113: 
                    114: 
                    115: 
1.2     ! parser    116: #include "XalanTransformer/XalanCompiledStylesheetDefault.hpp"
        !           117: #include "XalanTransformer/XalanDefaultDocumentBuilder.hpp"
        !           118: #include "XalanTransformer/XalanDefaultParsedSource.hpp"
        !           119: #include "XalanTransformer/XalanTransformerOutputStream.hpp"
        !           120: #include "XalanTransformer/XercesDOMParsedSource.hpp"
1.1       parser    121: 
                    122: 
                    123: 
1.2     ! parser    124: XSLTInit*      XalanTransformer2::s_xsltInit = 0;
1.1       parser    125: 
                    126: 
                    127: 
1.2     ! parser    128: XalanTransformer2::XalanTransformer2():
1.1       parser    129:        m_compiledStylesheets(),
                    130:        m_parsedSources(),
                    131:        m_paramPairs(),
                    132:        m_functionPairs(),
                    133:        m_errorMessage(1, '\0'),
                    134:        m_stylesheetExecutionContext(new StylesheetExecutionContextDefault)
                    135: {
                    136: }
                    137: 
                    138: 
                    139: 
1.2     ! parser    140: XalanTransformer2::~XalanTransformer2()
1.1       parser    141: {
                    142: #if !defined(XALAN_NO_NAMESPACES)
                    143:        using std::for_each;
                    144: #endif
                    145: 
                    146:        // Clean up all entries in the compliledStylesheets vector.
                    147:        for_each(m_compiledStylesheets.begin(),
                    148:                         m_compiledStylesheets.end(),
                    149:                         DeleteFunctor<XalanCompiledStylesheet>());
                    150: 
                    151:        // Clean up all entries in the compliledStylesheets vector.
                    152:        for_each(m_parsedSources.begin(),
                    153:                         m_parsedSources.end(),
                    154:                         DeleteFunctor<XalanParsedSource>());
                    155: 
                    156:        for (FunctionParamPairVectorType::size_type i = 0; i < m_functionPairs.size(); ++i)
                    157:        {                       
                    158:                delete m_functionPairs[i].second;
                    159:        }
                    160: 
                    161:        m_functionPairs.clear();
                    162: 
                    163:        delete m_stylesheetExecutionContext;
                    164: }
                    165: 
                    166: 
                    167: 
                    168: void
1.2     ! parser    169: XalanTransformer2::initialize()
1.1       parser    170: {
                    171:        // Initialize Xalan. 
                    172:        s_xsltInit = new XSLTInit;
                    173: }
                    174: 
                    175: 
                    176: 
                    177: void
1.2     ! parser    178: XalanTransformer2::terminate()
1.1       parser    179: {
                    180:        // Terminate Xalan and release memory.
                    181:        delete s_xsltInit;
                    182: 
                    183:        s_xsltInit = 0;
                    184: }
                    185: 
                    186: 
                    187: 
                    188: int
1.2     ! parser    189: XalanTransformer2::transform(
1.1       parser    190:        const XalanParsedSource&        theParsedXML, 
                    191:        const XSLTInputSource&          theStylesheetSource,
                    192:        const XSLTResultTarget&         theResultTarget)
                    193: {
                    194: #if !defined(XALAN_NO_NAMESPACES)
                    195:        using std::for_each;
                    196: #endif
                    197: 
                    198:        int     theResult = 0;
                    199: 
                    200:        // Clear the error message.
                    201:        m_errorMessage.resize(1, '\0');
                    202: 
                    203:        // Store error messages from problem listener.
                    204:        XalanDOMString  theErrorMessage;
                    205: 
                    206:        try
                    207:        {
                    208:                // Create the helper object that is necessary for running the processor...
                    209:                XalanAutoPtr<XalanParsedSourceHelper>   theHelper(theParsedXML.createHelper());
                    210:                assert(theHelper.get() != 0);
                    211: 
                    212:                DOMSupport&                                     theDOMSupport = theHelper->getDOMSupport();
                    213: 
                    214:                XMLParserLiaison&                               theParserLiaison = theHelper->getParserLiaison();
                    215: 
                    216:                // Create some more support objects...
                    217:                XSLTProcessorEnvSupportDefault  theXSLTProcessorEnvSupport;
                    218: 
                    219:                XObjectFactoryDefault                   theXObjectFactory;
                    220: 
                    221:                XPathFactoryDefault                     theXPathFactory;
                    222: 
                    223:                // Create a processor...
                    224:                XSLTEngineImpl  theProcessor(
                    225:                                theParserLiaison,
                    226:                                theXSLTProcessorEnvSupport,
                    227:                                theDOMSupport,
                    228:                                theXObjectFactory,
                    229:                                theXPathFactory);
                    230: 
                    231:                theXSLTProcessorEnvSupport.setProcessor(&theProcessor);
                    232: 
                    233:                // Create a problem listener and send output to a XalanDOMString.
                    234:                DOMStringPrintWriter    thePrintWriter(theErrorMessage);
                    235:                
                    236:                ProblemListenerDefault  theProblemListener(&thePrintWriter);
                    237: 
                    238:                theProcessor.setProblemListener(&theProblemListener);
                    239: 
                    240:                theParserLiaison.setExecutionContext(*m_stylesheetExecutionContext);
                    241: 
                    242:                // Create a stylesheet construction context, 
                    243:                // using the stylesheet's factory support objects.
                    244:                StylesheetConstructionContextDefault    theStylesheetConstructionContext(
                    245:                                        theProcessor,
                    246:                                        theXSLTProcessorEnvSupport,
                    247:                                        theXPathFactory);
                    248: 
                    249:                // Hack used to cast away const.
                    250:                XSLTResultTarget        tempResultTarget(theResultTarget);
                    251: 
                    252:                const EnsureReset       theReset(*this);
                    253: 
                    254:                // Set up the stylesheet execution context.
                    255:                m_stylesheetExecutionContext->setXPathEnvSupport(&theXSLTProcessorEnvSupport);
                    256: 
                    257:                m_stylesheetExecutionContext->setDOMSupport(&theDOMSupport);
                    258: 
                    259:                m_stylesheetExecutionContext->setXObjectFactory(&theXObjectFactory);
                    260: 
                    261:                m_stylesheetExecutionContext->setXSLTProcessor(&theProcessor);
                    262: 
                    263:                // Set the parameters if any.
                    264:                for (ParamPairVectorType::size_type i = 0; i < m_paramPairs.size(); ++i)
                    265:                {
                    266:                        theProcessor.setStylesheetParam(
                    267:                                        m_paramPairs[i].first,
                    268:                                        m_paramPairs[i].second);
                    269:                }
                    270: 
                    271:                // Set the functions if any.
                    272:                for (FunctionParamPairVectorType::size_type f = 0; f < m_functionPairs.size(); ++f)
                    273:                {
                    274:                        theXSLTProcessorEnvSupport.installExternalFunctionLocal(
                    275:                                        m_functionPairs[f].first.getNamespace(),
                    276:                                        m_functionPairs[f].first.getLocalPart(),
                    277:                                        *(m_functionPairs[f].second));
                    278:                }
                    279: 
                    280:                // Do the transformation...
                    281:                theProcessor.process(
                    282:                                        theParsedXML.getDocument(),
                    283:                                        theStylesheetSource,
                    284:                                        tempResultTarget,
                    285:                                        theStylesheetConstructionContext,
                    286:                                        *m_stylesheetExecutionContext);
                    287:        }
                    288:        catch (XSLException& e)
                    289:        {
                    290:                if (length(theErrorMessage) != 0)
                    291:                {
                    292:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    293:                }
                    294:                else
                    295:                {
                    296:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    297:                }
                    298: 
                    299:                theResult = -1;         
                    300:        }
                    301:        catch (SAXException& e)
                    302:        {
                    303:                if (length(theErrorMessage) != 0)
                    304:                {
                    305:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    306:                }
                    307:                else
                    308:                {
                    309:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    310:                }
                    311: 
                    312:                theResult = -2;
                    313:        }
                    314:        catch (XMLException& e)
                    315:        {
                    316:                if (length(theErrorMessage) != 0)
                    317:                {
                    318:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    319:                }
                    320:                else
                    321:                {
                    322:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    323:                }
                    324: 
                    325:                theResult = -3;
                    326:        }
                    327:        catch(const XalanDOMException&  e)
                    328:        {
                    329:                if (length(theErrorMessage) != 0)
                    330:                {
                    331:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    332:                }
                    333:                else
                    334:                {
                    335:                        XalanDOMString theMessage("XalanDOMException caught.  The code is ");
                    336:                        
                    337:                        append(theMessage,      LongToDOMString(long(e.getExceptionCode())));
                    338:                        append(theMessage,      XalanDOMString("."));                                            
                    339: 
                    340:                        TranscodeToLocalCodePage(theMessage, m_errorMessage, true);
                    341:                }
                    342: 
                    343:                theResult = -4;
                    344:        }
                    345: 
                    346:        return theResult;
                    347: }
                    348: 
                    349: 
                    350: 
                    351: int
1.2     ! parser    352: XalanTransformer2::transform(
1.1       parser    353:                        const XalanParsedSource&                theParsedXML, 
                    354:                        const XalanCompiledStylesheet*  theCompiledStylesheet,
                    355:                        const XSLTResultTarget&                 theResultTarget)
                    356: {
                    357: #if !defined(XALAN_NO_NAMESPACES)
                    358:        using std::for_each;
                    359: #endif
                    360: 
                    361:        int     theResult = 0;
                    362: 
                    363:        // Clear the error message.
                    364:        m_errorMessage.resize(1, '\0');
                    365: 
                    366:        // Store error messages from problem listener.
                    367:        XalanDOMString  theErrorMessage;
                    368: 
                    369:        try
                    370:        {
                    371:                // Create the helper object that is necessary for running the processor...
                    372:                XalanAutoPtr<XalanParsedSourceHelper>   theHelper(theParsedXML.createHelper());
                    373:                assert(theHelper.get() != 0);
                    374: 
                    375:                DOMSupport&                                     theDOMSupport = theHelper->getDOMSupport();
                    376: 
                    377:                XMLParserLiaison&                               theParserLiaison = theHelper->getParserLiaison();
                    378: 
                    379:                // Create some more support objects...
                    380:                XSLTProcessorEnvSupportDefault  theXSLTProcessorEnvSupport;
                    381: 
                    382:                XObjectFactoryDefault                   theXObjectFactory;
                    383: 
                    384:                XPathFactoryDefault                     theXPathFactory;
                    385: 
                    386:                // Create a processor...
                    387:                XSLTEngineImpl  theProcessor(
                    388:                                theParserLiaison,
                    389:                                theXSLTProcessorEnvSupport,
                    390:                                theDOMSupport,
                    391:                                theXObjectFactory,
                    392:                                theXPathFactory);
                    393: 
                    394:                theXSLTProcessorEnvSupport.setProcessor(&theProcessor);
                    395: 
                    396:                // Create a problem listener and send output to a XalanDOMString.
                    397:                DOMStringPrintWriter    thePrintWriter(theErrorMessage);
                    398:                
                    399:                ProblemListenerDefault  theProblemListener(&thePrintWriter);
                    400: 
                    401:                theProcessor.setProblemListener(&theProblemListener);
                    402: 
                    403:                // Since the result target is not const in our
                    404:                // internal intefaces, we'll pass in a local copy
                    405:                // of the one provided...
                    406:                XSLTResultTarget        tempResultTarget(theResultTarget);
                    407: 
                    408:                const EnsureReset       theReset(*this);
                    409: 
                    410:                // Set up the stylesheet execution context.
                    411:                m_stylesheetExecutionContext->setXPathEnvSupport(&theXSLTProcessorEnvSupport);
                    412: 
                    413:                m_stylesheetExecutionContext->setDOMSupport(&theDOMSupport);
                    414: 
                    415:                m_stylesheetExecutionContext->setXObjectFactory(&theXObjectFactory);
                    416: 
                    417:                m_stylesheetExecutionContext->setXSLTProcessor(&theProcessor);
                    418:                
                    419:                // Set the compiled stylesheet.
                    420:                m_stylesheetExecutionContext->setStylesheetRoot(theCompiledStylesheet->getStylesheetRoot());
                    421: 
                    422:                // Set the parameters if any.
                    423:                for (ParamPairVectorType::size_type i = 0; i < m_paramPairs.size(); ++i)
                    424:                {
                    425:                        theProcessor.setStylesheetParam(
                    426:                                        m_paramPairs[i].first,
                    427:                                        m_paramPairs[i].second);
                    428:                }
                    429: 
                    430:                // Set the functions if any.
                    431:                for (FunctionParamPairVectorType::size_type f = 0; f < m_functionPairs.size(); ++f)
                    432:                {
                    433:                        theXSLTProcessorEnvSupport.installExternalFunctionLocal(
                    434:                                        m_functionPairs[f].first.getNamespace(),
                    435:                                        m_functionPairs[f].first.getLocalPart(),
                    436:                                        *(m_functionPairs[f].second));
                    437:                }
                    438: 
                    439:                // Do the transformation...
                    440:                theProcessor.process(
                    441:                                        theParsedXML.getDocument(),     
                    442:                                        tempResultTarget,                                       
                    443:                                        *m_stylesheetExecutionContext);
                    444:        }
                    445:        catch (XSLException& e)
                    446:        {
                    447:                if (length(theErrorMessage) != 0)
                    448:                {
                    449:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    450:                }
                    451:                else
                    452:                {
                    453:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    454:                }
                    455: 
                    456:                theResult = -1;         
                    457:        }
                    458:        catch (SAXException& e)
                    459:        {
                    460:                if (length(theErrorMessage) != 0)
                    461:                {
                    462:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    463:                }
                    464:                else
                    465:                {
                    466:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    467:                }
                    468: 
                    469:                theResult = -2;
                    470:        }
                    471:        catch (XMLException& e)
                    472:        {
                    473:                if (length(theErrorMessage) != 0)
                    474:                {
                    475:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    476:                }
                    477:                else
                    478:                {
                    479:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    480:                }
                    481: 
                    482:                theResult = -3;
                    483:        }
                    484:        catch(const XalanDOMException&  e)
                    485:        {
                    486:                if (length(theErrorMessage) != 0)
                    487:                {
                    488:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    489:                }
                    490:                else
                    491:                {
                    492:                        XalanDOMString theMessage("XalanDOMException caught.  The code is ");
                    493:                        
                    494:                        append(theMessage,      LongToDOMString(long(e.getExceptionCode())));
                    495:                        append(theMessage,      XalanDOMString("."));                                            
                    496: 
                    497:                        TranscodeToLocalCodePage(theMessage, m_errorMessage, true);
                    498:                }
                    499: 
                    500:                theResult = -4;
                    501:        }
                    502: 
                    503:        return theResult;
                    504: }
                    505: 
                    506: 
                    507: 
                    508: int
1.2     ! parser    509: XalanTransformer2::transform(
1.1       parser    510:                        const XSLTInputSource&                  theInputSource, 
                    511:                        const XalanCompiledStylesheet*  theCompiledStylesheet,
                    512:                        const XSLTResultTarget&                 theResultTarget)
                    513: {
                    514:        const XalanParsedSource*        theParsedSource = 0;
                    515:  
                    516:        const int       theResult = parseSource(theInputSource, theParsedSource);
                    517:  
                    518:        if (theResult != 0)
                    519:        {
                    520:                return theResult;
                    521:        }
                    522:        else
                    523:        {
                    524:                assert(theParsedSource != 0);
                    525:  
                    526:                // Make sure the parsed source is destroyed when
                    527:                // the transformation is finished...
                    528:                EnsureDestroyParsedSource       theGuard(*this, theParsedSource);
                    529: 
                    530:                // Do the transformation...
                    531:                return transform(
                    532:                                                *theParsedSource,
                    533:                                                theCompiledStylesheet,
                    534:                                                theResultTarget);
                    535:        }
                    536: }
                    537: 
                    538: int
1.2     ! parser    539: XalanTransformer2::transform(
1.1       parser    540:        const XSLTInputSource&          theInputSource, 
                    541:        const XSLTInputSource&          theStylesheetSource,
                    542:        const XSLTResultTarget&         theResultTarget)
                    543: {
                    544:        // Parse the source document.
                    545:        const XalanParsedSource*        theParsedSource = 0;
                    546: 
                    547:        const int       theResult = parseSource(theInputSource, theParsedSource);
                    548: 
                    549:        if (theResult != 0)
                    550:        {
                    551:                return theResult;
                    552:        }
                    553:        else
                    554:        {
                    555:                assert(theParsedSource != 0);
                    556: 
                    557:                // Make sure the parsed source is destroyed when
                    558:                // the transformation is finished...
                    559:                EnsureDestroyParsedSource       theGuard(*this, theParsedSource);
                    560: 
                    561:                // Do the transformation...
                    562:                return transform(
                    563:                                                *theParsedSource,
                    564:                                                theStylesheetSource,
                    565:                                                theResultTarget);
                    566:        }
                    567: }
                    568: 
                    569: 
                    570: 
                    571: int
1.2     ! parser    572: XalanTransformer2::transform(
1.1       parser    573:                        const XSLTInputSource&          theInputSource,                 
                    574:                        const XSLTResultTarget&         theResultTarget)
                    575: {
                    576:        // Do the transformation...
                    577:        return transform(
                    578:                                        theInputSource, 
                    579:                                        XSLTInputSource(),
                    580:                                        theResultTarget);
                    581: }
                    582: 
                    583: 
                    584: 
                    585: int
1.2     ! parser    586: XalanTransformer2::transform(
1.1       parser    587:                        const XSLTInputSource&  theInputSource, 
                    588:                        const XSLTInputSource&  theStylesheetSource,
                    589:                        void*                                   theOutputHandle, 
                    590:                        XalanOutputHandlerType  theOutputHandler,
                    591:                        XalanFlushHandlerType   theFlushHandler)
                    592: {
                    593:        // Set to output target to the callback 
                    594:        XalanTransformerOutputStream    theOutputStream(theOutputHandle, theOutputHandler, theFlushHandler);
                    595: 
                    596:        XalanOutputStreamPrintWriter    thePrintWriter(theOutputStream);
                    597: 
                    598:        XSLTResultTarget                                theResultTarget(&thePrintWriter);
                    599: 
                    600:        // Do the transformation...
                    601:        return transform(
                    602:                                        theInputSource, 
                    603:                                        theStylesheetSource,
                    604:                                        theResultTarget);
                    605: }
                    606: 
                    607: 
                    608: 
                    609: int
1.2     ! parser    610: XalanTransformer2::transform(
1.1       parser    611:                        const XalanParsedSource&                theParsedSource, 
                    612:                        const XalanCompiledStylesheet*  theCompiledStylesheet,
                    613:                        void*                                                   theOutputHandle, 
                    614:                        XalanOutputHandlerType                  theOutputHandler,
                    615:                        XalanFlushHandlerType                   theFlushHandler)
                    616: {
                    617:        // Set to output target to the callback 
                    618:        XalanTransformerOutputStream    theOutputStream(theOutputHandle, theOutputHandler, theFlushHandler);
                    619: 
                    620:        XalanOutputStreamPrintWriter    thePrintWriter(theOutputStream);
                    621: 
                    622:        XSLTResultTarget                                theResultTarget(&thePrintWriter);
                    623: 
                    624:        // Do the transformation...
                    625:        return transform(
                    626:                                        theParsedSource,
                    627:                                        theCompiledStylesheet,
                    628:                                        theResultTarget);
                    629: }
                    630: 
                    631: 
                    632: 
                    633: int
1.2     ! parser    634: XalanTransformer2::transform(
1.1       parser    635:                        const XSLTInputSource&          theInputSource,                 
                    636:                        void*                                           theOutputHandle, 
                    637:                        XalanOutputHandlerType          theOutputHandler,
                    638:                        XalanFlushHandlerType           theFlushHandler)
                    639: {
                    640:        // Set to output target to the callback 
                    641:        XalanTransformerOutputStream    theOutputStream(theOutputHandle, theOutputHandler, theFlushHandler);
                    642: 
                    643:        XalanOutputStreamPrintWriter    thePrintWriter(theOutputStream);
                    644: 
                    645:        XSLTResultTarget                                theResultTarget(&thePrintWriter);
                    646: 
                    647:        // Do the transformation...
                    648:        return transform(
                    649:                                        theInputSource, 
                    650:                                        XSLTInputSource(),
                    651:                                        theResultTarget);
                    652: }
                    653: 
                    654: 
                    655: 
                    656: int
1.2     ! parser    657: XalanTransformer2::compileStylesheet(
1.1       parser    658:                        const XSLTInputSource&                          theStylesheetSource,
                    659:                        const XalanCompiledStylesheet*&         theCompiledStylesheet)
                    660: {
                    661:        // Clear the error message.
                    662:        m_errorMessage.resize(1, '\0');
                    663: 
                    664:        // Store error messages from problem listener.
                    665:        XalanDOMString  theErrorMessage;
                    666: 
                    667:        int                     theResult = 0;
                    668: 
                    669:        try
                    670:        {
                    671:                // Create some support objects that are necessary for running the processor...
                    672:                XalanSourceTreeDOMSupport               theDOMSupport;
                    673: 
                    674:                XalanSourceTreeParserLiaison    theParserLiaison(theDOMSupport);
                    675: 
                    676:                // Hook the two together...
                    677:                theDOMSupport.setParserLiaison(&theParserLiaison);
                    678: 
                    679:                // Create some more support objects...
                    680:                XSLTProcessorEnvSupportDefault  theXSLTProcessorEnvSupport;
                    681: 
                    682:                XObjectFactoryDefault                   theXObjectFactory;
                    683: 
                    684:                XPathFactoryDefault                     theXPathFactory;
                    685: 
                    686:                // Create a processor...
                    687:                XSLTEngineImpl  theProcessor(
                    688:                                theParserLiaison,
                    689:                                theXSLTProcessorEnvSupport,
                    690:                                theDOMSupport,
                    691:                                theXObjectFactory,
                    692:                                theXPathFactory);
                    693: 
                    694:                // Create a problem listener and send output to a XalanDOMString.
                    695:                DOMStringPrintWriter    thePrintWriter(theErrorMessage);
                    696: 
                    697:                ProblemListenerDefault  theProblemListener(&thePrintWriter);
                    698: 
                    699:                theProcessor.setProblemListener(&theProblemListener);
                    700: 
                    701:                // Create a new XalanCompiledStylesheet.
                    702:                theCompiledStylesheet =
                    703:                        new XalanCompiledStylesheetDefault(
                    704:                                                theStylesheetSource,
                    705:                                                theXSLTProcessorEnvSupport,
                    706:                                                theProcessor);
                    707: 
                    708:                // Store it in a vector.
                    709:                m_compiledStylesheets.push_back(theCompiledStylesheet);
                    710:        }
                    711:        catch (XSLException& e)
                    712:        {
                    713:                if (length(theErrorMessage) != 0)
                    714:                {
                    715:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    716:                }
                    717:                else
                    718:                {
                    719:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    720:                }
                    721: 
                    722:                theResult = -1;
                    723:        }
                    724:        catch (SAXException& e)
                    725:        {
                    726:                if (length(theErrorMessage) != 0)
                    727:                {
                    728:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    729:                }
                    730:                else
                    731:                {
                    732:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    733:                }
                    734: 
                    735:                theResult = -2;
                    736:        }
                    737:        catch (XMLException& e)
                    738:        {
                    739:                if (length(theErrorMessage) != 0)
                    740:                {
                    741:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    742:                }
                    743:                else
                    744:                {
                    745:                        TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    746:                }
                    747: 
                    748:                theResult = -3;
                    749:        }
                    750:        catch(const XalanDOMException&  e)
                    751:        {
                    752:                if (length(theErrorMessage) != 0)
                    753:                {
                    754:                        TranscodeToLocalCodePage(theErrorMessage, m_errorMessage, true);
                    755:                }
                    756:                else
                    757:                {
                    758:                        XalanDOMString theMessage("XalanDOMException caught.  The code is ");
                    759: 
                    760:                        append(theMessage,      LongToDOMString(long(e.getExceptionCode())));
                    761: 
                    762:                        append(theMessage,      XalanDOMString("."));
                    763: 
                    764:                        TranscodeToLocalCodePage(theMessage, m_errorMessage, true);
                    765:                }
                    766: 
                    767:                theResult = -4;
                    768:        }
                    769: 
                    770:        return theResult;
                    771: }
                    772: 
1.2     ! parser    773: void
        !           774: XalanTransformer2::compileStylesheet2(
        !           775:                        const XSLTInputSource&                          theStylesheetSource,
        !           776:                        const XalanCompiledStylesheet*&         theCompiledStylesheet) // PAF@design.ru
        !           777: {
        !           778:        // Clear the error message.
        !           779:        m_errorMessage.resize(1, '\0');
        !           780: 
        !           781:        // Store error messages from problem listener.
        !           782: //     XalanDOMString  theErrorMessage;
        !           783: 
        !           784:        int                     theResult = 0;
        !           785: 
        !           786:                // Create some support objects that are necessary for running the processor...
        !           787:                XalanSourceTreeDOMSupport               theDOMSupport;
        !           788: 
        !           789:                XalanSourceTreeParserLiaison    theParserLiaison(theDOMSupport);
        !           790: 
        !           791:                // Hook the two together...
        !           792:                theDOMSupport.setParserLiaison(&theParserLiaison);
        !           793: 
        !           794:                // Create some more support objects...
        !           795:                XSLTProcessorEnvSupportDefault  theXSLTProcessorEnvSupport;
        !           796: 
        !           797:                XObjectFactoryDefault                   theXObjectFactory;
        !           798: 
        !           799:                XPathFactoryDefault                     theXPathFactory;
        !           800: 
        !           801:                // Create a processor...
        !           802:                XSLTEngineImpl  theProcessor(
        !           803:                                theParserLiaison,
        !           804:                                theXSLTProcessorEnvSupport,
        !           805:                                theDOMSupport,
        !           806:                                theXObjectFactory,
        !           807:                                theXPathFactory);
        !           808: 
        !           809:                // Create a problem listener and send output to a XalanDOMString.
        !           810: //             DOMStringPrintWriter    thePrintWriter(theErrorMessage);
        !           811: 
        !           812: //             ProblemListenerDefault  theProblemListener(&thePrintWriter);
        !           813: 
        !           814: //             theProcessor.setProblemListener(&theProblemListener);
        !           815: 
        !           816:                // Create a new XalanCompiledStylesheet.
        !           817:                theCompiledStylesheet =
        !           818:                        new XalanCompiledStylesheetDefault(
        !           819:                                                theStylesheetSource,
        !           820:                                                theXSLTProcessorEnvSupport,
        !           821:                                                theProcessor);
        !           822: 
        !           823:                // Store it in a vector.
        !           824:                m_compiledStylesheets.push_back(theCompiledStylesheet);
        !           825: }
        !           826: 
1.1       parser    827: 
                    828: 
                    829: int
1.2     ! parser    830: XalanTransformer2::destroyStylesheet(const XalanCompiledStylesheet*    theStylesheet)
1.1       parser    831: {
                    832: #if !defined(XALAN_NO_NAMESPACES)
                    833:        using std::find;
                    834: #endif
                    835: 
                    836:        const CompiledStylesheetPtrVectorType::iterator         i =
                    837:                find(
                    838:                        m_compiledStylesheets.begin(),
                    839:                        m_compiledStylesheets.end(),
                    840:                        theStylesheet);
                    841: 
                    842:        if (i == m_compiledStylesheets.end())
                    843:        {
                    844:                const char* const       theStylesheetErrorMessage =
                    845:                                "An invalid compiled stylesheet was provided.";
                    846: 
                    847:                const unsigned int      theLength =
                    848:                        length(theStylesheetErrorMessage);
                    849: 
                    850:                m_errorMessage.resize(theLength + 1, '\0');
                    851: 
                    852:                strncpy(&*m_errorMessage.begin(), theStylesheetErrorMessage, theLength);
                    853: 
                    854:                return -1;
                    855:        }
                    856:        else
                    857:        {
                    858:                m_compiledStylesheets.erase(i);
                    859: 
                    860: #if defined(XALAN_CANNOT_DELETE_CONST)
                    861:                delete (XalanCompiledStylesheet*) theStylesheet;
                    862: #else
                    863:                delete theStylesheet;
                    864: #endif
                    865: 
                    866:                return 0;
                    867:        }
                    868: }
                    869: 
                    870: 
                    871: 
                    872: int
1.2     ! parser    873: XalanTransformer2::parseSource(
1.1       parser    874:                        const XSLTInputSource&          theInputSource,
                    875:                        const XalanParsedSource*&       theParsedSource,
                    876:                        bool                                            useXercesDOM)
                    877: {
                    878:        // Clear the error message.
                    879:        m_errorMessage.clear();
                    880:        m_errorMessage.push_back(0);
                    881: 
                    882:        int theResult = 0;
                    883: 
                    884:        try
                    885:        {
                    886:                if(useXercesDOM == true)
                    887:                {
                    888:                        theParsedSource = new XercesDOMParsedSource(theInputSource);
                    889:                }
                    890:                else
                    891:                {
                    892:                        theParsedSource = new XalanDefaultParsedSource(theInputSource);
                    893:                }
                    894: 
                    895:                // Store it in a vector.
                    896:                m_parsedSources.push_back(theParsedSource);
                    897:        }
                    898:        catch (XSLException& e)
                    899:        {
                    900:                TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    901: 
                    902:                theResult = -1;
                    903:        }
                    904:        catch (SAXException& e)
                    905:        {
                    906:                TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    907: 
                    908:                theResult = -2;
                    909:        }
                    910:        catch (XMLException& e)
                    911:        {
                    912:                TranscodeToLocalCodePage(e.getMessage(), m_errorMessage, true);
                    913: 
                    914:                theResult = -3;
                    915:        }
                    916:        catch(const XalanDOMException&  e)
                    917:        {
                    918:                XalanDOMString theMessage("XalanDOMException caught.  The code is ");
                    919:                        
                    920:                append(theMessage,      LongToDOMString(long(e.getExceptionCode())));
                    921: 
                    922:                append(theMessage,      XalanDOMString("."));                                            
                    923: 
                    924:                TranscodeToLocalCodePage(theMessage, m_errorMessage, true);
                    925: 
                    926:                theResult = -3;
                    927:        }
                    928: 
                    929:        return theResult;
                    930: }
                    931: 
                    932: 
                    933: 
                    934: int
1.2     ! parser    935: XalanTransformer2::destroyParsedSource(const XalanParsedSource*        theParsedSource)
1.1       parser    936: {
                    937: #if !defined(XALAN_NO_NAMESPACES)
                    938:        using std::find;
                    939: #endif
                    940: 
                    941:        const ParsedSourcePtrVectorType::iterator       i =
                    942:                find(
                    943:                        m_parsedSources.begin(),
                    944:                        m_parsedSources.end(),
                    945:                        theParsedSource);
                    946: 
                    947:        if (i == m_parsedSources.end())
                    948:        {
                    949:                const char* const       theParsedSourceErrorMessage =
                    950:                                "An invalid parsed source was provided.";
                    951: 
                    952:                const unsigned int      theLength =
                    953:                        length(theParsedSourceErrorMessage);
                    954: 
                    955:                m_errorMessage.resize(theLength + 1, '\0');
                    956: 
                    957:                strncpy(&*m_errorMessage.begin(), theParsedSourceErrorMessage, theLength);
                    958: 
                    959:                return -1;
                    960:        }
                    961:        else
                    962:        {
                    963:                m_parsedSources.erase(i);
                    964: 
                    965: #if defined(XALAN_CANNOT_DELETE_CONST)
                    966:                delete (XalanCompiledStylesheet*) theParsedSource;
                    967: #else
                    968:                delete theParsedSource;
                    969: #endif
                    970: 
                    971:                return 0;
                    972:        }
                    973: }
                    974: 
                    975: 
                    976: 
                    977: void
1.2     ! parser    978: XalanTransformer2::setStylesheetParam(
1.1       parser    979:                        const XalanDOMString&   key,
                    980:                        const XalanDOMString&   expression)
                    981: {
                    982:        // Store the stylesheet parameter in a vector.
                    983:        m_paramPairs.push_back(ParamPairType(key,  expression));
                    984: }
                    985: 
                    986: XalanDocumentBuilder*
1.2     ! parser    987: XalanTransformer2::createDocumentBuilder()
1.1       parser    988: {
                    989:        m_parsedSources.reserve(m_parsedSources.size() + 1);
                    990: 
                    991:        XalanDocumentBuilder* const     theNewBuilder = new XalanDefaultDocumentBuilder;
                    992: 
                    993:        m_parsedSources.push_back(theNewBuilder);
                    994: 
                    995:        return theNewBuilder;
                    996: }
                    997: 
                    998: 
                    999: 
                   1000: void
1.2     ! parser   1001: XalanTransformer2::destroyDocumentBuilder(XalanDocumentBuilder*        theDocumentBuilder)
1.1       parser   1002: {
                   1003:        destroyParsedSource(theDocumentBuilder);
                   1004: }
                   1005: 
                   1006: 
                   1007: 
                   1008: void
1.2     ! parser   1009: XalanTransformer2::installExternalFunction(
1.1       parser   1010:                        const XalanDOMString&   theNamespace,
                   1011:                        const XalanDOMString&   functionName,
                   1012:                        const Function&                 function)
                   1013: {
                   1014:        m_functionPairs.push_back(FunctionPairType(XalanQNameByValue(theNamespace, functionName), function.clone()));
                   1015: }
                   1016: 
                   1017: 
                   1018: 
                   1019: void
1.2     ! parser   1020: XalanTransformer2::installExternalFunctionGlobal(
1.1       parser   1021:                        const XalanDOMString&   theNamespace,
                   1022:                        const XalanDOMString&   functionName,
                   1023:                        const Function&                 function)
                   1024: {
                   1025:        XSLTProcessorEnvSupportDefault::installExternalFunctionGlobal(
                   1026:                        theNamespace,
                   1027:                        functionName,
                   1028:                        function);
                   1029: }
                   1030: 
                   1031: 
                   1032: 
                   1033: void
1.2     ! parser   1034: XalanTransformer2::uninstallExternalFunction(
1.1       parser   1035:                        const XalanDOMString&   theNamespace,
                   1036:                        const XalanDOMString&   functionName)
                   1037: {
                   1038:        for (FunctionParamPairVectorType::size_type i = 0; i < m_functionPairs.size(); ++i)
                   1039:        {
                   1040:                if(XalanQNameByReference(theNamespace, functionName).equals(m_functionPairs[i].first))
                   1041:                {
                   1042:                        delete m_functionPairs[i].second;
                   1043: 
                   1044:                        m_functionPairs.erase(m_functionPairs.begin() + i);     
                   1045:                }
                   1046:        }       
                   1047: }
                   1048: 
                   1049: 
                   1050: 
                   1051: void
1.2     ! parser   1052: XalanTransformer2::uninstallExternalFunctionGlobal(
1.1       parser   1053:                        const XalanDOMString&   theNamespace,
                   1054:                        const XalanDOMString&   functionName)
                   1055: {
                   1056:        XSLTProcessorEnvSupportDefault::uninstallExternalFunctionGlobal(
                   1057:                        theNamespace,
                   1058:                        functionName);
                   1059: }
                   1060: 
                   1061: 
                   1062: 
                   1063: 
                   1064: const char*
1.2     ! parser   1065: XalanTransformer2::getLastError() const
1.1       parser   1066: {
                   1067:        return &m_errorMessage[0]; 
                   1068: }
                   1069: 
                   1070: 
                   1071: 
                   1072: void
1.2     ! parser   1073: XalanTransformer2::reset()
1.1       parser   1074: {
                   1075:        try
                   1076:        {
                   1077:                // Reset objects.
                   1078:                m_stylesheetExecutionContext->setXPathEnvSupport(0);
                   1079: 
                   1080:                m_stylesheetExecutionContext->setDOMSupport(0);
                   1081:                
                   1082:                m_stylesheetExecutionContext->setXObjectFactory(0);
                   1083:                
                   1084:                m_stylesheetExecutionContext->setXSLTProcessor(0);
                   1085: 
                   1086:                m_stylesheetExecutionContext->reset();
                   1087: 
                   1088:                // Clear the ParamPairVectorType.
                   1089:                m_paramPairs.clear();
                   1090:        }
                   1091:        catch(...)
                   1092:        {
                   1093:        }
                   1094: }
                   1095: 
                   1096: 
                   1097: 
                   1098: 
1.2     ! parser   1099: XalanTransformer2::EnsureReset::~EnsureReset()
1.1       parser   1100: {
                   1101:        m_transformer.m_stylesheetExecutionContext->reset();
                   1102: 
                   1103:        m_transformer.reset();
                   1104: }
1.2     ! parser   1105: 
        !          1106: // PAF@design.ru
        !          1107: 
        !          1108: void
        !          1109: XalanTransformer2::transform2(
        !          1110:                        const XalanParsedSource&                theParsedXML, 
        !          1111:                        const XalanCompiledStylesheet*  theCompiledStylesheet,
        !          1112:                        const XSLTResultTarget&                 theResultTarget)
        !          1113: {
        !          1114: #if !defined(XALAN_NO_NAMESPACES)
        !          1115:        using std::for_each;
        !          1116: #endif
        !          1117: 
        !          1118:        // Create the helper object that is necessary for running the processor...
        !          1119:        XalanAutoPtr<XalanParsedSourceHelper>   theHelper(theParsedXML.createHelper());
        !          1120:        assert(theHelper.get() != 0);
        !          1121: 
        !          1122:        DOMSupport&                                     theDOMSupport = theHelper->getDOMSupport();
        !          1123: 
        !          1124:        XMLParserLiaison&                               theParserLiaison = theHelper->getParserLiaison();
        !          1125: 
        !          1126:        // Create some more support objects...
        !          1127:        XSLTProcessorEnvSupportDefault  theXSLTProcessorEnvSupport;
        !          1128: 
        !          1129:        XObjectFactoryDefault                   theXObjectFactory;
        !          1130: 
        !          1131:        XPathFactoryDefault                     theXPathFactory;
        !          1132: 
        !          1133:        // Create a processor...
        !          1134:        XSLTEngineImpl  theProcessor(
        !          1135:                        theParserLiaison,
        !          1136:                        theXSLTProcessorEnvSupport,
        !          1137:                        theDOMSupport,
        !          1138:                        theXObjectFactory,
        !          1139:                        theXPathFactory);
        !          1140: 
        !          1141:        theXSLTProcessorEnvSupport.setProcessor(&theProcessor);
        !          1142: 
        !          1143:        // Create a problem listener and send output to a XalanDOMString.
        !          1144:        //DOMStringPrintWriter  thePrintWriter(theErrorMessage);
        !          1145:        
        !          1146:        //ProblemListenerDefault        theProblemListener(&thePrintWriter);
        !          1147: 
        !          1148:        //theProcessor.setProblemListener(&theProblemListener);
        !          1149: 
        !          1150:        // Since the result target is not const in our
        !          1151:        // internal intefaces, we'll pass in a local copy
        !          1152:        // of the one provided...
        !          1153:        XSLTResultTarget        tempResultTarget(theResultTarget);
        !          1154: 
        !          1155:        const EnsureReset       theReset(*this);
        !          1156: 
        !          1157:        // Set up the stylesheet execution context.
        !          1158:        m_stylesheetExecutionContext->setXPathEnvSupport(&theXSLTProcessorEnvSupport);
        !          1159: 
        !          1160:        m_stylesheetExecutionContext->setDOMSupport(&theDOMSupport);
        !          1161: 
        !          1162:        m_stylesheetExecutionContext->setXObjectFactory(&theXObjectFactory);
        !          1163: 
        !          1164:        m_stylesheetExecutionContext->setXSLTProcessor(&theProcessor);
        !          1165:        
        !          1166:        // Set the compiled stylesheet.
        !          1167:        m_stylesheetExecutionContext->setStylesheetRoot(theCompiledStylesheet->getStylesheetRoot());
        !          1168: 
        !          1169:        // Set the parameters if any.
        !          1170:        for (ParamPairVectorType::size_type i = 0; i < m_paramPairs.size(); ++i)
        !          1171:        {
        !          1172:                theProcessor.setStylesheetParam(
        !          1173:                                m_paramPairs[i].first,
        !          1174:                                theXObjectFactory.createString(m_paramPairs[i].second)); /// PAF
        !          1175:        }
        !          1176: 
        !          1177:        // Set the functions if any.
        !          1178:        for (FunctionParamPairVectorType::size_type f = 0; f < m_functionPairs.size(); ++f)
        !          1179:        {
        !          1180:                theXSLTProcessorEnvSupport.installExternalFunctionLocal(
        !          1181:                                m_functionPairs[f].first.getNamespace(),
        !          1182:                                m_functionPairs[f].first.getLocalPart(),
        !          1183:                                *(m_functionPairs[f].second));
        !          1184:        }
        !          1185: 
        !          1186:        // Do the transformation...
        !          1187:        theProcessor.process(
        !          1188:                                theParsedXML.getDocument(),     
        !          1189:                                tempResultTarget,                                       
        !          1190:                                *m_stylesheetExecutionContext);
        !          1191: }
        !          1192: #endif

E-mail: