Diff for /parser3/src/classes/xdoc.C between versions 1.1 and 1.7

version 1.1, 2001/09/26 11:24:07 version 1.7, 2001/09/30 12:06:04
Line 1 Line 1
 /** @file  /** @file
         Parser: @b dom parser class.          Parser: @b xdoc parser class.
   
         Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)          Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
         Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)          Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
Line 19 Line 19
 #include <strstream>  #include <strstream>
 #include <Include/PlatformDefinitions.hpp>  #include <Include/PlatformDefinitions.hpp>
 #include <util/PlatformUtils.hpp>  #include <util/PlatformUtils.hpp>
   #include <util/TransENameMap.hpp>
 #include <XalanTransformer/XalanTransformer.hpp>  #include <XalanTransformer/XalanTransformer.hpp>
 #include <XalanTransformer/XalanParsedSource.hpp>  #include <XalanTransformer/XalanParsedSource.hpp>
 #include <XMLSupport/FormatterToXML.hpp>  #include <XMLSupport/FormatterToXML.hpp>
Line 28 Line 29
 #include <PlatformSupport/XalanFileOutputStream.hpp>  #include <PlatformSupport/XalanFileOutputStream.hpp>
 #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>  #include <PlatformSupport/XalanOutputStreamPrintWriter.hpp>
 #include <PlatformSupport/DOMStringPrintWriter.hpp>  #include <PlatformSupport/DOMStringPrintWriter.hpp>
   #include <XalanDOM/XalanElement.hpp>
   #include <XalanDOM/XalanNodeList.hpp>
   
 // defines  // defines
   
Line 53  public: Line 56  public:
   
 public: // Methoded  public: // Methoded
         bool used_directly() { return true; }          bool used_directly() { return true; }
           void configure_admin(Request& r);
 };  };
   
 // methods  // methods
Line 67  protected: // XalanOutputStream Line 71  protected: // XalanOutputStream
         virtual void writeData(const char *theBuffer, unsigned long theBufferLength) {          virtual void writeData(const char *theBuffer, unsigned long theBufferLength) {
                 char *copy=(char *)fstring.malloc((size_t)theBufferLength);                  char *copy=(char *)fstring.malloc((size_t)theBufferLength);
                 memcpy(copy, theBuffer, (size_t)theBufferLength);                  memcpy(copy, theBuffer, (size_t)theBufferLength);
                 fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "dom", 0);                  fstring.APPEND_CLEAN(copy, (size_t)theBufferLength, "xdoc", 0);
         }          }
   
         virtual void doFlush() {}          virtual void doFlush() {}
Line 234  static void _file(Request& r, const Stri Line 238  static void _file(Request& r, const Stri
   
 static void _set(Request& r, const String& method_name, MethodParams *params) {  static void _set(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXdoc& vdom=*static_cast<VXdoc *>(r.self);          VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
         Value& vxml=params->as_junction(0, "xml must be code");          Value& vxml=params->as_junction(0, "xml must be code");
         Temp_lang temp_lang(r, String::UL_XML);          Temp_lang temp_lang(r, String::UL_XML);
Line 242  static void _set(Request& r, const Strin Line 246  static void _set(Request& r, const Strin
   
         std::istrstream stream(xml.cstr());          std::istrstream stream(xml.cstr());
         const XalanParsedSource* parsedSource;          const XalanParsedSource* parsedSource;
         int error=vdom.transformer().parseSource(&stream, parsedSource);          int error=vdoc.transformer().parseSource(&stream, parsedSource);
   
         if(error)          if(error)
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &method_name,                          &method_name,
                         vdom.transformer().getLastError());                          vdoc.transformer().getLastError());
   
         // replace any previous parsed source          // replace any previous parsed source
         vdom.set_parsed_source(*parsedSource);          vdoc.set_parsed_source(*parsedSource);
 }  }
   
 static void _load(Request& r, const String& method_name, MethodParams *params) {  static void _load(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXdoc& vdom=*static_cast<VXdoc *>(r.self);          VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
         // filespec          // filespec
         const String& file_name=params->as_string(0, "file name must not be code");          const String& file_name=params->as_string(0, "file name must not be code");
         const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);          const char *filespec=r.absolute(file_name).cstr(String::UL_FILE_SPEC);
                   
         const XalanParsedSource* parsedSource;          const XalanParsedSource* parsedSource;
         int error=vdom.transformer().parseSource(filespec, parsedSource);          int error=vdoc.transformer().parseSource(filespec, parsedSource);
   
         if(error)          if(error)
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &file_name,                          &file_name,
                         vdom.transformer().getLastError());                          vdoc.transformer().getLastError());
   
         // replace any previous parsed source          // replace any previous parsed source
         vdom.set_parsed_source(*parsedSource);          vdoc.set_parsed_source(*parsedSource);
 }  }
   
 static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning,   static void add_xslt_param(const Hash::Key& aattribute, Hash::Val *ameaning, 
Line 285  static void add_xslt_param(const Hash::K Line 289  static void add_xslt_param(const Hash::K
 }  }
 static void _xslt(Request& r, const String& method_name, MethodParams *params) {  static void _xslt(Request& r, const String& method_name, MethodParams *params) {
         Pool& pool=r.pool();          Pool& pool=r.pool();
         VXdoc& vdom=*static_cast<VXdoc *>(r.self);          VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
         // params          // params
         if(params->size()>1) {          if(params->size()>1) {
                 Value& vparams=params->as_no_junction(1, "transform parameters parameter must not be code");                  Value& vparams=params->as_no_junction(1, "transform parameters parameter must not be code");
                 if(vparams.is_defined())                  if(vparams.is_defined())
                         if(Hash *params=vparams.get_hash())                          if(Hash *params=vparams.get_hash())
                                 params->for_each(add_xslt_param, &vdom.transformer());                                  params->for_each(add_xslt_param, &vdoc.transformer());
                         else                          else
                                 PTHROW(0, 0,                                  PTHROW(0, 0,
                                         &method_name,                                          &method_name,
Line 300  static void _xslt(Request& r, const Stri Line 304  static void _xslt(Request& r, const Stri
         }          }
   
         // source          // source
         const XalanParsedSource &parsed_source=vdom.get_parsed_source(pool, &method_name);          const XalanParsedSource &parsed_source=vdoc.get_parsed_source(pool, &method_name);
   
         // stylesheet          // stylesheet
         const String& stylesheet_file_name=params->as_string(0, "file name must not be code");          const String& stylesheet_file_name=params->as_string(0, "file name must not be code");
Line 309  static void _xslt(Request& r, const Stri Line 313  static void _xslt(Request& r, const Stri
         Stylesheet_connection& connection=XSLT_stylesheet_manager->get_connection(stylesheet_filespec);          Stylesheet_connection& connection=XSLT_stylesheet_manager->get_connection(stylesheet_filespec);
   
         // target          // target
         XalanDocument* target=vdom.parser_liaison().createDocument();          XalanDocument* target=vdoc.parser_liaison().createDocument();
         XSLTResultTarget domResultTarget(target);          XSLTResultTarget domResultTarget(target);
   
         // transform          // transform
         int error=vdom.transformer().transform(          int error=vdoc.transformer().transform(
                 parsed_source,                   parsed_source, 
                 &connection.stylesheet(),                   &connection.stylesheet(true/*nocache*/), 
                 domResultTarget);                  domResultTarget);
         connection.close();          connection.close();
         if(error)          if(error)
                 PTHROW(0, 0,                  PTHROW(0, 0,
                         &stylesheet_file_name,                          &stylesheet_file_name,
                         vdom.transformer().getLastError());                          vdoc.transformer().getLastError());
   
         // write out result          // write out result
         VXdoc& result=*new(pool) VXdoc(pool);          VXdoc& result=*new(pool) VXdoc(pool);
Line 329  static void _xslt(Request& r, const Stri Line 333  static void _xslt(Request& r, const Stri
         r.write_no_lang(result);          r.write_no_lang(result);
 }  }
   
   static void _getElementById(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
           VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
           // elementId
           const char *elementId=params->as_string(0, "elementID must not be code").cstr(String::UL_AS_IS);
   
           if(XalanElement *element=
                   vdoc.get_document(pool, &method_name).getElementById(XalanDOMString(elementId))) {
                   // write out result
                   VXnode& result=*new(pool) VXnode(pool, element);
                   r.write_no_lang(result);
           }
   }
   /*
   static void _getElementsByTagName(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
           VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
           // tagname
           const char *tagname=params->as_string(0, "tagname must not be code").cstr(String::UL_AS_IS);
   
           VHash& result=*new(pool) VHash(pool);
           if(const XalanNodeList *nodes=
                   vdoc.get_document(pool, &method_name).getElementsByTagName(XalanDOMString(tagname))) {
                   for(int i=0; i<nodes->getLength(); i++) {
                           String& skey=*new(pool) String(pool);
                           {
                                   char *buf=(char *)pool.malloc(MAX_NUMBER);
                                   snprintf(buf, MAX_NUMBER, "%d", i);
                                   skey << buf;
                           }
   
                           result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
                   }
           }
   
           // write out result
           r.write_no_lang(result);
   }
   
   static void _getElementsByTagNameNS(Request& r, const String& method_name, MethodParams *params) {
           Pool& pool=r.pool();
           VXdoc& vdoc=*static_cast<VXdoc *>(r.self);
   
           // namespaceURI;localName
           const char *namespaceURI=params->as_string(0, "namespaceURI must not be code").cstr(String::UL_AS_IS);
           const char *localName=params->as_string(0, "localName must not be code").cstr(String::UL_AS_IS);
   
           VHash& result=*new(pool) VHash(pool);
           if(const XalanNodeList *nodes=
                   vdoc.get_document(pool, &method_name).getElementsByTagNameNS(
                           XalanDOMString(namespaceURI), XalanDOMString(localName))) {
                   for(int i=0; i<nodes->getLength(); i++) {
                           String& skey=*new(pool) String(pool);
                           {
                                   char *buf=(char *)pool.malloc(MAX_NUMBER);
                                   snprintf(buf, MAX_NUMBER, "%d", i);
                                   skey << buf;
                           }
   
                           result.hash().put(skey, new(pool) VXnode(pool, nodes->item(i)));
                   }
           }
   
           // write out result
           r.write_no_lang(result);
   }
   */
 // constructor  // constructor
   
 MXdoc::MXdoc(Pool& apool) : MXnode(apool) {  MXdoc::MXdoc(Pool& apool) : MXnode(apool) {
         set_name(*NEW String(pool(), XDOC_CLASS_NAME));          set_name(*NEW String(pool(), XDOC_CLASS_NAME));
   
         // ^dom.save[some.xml]          // ^xdoc.save[some.xml]
         // ^dom.save[some.xml;options hash]          // ^xdoc.save[some.xml;options hash]
         add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);          add_native_method("save", Method::CT_DYNAMIC, _save, 1, 2);
   
         // ^dom.string[] <doc/>          // ^xdoc.string[] <doc/>
         // ^dom.string[options hash] <doc/>          // ^xdoc.string[options hash] <doc/>
         add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);          add_native_method("string", Method::CT_DYNAMIC, _string, 0, 1);
   
         // ^dom.file[] file with "<doc/>"          // ^xdoc.file[] file with "<doc/>"
         // ^dom.file[options hash] file with "<doc/>"          // ^xdoc.file[options hash] file with "<doc/>"
         add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);          add_native_method("file", Method::CT_DYNAMIC, _file, 0, 1);
   
         // ^dom::set[<some>xml</some>]          // ^xdoc::set[<some>xml</some>]
         add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1);          add_native_method("set", Method::CT_DYNAMIC, _set, 1, 1);
   
         // ^dom::load[some.xml]          // ^xdoc::load[some.xml]
         add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);          add_native_method("load", Method::CT_DYNAMIC, _load, 1, 1);
   
         // ^dom.xslt[stylesheet file_name]          // ^xdoc.xslt[stylesheet file_name]
         // ^dom.xslt[stylesheet file_name;params hash]          // ^xdoc.xslt[stylesheet file_name;params hash]
         add_native_method("xslt", Method::CT_DYNAMIC, _xslt, 1, 2);          add_native_method("xslt", Method::CT_DYNAMIC, _xslt, 1, 2);
   
           // ^xdoc.getElementById[elementId]
           add_native_method("getElementById", Method::CT_DYNAMIC, _getElementById, 1, 1);
   /*      
           // ^xdoc.getElementsByTagName[tagname]
           add_native_method("getElementsByTagName", Method::CT_DYNAMIC, _getElementsByTagName, 1, 1);
   
           // ^xdoc.getElementsByTagNameNS[namespaceURI;localName] = array of nodes
           add_native_method("getElementsByTagNameNS", Method::CT_DYNAMIC, _getElementsByTagNameNS, 2, 2);
   */
   }
   
   void MXdoc::configure_admin(Request& r) {
 }  }
   
 // global variable  // global variable
   
 Methoded *Xdoc_class;  Methoded *Xdoc_class;

Removed from v.1.1  
changed lines
  Added in v.1.7


E-mail: