Diff for /parser3/src/classes/xdoc.C between versions 1.162 and 1.166

version 1.162, 2009/07/06 12:13:30 version 1.166, 2011/05/18 01:25:06
Line 33  static const char * const IDENT_XDOC_C=" Line 33  static const char * const IDENT_XDOC_C="
   
 #define XDOC_CLASS_NAME "xdoc"  #define XDOC_CLASS_NAME "xdoc"
   
 #define XDOC_OUTPUT_METHOD_OPTION_NAME "method"  
 #define XDOC_OUTPUT_METHOD_OPTION_VALUE_XML "xml"  
 #define XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML "html"  
 #define XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT "text"  
   
 // class  // class
   
 class MXdoc: public MXnode {  class MXdoc: public MXnode {
 public: // VStateless_class  public: // VStateless_class
         Value* create_new_value(Pool&, HashStringValue*) { return new VXdoc(); }          Value* create_new_value(Pool&) { return new VXdoc(); }
   
 public:  public:
         MXdoc();          MXdoc();
Line 391  static void _create(Request& r, MethodPa Line 386  static void _create(Request& r, MethodPa
                 Temp_lang temp_lang(r, String::L_XML);                  Temp_lang temp_lang(r, String::L_XML);
                 const String& xml=r.process_to_string(param);                  const String& xml=r.process_to_string(param);
   
                 const char* cstr=xml.untaint_cstr(String::L_AS_IS, 0, &r.charsets);                  String::Body sbody=xml.cstr_to_string_body_untaint(r.flang, 0, &r.charsets);
                 xmldoc=xmlParseMemory(cstr, strlen(cstr));                  xmldoc=xmlParseMemory(sbody.cstr(), sbody.length());
   
                 //printf("document=0x%p\n", document);                  //printf("document=0x%p\n", document);
                 if(!xmldoc || xmlHaveGenericErrors())                  if(!xmldoc || xmlHaveGenericErrors())
                         throw XmlException(0);                          throw XmlException(0);
Line 471  static void _load(Request& r, MethodPara Line 467  static void _load(Request& r, MethodPara
         vdoc.set_xmldoc(r.charsets, *xmldoc);           vdoc.set_xmldoc(r.charsets, *xmldoc); 
 }  }
   
 static void param_option_over_output_option(  String::C xdoc2buf(Request& r, VXdoc& vdoc, 
                                             HashStringValue& param_options, const char* option_name,                                          XDocOutputOptions& oo,
                                             const String*& output_option) {                                          const String* file_spec,
         if(Value* value=param_options.get(String::Body(option_name)))                                          bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) {
                 output_option=&value->as_string();  
 }  
 static void param_option_over_output_option(  
                                             HashStringValue& param_options, const char* option_name,  
                                             int& output_option) {  
         if(Value* value=param_options.get(String::Body(option_name))) {  
                 const String& s=value->as_string();  
                 if(s=="yes")  
                         output_option=1;  
                 else if(s=="no")  
                         output_option=0;  
                 else  
                         throw Exception(PARSER_RUNTIME,  
                                 &s,  
                                 "%s must be either 'yes' or 'no'", option_name);  
         }  
 }  
   
 /// @test valid_options check  
 static void prepare_output_options(Request& r,  
                                    MethodParams& params, size_t index,  
                                    VXdoc::Output_options& oo) {  
 /*  
 <xsl:output  
   !method = "xml" | "html" | "text" | qname-but-not-ncname   
   !version = nmtoken   
   !encoding = string   
   !omit-xml-declaration = "yes" | "no"  
   !standalone = "yes" | "no"  
   !doctype-public = string   
   !doctype-system = string   
   cdata-section-elements = qnames   
   !indent = "yes" | "no"  
   !media-type = string />   
 */  
   
         // configuring with options from parameter...  
         if(params.count()>index) {  
                 Value& voptions=params.as_no_junction(index, "options must be string");  
                 if(voptions.is_defined()) {  
                         if(HashStringValue *options=voptions.get_hash()) {  
                                 // $.method[xml|html|text]  
                                 if(Value* vmethod=options->get(String::Body(XDOC_OUTPUT_METHOD_OPTION_NAME)))  
                                         oo.method=&vmethod->as_string();  
   
                                 // $.version[1.0]  
                                 param_option_over_output_option(*options, "version", oo.version);  
                                 // $.encoding[windows-1251|...]  
                                 param_option_over_output_option(*options, "encoding", oo.encoding);  
                                 // $.omit-xml-declaration[yes|no]  
                                 param_option_over_output_option(*options, "omit-xml-declaration", oo.omitXmlDeclaration);  
                                 // $.standalone[yes|no]  
                                 param_option_over_output_option(*options, "standalone", oo.standalone);  
                                 // $.indent[yes|no]  
                                 param_option_over_output_option(*options, "indent", oo.indent);  
                                 // $.media-type[text/{html|xml|plain}]  
                                 param_option_over_output_option(*options, "media-type", oo.mediaType);                             
                         }  
                 }  
         }  
   
         // default encoding from pool  
         if(!oo.encoding)  
                 oo.encoding=new String(r.charsets.source().NAME(), String::L_TAINTED);  
         // default method=xml  
         if(!oo.method)  
                 oo.method=new String(XDOC_OUTPUT_METHOD_OPTION_VALUE_XML);  
         // default mediaType = depending on method  
         if(!oo.mediaType) {  
                 if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_XML)  
                         oo.mediaType=new String("text/xml");  
                 else if(*oo.method==XDOC_OUTPUT_METHOD_OPTION_VALUE_HTML)  
                         oo.mediaType=new String("text/html");  
                 else // XDOC_OUTPUT_METHOD_OPTION_VALUE_TEXT & all others  
                         oo.mediaType=new String("text/plain");  
         }  
 }  
   
 struct Xdoc2buf_result {  
         char* str;  
         size_t length;  
 };  
 static Xdoc2buf_result xdoc2buf(Request& r, VXdoc& vdoc,   
                                          MethodParams& params, int index,  
                                          VXdoc::Output_options& oo,  
                                          const String* file_spec,  
                                          bool use_source_charset_to_render_and_client_charset_to_write_to_header=false) {  
         Xdoc2buf_result result;  
         prepare_output_options(r, params, index,  
                 oo);  
   
         const char* render_encoding;          const char* render_encoding;
         const char* header_encoding;          const char* header_encoding;
         if(use_source_charset_to_render_and_client_charset_to_write_to_header) {          if(use_source_charset_to_render_and_client_charset_to_write_to_header) {
Line 618  static Xdoc2buf_result xdoc2buf(Request& Line 523  static Xdoc2buf_result xdoc2buf(Request&
                 throw XmlException(0);                  throw XmlException(0);
   
         // write out result          // write out result
         char *gnome_str;  size_t gnome_length;          char *gnome_str;
           size_t gnome_length;
         if(outputBuffer->conv) {          if(outputBuffer->conv) {
                 gnome_length=outputBuffer->conv->use;                  gnome_length=outputBuffer->conv->use;
                 gnome_str=(char *)outputBuffer->conv->content;                  gnome_str=(char *)outputBuffer->conv->content;
Line 627  static Xdoc2buf_result xdoc2buf(Request& Line 533  static Xdoc2buf_result xdoc2buf(Request&
                 gnome_str=(char *)outputBuffer->buffer->content;                  gnome_str=(char *)outputBuffer->buffer->content;
         }          }
   
         if((result.length=gnome_length)) {          if(file_spec){
                 result.str=pa_strdup(gnome_str, gnome_length);                  file_write(r.charsets,
         } else                          *file_spec,
                 result.str=0;                          gnome_str,
                           gnome_length, 
         if(file_spec)  
                 file_write(*file_spec,  
                         gnome_str, gnome_length,   
                         true/*as_text*/);                          true/*as_text*/);
                   return String::C(); // actually, we don't need this output at all
           } else
                   return String::C(gnome_length ? pa_strdup(gnome_str, gnome_length) : 0, gnome_length);
   }
   
         return result;  inline HashStringValue* get_options(MethodParams& params, size_t index){
           return (params.count()>index) ? params.as_hash(index) : 0;
 }  }
   
 static void _file(Request& r, MethodParams& params) {  static void _file(Request& r, MethodParams& params) {
         VXdoc& vdoc=GET_SELF(r, VXdoc);          VXdoc& vdoc=GET_SELF(r, VXdoc);
         VXdoc::Output_options oo(vdoc.output_options);  
         Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0,   
                 oo,  
                 0/*not to file, to memory*/);  
         // write out result  
         r.write_no_lang(String(buf.str));  
   
         // write out result          XDocOutputOptions oo(r, get_options(params, 0));
           String::C buf=xdoc2buf(r, vdoc, oo, 0/*file_name. not to file, to memory*/);
   
         VFile& vfile=*new VFile;          VFile& vfile=*new VFile;
         VHash& vhcontent_type=*new VHash;          VHash& vhcontent_type=*new VHash;
         vhcontent_type.hash().put(          vhcontent_type.hash().put(
Line 661  static void _file(Request& r, MethodPara Line 565  static void _file(Request& r, MethodPara
   
         vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length,           vfile.set(false/*tainted*/, buf.str?buf.str:""/*to distinguish from stat-ed file*/, buf.length, 
                 0/*file_name*/, &vhcontent_type);                  0/*file_name*/, &vhcontent_type);
   
           // write out result
         r.write_no_lang(vfile);          r.write_no_lang(vfile);
 }  }
   
Line 669  static void _save(Request& r, MethodPara Line 575  static void _save(Request& r, MethodPara
   
         const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));          const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
                   
         VXdoc::Output_options oo(vdoc.output_options);          XDocOutputOptions oo(r, get_options(params, 1));
         xdoc2buf(r, vdoc, params, 1,           xdoc2buf(r, vdoc, oo, &file_spec);
                 oo,  
                 &file_spec);  
 }  }
   
 static void _string(Request& r, MethodParams& params) {  static void _string(Request& r, MethodParams& params) {
         VXdoc& vdoc=GET_SELF(r, VXdoc);          VXdoc& vdoc=GET_SELF(r, VXdoc);
         VXdoc::Output_options oo(vdoc.output_options);  
         Xdoc2buf_result buf=xdoc2buf(r, vdoc, params, 0,           XDocOutputOptions oo(r, get_options(params, 0));
                 oo,          String::C buf=xdoc2buf(r, vdoc, oo,
                 0/*not to file, to memory*/,                  0/*file_name. not to file, to memory*/,
                 true/*use source charset to render, client charset to put to header*/);                  true/*use source charset to render, client charset to put to header*/);
   
         // write out result          // write out result
         r.write_no_lang(String(buf.str, String::L_AS_IS));          r.write_no_lang(String(buf, String::L_AS_IS));
 }  }
   
 #ifndef DOXYGEN  #ifndef DOXYGEN
Line 743  static VXdoc& _transform(Request& r, con Line 648  static VXdoc& _transform(Request& r, con
                 !indent = "yes" | "no"                  !indent = "yes" | "no"
                 !media-type = string />                   !media-type = string /> 
         */          */
         VXdoc::Output_options& oo=result.output_options;          XDocOutputOptions& oo=result.output_options;
   
         oo.method=stylesheet->method?&r.transcode(stylesheet->method):0;          oo.method=stylesheet->method?&r.transcode(stylesheet->method):0;
         oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0;          oo.encoding=stylesheet->encoding?&r.transcode(stylesheet->encoding):0;
Line 762  static void _transform(Request& r, Metho Line 667  static void _transform(Request& r, Metho
         // params          // params
         Array<const xmlChar*> transform_strings;          Array<const xmlChar*> transform_strings;
         const xmlChar** transform_params=0;          const xmlChar** transform_params=0;
         if(params.count()>1) {          if(params.count()>1)
                 Value& vparams=params.as_no_junction(1, "transform parameters must be hash");                  if(HashStringValue* hash=params.as_hash(1)) {
                 if(!vparams.is_string())                          transform_params=new(UseGC) const xmlChar*[hash->count()*2+1];
                         if(HashStringValue* hash=vparams.get_hash()) {                          Add_xslt_param_info info={
                                 transform_params=new(UseGC) const xmlChar*[hash->count()*2+1];                                  &r, 
                                 Add_xslt_param_info info={                                  &transform_strings,
                                         &r,                                   transform_params
                                         &transform_strings,                          };
                                         transform_params                          hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info);
                                 };                          transform_params[hash->count()*2]=0;
                                 hash->for_each<Add_xslt_param_info*>(add_xslt_param, &info);                  }
                                 transform_params[hash->count()*2]=0;                              
                         } else  
                                 throw Exception(PARSER_RUNTIME,  
                                         0,  
                                         "transform parameters parameter must be hash");  
         }  
   
         VXdoc* result;          VXdoc* result;
         if(Value *vxdoc=params[0].as(VXDOC_TYPE, false)) { // stylesheet (xdoc)          if(Value *vxdoc=params[0].as(VXDOC_TYPE)) { // stylesheet (xdoc)
                 xmlDoc& stylesheetdoc=static_cast<VXdoc *>(vxdoc)->get_xmldoc();                  xmlDoc& stylesheetdoc=static_cast<VXdoc *>(vxdoc)->get_xmldoc();
                 // compile xdoc stylesheet                  // compile xdoc stylesheet
                 xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc));                   xsltStylesheet_auto_ptr stylesheet_ptr(xsltParseStylesheetDoc(&stylesheetdoc)); 

Removed from v.1.162  
changed lines
  Added in v.1.166


E-mail: