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