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

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.26    ! parser      8: static const char *RCSId="$Id: dom.C,v 1.25 2001/09/18 16:05:42 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"
1.20      parser     18: #include "pa_xslt_stylesheet_manager.h"
                     19: #include "pa_stylesheet_connection.h"
1.26    ! parser     20: #include "pa_vfile.h"
1.23      parser     21: #include "dnode.h"
1.2       parser     22: 
1.16      parser     23: #include <strstream>
1.2       parser     24: #include <Include/PlatformDefinitions.hpp>
                     25: #include <util/PlatformUtils.hpp>
                     26: #include <XalanTransformer/XalanTransformer.hpp>
1.4       parser     27: #include <XalanTransformer/XalanParsedSource.hpp>
1.26    ! parser     28: #include <XMLSupport/FormatterToXML.hpp>
        !            29: #include <XMLSupport/FormatterToHTML.hpp>
        !            30: #include <XMLSupport/FormatterToText.hpp>
        !            31: #include <XMLSupport/FormatterTreeWalker.hpp>
        !            32: #include <PlatformSupport/XalanFileOutputStream.hpp>
        !            33: #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
        !            34: #include <PlatformSupport/DOMStringPrintWriter.hpp>
1.6       parser     35: 
1.2       parser     36: // defines
                     37: 
1.4       parser     38: #define DOM_CLASS_NAME "dom"
1.2       parser     39: 
1.26    ! parser     40: #define DOM_OUTPUT_METHOD_OPTION_NAME "method"
        !            41: #define DOM_OUTPUT_METHOD_OPTION_VALUE_XML "xml"
        !            42: #define DOM_OUTPUT_METHOD_OPTION_VALUE_HTML "html"
        !            43: #define DOM_OUTPUT_METHOD_OPTION_VALUE_TEXT "text"
        !            44: 
        !            45: #define DOM_OUTPUT_ENCODING_OPTION_NAME "encoding"
        !            46: 
        !            47: #define DOM_OUTPUT_DEFAULT_INDENT 4
        !            48: 
1.2       parser     49: // class
                     50: 
1.24      parser     51: class MDom : public MDnode {
1.2       parser     52: public: // VStateless_class
1.11      parser     53:        Value *create_new_value(Pool& pool) { return new(pool) VDom(pool); }
1.2       parser     54: 
                     55: public:
                     56:        MDom(Pool& pool);
                     57: 
                     58: public: // Methoded
                     59:        bool used_directly() { return true; }
                     60: };
                     61: 
                     62: // methods
                     63: 
1.26    ! parser     64: class ParserStringXalanOutputStream: public XalanOutputStream {
        !            65: public:
        !            66:        
        !            67:        explicit ParserStringXalanOutputStream(String& astring) : fstring(astring) {}
        !            68: 
        !            69: protected: // XalanOutputStream
        !            70: 
        !            71:        virtual void writeData(const char *theBuffer, unsigned long theBufferLength) {
        !            72:                char *copy=(char *)fstring.malloc((size_t)theBufferLength);
        !            73:                memcpy(copy, theBuffer, (size_t)theBufferLength);
        !            74:                fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "dom", -1);
        !            75:        }
        !            76: 
        !            77:        virtual void doFlush() {}
        !            78: 
        !            79: private:
        !            80: 
        !            81:        String& fstring;
        !            82:        
        !            83: };
        !            84: 
        !            85: static void create_optioned_listener(
        !            86:                                                                         const char *& content_type, const char *& charset, FormatterListener *& listener, 
        !            87:                                                                         Pool& pool, 
        !            88:                                                                         const String& method_name, MethodParams *params, int index, Writer& writer) {
        !            89:        charset=0;
        !            90:        const String *method=0;
        !            91:        XalanDOMString xalan_encoding;
        !            92: 
        !            93:        Value& voptions=params->as_no_junction(index, "options must not be code");
        !            94:        if(voptions.is_defined()) {
        !            95:                if(Hash *options=voptions.get_hash()) {
        !            96:                        // $.method[xml|html|text]
        !            97:                        if(Value *vmethod=static_cast<Value *>(options->get(*new(pool) 
        !            98:                                String(pool, DOM_OUTPUT_METHOD_OPTION_NAME))))
        !            99:                                method=&vmethod->as_string();
        !           100: 
        !           101:                        // $.encoding[windows-1251|...]
        !           102:                        if(Value *vencoding=static_cast<Value *>(options->get(*new(pool) 
        !           103:                                String(pool, DOM_OUTPUT_ENCODING_OPTION_NAME)))) {
        !           104:                                charset=vencoding->as_string().cstr();
        !           105:                                xalan_encoding.append(charset, strlen(charset));
        !           106:                        }
        !           107:                } else
        !           108:                        PTHROW(0, 0,
        !           109:                                &method_name,
        !           110:                                "options must be hash");
        !           111:        }
        !           112: 
        !           113:        if(!method/*default='xml'*/ || *method == DOM_OUTPUT_METHOD_OPTION_VALUE_XML) {
        !           114:                content_type="text/xml";
        !           115:                listener=new FormatterToXML(writer,
        !           116:                        XalanDOMString(),  // version
        !           117:                        true, // doIndent
        !           118:                        DOM_OUTPUT_DEFAULT_INDENT, // indent 
        !           119:                        xalan_encoding  // encoding
        !           120:                );
        !           121:        } else if(*method == DOM_OUTPUT_METHOD_OPTION_VALUE_HTML) {
        !           122:                content_type="text/html";
        !           123:                listener=new FormatterToHTML(writer,
        !           124:                        xalan_encoding,  // encoding
        !           125:                        XalanDOMString(),  // mediaType 
        !           126:                        XalanDOMString(),  // doctypeSystem; String to be printed at the top of the document 
        !           127:                        XalanDOMString(),  // doctypePublic  
        !           128:                        true, // doIndent 
        !           129:                        DOM_OUTPUT_DEFAULT_INDENT // indent 
        !           130:                );
        !           131:        } else if(*method == DOM_OUTPUT_METHOD_OPTION_VALUE_TEXT) {
        !           132:                content_type="text/plain";
        !           133:                listener=new FormatterToText(writer,
        !           134:                        xalan_encoding  // encoding
        !           135:                );
        !           136:        } else
        !           137:                PTHROW(0, 0,
        !           138:                        method,
        !           139:                        DOM_OUTPUT_METHOD_OPTION_NAME " option is invalid; valid methods are: "
        !           140:                                "'" DOM_OUTPUT_METHOD_OPTION_VALUE_XML "', "
        !           141:                                "'" DOM_OUTPUT_METHOD_OPTION_VALUE_HTML "', "
        !           142:                                "'" DOM_OUTPUT_METHOD_OPTION_VALUE_TEXT "'");                   
        !           143: 
        !           144:        // never reached
        !           145: }
        !           146: 
        !           147: static void _save(Request& r, const String& method_name, MethodParams *params) {
        !           148:        Pool& pool=r.pool();
        !           149:        VDnode& vnode=*static_cast<VDnode *>(r.self);
        !           150: 
        !           151:        // filespec
        !           152:        const String& file_name=params->as_string(1, "file name must not be code");
        !           153:        const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
        !           154:        
        !           155:        // node
        !           156:        XalanNode& node=vnode.get_node(pool, &method_name);
        !           157: 
        !           158:        try {
        !           159:                XalanFileOutputStream stream(XalanDOMString(filespec, strlen(filespec)));
        !           160:                XalanOutputStreamPrintWriter writer(stream);
        !           161:                const char *content_type, *charset;
        !           162:                FormatterListener *formatterListener;
        !           163:                create_optioned_listener(content_type, charset, formatterListener, 
        !           164:                        pool, method_name, params, 0, writer);
        !           165:                FormatterTreeWalker treeWalker(*formatterListener);
        !           166:                treeWalker.traverse(&node); // Walk that node and produce the XML...
        !           167:        } catch(const XSLException& e) {
        !           168:                _throw(pool, &method_name, e);
        !           169:        }
        !           170: }
        !           171: 
        !           172: static void _string(Request& r, const String& method_name, MethodParams *params) {
        !           173:        Pool& pool=r.pool();
        !           174:        VDnode& vnode=*static_cast<VDnode *>(r.self);
        !           175: 
        !           176:        // node
        !           177:        XalanNode& node=vnode.get_node(pool, &method_name);
        !           178: 
        !           179:        try {
        !           180:                String parserString=*new(pool) String(pool);
        !           181:                ParserStringXalanOutputStream stream(parserString);
        !           182:                XalanOutputStreamPrintWriter writer(stream);
        !           183:                const char *content_type, *charset;
        !           184:                FormatterListener *formatterListener;
        !           185:                create_optioned_listener(content_type, charset, formatterListener, 
        !           186:                        pool, method_name, params, 0, writer);
        !           187:                FormatterTreeWalker treeWalker(*formatterListener);
        !           188:                treeWalker.traverse(&node); // Walk that node and produce the XML...
        !           189: 
        !           190:                // write out result
        !           191:                r.write_no_lang(parserString);
        !           192:        } catch(const XSLException& e) {
        !           193:                _throw(pool, &method_name, e);
        !           194:        }
        !           195: }
        !           196: 
        !           197: 
        !           198: static void _file(Request& r, const String& method_name, MethodParams *params) {
        !           199:        Pool& pool=r.pool();
        !           200:        VDnode& vnode=*static_cast<VDnode *>(r.self);
        !           201: 
        !           202:        // node
        !           203:        XalanNode& node=vnode.get_node(pool, &method_name);
        !           204: 
        !           205:        try {
        !           206:                String& parserString=*new(pool) String(pool);
        !           207:                ParserStringXalanOutputStream stream(parserString);
        !           208:                XalanOutputStreamPrintWriter writer(stream);
        !           209:                const char *content_type, *charset;
        !           210:                FormatterListener *formatterListener;
        !           211:                create_optioned_listener(content_type, charset, formatterListener, 
        !           212:                        pool, method_name, params, 0, writer);
        !           213:                FormatterTreeWalker treeWalker(*formatterListener);
        !           214:                treeWalker.traverse(&node); // Walk that node and produce the XML...
        !           215: 
        !           216:                // write out result
        !           217:                VFile& vfile=*new(pool) VFile(pool);
        !           218:                const char *cstr=parserString.cstr();
        !           219:                String *scontent_type=new(pool) String(pool, content_type);
        !           220:                Value *vcontent_type;
        !           221:                if(charset) {
        !           222:                        VHash *vhcontent_type=new(pool) VHash(pool);
        !           223:                        vhcontent_type->hash().put(*value_name, new(pool) VString(*scontent_type));
        !           224:                        String *scharset=new(pool) String(pool, charset);
        !           225:                        vhcontent_type->hash().put(*new(pool) String(pool, "charset"), new(pool) VString(*scharset));
        !           226:                        vcontent_type=vhcontent_type;
        !           227:                } else
        !           228:                        vcontent_type=new(pool) VString(*scontent_type);
        !           229:                vfile.set(false/*tainted*/, cstr, strlen(cstr), 0/*file_name*/, vcontent_type);
        !           230:                r.write_no_lang(vfile);
        !           231:        } catch(const XSLException& e) {
        !           232:                _throw(pool, &method_name, e);
        !           233:        }
        !           234: }
        !           235: 
1.16      parser    236: static void _set(Request& r, const String& method_name, MethodParams *params) {
                    237:        Pool& pool=r.pool();
1.26    ! parser    238:        VDom& vdom=*static_cast<VDom *>(r.self);
1.16      parser    239: 
1.17      parser    240:        Value& vxml=params->as_junction(0, "xml must be code");
                    241:        Temp_lang temp_lang(r, String::UL_XML);
                    242:        const String& xml=r.process(vxml).as_string();
                    243: 
                    244:        std::istrstream stream(xml.cstr());
1.16      parser    245:        XalanParsedSource* parsedSource;
1.26    ! parser    246:        int error=vdom.transformer().parseSource(&stream, parsedSource);
1.16      parser    247: 
                    248:        if(error)
                    249:                PTHROW(0, 0,
                    250:                        &method_name,
1.26    ! parser    251:                        vdom.transformer().getLastError());
1.16      parser    252: 
                    253:        // replace any previous parsed source
1.26    ! parser    254:        vdom.set_parsed_source(*parsedSource);
1.16      parser    255: }
                    256: 
1.2       parser    257: static void _load(Request& r, const String& method_name, MethodParams *params) {
                    258:        Pool& pool=r.pool();
1.26    ! parser    259:        VDom& vdom=*static_cast<VDom *>(r.self);
1.2       parser    260: 
1.10      parser    261:        // filespec
1.20      parser    262:        const String& file_name=params->as_string(0, "file name must not be code");
                    263:        const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
1.2       parser    264:        
                    265:        XalanParsedSource* parsedSource;
1.26    ! parser    266:        int error=vdom.transformer().parseSource(filespec, parsedSource);
1.2       parser    267: 
                    268:        if(error)
                    269:                PTHROW(0, 0,
1.20      parser    270:                        &file_name,
1.26    ! parser    271:                        vdom.transformer().getLastError());
1.2       parser    272: 
1.15      parser    273:        // replace any previous parsed source
1.26    ! parser    274:        vdom.set_parsed_source(*parsedSource);
1.2       parser    275: }
                    276: 
1.12      parser    277: static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning, 
1.20      parser    278:                                                   void *info) {
1.12      parser    279:        XalanTransformer& transformer=*static_cast<XalanTransformer *>(info);
                    280:        const char *attribute_cstr=aattribute.cstr();
                    281:        const char *meaning_cstr=static_cast<Value *>(ameaning)->as_string().cstr();
                    282: 
                    283:        transformer.setStylesheetParam(
1.23      parser    284:                XalanDOMString(attribute_cstr),  
                    285:                XalanDOMString(meaning_cstr));
1.12      parser    286: }
1.10      parser    287: static void _xslt(Request& r, const String& method_name, MethodParams *params) {
                    288:        Pool& pool=r.pool();
1.26    ! parser    289:        VDom& vdom=*static_cast<VDom *>(r.self);
1.10      parser    290: 
1.12      parser    291:        // params
                    292:        if(params->size()>1) {
1.18      parser    293:                Value& vparams=params->as_no_junction(1, "transform parameters parameter must not be code");
1.12      parser    294:                if(vparams.is_defined())
                    295:                        if(Hash *params=vparams.get_hash())
1.26    ! parser    296:                                params->for_each(add_xslt_param, &vdom.transformer());
1.12      parser    297:                        else
                    298:                                PTHROW(0, 0,
                    299:                                        &method_name,
1.18      parser    300:                                        "transform parameters parameter must be hash");
1.12      parser    301:        }
                    302: 
1.10      parser    303:        // source
1.26    ! parser    304:        XalanParsedSource &parsed_source=vdom.get_parsed_source(pool, &method_name);
1.10      parser    305: 
                    306:        // stylesheet
1.20      parser    307:        const String& stylesheet_file_name=params->as_string(0, "file name must not be code");
                    308:        const String& stylesheet_filespec=r.absolute(stylesheet_file_name);
                    309:        //_asm int 3;
                    310:        Stylesheet_connection& connection=XSLT_stylesheet_manager->get_connection(stylesheet_filespec);
1.10      parser    311: 
                    312:        // target
1.26    ! parser    313:        XalanDocument* target=vdom.parser_liaison().createDocument();
1.10      parser    314:        XSLTResultTarget domResultTarget(target);
                    315: 
                    316:        // transform
1.26    ! parser    317:        int error=vdom.transformer().transform(
1.20      parser    318:                parsed_source, 
                    319:                &connection.stylesheet(), 
                    320:                domResultTarget);
                    321:        connection.close();
1.10      parser    322:        if(error)
                    323:                PTHROW(0, 0,
1.20      parser    324:                        &stylesheet_file_name,
1.26    ! parser    325:                        vdom.transformer().getLastError());
1.10      parser    326: 
                    327:        // write out result
1.11      parser    328:        VDom& result=*new(pool) VDom(pool);
                    329:        result.set_document(*target);
                    330:        r.write_no_lang(result);
1.10      parser    331: }
                    332: 
1.2       parser    333: // constructor
                    334: 
1.24      parser    335: MDom::MDom(Pool& apool) : MDnode(apool) {
1.4       parser    336:        set_name(*NEW String(pool(), DOM_CLASS_NAME));
1.26    ! parser    337: 
        !           338:        // ^dom.save[options hash;some.xml]
        !           339:        add_native_method("save", Method::CT_DYNAMIC, _save, 2, 2);
        !           340: 
        !           341:        // ^dom.string[options hash] <doc/>
        !           342:        add_native_method("string", Method::CT_DYNAMIC, _string, 1, 1);
        !           343: 
        !           344:        // ^dom.file[options hash] file with "<doc/>"
        !           345:        add_native_method("file", Method::CT_DYNAMIC, _file, 1, 1);
1.16      parser    346: 
                    347:        // ^dom::set[<some>xml</some>]
                    348:        add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1);
1.2       parser    349: 
1.3       parser    350:        // ^dom::load[some.xml]
1.2       parser    351:        add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
1.10      parser    352: 
1.20      parser    353:        // ^dom.xslt[stylesheet file_name]
                    354:        // ^dom.xslt[stylesheet file_name;params hash]
1.12      parser    355:        add_native_method("xslt", Method::CT_DYNAMIC, _xslt, 1, 2);
1.6       parser    356: 
1.2       parser    357: }
                    358: // global variable
                    359: 
                    360: Methoded *Dom_class;
                    361: 
                    362: // creator
                    363: 
                    364: Methoded *MDom_create(Pool& pool) {
                    365:        return Dom_class=new(pool) MDom(pool);
                    366: }

E-mail: