Diff for /parser3/src/classes/xnode.C between versions 1.40 and 1.53

version 1.40, 2002/02/26 12:49:45 version 1.53, 2002/10/15 15:12:57
Line 3 Line 3
   
         Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
   
         $Id$  
 */  */
 #include "classes.h"  #include "classes.h"
 #ifdef XML  #ifdef XML
   
   static const char* IDENT_XNODE_C="$Date$";
   
 #include "pa_charset.h"  #include "pa_charset.h"
 #include "pa_request.h"  #include "pa_request.h"
 #include "pa_vxnode.h"  #include "pa_vxnode.h"
Line 24  extern "C" { Line 24  extern "C" {
 #include "gdome.h"  #include "gdome.h"
 #include "libxml/xpath.h"  #include "libxml/xpath.h"
   
 // defines   
   
 #define XNODE_CLASS_NAME "xnode"  
   
 // classes  // classes
   
 class xmlXPathObject_auto_ptr {  class xmlXPathObject_auto_ptr {
Line 98  private: Line 94  private:
   
 // helpers  // helpers
   
 GdomeNode *as_node(Pool& pool, const String& method_name, MethodParams *params,   GdomeNode *as_node(const String& method_name, MethodParams *params, 
                                                 int index, const char *msg) {                                                  int index, const char *msg) {
         Value& value=params->as_no_junction(index, msg);          Value& value=params->as_no_junction(index, msg);
         if(!(strcmp(value.type(), VXNODE_TYPE)==0           if(Value *vxnode=value.as(VXNODE_TYPE, false))
                 || strcmp(value.type(), VXDOC_TYPE)==0))                  return static_cast<VXnode *>(vxnode)->get_node(&method_name);
                 throw Exception(0, 0,          else
                   throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         msg);                          msg);
   
         VXnode& vnode=*static_cast<VXnode *>(&value);  
         return vnode.get_node(&method_name);  
 }  }
   
 // helpers  // helpers
   
 GdomeAttr * as_attr(Pool& pool, const String& method_name, MethodParams *params,   GdomeAttr * as_attr(Pool& pool, const String& method_name, MethodParams *params, 
                                                 int index, const char *msg) {                                                  int index, const char *msg) {
         GdomeNode *node=as_node(pool, method_name, params, index, msg);          GdomeNode *node=as_node(method_name, params, index, msg);
         GdomeException exc;          GdomeException exc;
         if(gdome_n_nodeType(node, &exc)!=GDOME_ATTRIBUTE_NODE)          if(gdome_n_nodeType(node, &exc)!=GDOME_ATTRIBUTE_NODE)
                 throw Exception(0, 0,                   throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         msg);                          msg);
   
Line 132  GdomeAttr * as_attr(Pool& pool, const St Line 126  GdomeAttr * as_attr(Pool& pool, const St
 // Node insertBefore(in Node newChild,in Node refChild) raises(DOMException);  // Node insertBefore(in Node newChild,in Node refChild) raises(DOMException);
 static void _insertBefore(Request& r, const String& method_name, MethodParams *params) {  static void _insertBefore(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *selfNode=vnode.get_node(&method_name);          GdomeNode *selfNode=vnode.get_node(&method_name);
         GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node");          GdomeNode *newChild=as_node(method_name, params, 0, "newChild must be node");
         GdomeNode *refChild=as_node(pool, method_name, params, 1, "refChild must be node");          GdomeNode *refChild=as_node(method_name, params, 1, "refChild must be node");
                   
         GdomeException exc;          GdomeException exc;
         if(GdomeNode *retNode=gdome_n_insertBefore(selfNode, newChild, refChild, &exc)) {          if(GdomeNode *retNode=gdome_n_insertBefore(selfNode, newChild, refChild, &exc)) {
Line 143  static void _insertBefore(Request& r, co Line 137  static void _insertBefore(Request& r, co
                 VXnode& result=*new(pool) VXnode(pool, retNode);                  VXnode& result=*new(pool) VXnode(pool, retNode);
                 r.write_no_lang(result);                                  r.write_no_lang(result);                
         } else          } else
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 151  static void _insertBefore(Request& r, co Line 145  static void _insertBefore(Request& r, co
 // Node replaceChild(in Node newChild,in Node oldChild) raises(DOMException);  // Node replaceChild(in Node newChild,in Node oldChild) raises(DOMException);
 static void _replaceChild(Request& r, const String& method_name, MethodParams *params) {  static void _replaceChild(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *selfNode=vnode.get_node(&method_name);          GdomeNode *selfNode=vnode.get_node(&method_name);
         GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node");          GdomeNode *newChild=as_node(method_name, params, 0, "newChild must be node");
         GdomeNode *refChild=as_node(pool, method_name, params, 1, "refChild must be node");          GdomeNode *refChild=as_node(method_name, params, 1, "refChild must be node");
                   
         GdomeException exc;          GdomeException exc;
         if(GdomeNode *retNode=gdome_n_replaceChild(selfNode, newChild, refChild, &exc)) {          if(GdomeNode *retNode=gdome_n_replaceChild(selfNode, newChild, refChild, &exc)) {
                 // write out result                  // write out result
                 r.write_no_lang(*new(pool) VXnode(pool, retNode));                                r.write_no_lang(*new(pool) VXnode(pool, retNode));              
         } else          } else
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 169  static void _replaceChild(Request& r, co Line 163  static void _replaceChild(Request& r, co
 // Node removeChild(in Node oldChild) raises(DOMException);  // Node removeChild(in Node oldChild) raises(DOMException);
 static void _removeChild(Request& r, const String& method_name, MethodParams *params) {  static void _removeChild(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *selfNode=vnode.get_node(&method_name);          GdomeNode *selfNode=vnode.get_node(&method_name);
         GdomeNode *oldChild=as_node(pool, method_name, params, 0, "oldChild must be node");          GdomeNode *oldChild=as_node(method_name, params, 0, "oldChild must be node");
                   
         GdomeException exc;          GdomeException exc;
         if(GdomeNode *retNode=gdome_n_removeChild(selfNode, oldChild, &exc)) {          if(GdomeNode *retNode=gdome_n_removeChild(selfNode, oldChild, &exc)) {
                 // write out result                  // write out result
                 r.write_no_lang(*new(pool) VXnode(pool, retNode));                  r.write_no_lang(*new(pool) VXnode(pool, retNode));
         } else          } else
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 186  static void _removeChild(Request& r, con Line 180  static void _removeChild(Request& r, con
 // Node appendChild(in Node newChild) raises(DOMException);  // Node appendChild(in Node newChild) raises(DOMException);
 static void _appendChild(Request& r, const String& method_name, MethodParams *params) {  static void _appendChild(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *selfNode=vnode.get_node(&method_name);          GdomeNode *selfNode=vnode.get_node(&method_name);
         GdomeNode *newChild=as_node(pool, method_name, params, 0, "newChild must be node");          GdomeNode *newChild=as_node(method_name, params, 0, "newChild must be node");
                   
         GdomeException exc;          GdomeException exc;
         if(GdomeNode *retNode=gdome_n_appendChild(selfNode, newChild, &exc)) {          if(GdomeNode *retNode=gdome_n_appendChild(selfNode, newChild, &exc)) {
                 // write out result                  // write out result
                 r.write_no_lang(*new(pool) VXnode(pool, retNode));                                r.write_no_lang(*new(pool) VXnode(pool, retNode));              
         }  else          }  else
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 203  static void _appendChild(Request& r, con Line 197  static void _appendChild(Request& r, con
 // boolean hasChildNodes();  // boolean hasChildNodes();
 static void _hasChildNodes(Request& r, const String& method_name, MethodParams *) {  static void _hasChildNodes(Request& r, const String& method_name, MethodParams *) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *node=vnode.get_node(&method_name);          GdomeNode *node=vnode.get_node(&method_name);
   
         GdomeException exc;          GdomeException exc;
         // write out result          // write out result
         VBool& result=*new(pool) VBool(pool, gdome_n_hasChildNodes(node, &exc)!=0);          bool result=gdome_n_hasChildNodes(node, &exc)!=0;
         result.set_name(method_name);          r.write_no_lang(*new(pool) VBool(pool, result));
         r.write_no_lang(result);  
 }  }
   
 // Node cloneNode(in boolean deep);  // Node cloneNode(in boolean deep);
 static void _cloneNode(Request& r, const String& method_name, MethodParams *params) {  static void _cloneNode(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *node=vnode.get_node(&method_name);          GdomeNode *node=vnode.get_node(&method_name);
   
         bool deep=params->as_bool(0, "deep must be bool", r);          bool deep=params->as_bool(0, "deep must be bool", r);
   
         GdomeException exc;          GdomeException exc;
         // write out result          // write out result
         VXnode& result=*new(pool) VXnode(pool, gdome_n_cloneNode(node, deep, &exc));          r.write_no_lang(*new(pool) VXnode(pool, gdome_n_cloneNode(node, deep, &exc)));
         result.set_name(method_name);  
         r.write_no_lang(result);  
 }  }
   
 // DOM1 element  // DOM1 element
   
 GdomeElement *get_self_element(Request& r, const String& method_name) {  GdomeElement *get_self_element(Request& r, const String& method_name) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *node=vnode.get_node(&method_name);          GdomeNode *node=vnode.get_node(&method_name);
   
         GdomeException exc;          GdomeException exc;
         if(gdome_n_nodeType(node, &exc)!=GDOME_ELEMENT_NODE)          if(gdome_n_nodeType(node, &exc)!=GDOME_ELEMENT_NODE)
                 throw Exception(0, 0,                   throw Exception("parser.runtime",
                         &method_name,                          &method_name,
                         "method can be called on node of ELEMENT type");                          "method can only be called on nodes of ELEMENT type");
   
         return GDOME_EL(node);          return GDOME_EL(node);
 }  }
Line 254  static void _getAttribute(Request& r, co Line 245  static void _getAttribute(Request& r, co
         GdomeDOMString *attribute_value=          GdomeDOMString *attribute_value=
                 gdome_el_getAttribute(element, pool.transcode(name).get(), &exc);                  gdome_el_getAttribute(element, pool.transcode(name).get(), &exc);
         // write out result          // write out result
         r.write_no_lang(*new(pool) VString(pool.transcode(attribute_value)));          r.write_no_lang(pool.transcode(attribute_value, &name));
 }  }
   
 // void setAttribute(in DOMString name, in DOMString value) raises(DOMException);  // void setAttribute(in DOMString name, in DOMString value) raises(DOMException);
Line 270  static void _setAttribute(Request& r, co Line 261  static void _setAttribute(Request& r, co
                 pool.transcode(attribute_value).get(),                  pool.transcode(attribute_value).get(),
                 &exc);                  &exc);
         if(exc)          if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 284  static void _removeAttribute(Request& r, Line 275  static void _removeAttribute(Request& r,
         GdomeException exc;          GdomeException exc;
         gdome_el_removeAttribute(element, pool.transcode(name).get(), &exc);          gdome_el_removeAttribute(element, pool.transcode(name).get(), &exc);
         if(exc)          if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 301  static void _getAttributeNode(Request& r Line 292  static void _getAttributeNode(Request& r
                 VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)attr);                  VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)attr);
                 r.write_no_lang(result);                  r.write_no_lang(result);
         } else if(exc)          } else if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }         }       
Line 318  static void _setAttributeNode(Request& r Line 309  static void _setAttributeNode(Request& r
                 VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)returnAttr);                  VXnode& result=*new(pool) VXnode(pool, (GdomeNode *)returnAttr);
                 r.write_no_lang(result);                  r.write_no_lang(result);
         } else          } else
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }         }       
Line 332  static void _removeAttributeNode(Request Line 323  static void _removeAttributeNode(Request
         GdomeException exc;          GdomeException exc;
         gdome_el_removeAttributeNode(element, oldAttr, &exc);          gdome_el_removeAttributeNode(element, oldAttr, &exc);
         if(exc)          if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }         }       
Line 360  static void _getElementsByTagName(Reques Line 351  static void _getElementsByTagName(Reques
                         result.hash(0).put(skey, new(pool) VXnode(pool, gdome_nl_item(nodes, i, &exc)));                          result.hash(0).put(skey, new(pool) VXnode(pool, gdome_nl_item(nodes, i, &exc)));
                 }                  }
         } else if(exc)          } else if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
   
Line 404  static void _getElementsByTagNameNS(Requ Line 395  static void _getElementsByTagNameNS(Requ
 // void normalize();  // void normalize();
 static void _normalize(Request& r, const String& method_name, MethodParams *) {  static void _normalize(Request& r, const String& method_name, MethodParams *) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
         GdomeNode *selfNode=vnode.get_node(&method_name);          GdomeNode *selfNode=vnode.get_node(&method_name);
   
         GdomeException exc;          GdomeException exc;
         gdome_n_normalize(selfNode, &exc);          gdome_n_normalize(selfNode, &exc);
         if(exc)          if(exc)
                 throw Exception(0, 0,                   throw Exception(
                         &method_name,                           &method_name, 
                         exc);                          exc);
 }  }
Line 422  static void _selectX(Request& r, const S Line 413  static void _selectX(Request& r, const S
                                                           Value *& result)) {                                                            Value *& result)) {
 //      _asm int 3;  //      _asm int 3;
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXnode& vnode=*static_cast<VXnode *>(r.self);          VXnode& vnode=*static_cast<VXnode *>(r.get_self());
   
         // expression          // expression
         const String& expression=params->as_string(0, "expression must be string");          const String& expression=params->as_string(0, "expression must be string");
Line 441  static void _selectX(Request& r, const S Line 432  static void _selectX(Request& r, const S
   
         if(xmlHaveGenericErrors()) {          if(xmlHaveGenericErrors()) {
                 GdomeException exc=0;                  GdomeException exc=0;
                 throw Exception(0, 0,                  throw Exception(&expression, exc);
                         &expression,   
                         exc);  
         }          }
   
         Value *result=0;          Value *result=0;
         if(res.get())          if(res.get())
                 handler(pool, expression, res, result);                  handler(pool, expression, res, result);
         if(result) {          if(result)
                 result->set_name(method_name);  
                 r.write_no_lang(*result);                  r.write_no_lang(*result);
         }  
 }  }
   
 static void selectNodesHandler(Pool& pool,  static void selectNodesHandler(Pool& pool,
Line 481  static void selectNodesHandler(Pool& poo Line 468  static void selectNodesHandler(Pool& poo
                         }                          }
                 break;                  break;
         default:           default: 
                 throw Exception(0, 0,                  throw Exception(0,
                         &expression,                          &expression,
                         "wrong xmlXPathEvalExpression result type (%d)", res->type);                          "wrong xmlXPathEvalExpression result type (%d)", res->type);
                 break; // never                  break; // never
Line 498  static void selectNodeHandler(Pool& pool Line 485  static void selectNodeHandler(Pool& pool
         case XPATH_NODESET:           case XPATH_NODESET: 
                 if(res->nodesetval && res->nodesetval->nodeNr) { // empty result strangly has NODESET  res->type                  if(res->nodesetval && res->nodesetval->nodeNr) { // empty result strangly has NODESET  res->type
                         if(res->nodesetval->nodeNr>1)                          if(res->nodesetval->nodeNr>1)
                                 throw Exception(0, 0,                                  throw Exception("parser.runtime",
                                 &expression,                                  &expression,
                                 "resulted not in a single node (%d)", res->nodesetval->nodeNr);                                  "resulted not in a single node (%d)", res->nodesetval->nodeNr);
                                                   
Line 515  static void selectNodeHandler(Pool& pool Line 502  static void selectNodeHandler(Pool& pool
                 result=new(pool) VString(                  result=new(pool) VString(
                         pool.transcode(                          pool.transcode(
                                 GdomeDOMString_auto_ptr(                                  GdomeDOMString_auto_ptr(
                                         gdome_str_mkref_dup((const gchar *)res->stringval)).get()));                                          gdome_str_mkref_dup((const gchar *)res->stringval)).get(), &expression));
                 break;                  break;
         default:           default: 
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &expression,                          &expression,
                         "wrong xmlXPathEvalExpression result type (%d)", res->type);                          "wrong xmlXPathEvalExpression result type (%d)", res->type);
                 break; // never                  break; // never
Line 538  static void selectBoolHandler(Pool& pool Line 525  static void selectBoolHandler(Pool& pool
                         break;                          break;
                 // else[nodeset] fall down to default                  // else[nodeset] fall down to default
         default:           default: 
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &expression,                          &expression,
                         "wrong xmlXPathEvalExpression result type (%d)", res->type);                          "wrong xmlXPathEvalExpression result type (%d)", res->type);
                 break; // never                  break; // never
Line 558  static void selectNumberHandler(Pool& po Line 545  static void selectNumberHandler(Pool& po
                         break;                          break;
                 // else[nodeset] fall down to default                  // else[nodeset] fall down to default
         default:           default: 
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &expression,                          &expression,
                         "wrong xmlXPathEvalExpression result type (%d)", res->type);                          "wrong xmlXPathEvalExpression result type (%d)", res->type);
                 break; // never                  break; // never
Line 576  static void selectStringHandler(Pool& po Line 563  static void selectStringHandler(Pool& po
                 result=new(pool) VString(                  result=new(pool) VString(
                         pool.transcode(                          pool.transcode(
                                 GdomeDOMString_auto_ptr(                                  GdomeDOMString_auto_ptr(
                                         gdome_str_mkref_dup((const gchar *)res->stringval)).get()));                                          gdome_str_mkref_dup((const gchar *)res->stringval)).get(), &expression));
                 break;                  break;
         case XPATH_NODESET:           case XPATH_NODESET: 
                 if(!(res->nodesetval && res->nodesetval->nodeNr))                  if(!(res->nodesetval && res->nodesetval->nodeNr))
                         break;                          break;
                 // else[nodeset] fall down to default                  // else[nodeset] fall down to default
         default:           default: 
                 throw Exception(0, 0,                  throw Exception("parser.runtime",
                         &expression,                          &expression,
                         "wrong xmlXPathEvalExpression result type (%d)", res->type);                          "wrong xmlXPathEvalExpression result type (%d)", res->type);
                 break; // never                  break; // never
Line 617  static void _selectString(Request& r, co Line 604  static void _selectString(Request& r, co
   
 // constructor  // constructor
   
 MXnode::MXnode(Pool& apool) : Methoded(apool),   MXnode::MXnode(Pool& apool, const char *aname, VStateless_class *abase) : 
         consts(apool) {          Methoded(apool, aname?aname:"xnode", abase), 
         set_name(*NEW String(pool(), XNODE_CLASS_NAME));  
   
           consts(apool) 
   {
         /// DOM1 node          /// DOM1 node
   
         // Node insertBefore(in Node newChild,in Node refChild) raises(DOMException);          // Node insertBefore(in Node newChild,in Node refChild) raises(DOMException);

Removed from v.1.40  
changed lines
  Added in v.1.53


E-mail: