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

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.5     ! parser      8: static const char *RCSId="$Id: dom.C,v 1.4 2001/09/10 08:23:49 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>
1.5     ! parser     21: #include <util/XMLString.hpp>
1.2       parser     22: #include <XalanTransformer/XalanTransformer.hpp>
1.4       parser     23: #include <XalanTransformer/XalanParsedSource.hpp>
                     24: //#include <DOMSupport/DOMServices.hpp> 
                     25: #include <PlatformSupport/XalanFileOutputStream.hpp>
                     26: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
                     27: #include <XMLSupport/FormatterToXML.hpp>
                     28: #include <XMLSupport/FormatterTreeWalker.hpp>
1.2       parser     29: 
                     30: // defines
                     31: 
1.4       parser     32: #define DOM_CLASS_NAME "dom"
1.2       parser     33: 
                     34: // class
                     35: 
                     36: class MDom : public Methoded {
                     37: public: // VStateless_class
                     38:        Value *create_new_value(Pool& pool) { return new(pool) VDOM(pool, 0); }
                     39: 
                     40: public:
                     41:        MDom(Pool& pool);
                     42: 
                     43: public: // Methoded
                     44:        bool used_directly() { return true; }
                     45: };
                     46: 
                     47: // methods
                     48: 
                     49: static void _load(Request& r, const String& method_name, MethodParams *params) {
                     50:        Pool& pool=r.pool();
                     51:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                     52: 
                     53:        // filename
                     54:        const String& filename=params->as_string(0, "file name must not be code");
                     55: 
                     56:        // filespec
                     57:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                     58:        
                     59: //     XSLTInputSource::XSLTInputSource (  std::istream * stream )  
                     60: //     XalanNode *node=inputSource.getNode();
                     61:        XSLTInputSource inputSource(filespec);
                     62:        XalanParsedSource* parsedSource;
                     63:        int error=vDOM.getXalanTransformer().parseSource(inputSource, parsedSource);
                     64: 
                     65:        if(error)
                     66:                PTHROW(0, 0,
                     67:                        &filename,
                     68:                        vDOM.getXalanTransformer().getLastError());
                     69: 
                     70:        // replace any previous node value
                     71:        vDOM.setParsedSource(parsedSource);
                     72: }
                     73: 
1.5     ! parser     74: const char *strX(const XalanDOMString& s) {
        !            75:        return XMLString::transcode(s.c_str());
        !            76: }
        !            77: 
        !            78: static void _throw(Pool& pool, const String *source, const XSLException& e) {
        !            79:        if(e.getURI().empty())
        !            80:                PTHROW(0, 0,
        !            81:                        source,
        !            82:                        "%s (%s)",
        !            83:                                strX(e.getMessage()),  // message for exception
        !            84:                                strX(e.getType()) // type of exception
        !            85:                );
        !            86:        else
        !            87:                PTHROW(0, 0,
        !            88:                        source,
        !            89:                        "%s (%s) %s(%d:%d)'", 
        !            90:                                strX(e.getMessage()),  // message for exception
        !            91:                                strX(e.getType()), // type of exception
        !            92:                                
        !            93:                                strX(e.getURI()),  // URI for the associated document, if any
        !            94:                                e.getLineNumber(),  // line number, or -1 if unknown
        !            95:                                e.getColumnNumber() // column number, or -1 if unknown
        !            96:                );
        !            97: }
        !            98: 
1.3       parser     99: static void _save(Request& r, const String& method_name, MethodParams *params) {
                    100:        Pool& pool=r.pool();
                    101:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                    102: 
                    103:        // filename
                    104:        const String& filename=params->as_string(0, "file name must not be code");
                    105: 
                    106:        // filespec
                    107:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                    108:        
1.4       parser    109:        XalanParsedSource* parsedSource=vDOM.getParsedSource();
                    110:        if(!parsedSource)
                    111:                PTHROW(0, 0,
                    112:                        &method_name,
                    113:                        "on empty document");
1.3       parser    114: 
1.5     ! parser    115:        try {
        !           116:                XalanDocument *document=parsedSource->getDocument();
        !           117:                XalanFileOutputStream fileOutputStream(XalanDOMString(filespec, strlen(filespec)));
        !           118:                XalanOutputStreamPrintWriter outputStreamPrintWriter(fileOutputStream);
        !           119:                FormatterToXML formatterListener(outputStreamPrintWriter);
        !           120:                FormatterTreeWalker theTreeWalker(formatterListener);
        !           121:                theTreeWalker.traverse(document); // Walk the document and produce the XML...
        !           122:        } catch(const XSLException& e) {
        !           123:                _throw(pool, &method_name, e);
        !           124:        }
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: