dbertoni    00/05/01 07:57:49

  Added:       c/samples/ExternalFunction ExternalFunction.cpp
                        ExternalFunction.dsp foo.xml foo.xsl
  Log:
  Initial revision.
  
  Revision  Changes    Path
  1.1                  xml-xalan/c/samples/ExternalFunction/ExternalFunction.cpp
  
  Index: ExternalFunction.cpp
  ===================================================================
  #include <cmath>
  #include <ctime>
  #include <iostream>
  #include <fstream>
  
  
  
  #include <util/PlatformUtils.hpp>
  
  
  
  #include <PlatformSupport/DOMStringHelper.hpp>
  
  
  
  #include <DOMSupport/DOMSupportDefault.hpp>
  
  
  
  #include <XPath/XObjectFactoryDefault.hpp>
  #include <XPath/XPath.hpp>
  #include <XPath/XPathSupportDefault.hpp>
  #include <XPath/XPathFactoryDefault.hpp>
  
  
  
  #include <XSLT/StylesheetConstructionContextDefault.hpp>
  #include <XSLT/StylesheetExecutionContextDefault.hpp>
  #include <XSLT/XSLTEngineImpl.hpp>
  #include <XSLT/XSLTInputSource.hpp>
  #include <XSLT/XSLTProcessorEnvSupportDefault.hpp>
  #include <XSLT/XSLTResultTarget.hpp>
  
  
  
  #include <XercesParserLiaison/XercesParserLiaison.hpp>
  
  
  
  #include <XercesPlatformSupport/TextFileOutputStream.hpp>
  #include <XercesPlatformSupport/XercesDOMPrintWriter.hpp>
  
  
  
  // This class defines a function that will return the square root
  // of its argument.
  class FunctionSquareRoot : public Function
  {
  public:
  
        /**
         * Execute an XPath function object.  The function must return a valid
         * object.
         *
         * @param executionContext executing context
         * @param context          current context node
         * @param opPos            current op position
         * @param args             vector of pointers to XObject arguments
         * @return                 pointer to the result XObject
         */
        virtual XObject*
        execute(
                        XPathExecutionContext&                  
executionContext,
                        XalanNode*                                              
context,
                        int                                                     
        opPos,
                        const XObjectArgVectorType&             args)
        {
                if (args.size() != 1)
                {
                        executionContext.error("The square-root() function 
takes one argument!", context);
                }
  
                assert(args[0] != 0);
  
                return 
executionContext.getXObjectFactory().createNumber(sqrt(args[0]->num()));
        }
  
        /**
         * Create a copy of the function object.
         *
         * @return pointer to the new object
         */
  #if defined(XALAN_NO_COVARIANT_RETURN_TYPE)
        virtual Function*
  #else
        virtual FunctionSquareRoot*
  #endif
        clone() const
        {
                return new FunctionSquareRoot(*this);
        }
  
  private:
  
        // Not implemented...
        FunctionSquareRoot&
        operator=(const FunctionSquareRoot&);
  
        bool
        operator==(const FunctionSquareRoot&) const;
  };
  
  
  
  // This class defines a function that will return the cube
  // of its argument.
  class FunctionCube : public Function
  {
  public:
  
        /**
         * Execute an XPath function object.  The function must return a valid
         * object.
         *
         * @param executionContext executing context
         * @param context          current context node
         * @param opPos            current op position
         * @param args             vector of pointers to XObject arguments
         * @return                 pointer to the result XObject
         */
        virtual XObject*
        execute(
                        XPathExecutionContext&                  
executionContext,
                        XalanNode*                                              
context,
                        int                                                     
        opPos,
                        const XObjectArgVectorType&             args)
        {
                if (args.size() != 1)
                {
                        executionContext.error("The cube() function takes one 
argument!", context);
                }
  
                assert(args[0] != 0);
  
                return 
executionContext.getXObjectFactory().createNumber(pow(args[0]->num(), 3));
        }
  
        /**
         * Create a copy of the function object.
         *
         * @return pointer to the new object
         */
  #if defined(XALAN_NO_COVARIANT_RETURN_TYPE)
        virtual Function*
  #else
        virtual FunctionCube*
  #endif
        clone() const
        {
                return new FunctionCube(*this);
        }
  
  private:
  
        // Not implemented...
        FunctionCube&
        operator=(const FunctionCube&);
  
        bool
        operator==(const FunctionCube&) const;
  };
  
  
  
  // This class defines a function that runs the C function
  // asctime() using the current system time.
  class FunctionAsctime : public Function
  {
  public:
  
        /**
         * Execute an XPath function object.  The function must return a valid
         * object.
         *
         * @param executionContext executing context
         * @param context          current context node
         * @param opPos            current op position
         * @param args             vector of pointers to XObject arguments
         * @return                 pointer to the result XObject
         */
        virtual XObject*
        execute(
                        XPathExecutionContext&                  
executionContext,
                        XalanNode*                                              
context,
                        int                                                     
        opPos,
                        const XObjectArgVectorType&             args)
        {
                if (args.size() != 0)
                {
                        executionContext.error("The asctime() function takes no 
arguments!", context);
                }
  
                time_t  theTime;
  
                time(&theTime);
  
                char* const     theTimeString = asctime(localtime(&theTime));
  
                // The resulting string has a newline character at the end,
                // so get rid of it.
                theTimeString[strlen(theTimeString) - 1] = '\0';
  
                return 
executionContext.getXObjectFactory().createString(XalanDOMString(theTimeString));
        }
  
        /**
         * Create a copy of the function object.
         *
         * @return pointer to the new object
         */
  #if defined(XALAN_NO_COVARIANT_RETURN_TYPE)
        virtual Function*
  #else
        virtual FunctionAsctime*
  #endif
        clone() const
        {
                return new FunctionAsctime(*this);
        }
  
  private:
  
        // Not implemented...
        FunctionAsctime&
        operator=(const FunctionAsctime&);
  
        bool
        operator==(const FunctionAsctime&) const;
  };
  
  
  
  int
  main(
                        int                             argc,
                        const char*             /* argv */[])
  {
  #if !defined(XALAN_NO_NAMESPACES)
        using std::cerr;
        using std::endl;
        using std::ofstream;
  #endif
  
        if (argc != 1)
        {
                cerr << "Usage: ExternalFunction"
                         << endl
                         << endl;
        }
        else
        {
                try
                {
                        // Call the static initializers...
                        XMLPlatformUtils::Initialize();
                        XSLTEngineImpl::Initialize();
  
                        // Create the support objects that are necessary for 
running the processor...
                        DOMSupportDefault                               
theDOMSupport;
                        XercesParserLiaison                             
theParserLiaison(theDOMSupport);
                        XPathSupportDefault                             
theXPathSupport(theDOMSupport);
                        XSLTProcessorEnvSupportDefault  
theXSLTProcessorEnvSupport;
                        XObjectFactoryDefault                   
theXObjectFactory(theXSLTProcessorEnvSupport, theXPathSupport);
                        XPathFactoryDefault                             
theXPathFactory;
  
                        // Create a processor...
                        XSLTEngineImpl  theProcessor(
                                        theParserLiaison,
                                        theXPathSupport,
                                        theXSLTProcessorEnvSupport,
                                        theXObjectFactory,
                                        theXPathFactory);
  
                        // Connect the processor to the support object...
                        theXSLTProcessorEnvSupport.setProcessor(&theProcessor);
  
                        // Use the parser liaison as the formatter...
                        theProcessor.setFormatter(&theParserLiaison);
  
                        // Create a stylesheet construction context, and a 
stylesheet
                        // execution context...
                        StylesheetConstructionContextDefault    
theConstructionContext(
                                                theProcessor,
                                                theXSLTProcessorEnvSupport,
                                                theXObjectFactory,
                                                theXPathFactory);
  
                        StylesheetExecutionContextDefault               
theExecutionContext(
                                                theProcessor,
                                                theXSLTProcessorEnvSupport,
                                                theXPathSupport,
                                                theXObjectFactory);
  
                        // Our input files...
                        // WARNING!!! You may need to modify these absolute 
paths depending on where
                        // you've put the Xalan sources.
                        const DOMString         theXMLFileName("foo.xml");
                        const DOMString         theXSLFileName("foo.xsl");
  
                        // Our input sources...
                        XSLTInputSource         
theInputSource(c_wstr(theXMLFileName));
                        XSLTInputSource         
theStylesheetSource(c_wstr(theXSLFileName));
  
                        // Our output target...
                        TextFileOutputStream    theOutputStream("foo.out");
                        XercesDOMPrintWriter    
theResultWriter(theOutputStream);
                        XSLTResultTarget                
theResultTarget(&theResultWriter);
  
                        // Install the function directly into the XPath
                        // function table.  We don't recommend doing this,
                        // but you can if you want to be evil! ;-)  Since
                        // the function is in the XPath table, the XPath
                        // parser will treat it like any other XPath
                        // function.  Therefore, it cannot have a namespace
                        // prefix.  It will also be available to every
                        // processor in the executable's process, since there
                        // is one static XPath function table.
                        XPath::installFunction(
                                "asctime",
                                FunctionAsctime());
  
                        // The namespace for our functions...
                        const XalanDOMString    
theNamespace("http://ExternalFunction.xalan-c++.xml.apache.org";);
  
                        // Install the function in the global space.  All
                        // instances will know about the function, so all
                        // processors which use an instance of this the
                        // class XPathEnvSupportDefault will be able to
                        // use the function.
                        
XSLTProcessorEnvSupportDefault::installExternalFunctionGlobal(
                                theNamespace,
                                "square-root",
                                FunctionSquareRoot());
  
                        // Install the function in the local space.  It will 
only
                        // be installed in this instance, so no other instances
                        // will know about the function...
                        theXSLTProcessorEnvSupport.installExternalFunctionLocal(
                                theNamespace,
                                "cube",
                                FunctionCube());
  
                        theProcessor.process(
                                                theInputSource,
                                                &theStylesheetSource,
                                                theResultTarget,
                                                theConstructionContext,
                                                theExecutionContext);
                }
                catch(...)
                {
                        cerr << "Exception caught!!!"
                                 << endl
                                 << endl;
                }
        }
  
        return 0;
  }
  
  
  
  1.1                  xml-xalan/c/samples/ExternalFunction/ExternalFunction.dsp
  
  Index: ExternalFunction.dsp
  ===================================================================
  # Microsoft Developer Studio Project File - Name="ExternalFunction" - Package 
Owner=<4>
  # Microsoft Developer Studio Generated Build File, Format Version 6.00
  # ** DO NOT EDIT **
  
  # TARGTYPE "Win32 (x86) Console Application" 0x0103
  
  CFG=ExternalFunction - Win32 Debug
  !MESSAGE This is not a valid makefile. To build this project using NMAKE,
  !MESSAGE use the Export Makefile command and run
  !MESSAGE 
  !MESSAGE NMAKE /f "ExternalFunction.mak".
  !MESSAGE 
  !MESSAGE You can specify a configuration when running NMAKE
  !MESSAGE by defining the macro CFG on the command line. For example:
  !MESSAGE 
  !MESSAGE NMAKE /f "ExternalFunction.mak" CFG="ExternalFunction - Win32 Debug"
  !MESSAGE 
  !MESSAGE Possible choices for configuration are:
  !MESSAGE 
  !MESSAGE "ExternalFunction - Win32 Release" (based on "Win32 (x86) Console 
Application")
  !MESSAGE "ExternalFunction - Win32 Debug" (based on "Win32 (x86) Console 
Application")
  !MESSAGE 
  
  # Begin Project
  # PROP AllowPerConfigDependencies 0
  # PROP Scc_ProjName ""
  # PROP Scc_LocalPath ""
  CPP=cl.exe
  RSC=rc.exe
  
  !IF  "$(CFG)" == "ExternalFunction - Win32 Release"
  
  # PROP BASE Use_MFC 0
  # PROP BASE Use_Debug_Libraries 0
  # PROP BASE Output_Dir "Release"
  # PROP BASE Intermediate_Dir "Release"
  # PROP BASE Target_Dir ""
  # PROP Use_MFC 0
  # PROP Use_Debug_Libraries 0
  # PROP Output_Dir "..\..\Build\Win32\VC6\Release"
  # PROP Intermediate_Dir "..\..\Build\Win32\VC6\Release\ExternalFunction"
  # PROP Ignore_Export_Lib 0
  # PROP Target_Dir ""
  # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D 
"_MBCS" /YX /FD /c
  # ADD CPP /nologo /MDd /W4 /GR /GX /O2 /I "..\..\..\..\xml-xerces\c\src" /I 
"..\..\src" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
  # ADD BASE RSC /l 0x409 /d "NDEBUG"
  # ADD RSC /l 0x409 /d "NDEBUG"
  BSC32=bscmake.exe
  # ADD BASE BSC32 /nologo
  # ADD BSC32 /nologo
  LINK32=link.exe
  # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib 
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib 
odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib 
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib 
odbccp32.lib /nologo /subsystem:console /machine:I386
  # ADD LINK32 ..\..\..\..\xml-xerces\c\Build\Win32\VC6\Release\*.lib 
..\..\Build\Win32\VC6\Release\*.lib kernel32.lib user32.lib gdi32.lib 
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib 
uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib 
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib 
odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
  
  !ELSEIF  "$(CFG)" == "ExternalFunction - Win32 Debug"
  
  # PROP BASE Use_MFC 0
  # PROP BASE Use_Debug_Libraries 1
  # PROP BASE Output_Dir "Debug"
  # PROP BASE Intermediate_Dir "Debug"
  # PROP BASE Target_Dir ""
  # PROP Use_MFC 0
  # PROP Use_Debug_Libraries 1
  # PROP Output_Dir "..\..\Build\Win32\VC6\Debug"
  # PROP Intermediate_Dir "..\..\Build\Win32\VC6\Debug\ExternalFunction"
  # PROP Ignore_Export_Lib 0
  # PROP Target_Dir ""
  # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D 
"_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
  # ADD CPP /nologo /MDd /W4 /Gm /GR /GX /Zi /Od /I 
"..\..\..\..\xml-xerces\c\src" /I "..\..\src" /D "WIN32" /D "_DEBUG" /D 
"_CONSOLE" /D "_MBCS" /D "XML_DEBUG" /YX /FD /GZ /c
  # ADD BASE RSC /l 0x409 /d "_DEBUG"
  # ADD RSC /l 0x409 /d "_DEBUG"
  BSC32=bscmake.exe
  # ADD BASE BSC32 /nologo
  # ADD BSC32 /nologo
  LINK32=link.exe
  # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib 
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib 
odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib 
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib 
odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
  # ADD LINK32 ..\..\..\..\xml-xerces\c\Build\Win32\VC6\Debug\*.lib 
..\..\Build\Win32\VC6\Debug\*.lib kernel32.lib user32.lib gdi32.lib 
winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib 
uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib 
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib 
odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 
/pdbtype:sept
  
  !ENDIF 
  
  # Begin Target
  
  # Name "ExternalFunction - Win32 Release"
  # Name "ExternalFunction - Win32 Debug"
  # Begin Group "Source Files"
  
  # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
  # Begin Source File
  
  SOURCE=.\ExternalFunction.cpp
  # End Source File
  # End Group
  # Begin Group "Header Files"
  
  # PROP Default_Filter "h;hpp;hxx;hm;inl"
  # End Group
  # Begin Group "Resource Files"
  
  # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
  # End Group
  # End Target
  # End Project
  
  
  
  1.1                  xml-xalan/c/samples/ExternalFunction/foo.xml
  
  Index: foo.xml
  ===================================================================
  <?xml version="1.0"?>
  <doc>Hello</doc>
  
  
  
  1.1                  xml-xalan/c/samples/ExternalFunction/foo.xsl
  
  Index: foo.xsl
  ===================================================================
  <?xml version="1.0"?> 
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"
                                
xmlns:external="http://ExternalFunction.xalan-c++.xml.apache.org";>
    <xsl:template match="doc">
      <out><xsl:value-of select="external:cube(external:square-root(9))"/></out>
        <xsl:text>
  </xsl:text>
      <out><xsl:value-of select="asctime()"/></out>
    </xsl:template>
  </xsl:stylesheet>
  
  
  

Reply via email to