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

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.6     ! parser      8: static const char *RCSId="$Id: dom.C,v 1.5 2001/09/10 09:02:20 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 <PlatformSupport/XalanFileOutputStream.hpp>
                     25: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
1.6     ! parser     26: #include <PlatformSupport/DOMStringPrintWriter.hpp>
1.4       parser     27: #include <XMLSupport/FormatterToXML.hpp>
                     28: #include <XMLSupport/FormatterTreeWalker.hpp>
1.2       parser     29: 
1.6     ! parser     30: 
1.2       parser     31: // defines
                     32: 
1.4       parser     33: #define DOM_CLASS_NAME "dom"
1.2       parser     34: 
                     35: // class
                     36: 
                     37: class MDom : public Methoded {
                     38: public: // VStateless_class
                     39:        Value *create_new_value(Pool& pool) { return new(pool) VDOM(pool, 0); }
                     40: 
                     41: public:
                     42:        MDom(Pool& pool);
                     43: 
                     44: public: // Methoded
                     45:        bool used_directly() { return true; }
                     46: };
                     47: 
                     48: // methods
                     49: 
                     50: static void _load(Request& r, const String& method_name, MethodParams *params) {
                     51:        Pool& pool=r.pool();
                     52:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                     53: 
                     54:        // filename
                     55:        const String& filename=params->as_string(0, "file name must not be code");
                     56: 
                     57:        // filespec
                     58:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                     59:        
                     60: //     XSLTInputSource::XSLTInputSource (  std::istream * stream )  
                     61: //     XalanNode *node=inputSource.getNode();
                     62:        XSLTInputSource inputSource(filespec);
                     63:        XalanParsedSource* parsedSource;
                     64:        int error=vDOM.getXalanTransformer().parseSource(inputSource, parsedSource);
                     65: 
                     66:        if(error)
                     67:                PTHROW(0, 0,
                     68:                        &filename,
                     69:                        vDOM.getXalanTransformer().getLastError());
                     70: 
                     71:        // replace any previous node value
                     72:        vDOM.setParsedSource(parsedSource);
                     73: }
                     74: 
1.5       parser     75: const char *strX(const XalanDOMString& s) {
                     76:        return XMLString::transcode(s.c_str());
                     77: }
                     78: 
                     79: static void _throw(Pool& pool, const String *source, const XSLException& e) {
                     80:        if(e.getURI().empty())
                     81:                PTHROW(0, 0,
                     82:                        source,
                     83:                        "%s (%s)",
                     84:                                strX(e.getMessage()),  // message for exception
                     85:                                strX(e.getType()) // type of exception
                     86:                );
                     87:        else
                     88:                PTHROW(0, 0,
                     89:                        source,
                     90:                        "%s (%s) %s(%d:%d)'", 
                     91:                                strX(e.getMessage()),  // message for exception
                     92:                                strX(e.getType()), // type of exception
                     93:                                
                     94:                                strX(e.getURI()),  // URI for the associated document, if any
                     95:                                e.getLineNumber(),  // line number, or -1 if unknown
                     96:                                e.getColumnNumber() // column number, or -1 if unknown
                     97:                );
                     98: }
                     99: 
1.6     ! parser    100: class ParserStringOutputStream: public XalanOutputStream {
        !           101: public:
        !           102:        
        !           103:        explicit
        !           104:                ParserStringOutputStream(String& astring) : fstring(astring) {}
        !           105: /*     
        !           106:        virtual
        !           107:                ~XalanNullOutputStream() {}
        !           108: */     
        !           109: protected:
        !           110:        
        !           111:        // These are inherited from XalanOutputStream...
        !           112:        virtual void
        !           113:                writeData(
        !           114:                const char*     theBuffer,
        !           115:                unsigned long   theBufferLength) {
        !           116:                char *copy=(char *)fstring.malloc((size_t)theBufferLength);
        !           117:                memcpy(copy, theBuffer, (size_t)theBufferLength);
        !           118:                fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "dom", -1);
        !           119:        }
        !           120: 
        !           121:        virtual void
        !           122:                doFlush() {}
        !           123: 
        !           124: private:
        !           125: 
        !           126:        String& fstring;
        !           127:        
        !           128: };
        !           129: 
        !           130: 
1.3       parser    131: static void _save(Request& r, const String& method_name, MethodParams *params) {
                    132:        Pool& pool=r.pool();
                    133:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
                    134: 
                    135:        // filename
                    136:        const String& filename=params->as_string(0, "file name must not be code");
                    137: 
                    138:        // filespec
                    139:        const char *filespec=r.absolute(filename).cstr(String::UL_FILE_NAME);
                    140:        
1.4       parser    141:        XalanParsedSource* parsedSource=vDOM.getParsedSource();
                    142:        if(!parsedSource)
                    143:                PTHROW(0, 0,
                    144:                        &method_name,
                    145:                        "on empty document");
1.3       parser    146: 
1.5       parser    147:        try {
                    148:                XalanDocument *document=parsedSource->getDocument();
1.6     ! parser    149:                XalanFileOutputStream stream(XalanDOMString(filespec, strlen(filespec)));
        !           150:                XalanOutputStreamPrintWriter writer(stream);
        !           151:                FormatterToXML formatterListener(writer);
        !           152:                FormatterTreeWalker treeWalker(formatterListener);
        !           153:                treeWalker.traverse(document); // Walk the document and produce the XML...
        !           154:        } catch(const XSLException& e) {
        !           155:                _throw(pool, &method_name, e);
        !           156:        }
        !           157: }
        !           158: 
        !           159: static void _string(Request& r, const String& method_name, MethodParams *params) {
        !           160:        Pool& pool=r.pool();
        !           161:        VDOM& vDOM=*static_cast<VDOM *>(r.self);
        !           162: 
        !           163:        XalanParsedSource* parsedSource=vDOM.getParsedSource();
        !           164:        if(!parsedSource)
        !           165:                PTHROW(0, 0,
        !           166:                        &method_name,
        !           167:                        "on empty document");
        !           168: 
        !           169:        try {
        !           170:                XalanDocument *document=parsedSource->getDocument();
        !           171:                String *parserString=new(pool) String(pool);
        !           172:                ParserStringOutputStream stream(*parserString);
        !           173:                XalanOutputStreamPrintWriter writer(stream);
        !           174:                FormatterToXML formatterListener(writer);
        !           175:                FormatterTreeWalker treeWalker(formatterListener);
        !           176:                treeWalker.traverse(document); // Walk the document and produce the XML...
        !           177: 
        !           178:                // write out result
        !           179:                r.write_no_lang(*parserString);
1.5       parser    180:        } catch(const XSLException& e) {
                    181:                _throw(pool, &method_name, e);
                    182:        }
1.3       parser    183: }
                    184: 
1.2       parser    185: // constructor
                    186: 
                    187: MDom::MDom(Pool& apool) : Methoded(apool) {
1.4       parser    188:        set_name(*NEW String(pool(), DOM_CLASS_NAME));
1.2       parser    189: 
1.3       parser    190:        // ^dom::load[some.xml]
1.2       parser    191:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.3       parser    192: 
                    193:        // ^dom.save[some.xml]
                    194:        add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.2       parser    195: 
1.6     ! parser    196:        // ^dom.string[] <doc/>
        !           197:        add_native_method("string", Method::CT_DYNAMIC, _string, 0, 0);
        !           198: 
1.2       parser    199: }
                    200: // global variable
                    201: 
                    202: Methoded *Dom_class;
                    203: 
                    204: // creator
                    205: 
                    206: Methoded *MDom_create(Pool& pool) {
                    207:        // Use the static initializers to initialize the Xalan-C++ and Xerces-C++ platforms. 
                    208:        // You must initialize Xerces-C++ once per process
                    209:        XMLPlatformUtils::Initialize();
                    210:        XalanTransformer::initialize();
                    211: 
                    212: 
                    213:        return Dom_class=new(pool) MDom(pool);
                    214: }

E-mail: