Ejemplo n.º 1
0
/**
 * Sets the style file
 *
 * \param xsldoc Either a filename or source of XSL file
 */
void CXSLTransform::SetXSLDoc(CString xsldoc)
{
	if (m_XSLDoc.Compare(xsldoc) != 0) {
		m_XSLDoc = xsldoc;
		Reset();
		if (xsldoc.IsEmpty()) {
			return;
		}
		xmlErrorPtr err = NULL;

		xmlDocPtr xsl = NULL;
		CFileFind ff;
		if (xsldoc.GetLength() < MAX_PATH && ff.FindFile(xsldoc)) {
			// xsldoc is a filename
			ff.Close();
			m_Style = xsltParseStylesheetFile((const xmlChar *)(LPCSTR)xsldoc);
			if (!m_Style || m_Style->errors > 0) {
				xmlFreeDoc(xsl);
				err = xmlGetLastError();
				if (err != NULL)
					m_LastError.Format(AfxLoadString(IDS_STRING_XSLPARSEERR), err->file, err->line, err->int2, err->code, err->message);
				else
					m_LastError.LoadString(IDS_STRING_UNKNOWN_XSLERROR);
				xmlResetLastError();
#ifdef _DEBUG
				TRACE1("CXSLTransform::SetXSLDoc(): %s\n", m_LastError);
#endif
			}
		} else {
			// xsldoc is a XSL source
			xsl = xmlParseMemory(xsldoc, xsldoc.GetLength());
			if (xsl) {
				m_Style = xsltParseStylesheetDoc(xsl);
				if (!m_Style || m_Style->errors > 0) {
					xmlFreeDoc(xsl);
					err = xmlGetLastError();
					if (err != NULL)
						m_LastError.Format(AfxLoadString(IDS_STRING_XSLPARSEERR), err->file, err->line, err->int2, err->code, err->message);
					else
						m_LastError.LoadString(IDS_STRING_UNKNOWN_XSLERROR);
					xmlResetLastError();
#ifdef _DEBUG
					TRACE1("CXSLTransform::SetXSLDoc(): %s\n", m_LastError);
#endif
				}
			} else {
				// XML parsing error
				err = xmlGetLastError();
				m_LastError.Format(AfxLoadString(IDS_STRING_XMLPARSEERR), err->file, err->line, err->int2, err->code, err->message);
				xmlResetLastError();
#ifdef _DEBUG
				TRACE1("CXSLTransform::SetXSLDoc(): %s\n", m_LastError);
#endif
			}
		}
	}
}
Ejemplo n.º 2
0
htmlDocPtr htmlParse(void *buffer, int buffer_len, void *url, void *encoding, int options, void *error_buffer, int error_buffer_len) {
	const char *c_buffer       = (char*)buffer;
	const char *c_url          = (char*)url;
	const char *c_encoding     = (char*)encoding;
	xmlDoc *doc = NULL;
	
	xmlResetLastError();
	doc = htmlReadMemory(c_buffer, buffer_len, c_url, c_encoding, options);

	if(doc == NULL) {
		xmlErrorPtr error;
	    xmlFreeDoc(doc);
	    error = xmlGetLastError();
		if(error != NULL && error_buffer != NULL && error->level >= XML_ERR_ERROR) {
			char *c_error_buffer = (char*)error_buffer;
			if (error->message != NULL) {
				strncpy(c_error_buffer, error->message, error_buffer_len-1);
				c_error_buffer[error_buffer_len-1] = '\0';
			}
			else {
				snprintf(c_error_buffer, error_buffer_len, "xml parsing error:%d", error->code);
			}
		}
	}
	return doc;
}
Ejemplo n.º 3
0
/*
 * call-seq:
 *  read_memory(string, url, encoding, options)
 *
 * Create a new document from a String
 */
static VALUE read_memory( VALUE klass,
                          VALUE string,
                          VALUE url,
                          VALUE encoding,
                          VALUE options )
{
    const char * c_buffer = StringValuePtr(string);
    const char * c_url    = NIL_P(url)      ? NULL : StringValuePtr(url);
    const char * c_enc    = NIL_P(encoding) ? NULL : StringValuePtr(encoding);
    int len               = (int)RSTRING_LEN(string);
    VALUE error_list      = rb_ary_new();

    xmlResetLastError();
    xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
    xmlDocPtr doc = xmlReadMemory(c_buffer, len, c_url, c_enc, (int)NUM2INT(options));
    xmlSetStructuredErrorFunc(NULL, NULL);

    if(doc == NULL) {
        xmlFreeDoc(doc);

        xmlErrorPtr error = xmlGetLastError();
        if(error)
            rb_exc_raise(Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error));
        else
            rb_raise(rb_eRuntimeError, "Could not parse document");

        return Qnil;
    }

    VALUE document = Nokogiri_wrap_xml_document(klass, doc);
    rb_iv_set(document, "@errors", error_list);
    return document;
}
Ejemplo n.º 4
0
gboolean
parse_feed (gchar*      data,
            gint64      length,
            GSList*     parsers,
            KatzeArray* array,
            GError**    error)
{
    xmlDocPtr doc;
    xmlErrorPtr xerror;

    LIBXML_TEST_VERSION

    doc = xmlReadMemory (
                data, length, "feedfile.xml", NULL,
                XML_PARSE_NOWARNING | XML_PARSE_NOERROR /*| XML_PARSE_RECOVER*/
                );

    if (doc)
    {
        feed_parse_doc (doc, parsers, array, error);
        xmlFreeDoc (doc);
    }
    else
    {
        xerror = xmlGetLastError ();
        *error = g_error_new (FEED_PARSE_ERROR,
                              FEED_PARSE_ERROR_PARSE,
                              _("Failed to parse XML feed: %s"),
                              xerror->message);
        xmlResetLastError ();
    }
    xmlMemoryDump ();

    return *error ? FALSE : TRUE;
}
Ejemplo n.º 5
0
PLIST_API void plist_cleanup(void)
{
    /* free memory from parser initialization */
    xmlCleanupCharEncodingHandlers();
    xmlDictCleanup();
    xmlResetLastError();
    xmlCleanupGlobals();
    xmlCleanupThreads();
    xmlCleanupMemory();
}
Ejemplo n.º 6
0
void
xml_throw_unless(bool condition) {
	if (!condition) {
		char const *message = "unknown XML error";
		xmlErrorPtr xmlErr = xmlGetLastError();
		if (xmlErr && xmlErr->message) {
			message = xmlErr->message;
		}
		error err("%s", message);
		xmlResetLastError();
		throw err;
	}
}
Ejemplo n.º 7
0
void RelaxNGSchema::parse_context(_xmlRelaxNGParserCtxt* context)
{
  xmlResetLastError();
  release_underlying();

  if (!context)
    throw parse_error("RelaxNGSchema::parse_context(): Could not create parser context.\n" + format_xml_error());
  
  RelaxNGSchemaParserContextHolder holder(context);

  pimpl_->schema = xmlRelaxNGParse(context);
  if (!pimpl_->schema)
    throw parse_error("RelaxNGSchema::parse_context(): Schema could not be parsed.\n" + format_xml_error());
}
Ejemplo n.º 8
0
//----------------------------------------------------------
void ParserXML::reiniciarParser(void)
{
    elem_raiz=elem_atual=NULL;
    if(doc_xml)
    {
        xmlFreeDoc(doc_xml);
        doc_xml=NULL;
    }
    decl_dtd=buffer_xml=decl_xml="";

    while(!pilha_elems.empty())
        pilha_elems.pop();

    xmlResetLastError();
    nome_doc_xml="";
}
Ejemplo n.º 9
0
void XMLParser::restartParser(void)
{
    root_elem=curr_elem=nullptr;

    if(xml_doc)
    {
        xmlFreeDoc(xml_doc);
        xml_doc=nullptr;
    }
    dtd_decl=xml_buffer=xml_decl="";

    while(!elems_stack.empty())
        elems_stack.pop();

    xml_doc_filename="";
    xmlResetLastError();
}
Ejemplo n.º 10
0
/*
 * call-seq:
 *  read_io(io, url, encoding, options)
 *
 * Create a new document from an IO object
 */
static VALUE read_io( VALUE klass,
                      VALUE io,
                      VALUE url,
                      VALUE encoding,
                      VALUE options )
{
  const char * c_url    = NIL_P(url)      ? NULL : StringValuePtr(url);
  const char * c_enc    = NIL_P(encoding) ? NULL : StringValuePtr(encoding);
  VALUE error_list      = rb_ary_new();
  VALUE document;
  xmlDocPtr doc;

  xmlResetLastError();
  Nokogiri_install_error_catcher(error_list);

  doc = xmlReadIO(
      (xmlInputReadCallback)io_read_callback,
      (xmlInputCloseCallback)io_close_callback,
      (void *)io,
      c_url,
      c_enc,
      (int)NUM2INT(options)
  );
  Nokogiri_remove_error_catcher();

  if(doc == NULL) {
    xmlErrorPtr error;

    xmlFreeDoc(doc);

    error = xmlGetLastError();
    if(error)
      rb_exc_raise(Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error));
    else
      rb_raise(rb_eRuntimeError, "Could not parse document");

    return Qnil;
  }

  document = Nokogiri_wrap_xml_document(klass, doc);
  rb_iv_set(document, "@errors", error_list);
  return document;
}
Ejemplo n.º 11
0
/*
 * call-seq:
 *  read_io(io, url, encoding, options)
 *
 * Create a new document from an IO object
 */
static VALUE read_io( VALUE klass,
                      VALUE io,
                      VALUE url,
                      VALUE encoding,
                      VALUE options )
{
  const char * c_url    = (url == Qnil) ? NULL : StringValuePtr(url);
  const char * c_enc    = (encoding == Qnil) ? NULL : StringValuePtr(encoding);
  VALUE error_list      = rb_ary_new();

  xmlInitParser();
  xmlResetLastError();
  xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);

  xmlDocPtr doc = xmlReadIO(
      (xmlInputReadCallback)io_read_callback,
      (xmlInputCloseCallback)io_close_callback,
      (void *)io,
      c_url,
      c_enc,
      NUM2INT(options)
  );
  xmlSetStructuredErrorFunc(NULL, NULL);

  if(doc == NULL) {
    xmlFreeDoc(doc);

    xmlErrorPtr error = xmlGetLastError();
    if(error)
      rb_funcall(rb_mKernel, rb_intern("raise"), 1,
          Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error)
      );
    else
      rb_raise(rb_eRuntimeError, "Could not parse document");

    return Qnil;
  }

  VALUE document = Nokogiri_wrap_xml_document(klass, doc);
  rb_funcall(document, rb_intern("errors="), 1, error_list);
  return document;
}
Ejemplo n.º 12
0
int Document::ProcessXInclude(bool generateXIncludeNodes)
{
    NodeMap nmap;
    xmlNodePtr root = xmlDocGetRootElement(xml());
    find_wrappers(root, nmap);
    
    xmlResetLastError();
    int substitutionCount = xmlXIncludeProcessTreeFlags(root, generateXIncludeNodes ? 0 : XML_PARSE_NOXINCNODE);
    
    prune_unchanged_wrappers(Node::xml(), nmap);
    for ( auto item : nmap )
    {
        delete item.first;
    }
    
    if ( substitutionCount < 0 )
        throw InternalError("Failed to process XInclude", xmlGetLastError());
    
    return substitutionCount;
}
Ejemplo n.º 13
0
void SaxParser::finish_chunk_parsing()
{
  xmlResetLastError();
  if(!context_)
  {
    context_ = xmlCreatePushParserCtxt(
      sax_handler_.get(),
      0, // this, // user_data
      0, // chunk
      0, // size
      0); // no filename for fetching external entities

    if(!context_)
    {
      throw internal_error("Could not create parser context\n" + format_xml_error());
    }
    initialize_context();
  }
  else
    xmlCtxtResetLastError(context_);

  int parseError = XML_ERR_OK;
  if(!exception_)
    //This is called just to terminate parsing.
    parseError = xmlParseChunk(context_, 0 /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */);

  Glib::ustring error_str = format_xml_parser_error(context_);
  if (error_str.empty() && parseError != XML_ERR_OK)
    error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(parseError);

  release_underlying(); // Free context_

  check_for_exception();

  if(!error_str.empty())
  {
    throw parse_error(error_str);
  }
}
Ejemplo n.º 14
0
// implementation of this function is inspired by the SAX documentation by James Henstridge.
// (http://www.daa.com.au/~james/gnome/xml-sax/implementing.html)
void SaxParser::parse()
{
  //TODO If this is not the first parsing with this SaxParser, the xmlDoc object
  // in entity_resolver_doc_ should be deleted and replaced by a new one.
  // Otherwise entity declarations from a previous parsing may erroneously affect
  // this parsing. This would be much easier if entity_resolver_doc_ were a
  // std::auto_ptr<Document>, so the xmlpp::Document could be deleted and a new
  // one created. A good place for such code would be in an overridden
  // SaxParser::initialize_context(). It would be an ABI break.

  if(!context_)
  {
    throw internal_error("Parser context not created.");
  }

  xmlSAXHandlerPtr old_sax = context_->sax;
  context_->sax = sax_handler_.get();

  xmlResetLastError();
  initialize_context();
  
  const int parseError = xmlParseDocument(context_);

  context_->sax = old_sax;

  Glib::ustring error_str = format_xml_parser_error(context_);
  if (error_str.empty() && parseError == -1)
    error_str = "xmlParseDocument() failed.";

  release_underlying(); // Free context_

  check_for_exception();

  if(!error_str.empty())
  {
    throw parse_error(error_str);
  }
}
Ejemplo n.º 15
0
void SaxParser::parse_chunk_raw(const unsigned char* contents, size_type bytes_count)
{
  KeepBlanks k(KeepBlanks::Default);
  xmlResetLastError();

  if(!context_)
  {
    context_ = xmlCreatePushParserCtxt(
      sax_handler_.get(),
      0, // user_data
      0, // chunk
      0, // size
      0); // no filename for fetching external entities

    if(!context_)
    {
      throw internal_error("Could not create parser context\n" + format_xml_error());
    }
    initialize_context();
  }
  else
    xmlCtxtResetLastError(context_);
  
  int parseError = XML_ERR_OK;
  if(!exception_)
    parseError = xmlParseChunk(context_, (const char*)contents, bytes_count, 0 /* don't terminate */);

  check_for_exception();

  Glib::ustring error_str = format_xml_parser_error(context_);
  if (error_str.empty() && parseError != XML_ERR_OK)
    error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(parseError);
  if(!error_str.empty())
  {
    throw parse_error(error_str);
  }
}
Ejemplo n.º 16
0
/*
 * call-seq:
 *  dup
 *
 * Copy this Document.  An optional depth may be passed in, but it defaults
 * to a deep copy.  0 is a shallow copy, 1 is a deep copy.
 */
static VALUE duplicate_document(int argc, VALUE *argv, VALUE self)
{
  xmlDocPtr doc, dup;
  VALUE copy;
  VALUE level;
  VALUE error_list      = rb_ary_new();

  if(rb_scan_args(argc, argv, "01", &level) == 0)
    level = INT2NUM((long)1);

  Data_Get_Struct(self, xmlDoc, doc);

  xmlResetLastError();
  xmlSetStructuredErrorFunc((void *)error_list, Nokogiri_error_array_pusher);
  dup = xmlCopyDoc(doc, (int)NUM2INT(level));
  xmlSetStructuredErrorFunc(NULL, NULL);

  if(dup == NULL) return Qnil;

  dup->type = doc->type;
  copy = Nokogiri_wrap_xml_document(rb_obj_class(self), dup);
  rb_iv_set(copy, "@errors", error_list);
  return copy ;
}
Ejemplo n.º 17
0
 void requestInit() override {
   xmlResetLastError();
 }
Ejemplo n.º 18
0
void HHVM_FUNCTION(libxml_clear_errors) {
  xmlResetLastError();
  tl_libxml_request_data->m_errors.reset();
}
/*
 * call-seq:
 *  evaluate(search_path, handler = nil)
 *
 * Evaluate the +search_path+ returning an XML::XPath object.
 */
static VALUE evaluate(int argc, VALUE *argv, VALUE self)
{
  VALUE search_path, xpath_handler;
  VALUE thing = Qnil;
  xmlXPathContextPtr ctx;
  xmlXPathObjectPtr xpath;
  xmlChar *query;

  Data_Get_Struct(self, xmlXPathContext, ctx);

  if(rb_scan_args(argc, argv, "11", &search_path, &xpath_handler) == 1)
    xpath_handler = Qnil;

  query = (xmlChar *)StringValuePtr(search_path);

  if(Qnil != xpath_handler) {
    /* FIXME: not sure if this is the correct place to shove private data. */
    ctx->userData = (void *)xpath_handler;
    xmlXPathRegisterFuncLookup(ctx, lookup, (void *)xpath_handler);
  }

  xmlResetLastError();
  xmlSetStructuredErrorFunc(NULL, xpath_exception_handler);

  /* For some reason, xmlXPathEvalExpression will blow up with a generic error */
  /* when there is a non existent function. */
  xmlSetGenericErrorFunc(NULL, xpath_generic_exception_handler);

  xpath = xmlXPathEvalExpression(query, ctx);
  xmlSetStructuredErrorFunc(NULL, NULL);
  xmlSetGenericErrorFunc(NULL, NULL);

  if(xpath == NULL) {
    VALUE xpath = rb_const_get(mNokogiriXml, rb_intern("XPath"));
    VALUE klass = rb_const_get(xpath, rb_intern("SyntaxError"));

    xmlErrorPtr error = xmlGetLastError();
    rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
  }

  assert(ctx->doc);
  assert(DOC_RUBY_OBJECT_TEST(ctx->doc));

  switch(xpath->type) {
    case XPATH_STRING:
      thing = NOKOGIRI_STR_NEW2(xpath->stringval);
      break;
    case XPATH_NODESET:
      if(NULL == xpath->nodesetval) {
        thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
          DOC_RUBY_OBJECT(ctx->doc));
      } else {
        thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
            DOC_RUBY_OBJECT(ctx->doc));
      }
      break;
    case XPATH_NUMBER:
      thing = rb_float_new(xpath->floatval);
      break;
    case XPATH_BOOLEAN:
      thing = xpath->boolval == 1 ? Qtrue : Qfalse;
      break;
    default:
      thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
        DOC_RUBY_OBJECT(ctx->doc));
  }

  xmlXPathFreeNodeSetList(xpath);

  return thing;
}
Ejemplo n.º 20
0
void SaxParser::parse_stream(std::istream& in)
{
  if(context_)
  {
    throw parse_error("Attempt to start a second parse while a parse is in progress.");
  }

  KeepBlanks k(KeepBlanks::Default);
  xmlResetLastError();

  context_ = xmlCreatePushParserCtxt(
      sax_handler_.get(),
      0, // user_data
      0, // chunk
      0, // size
      0); // no filename for fetching external entities

  if(!context_)
  {
    throw internal_error("Could not create parser context\n" + format_xml_error());
  }

  initialize_context();

  //TODO: Shouldn't we use a Glib::ustring here, and some alternative to std::getline()?
  int firstParseError = XML_ERR_OK;
  std::string line;
  while( ( ! exception_ )
      && std::getline(in, line))
  {
    // since getline does not get the line separator, we have to add it since the parser care
    // about layout in certain cases.
    line += '\n';

    const int parseError = xmlParseChunk(context_, line.c_str(),
      line.size() /* This is a std::string, not a ustring, so this is the number of bytes. */,
      0 /* don't terminate */);

    // Save the first error code if any, but read on.
    // More errors might be reported and then thrown by check_for_exception().
    if (parseError != XML_ERR_OK && firstParseError == XML_ERR_OK)
      firstParseError = parseError;
  }

  if( ! exception_ )
  {
     //This is called just to terminate parsing.
    const int parseError = xmlParseChunk(context_, 0 /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */);

    if (parseError != XML_ERR_OK && firstParseError == XML_ERR_OK)
      firstParseError = parseError;
  }

  Glib::ustring error_str = format_xml_parser_error(context_);
  if (error_str.empty() && firstParseError != XML_ERR_OK)
    error_str = "Error code from xmlParseChunk(): " + Glib::ustring::format(firstParseError);

  release_underlying(); // Free context_

  check_for_exception();

  if(!error_str.empty())
  {
    throw parse_error(error_str);
  }
}
Ejemplo n.º 21
0
Archivo: xlibxml.c Proyecto: GNOME/gxml
/**
 * gxml_reset_last_error:
 *
 * Deprecated: 0.15
 */
void gxml_reset_last_error ()
{
  xmlResetLastError ();
}