Annotation of parser3/src/classes/dom.C, revision 1.4

1.2       parser      1: /** @file
                      2:        Parser: @b dom parser class.
                      3: 
                      4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      5: 
                      6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      7: */
1.4     ! parser      8: static const char *RCSId="$Id: dom.C,v 1.3 2001/09/07 16:51:24 parser Exp $"; 
1.2       parser      9: 
                     10: #if _MSC_VER
                     11: #      pragma warning(disable:4291)   // disable warning 
                     12: //     "no matching operator delete found; memory will not be freed if initialization throws an exception
                     13: #endif
                     14: 
                     15: #include "classes.h"
                     16: #include "pa_request.h"
                     17: #include "pa_vdom.h"
                     18: 
                     19: #include <Include/PlatformDefinitions.hpp>
                     20: #include <util/PlatformUtils.hpp>
                     21: #include <XalanTransformer/XalanTransformer.hpp>
1.4     ! parser     22: #include <XalanTransformer/XalanParsedSource.hpp>
        !            23: //#include <DOMSupport/DOMServices.hpp> 
        !            24: #include <PlatformSupport/XalanFileOutputStream.hpp>
        !            25: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
        !            26: #include <XMLSupport/FormatterToXML.hpp>
        !            27: #include <XMLSupport/FormatterTreeWalker.hpp>
1.2       parser     28: 
                     29: // defines
                     30: 
1.4     ! parser     31: #define DOM_CLASS_NAME "dom"
1.2       parser     32: 
                     33: // class
                     34: 
                     35: class MDom : public Methoded {
                     36: public: // VStateless_class
                     37:        Value *create_new_value(Pool& pool) { return new(pool) VDOM(pool, 0); }
                     38: 
                     39: public:
                     40:        MDom(Pool& pool);
                     41: 
                     42: public: // Methoded
                     43:        bool used_directly() { return true; }
                     44: };
                     45: 
                     46: // methods
                     47: 
                     48: static void _load(Request& r, const String& method_name, MethodParams *params) {
                     49:        Pool& pool=r.pool();
                     50:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                     51: 
                     52:        // filename
                     53:        const String& filename=params->as_string(0, "file name must not be code");
                     54: 
                     55:        // filespec
                     56:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                     57:        
                     58: //     XSLTInputSource::XSLTInputSource (  std::istream * stream )  
                     59: //     XalanNode *node=inputSource.getNode();
                     60:        XSLTInputSource inputSource(filespec);
                     61:        XalanParsedSource* parsedSource;
                     62:        int error=vDOM.getXalanTransformer().parseSource(inputSource, parsedSource);
                     63: 
                     64:        if(error)
                     65:                PTHROW(0, 0,
                     66:                        &filename,
                     67:                        vDOM.getXalanTransformer().getLastError());
                     68: 
                     69:        // replace any previous node value
                     70:        vDOM.setParsedSource(parsedSource);
                     71: }
                     72: 
1.3       parser     73: static void _save(Request& r, const String& method_name, MethodParams *params) {
                     74:        Pool& pool=r.pool();
                     75:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                     76: 
                     77:        // filename
                     78:        const String& filename=params->as_string(0, "file name must not be code");
                     79: 
                     80:        // filespec
                     81:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                     82:        
1.4     ! parser     83:        XalanParsedSource* parsedSource=vDOM.getParsedSource();
        !            84:        if(!parsedSource)
        !            85:                PTHROW(0, 0,
        !            86:                        &method_name,
        !            87:                        "on empty document");
1.3       parser     88: 
1.4     ! parser     89:        XalanDocument *document=parsedSource->getDocument();
1.3       parser     90: 
1.4     ! parser     91: /*
        !            92:        XalanDOMString data;
        !            93:        DOMServices::getNodeData(*document, data);
        !            94:        const XalanDOMChar *cstr=data.c_str();
        !            95: ^^^^^^^^^^^just a text from one node
        !            96: */
        !            97: 
        !            98:          //const XalanDOMString & theFileName
        !            99:        XalanFileOutputStream fileOutputStream(XalanDOMString(filespec, strlen(filespec)));
        !           100:        XalanOutputStreamPrintWriter outputStreamPrintWriter(fileOutputStream);
        !           101:        FormatterToXML formatterListener(outputStreamPrintWriter);
        !           102: /*
        !           103:        // Sends the data for a node to a FormatterListener
        !           104:        DOMServices::getNodeData(*document, formatterListener, 
        !           105:                (DOMServices::MemberFunctionPtr)formatterListener.characters);
        !           106:        formatterListener.endDocument();
        !           107: */
        !           108:        // Create a FormatterTreeWalker with the the
        !           109:        // new formatter...
        !           110:        FormatterTreeWalker theTreeWalker(formatterListener);
        !           111: 
        !           112:        // Walk the document and produce the XML...
        !           113:        theTreeWalker.traverse(document);
        !           114: /*
        !           115:        // Set up a XercesParserLiaison and use it to wrap the DOM_Document
        !           116:        // in a XalanDocument.
        !           117:        XercesDOMSupport   theDOMSupport;
        !           118:        XercesParserLiaison     theParserLiaison(theDOMSupport);
        !           119: 
        !           120:        // You can also convert the XalanDocument to a Xerces DOM_Document.
        !           121:        DOM_Document xercesDocument = theParserLiaison.mapXercesDocument(xalanDocument);
        !           122:        //DOMPrint sample
        !           123: */
        !           124:        // TODO error handling ...
1.3       parser    125: }
                    126: 
1.2       parser    127: // constructor
                    128: 
                    129: MDom::MDom(Pool& apool) : Methoded(apool) {
1.4     ! parser    130:        set_name(*NEW String(pool(), DOM_CLASS_NAME));
1.2       parser    131: 
1.3       parser    132:        // ^dom::load[some.xml]
1.2       parser    133:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.3       parser    134: 
                    135:        // ^dom.save[some.xml]
                    136:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.2       parser    137: 
                    138: }
                    139: // global variable
                    140: 
                    141: Methoded *Dom_class;
                    142: 
                    143: // creator
                    144: 
                    145: Methoded *MDom_create(Pool& pool) {
                    146:        // Use the static initializers to initialize the Xalan-C++ and Xerces-C++ platforms. 
                    147:        // You must initialize Xerces-C++ once per process
                    148:        XMLPlatformUtils::Initialize();
                    149:        XalanTransformer::initialize();
1.4     ! parser    150:        // Must be called before any other functions are called. 
        !           151: //     DOMServices::initialize (); 
1.2       parser    152: 
                    153: 
                    154:        return Dom_class=new(pool) MDom(pool);
                    155: }

E-mail: