Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Parser.XML2
Viewing contents of Public_Parser_XML2-1.16/XML2.c

/* Generated from "XML2.cmod" by precompile.pike
 *
 * Do NOT edit this file.
 */

#undef PRECOMPILE_API_VERSION
#define PRECOMPILE_API_VERSION 2


#line 1 "XML2.cmod"
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: XML2.cmod,v 1.67 2005/05/06 03:13:35 hww3 Exp $
 */

/*
 * File licensing and authorship information block.
 *
 * Version: MPL 1.1/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *
 * Bill Welliver 
 *
 * Portions created by the Initial Developer are Copyright (C) Bill Welliver
 * All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of the LGPL, and not to allow others to use your version
 * of this file under the terms of the MPL, indicate your decision by
 * deleting the provisions above and replace them with the notice
 * and other provisions required by the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL or the LGPL.
 *
 * Significant Contributors to this file are:
 *
 *
 */

#include "xml2.h"

  int docloader_num = -1;

/*! @module Public
 */

/*! @module Parser
 */

/*! @module XML2
 */

/*
  
    start of Public.Parser.XML2 module functions

*/


#undef var_docloader_defined
#define var_docloader_defined

#undef var_object_data_defined
#define var_object_data_defined

#undef THIS
#define THIS ((struct _struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef THIS_
#define THIS_ ((struct _struct *)(Pike_interpreter.frame_pointer->current_storage))

#undef OBJ2_
#define OBJ2_(o) ((struct _struct *)(o->storage+_storage_offset))

#undef GET__STORAGE
#define GET__STORAGE ((struct _struct *)(o->storage+_storage_offset)
static ptrdiff_t _storage_offset;
struct _struct {

#ifdef var_docloader_defined
  struct svalue docloader;

#endif /* var_docloader_defined */

#ifdef var_object_data_defined
#line 80 "XML2.cmod"
PARSER_OBJECT_DATA * object_data;
#endif /* var_object_data_defined */
};
/*! @decl array(Node) select_xpath_nodes(string xpath, object node)
 *!
 *! selects nodes based on an XPath query and returns the result.
 */
#define f_select_xpath_nodes_defined
ptrdiff_t f_select_xpath_nodes_fun_num = 0;
void f_select_xpath_nodes(INT32 args) {
#line 88 "XML2.cmod"
struct pike_string * xpath;
#line 88 "XML2.cmod"
struct object * node;
#line 88 "XML2.cmod"
if(args != 2) wrong_number_of_args_error("select_xpath_nodes",args,2);
#line 88 "XML2.cmod"
if(Pike_sp[0-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("select_xpath_nodes",1,"string");
#line 88 "XML2.cmod"
debug_malloc_pass(xpath=Pike_sp[0-2].u.string);
#line 88 "XML2.cmod"
if(Pike_sp[1-2].type != PIKE_T_OBJECT) SIMPLE_BAD_ARG_ERROR("select_xpath_nodes",2,"object");
#line 88 "XML2.cmod"
debug_malloc_pass(node=Pike_sp[1-2].u.object);
{
  xmlXPathContextPtr xpathCtx;
  xmlXPathObjectPtr xpathObj;
  xmlNodeSetPtr set;
  xmlNodePtr xn;
  struct object * o;
  int num_nodes = 0;

  CHECK_NODE_PASSED(node);

/*  check_node_created(); */
  if(OBJ2_NODE(node)->object_data->node == NULL)
  { 
    pop_n_elems(2);
    Pike_error("Node not initialized.\n");
  }

  xpathCtx = xmlXPathNewContext(OBJ2_NODE(node)->object_data->node->doc);
  if(xpathCtx == NULL)
  {
    pop_n_elems(2);
    Pike_error("Unable to create new XPath context.\n");
  }
  xpathObj = xmlXPathEvalExpression((xmlChar *)xpath->str, xpathCtx);
  if(xpathObj == NULL) {  
    pop_n_elems(2);
    xmlXPathFreeContext(xpathCtx);
    Pike_error("Unable to evaluate the XPath expression.\n");
  }

  /* now we should generate an array from the node set. */
  set = xpathObj->nodesetval;


  if(set == NULL)
  {
    pop_n_elems(2);
    push_int(0);
    xmlXPathFreeContext(xpathCtx);
    xmlXPathFreeObject(xpathObj);
    return;
  }  

  pop_n_elems(2);

  if(set->nodeNr > 0)
  {
  xn = set->nodeTab[0];
    if(xn != NULL && set->nodeNr > 0) do
    {
      struct Node_struct * ns;
      NODE_OBJECT_DATA * od;

      apply(Pike_fp->current_object, "Node", 0);
      ns = OBJ2_NODE((Pike_sp[0-1].u.object));
      od = (NODE_OBJECT_DATA *)(ns->object_data);

      od->node=xn;
      od->refs = OBJ2_NODE(node)->object_data->refs;
    
      (* od->refs)++;

      num_nodes++;
      xn = set->nodeTab[num_nodes];
    } 
    while (((num_nodes +1) <= set->nodeNr) && xn != NULL);


    if(num_nodes>0) {
      f_aggregate(num_nodes);
    }
  }
  else push_int(0);

  xmlXPathFreeContext(xpathCtx);
  xmlXPathFreeObject(xpathObj);

  return;
}

}
/*! @decl void set_xslt_docloader(function f) 
 *!
 *! sets the xslt loader function. this is global for the process.
 *!
 *! the callout should have the following signature:
 *!
 *!  Node f(string uri, int options, int type)
 *!
 *!  where: 
 *!    uri is the URI of the document to load
 *!    options are a set of xmlParserOption
 *!    the xsltLoadType indicating the kind of loading required
 */
#define f_set_xslt_docloader_defined
ptrdiff_t f_set_xslt_docloader_fun_num = 0;
void f_set_xslt_docloader(INT32 args) {
#line 182 "XML2.cmod"
struct svalue * f;
#line 182 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("set_xslt_docloader",args,1);
#line 182 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_FUNCTION) SIMPLE_BAD_ARG_ERROR("set_xslt_docloader",1,"function");
#line 182 "XML2.cmod"
f=Pike_sp+0-1; dmalloc_touch_svalue(Pike_sp+0-1);
{
  assign_svalue(&(THIS->docloader), f);
#ifdef HAVE_XSLTSETLOADERFUNC
#else
  Pike_error("set_docloader not available. upgrade your libxslt.\n");
#endif
}

}
/*! @decl void clear_xslt_docloader()
 *!
 *! clears the system wide docloader callout, if one was set.
 *!
 */
#define f_clear_xslt_docloader_defined
ptrdiff_t f_clear_xslt_docloader_fun_num = 0;
void f_clear_xslt_docloader(INT32 args) {
#line 196 "XML2.cmod"
if(args != 0) wrong_number_of_args_error("clear_xslt_docloader",args,0);
{
  push_int(0);
  assign_svalue(&(THIS->docloader), Pike_sp-1);
  pop_stack();
#ifdef HAVE_XSLTSETLOADERFUNC
#else
  Pike_error("set_docloader not available. upgrade your libxslt.\n");
#endif
}

}
/*! @decl void set_xml_parser_options(int options)
 *!
 *! sets parser options. 
 *! 
 *! @param options
 *!   a bitwise or of @[Public.Parser.XML2.Constants] options 
 *!   where valid options start with PARSE_. 
 */
#define f_set_xml_parser_options_defined
ptrdiff_t f_set_xml_parser_options_fun_num = 0;
void f_set_xml_parser_options(INT32 args) {
#line 215 "XML2.cmod"
INT_TYPE options;
#line 215 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("set_xml_parser_options",args,1);
#line 215 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_BAD_ARG_ERROR("set_xml_parser_options",1,"int");
options=Pike_sp[0-1].u.integer;
#line 216 "XML2.cmod"
{
  THIS->object_data->xml_parser_options = options;
  pop_stack();
}

}
/*! @decl void set_html_parser_options(int options)
 *!
 *! sets parser options. 
 *! 
 *! @param options
 *!   a bitwise or of @[Public.Parser.XML2.Constants] options 
 *!   where valid options start with PARSE_. 
 */
#define f_set_html_parser_options_defined
ptrdiff_t f_set_html_parser_options_fun_num = 0;
void f_set_html_parser_options(INT32 args) {
#line 229 "XML2.cmod"
INT_TYPE options;
#line 229 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("set_html_parser_options",args,1);
#line 229 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_BAD_ARG_ERROR("set_html_parser_options",1,"int");
options=Pike_sp[0-1].u.integer;
#line 230 "XML2.cmod"
{
  THIS->object_data->html_parser_options = options;
  pop_stack();
}

}
/*! @decl int substituteEntitiesDefault(int def)
 */
#define f_substituteEntitiesDefault_defined
ptrdiff_t f_substituteEntitiesDefault_fun_num = 0;
void f_substituteEntitiesDefault(INT32 args) {
#line 237 "XML2.cmod"
INT_TYPE def;
#line 237 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("substituteEntitiesDefault",args,1);
#line 237 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_BAD_ARG_ERROR("substituteEntitiesDefault",1,"int");
def=Pike_sp[0-1].u.integer;
#line 238 "XML2.cmod"
{
  def = def ? 1 : 0;

  pop_n_elems(args);

  xmlSubstituteEntitiesDefault(def);

  push_int(def);
}

}
#line 248 "XML2.cmod"
/* @decl int keepBlanksDefault(int def)
 */
#define f_keepBlanksDefault_defined
ptrdiff_t f_keepBlanksDefault_fun_num = 0;
void f_keepBlanksDefault(INT32 args) {
#line 250 "XML2.cmod"
INT_TYPE def;
#line 250 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("keepBlanksDefault",args,1);
#line 250 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_INT) SIMPLE_BAD_ARG_ERROR("keepBlanksDefault",1,"int");
def=Pike_sp[0-1].u.integer;
#line 251 "XML2.cmod"
{
  def = def ? 1 : 0;

  pop_n_elems(args);

  push_int(xmlKeepBlanksDefault(def));
}

}
/*!  @decl string utf8_to_html(string str)
 *!
 */
#define f_utf8_to_html_defined
ptrdiff_t f_utf8_to_html_fun_num = 0;
void f_utf8_to_html(INT32 args) {
#line 262 "XML2.cmod"
struct pike_string * str;
#line 262 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("utf8_to_html",args,1);
#line 262 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("utf8_to_html",1,"string");
#line 262 "XML2.cmod"
debug_malloc_pass(str=Pike_sp[0-1].u.string);
{
  char *html = NULL;
  int outlen, inlen;

  outlen = str->len << 1;
  html = (char*)malloc(outlen + 1);
  if (!html)
    Pike_error("Out of memory");

  inlen = str->len;
  if ( UTF8ToHtml(html, &outlen, str->str, &inlen) < 0 ) {
    free(html);
    Pike_error("Cannot convert to html!");
  }
  html[outlen] = '\0';
  pop_n_elems(args);
  push_text(html);
  free(html);
}


}
/*!  @decl string utf8_to_isolat1(string str)
 *!
 */
#define f_utf8_to_isolat1_defined
ptrdiff_t f_utf8_to_isolat1_fun_num = 0;
void f_utf8_to_isolat1(INT32 args) {
#line 287 "XML2.cmod"
struct pike_string * str;
#line 287 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("utf8_to_isolat1",args,1);
#line 287 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("utf8_to_isolat1",1,"string");
#line 287 "XML2.cmod"
debug_malloc_pass(str=Pike_sp[0-1].u.string);
{
  char *html = NULL;
  int outlen, inlen;

  outlen = str->len << 1;
  html = (char*)malloc(outlen + 1);
  if (!html)
    Pike_error("Out of memory");

  inlen = str->len;
  if ( UTF8Toisolat1(html, &outlen, str->str, &inlen) < 0 ) {
    free(html);
    Pike_error("Cannot convert to isolat1!");
  }
  html[outlen] = '\0';
  pop_n_elems(args);
  push_text(html);
  free(html);
}

}
/*! @decl int utf8_check(string str)
 *!
 */
#define f_utf8_check_defined
ptrdiff_t f_utf8_check_fun_num = 0;
void f_utf8_check(INT32 args) {
#line 311 "XML2.cmod"
struct pike_string * str;
#line 311 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("utf8_check",args,1);
#line 311 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("utf8_check",1,"string");
#line 311 "XML2.cmod"
debug_malloc_pass(str=Pike_sp[0-1].u.string);
{
  int result;

  result = xmlCheckUTF8(str->str);
  pop_n_elems(args);
  push_int(result);
}

}
/*! @decl array(int) get_encodings()
 *!
 *! gets the list of available character encoders.
 *!
 *! @returns
 *!  an array of available encoder identifiers.
 *!
 */
#define f_get_encodings_defined
ptrdiff_t f_get_encodings_fun_num = 0;
void f_get_encodings(INT32 args) {
#line 328 "XML2.cmod"
if(args != 0) wrong_number_of_args_error("get_encodings",args,0);
{
   int num = 0;
   int cur = 0;
   int upper_limit = 25;
   xmlCharEncodingHandlerPtr encoder;

   for (cur = 0; cur < upper_limit; cur++)
   {
     encoder = xmlGetCharEncodingHandler((xmlCharEncoding)cur);
     if(encoder != NULL)
     {
       push_int(cur);
       num++;
       xmlFree(encoder);
     }
   }
   f_aggregate(num);
   return;  
}

}
/*! @decl string render_xml(Node n)
 *!   Renders a node tree as an XML string. The entire document tree will 
 *!  be rendered. To render a portion of a tree, use @[Node.render_xml].
 *!
 *!   @param n
 *!     a Node object for a given XML document.
 *!
 */
#define f_render_xml_defined
ptrdiff_t f_render_xml_fun_num = 0;
void f_render_xml(INT32 args) {
#line 357 "XML2.cmod"
struct object * n;
#line 357 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("render_xml",args,1);
#line 357 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_OBJECT) SIMPLE_BAD_ARG_ERROR("render_xml",1,"object");
#line 357 "XML2.cmod"
debug_malloc_pass(n=Pike_sp[0-1].u.object);
{
  int dumped;
  xmlChar * buf;
  char * str;
  int bufsize;

  CHECK_NODE_PASSED(n);

/*  check_node_created(); */
  if(OBJ2_NODE(n)->object_data->node == NULL)
  { 
    Pike_error("Node not initialized.\n");
  }

  xmlDocDumpFormatMemory(OBJ2_NODE(n)->object_data->node->doc, &buf, &bufsize, 1);

  if(buf!=NULL)
  {
    str = (char *)xmlStrdup(buf);
    xmlFree(buf);
    push_text(str);
  }  
  else
  {
    push_int(0);
  }
}

}
/*! @decl string render_html(Node n)
 *!   Renders a node tree as an HTML string. The entire document tree will 
 *!  be rendered. To render a portion of a tree, use @[Node.render_html].
 *!
 *!   @param n
 *!     a Node object for a given HTML document.
 *!
 */
#define f_render_html_defined
ptrdiff_t f_render_html_fun_num = 0;
void f_render_html(INT32 args) {
#line 394 "XML2.cmod"
struct object * n;
#line 394 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("render_html",args,1);
#line 394 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_OBJECT) SIMPLE_BAD_ARG_ERROR("render_html",1,"object");
#line 394 "XML2.cmod"
debug_malloc_pass(n=Pike_sp[0-1].u.object);
{
  int dumped;
  xmlChar * buf;
  char * str;
  int bufsize;

  CHECK_NODE_PASSED(n);

/*  check_node_created(); */
  if(OBJ2_NODE(n)->object_data->node == NULL)
  { 
    Pike_error("Node not initialized.\n");
  }

  htmlDocDumpMemory(OBJ2_NODE(n)->object_data->node->doc, &buf, &bufsize, 1);

  if(buf!=NULL)
  {
    str = (char *)xmlStrdup(buf);
    xmlFree(buf);
    push_text(str);
  }  
  else
  {
    push_int(0);
  }
}

}
#line 423 "XML2.cmod"
void my_generic_error(void * userData, xmlErrorPtr error)
{
    printf("error level: %d, message: %s\n", error->level, error->message);
}

/*! @decl Node recover_xml(string xml)
 *!   Parse an string containing XML, even if it is not Well Formed.
 *!  @param xml
 *!   String containing XML data to parse.
 *!
 *!  @returns
 *!    a @[Node] object containing the root of the tree.
 */
#define f_parse_xml_force_defined
ptrdiff_t f_parse_xml_force_fun_num = 0;
void f_parse_xml_force(INT32 args) {
#line 436 "XML2.cmod"
struct pike_string * xml;
#line 436 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("parse_xml_force",args,1);
#line 436 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xml_force",1,"string");
#line 436 "XML2.cmod"
debug_malloc_pass(xml=Pike_sp[0-1].u.string);
{
  xmlDocPtr doc;
  xmlNodePtr node;
  struct Node_struct * ns;
  NODE_OBJECT_DATA * od;

  doc = xmlRecoverMemory(xml->str, xml->len);

  if(doc == NULL)
  {
    pop_n_elems(2);
    Pike_error("Unable to parse XML.\n");
  }  

  node = xmlDocGetRootElement(doc);
  if(node == NULL)
  {
    pop_n_elems(2);
    xmlFreeDoc(doc);
    Pike_error("Unable to find Root Node.\n");
  }

  /* ok, we have a parsed file. now let's turn it into a real live pike object. */

  pop_n_elems(2);

  apply(Pike_fp->current_object, "Node", 0);
  
  ns = OBJ2_NODE((Pike_sp[0-1].u.object));
  od = (NODE_OBJECT_DATA *)(ns->object_data);

  od->refs = malloc(sizeof(INT32));
  (* od->refs)=1;
  od->node = node;
}

}
#line 473 "XML2.cmod"
xmlParserInputPtr my_entity_loader(const char * URL, const char * ID,
   xmlParserCtxtPtr context)
{
  printf("request for entity %s\n", URL);
  return NULL; 
}

/*! @decl Node parse_xml(string xml, string|void name)
 *!   Parse an string containing XML.
 *!  @param xml
 *!   String containing XML data to parse.
 *!  @param name
 *!   String containing name/URI of file.
 *!
 *!  @returns
 *!    a @[Node] object containing the root of the tree.
 */
#define f_parse_xml_1_defined
ptrdiff_t f_parse_xml_1_fun_num = 0;
void f_parse_xml_1(INT32 args) {
#line 490 "XML2.cmod"
struct pike_string * xml;
#line 490 "XML2.cmod"
struct pike_string * name;
#line 490 "XML2.cmod"
if(args != 2) wrong_number_of_args_error("parse_xml",args,2);
#line 490 "XML2.cmod"
if(Pike_sp[0-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xml",1,"string");
#line 490 "XML2.cmod"
debug_malloc_pass(xml=Pike_sp[0-2].u.string);
#line 490 "XML2.cmod"
if(Pike_sp[1-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xml",2,"string");
#line 490 "XML2.cmod"
debug_malloc_pass(name=Pike_sp[1-2].u.string);
{
  xmlDocPtr doc;
  xmlNodePtr node;
  struct Node_struct * ns;
  NODE_OBJECT_DATA * od;
  char * err_ctx = "parse_xml";

  entity_loader = my_entity_loader;

  /*  xmlSetExternalEntityLoader(entity_loader); */

  generic_handler = my_generic_error;
  /* xmlSetStructuredErrorFunc((void *)err_ctx, generic_handler); */

  doc = xmlReadMemory(xml->str, xml->len, name->str, NULL, 
                      THIS->object_data->xml_parser_options);
  if(doc == NULL)
  {
    pop_n_elems(2);
    Pike_error("Unable to parse XML.\n");
  }  

  node = xmlDocGetRootElement(doc);
  if(node == NULL)
  {
    pop_n_elems(2);
    xmlFreeDoc(doc);
    Pike_error("Unable to find Root Node.\n");
  }

  /* ok, we have a parsed file. now let's turn it into a real live pike object. */

  pop_n_elems(2);

  apply(Pike_fp->current_object, "Node", 0);
  
  ns = OBJ2_NODE((Pike_sp[0-1].u.object));
  od = (NODE_OBJECT_DATA *)(ns->object_data);

  od->refs = malloc(sizeof(INT32));
  (* od->refs)=1;
  od->node = node;
}

}
#define f_parse_xml_2_defined
ptrdiff_t f_parse_xml_2_fun_num = 0;
void f_parse_xml_2(INT32 args) {
#line 535 "XML2.cmod"
struct pike_string * xml;
#line 535 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("parse_xml",args,1);
#line 535 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xml",1,"string");
#line 535 "XML2.cmod"
debug_malloc_pass(xml=Pike_sp[0-1].u.string);
{
  push_text("noname.xml");
  f_parse_xml(2);
}

}

#if defined(f_parse_xml_1_defined) || defined(f_parse_xml_2_defined)
#define f_parse_xml_defined
ptrdiff_t f_parse_xml_fun_num = 0;
void f_parse_xml(INT32 args) {
  switch(args) {
   case 1:

#ifdef f_parse_xml_2_defined
    f_parse_xml_2(args);
    return;

#endif /* f_parse_xml_2_defined */
    break;
   case 2:

#ifdef f_parse_xml_1_defined
    f_parse_xml_1(args);
    return;

#endif /* f_parse_xml_1_defined */
    break;
   default:
    wrong_number_of_args_error("parse_xml",args,1);
  }
}

#endif /* f_parse_xml_1_defined, f_parse_xml_2_defined */
/*! @decl Node parse_html(string html, string|void name)
 *!   Parse an string containing HTML.
 *!  @param xml
 *!   String containing HTML data to parse.
 *!  @param name
 *!   String containing name/URI of file.
 *!
 *!  @returns
 *!    a @[Node] object containing the root of the tree.
 */
#define f_parse_html_1_defined
ptrdiff_t f_parse_html_1_fun_num = 0;
void f_parse_html_1(INT32 args) {
#line 551 "XML2.cmod"
struct pike_string * html;
#line 551 "XML2.cmod"
struct pike_string * name;
#line 551 "XML2.cmod"
if(args != 2) wrong_number_of_args_error("parse_html",args,2);
#line 551 "XML2.cmod"
if(Pike_sp[0-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_html",1,"string");
#line 551 "XML2.cmod"
debug_malloc_pass(html=Pike_sp[0-2].u.string);
#line 551 "XML2.cmod"
if(Pike_sp[1-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_html",2,"string");
#line 551 "XML2.cmod"
debug_malloc_pass(name=Pike_sp[1-2].u.string);
{
  htmlDocPtr doc;
  xmlNodePtr node;
  struct Node_struct * ns;
  NODE_OBJECT_DATA * od;

  doc = htmlReadMemory(html->str, html->len, name->str, NULL, 
                       THIS->object_data->html_parser_options);
  if(doc == NULL)
  {
    pop_n_elems(2);
    Pike_error("Unable to parse HTML.\n");
  }  

  node = xmlDocGetRootElement(doc);
  if(node == NULL)
  {
    pop_n_elems(2);
    xmlFreeDoc(doc);
    Pike_error("Unable to find Root Node.\n");
  }

  /* ok, we have a parsed file. now let's turn it into a real live pike object. */

  pop_n_elems(2);

  apply(Pike_fp->current_object, "Node", 0);
  
  ns = OBJ2_NODE((Pike_sp[0-1].u.object));
  od = (NODE_OBJECT_DATA *)(ns->object_data);

  od->refs = malloc(sizeof(INT32));
  (* od->refs)=1;
  od->node = node;
}

}
#define f_parse_html_2_defined
ptrdiff_t f_parse_html_2_fun_num = 0;
void f_parse_html_2(INT32 args) {
#line 588 "XML2.cmod"
struct pike_string * html;
#line 588 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("parse_html",args,1);
#line 588 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_html",1,"string");
#line 588 "XML2.cmod"
debug_malloc_pass(html=Pike_sp[0-1].u.string);
{
  push_text("noname.html");
  f_parse_html(2);
}

}

#if defined(f_parse_html_1_defined) || defined(f_parse_html_2_defined)
#define f_parse_html_defined
ptrdiff_t f_parse_html_fun_num = 0;
void f_parse_html(INT32 args) {
  switch(args) {
   case 1:

#ifdef f_parse_html_2_defined
    f_parse_html_2(args);
    return;

#endif /* f_parse_html_2_defined */
    break;
   case 2:

#ifdef f_parse_html_1_defined
    f_parse_html_1(args);
    return;

#endif /* f_parse_html_1_defined */
    break;
   default:
    wrong_number_of_args_error("parse_html",args,1);
  }
}

#endif /* f_parse_html_1_defined, f_parse_html_2_defined */
/*! @decl Node new_xml(string version)
 *!   Create a new XML document with a root node.
 *!
 *!  @param version
 *!     the version of XML to create
 *!  @param root_name
 *!      the name of the root node
 *!  @returns
 *!    a @[Node] object containing the root of the tree.
 */
#define f_new_xml_defined
ptrdiff_t f_new_xml_fun_num = 0;
void f_new_xml(INT32 args) {
#line 604 "XML2.cmod"
struct pike_string * version;
#line 604 "XML2.cmod"
struct pike_string * root_name;
#line 604 "XML2.cmod"
if(args != 2) wrong_number_of_args_error("new_xml",args,2);
#line 604 "XML2.cmod"
if(Pike_sp[0-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("new_xml",1,"string");
#line 604 "XML2.cmod"
debug_malloc_pass(version=Pike_sp[0-2].u.string);
#line 604 "XML2.cmod"
if(Pike_sp[1-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("new_xml",2,"string");
#line 604 "XML2.cmod"
debug_malloc_pass(root_name=Pike_sp[1-2].u.string);
{
  xmlDocPtr doc;
  xmlNodePtr node;
  struct Node_struct * ns;
  NODE_OBJECT_DATA * od;

  doc = xmlNewDoc(version->str);

  if(doc == NULL)
  {
    Pike_error("Unable to create new XML document.\n");
  }  
  node = xmlNewNode(NULL, (xmlChar *)root_name->str);
  xmlDocSetRootElement(doc, node);
  if(node == NULL)
  {
    xmlFreeDoc(doc);
    Pike_error("Unable to find Root Node.\n");
  }

  /* ok, we have a parsed file. now let's turn it into a real live pike object. */

  pop_stack();

  apply(Pike_fp->current_object, "Node", 0);
  
  ns = OBJ2_NODE((Pike_sp[0-1].u.object));
  od = (NODE_OBJECT_DATA *)(ns->object_data);

  od->refs = malloc(sizeof(INT32));
  (* od->refs)=1;
  od->node = node;
}

}
/*! @decl Node parse_xslt(string xslt, string|void name)
 *!   Parse a string containing an XSLT stylesheet.
 *!
 *!  @param name
 *!   String containing name/URI of file.
 *!
 *!  @returns
 *!    a @[Stylesheet] object containing the parsed stylesheet.
 */
#define f_parse_xslt_1_defined
ptrdiff_t f_parse_xslt_1_fun_num = 0;
void f_parse_xslt_1(INT32 args) {
#line 648 "XML2.cmod"
struct pike_string * xslt;
#line 648 "XML2.cmod"
if(args != 1) wrong_number_of_args_error("parse_xslt",args,1);
#line 648 "XML2.cmod"
if(Pike_sp[0-1].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xslt",1,"string");
#line 648 "XML2.cmod"
debug_malloc_pass(xslt=Pike_sp[0-1].u.string);
{
  push_text("noname.xsl");
  f_parse_xslt(2);
}

}
#define f_parse_xslt_2_defined
ptrdiff_t f_parse_xslt_2_fun_num = 0;
void f_parse_xslt_2(INT32 args) {
#line 654 "XML2.cmod"
struct pike_string * xslt;
#line 654 "XML2.cmod"
struct pike_string * name;
#line 654 "XML2.cmod"
if(args != 2) wrong_number_of_args_error("parse_xslt",args,2);
#line 654 "XML2.cmod"
if(Pike_sp[0-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xslt",1,"string");
#line 654 "XML2.cmod"
debug_malloc_pass(xslt=Pike_sp[0-2].u.string);
#line 654 "XML2.cmod"
if(Pike_sp[1-2].type != PIKE_T_STRING) SIMPLE_BAD_ARG_ERROR("parse_xslt",2,"string");
#line 654 "XML2.cmod"
debug_malloc_pass(name=Pike_sp[1-2].u.string);
{
  struct object * node_obj = NULL;
  xsltStylesheetPtr sty = NULL;
  xmlDocPtr doc = NULL;
  struct Stylesheet_struct * ss;
  STYLESHEET_OBJECT_DATA * od;

  /* first, we must parse the xml file. conveniently, we have a function */
  /* that does that already! */
  f_parse_xml(2);

  /* we're going to hang on to the node object. */
  node_obj = Pike_sp[0-1].u.object;
  add_ref(node_obj);

  doc = OBJ2_NODE(node_obj)->object_data->node->doc;

  if(doc==NULL)
  {
    Pike_error("whoa, horsie! we don't have an xml document!\n");
  }

  sty = xsltParseStylesheetDoc(doc);

  if(sty == NULL)
  {
    Pike_error("Unable to parse stylesheet node.\n");
  }  

  /* ok, we have a parsed sheet. now let's turn it into a real live pike object. */

  pop_stack();

  apply(Pike_fp->current_object, "Stylesheet", 0);
  
  ss = OBJ2_STYLESHEET((Pike_sp[0-1].u.object));
  od = (STYLESHEET_OBJECT_DATA *)(ss->object_data);

  od->refs = malloc(sizeof(INT32));
  (* od->refs)=1;

  /* TODO: not sure if this is a leak, but we need it to prevent the 
     xmlNode from being freed a second time at the pike Node's destruction  
     (the Stylesheet will free that itself. Note that this is a tricky 
     subject... since the two are linked, perhaps we should call 
     Node.destroy() when the Stylesheet is destroyed... */
  *(OBJ2_NODE(node_obj)->object_data->refs)++;
  od->stylesheet = sty;
  ss->node = node_obj;
}

}

#if defined(f_parse_xslt_1_defined) || defined(f_parse_xslt_2_defined)
#define f_parse_xslt_defined
ptrdiff_t f_parse_xslt_fun_num = 0;
void f_parse_xslt(INT32 args) {
  switch(args) {
   case 1:

#ifdef f_parse_xslt_1_defined
    f_parse_xslt_1(args);
    return;

#endif /* f_parse_xslt_1_defined */
    break;
   case 2:

#ifdef f_parse_xslt_2_defined
    f_parse_xslt_2(args);
    return;

#endif /* f_parse_xslt_2_defined */
    break;
   default:
    wrong_number_of_args_error("parse_xslt",args,1);
  }
}

#endif /* f_parse_xslt_1_defined, f_parse_xslt_2_defined */

#undef extra_defined
#define extra_defined

#undef internal_init_defined
#define internal_init_defined

#undef event_handler_defined
#define event_handler_defined
static void init_struct(void)
#line 724 "XML2.cmod"
{
  PARSER_OBJECT_DATA * dta;

  dta = (PARSER_OBJECT_DATA*)malloc(sizeof(PARSER_OBJECT_DATA));
  if(dta == NULL)
  {
    Pike_error("xml2_init: unable to allocate memory.\n");
  }

  dta->xml_parser_options = 0;
  dta->html_parser_options = 0;
  THIS->object_data = dta;
 
  LIBXML_TEST_VERSION
}


#undef internal_exit_defined
#define internal_exit_defined

#undef event_handler_defined
#define event_handler_defined
static void exit_struct(void)
#line 741 "XML2.cmod"
{

  pike_exit_xml2_xmlreader();
  pike_exit_xml2_stylesheet();
  pike_exit_xml2_sax();
  pike_exit_xml2_node();
  pike_exit_xml2_constants();
  
#ifdef HAVE_XSLT
xsltCleanupGlobals();
#endif /* HAVE_XSLT */

  xmlCleanupParser();
}

/*! @endmodule
 */

/*! @endmodule
 */

/*! @endmodule
 */




#ifdef event_handler_defined
static void event_handler(int ev) {
  switch(ev) {

#ifdef internal_init_defined
  case PROG_EVENT_INIT: init_struct(); break;

#endif /* internal_init_defined */

#ifdef internal_exit_defined
  case PROG_EVENT_EXIT: exit_struct(); break;

#endif /* internal_exit_defined */
  default: break; 
  }
}

#endif /* event_handler_defined */
PIKE_MODULE_INIT {

#ifdef THIS_
  _storage_offset = ADD_STORAGE(struct _struct);
#endif /* THIS_ */

#ifdef var_docloader_defined
  PIKE_MAP_VARIABLE("docloader", _storage_offset + OFFSETOF(_struct, docloader),
                    tFuncV(tNone,tMix,tAny), PIKE_T_MIXED, 0);
#endif /* var_docloader_defined */

#ifdef extra_defined
#line 707 "XML2.cmod"
{
  struct pike_string * ps;
  MAKE_CONST_STRING(ps, "docloader");
  docloader_num = isidentifier(ps);
  THIS->object_data->docloader_num = docloader_num;

  add_string_constant("__version", "1.16", 0);
  add_string_constant("__author", "Bill Welliver ", 0);

  pike_init_xml2_constants();
  pike_init_xml2_node();
  pike_init_xml2_sax();
  pike_init_xml2_stylesheet();
  pike_init_xml2_xmlreader();
}


#endif /* extra_defined */

#ifdef event_handler_defined
  pike_set_prog_event_callback(event_handler);

#endif /* event_handler_defined */

#ifdef f_select_xpath_nodes_defined
  f_select_xpath_nodes_fun_num =
#line 88 "XML2.cmod"
    ADD_FUNCTION2("select_xpath_nodes", f_select_xpath_nodes, tFunc(tString tObj,tArray), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_select_xpath_nodes_defined */

#ifdef f_set_xslt_docloader_defined
  f_set_xslt_docloader_fun_num =
#line 182 "XML2.cmod"
    ADD_FUNCTION2("set_xslt_docloader", f_set_xslt_docloader, tFunc(tFuncV(tNone,tMix,tAny),tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_set_xslt_docloader_defined */

#ifdef f_clear_xslt_docloader_defined
  f_clear_xslt_docloader_fun_num =
#line 196 "XML2.cmod"
    ADD_FUNCTION2("clear_xslt_docloader", f_clear_xslt_docloader, tFunc(tNone,tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_clear_xslt_docloader_defined */

#ifdef f_set_xml_parser_options_defined
  f_set_xml_parser_options_fun_num =
#line 215 "XML2.cmod"
    ADD_FUNCTION2("set_xml_parser_options", f_set_xml_parser_options, tFunc("\10\200\0\0\0\177\377\377\377",tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_set_xml_parser_options_defined */

#ifdef f_set_html_parser_options_defined
  f_set_html_parser_options_fun_num =
#line 229 "XML2.cmod"
    ADD_FUNCTION2("set_html_parser_options", f_set_html_parser_options, tFunc("\10\200\0\0\0\177\377\377\377",tVoid), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_set_html_parser_options_defined */

#ifdef f_substituteEntitiesDefault_defined
  f_substituteEntitiesDefault_fun_num =
#line 237 "XML2.cmod"
    ADD_FUNCTION2("substituteEntitiesDefault", f_substituteEntitiesDefault, tFunc("\10\200\0\0\0\177\377\377\377","\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_substituteEntitiesDefault_defined */

#ifdef f_keepBlanksDefault_defined
  f_keepBlanksDefault_fun_num =
#line 250 "XML2.cmod"
    ADD_FUNCTION2("keepBlanksDefault", f_keepBlanksDefault, tFunc("\10\200\0\0\0\177\377\377\377","\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_keepBlanksDefault_defined */

#ifdef f_utf8_to_html_defined
  f_utf8_to_html_fun_num =
#line 262 "XML2.cmod"
    ADD_FUNCTION2("utf8_to_html", f_utf8_to_html, tFunc(tString,tString), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_utf8_to_html_defined */

#ifdef f_utf8_to_isolat1_defined
  f_utf8_to_isolat1_fun_num =
#line 287 "XML2.cmod"
    ADD_FUNCTION2("utf8_to_isolat1", f_utf8_to_isolat1, tFunc(tString,tString), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_utf8_to_isolat1_defined */

#ifdef f_utf8_check_defined
  f_utf8_check_fun_num =
#line 311 "XML2.cmod"
    ADD_FUNCTION2("utf8_check", f_utf8_check, tFunc(tString,"\10\200\0\0\0\177\377\377\377"), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_utf8_check_defined */

#ifdef f_get_encodings_defined
  f_get_encodings_fun_num =
#line 328 "XML2.cmod"
    ADD_FUNCTION2("get_encodings", f_get_encodings, tFunc(tNone,tArray), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_get_encodings_defined */

#ifdef f_render_xml_defined
  f_render_xml_fun_num =
#line 357 "XML2.cmod"
    ADD_FUNCTION2("render_xml", f_render_xml, tFunc(tObj,tString), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_render_xml_defined */

#ifdef f_render_html_defined
  f_render_html_fun_num =
#line 394 "XML2.cmod"
    ADD_FUNCTION2("render_html", f_render_html, tFunc(tObj,tString), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_render_html_defined */

#ifdef f_parse_xml_force_defined
  f_parse_xml_force_fun_num =
#line 436 "XML2.cmod"
    ADD_FUNCTION2("parse_xml_force", f_parse_xml_force, tFunc(tString,tObj), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_parse_xml_force_defined */

#ifdef f_parse_xml_defined
  f_parse_xml_fun_num =
#line 535 "XML2.cmod"
    ADD_FUNCTION2("parse_xml", f_parse_xml, tOr(tFunc(tString tString,tObj),tFunc(tString,tObj)), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_parse_xml_defined */

#ifdef f_parse_html_defined
  f_parse_html_fun_num =
#line 588 "XML2.cmod"
    ADD_FUNCTION2("parse_html", f_parse_html, tOr(tFunc(tString tString,tObj),tFunc(tString,tObj)), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_parse_html_defined */

#ifdef f_new_xml_defined
  f_new_xml_fun_num =
#line 604 "XML2.cmod"
    ADD_FUNCTION2("new_xml", f_new_xml, tFunc(tString tString,tObj), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_new_xml_defined */

#ifdef f_parse_xslt_defined
  f_parse_xslt_fun_num =
#line 654 "XML2.cmod"
    ADD_FUNCTION2("parse_xslt", f_parse_xslt, tOr(tFunc(tString,tObj),tFunc(tString tString,tObj)), 0, OPT_EXTERNAL_DEPEND|OPT_SIDE_EFFECT);

#endif /* f_parse_xslt_defined */
}
PIKE_MODULE_EXIT {
}


gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University