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

1.1       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: #include "classes.h"
        !             9: #ifdef XML
1.1       parser     10: 
1.5     ! parser     11: static const char *RCSId="$Id: dnode.C,v 1.4 2001/09/20 14:25:06 parser Exp $"; 
1.1       parser     12: 
                     13: #include "pa_request.h"
                     14: #include "pa_vdnode.h"
1.2       parser     15: 
1.1       parser     16: #include "dnode.h"
                     17: 
                     18: #include <util/XMLString.hpp>
                     19: #include <XalanSourceTree/XalanSourceTreeDOMSupport.hpp>
                     20: #include <XPath/XPathEvaluator.hpp>
                     21: #include <XPath/NodeRefList.hpp>
                     22: 
                     23: // defines
                     24: 
                     25: #define DNODE_CLASS_NAME "dnode"
                     26: 
                     27: // class
                     28: 
                     29: // methods
                     30: 
1.3       parser     31: static void _select(Request& r, const String& method_name, MethodParams *params) {
1.1       parser     32: //     _asm int 3;
                     33:        Pool& pool=r.pool();
1.2       parser     34:        VDnode& vnode=*static_cast<VDnode *>(r.self);
1.1       parser     35: 
                     36:        // expression
                     37:        const String& expression=params->as_string(0, "expression must not be code");
                     38:        const char *expression_cstr=expression.cstr(String::UL_AS_IS);
                     39:        XalanDOMString dstring(expression_cstr);
                     40:        const XalanDOMChar *expression_dcstr=dstring.c_str();
                     41: 
                     42:        XPathEvaluator evaluator;
                     43:        // Initialize the XalanSourceTree subsystem...
                     44: //     XalanSourceTreeInit             theSourceTreeInit;
                     45:        // We'll use these to parse the XML file.
                     46:        XalanSourceTreeDOMSupport dom_support;
                     47: 
                     48:        try {
                     49:                NodeRefList list=evaluator.selectNodeList(dom_support, 
1.2       parser     50:                        &vnode.get_node(pool, &method_name), 
1.1       parser     51:                        expression_dcstr);
                     52: 
                     53:                VHash& result=*new(pool) VHash(pool);
                     54:                for(int i=0; i<list.getLength(); i++) {
                     55:                        String& skey=*new(pool) String(pool);
                     56:                        {
                     57:                                char *buf=(char *)pool.malloc(MAX_NUMBER);
                     58:                                snprintf(buf, MAX_NUMBER, "%d", i);
                     59:                                skey << buf;
                     60:                        }
                     61: 
                     62:                        result.hash().put(skey, new(pool) VDnode(pool, list.item(i)));
                     63:                }
                     64:                result.set_name(method_name);
                     65:                r.write_no_lang(result);
                     66:        } catch(const XSLException& e) {
1.4       parser     67:                r._throw(&expression, e);
1.1       parser     68:        }
                     69: }
                     70: 
1.3       parser     71: static void _select_single(Request& r, const String& method_name, MethodParams *params) {
                     72: //     _asm int 3;
                     73:        Pool& pool=r.pool();
                     74:        VDnode& vnode=*static_cast<VDnode *>(r.self);
                     75: 
                     76:        // expression
                     77:        const String& expression=params->as_string(0, "expression must not be code");
                     78:        const char *expression_cstr=expression.cstr(String::UL_AS_IS);
                     79:        XalanDOMString dstring(expression_cstr);
                     80:        const XalanDOMChar *expression_dcstr=dstring.c_str();
                     81: 
                     82:        XPathEvaluator evaluator;
                     83:        // Initialize the XalanSourceTree subsystem...
                     84: //     XalanSourceTreeInit             theSourceTreeInit;
                     85:        // We'll use these to parse the XML file.
                     86:        XalanSourceTreeDOMSupport dom_support;
                     87: 
                     88:        try {
                     89:                 if(XalanNode *node=evaluator.selectSingleNode(dom_support, 
                     90:                        &vnode.get_node(pool, &method_name), 
                     91:                        expression_dcstr)) {
                     92: 
                     93:                        VDnode& result=*new(pool) VDnode(pool, node);
                     94:                        result.set_name(method_name);
                     95:                        r.write_no_lang(result);
                     96:                }
                     97:        } catch(const XSLException& e) {
1.4       parser     98:                r._throw(&expression, e);
1.3       parser     99:        }
                    100: }
                    101: 
1.1       parser    102: // constructor
                    103: 
                    104: MDnode::MDnode(Pool& apool) : Methoded(apool) {
                    105:        set_name(*NEW String(pool(), DNODE_CLASS_NAME));
1.2       parser    106: 
1.3       parser    107:        // ^node.select[/some/xpath/query] = hash $.#[dnode]
                    108:        add_native_method("select", Method::CT_DYNAMIC, _select, 1, 1);
1.1       parser    109: 
1.3       parser    110:        // ^node.select-single[/some/xpath/query] = first dnode
                    111:        add_native_method("select-single", Method::CT_DYNAMIC, _select_single, 1, 1);
1.1       parser    112: 
                    113: }
                    114: // global variable
                    115: 
                    116: Methoded *Dnode_class;
                    117: 
1.5     ! parser    118: #endif
        !           119: 
1.1       parser    120: // creator
                    121: Methoded *MDnode_create(Pool& pool) {
1.5     ! parser    122:        return 
        !           123: #ifdef XML
        !           124:                Dnode_class=new(pool) MDnode(pool)
        !           125: #else
        !           126:                0
        !           127: #endif
        !           128:                ;
1.1       parser    129: }

E-mail: