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

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

E-mail: