Example #1
0
	XDocument* XParser::ParseMemory(const char* content, bool keep_blank/*=false*/)
	{
		xmlParserCtxtPtr pxParseCtxt = NULL;

		xmlKeepBlanksDefault(keep_blank?1:0);
		xmlDoValidityCheckingDefaultValue =0;
		pxParseCtxt = xmlCreateMemoryParserCtxt((const char*)content, strlen(content));
		if(pxParseCtxt==NULL)
		{
			return NULL;
		}

		if(!ParseContext(pxParseCtxt))
		{
			xmlFreeParserCtxt(pxParseCtxt);
			return NULL;
		}

		XDocument* pagXmlDoc = NULL;
		pagXmlDoc = new XDocument(pxParseCtxt->myDoc);

		xmlFreeParserCtxt(pxParseCtxt);

		return pagXmlDoc;
	}
Example #2
0
gboolean
ide_xml_sax_parse (IdeXmlSax   *self,
                   const gchar *data,
                   gsize        length,
                   const gchar *uri,
                   gpointer     user_data)
{
  gboolean wellformed;

  g_return_val_if_fail (IDE_IS_XML_SAX (self), FALSE);
  g_return_val_if_fail (data != NULL, FALSE);
  g_return_val_if_fail (length > 0, FALSE);

  g_return_val_if_fail (self->initialized == TRUE, FALSE);
  g_return_val_if_fail (self->context == NULL, FALSE);

  self->context = xmlCreateMemoryParserCtxt (data, length);
  self->context->userData = user_data;

  self->context->sax = &self->handler;
  self->handler.initialized = XML_SAX2_MAGIC;
  xmlCtxtUseOptions (self->context, XML_PARSE_RECOVER | XML_PARSE_NOENT);

  xmlParseDocument (self->context);
  wellformed = self->context->wellFormed;

  self->context->sax = NULL;
  g_clear_pointer (&self->context, xmlFreeParserCtxt);

  return wellformed;
}
Example #3
0
void SaxParser::parse_memory_raw(const unsigned char* contents, size_type bytes_count)
{
  if(context_)
  {
    throw parse_error("Attempt to start a second parse while a parse is in progress.");
  }

  KeepBlanks k(KeepBlanks::Default);

  context_ = xmlCreateMemoryParserCtxt((const char*)contents, bytes_count);
  parse();
}
/*
 * call-seq:
 *  parse_memory(data)
 *
 * Parse the XML stored in memory in +data+
 */
static VALUE parse_memory(VALUE klass, VALUE data)
{
  if(NIL_P(data)) rb_raise(rb_eArgError, "data cannot be nil");
  if(!(int)RSTRING_LEN(data))
    rb_raise(rb_eRuntimeError, "data cannot be empty");

  xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(
      StringValuePtr(data),
      (int)RSTRING_LEN(data)
  );

  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}
Example #5
0
xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size)
{
	xmlParserCtxtPtr ctxt = NULL;
	xmlDocPtr ret;


/*
	xmlInitParser();
*/
	ctxt = xmlCreateMemoryParserCtxt(buf, buf_size);
	if (ctxt) {
		zend_bool old;

		ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
		ctxt->sax->comment = soap_Comment;
		ctxt->sax->warning = NULL;
		ctxt->sax->error = NULL;
		/*ctxt->sax->fatalError = NULL;*/
#if LIBXML_VERSION >= 20703
		ctxt->options |= XML_PARSE_HUGE;
#endif
		old = php_libxml_disable_entity_loader(1);
		xmlParseDocument(ctxt);
		php_libxml_disable_entity_loader(old);
		if (ctxt->wellFormed) {
			ret = ctxt->myDoc;
			if (ret->URL == NULL && ctxt->directory != NULL) {
				ret->URL = xmlCharStrdup(ctxt->directory);
			}
		} else {
			ret = NULL;
			xmlFreeDoc(ctxt->myDoc);
			ctxt->myDoc = NULL;
		}
		xmlFreeParserCtxt(ctxt);
	} else {
		ret = NULL;
	}

/*
	xmlCleanupParser();
*/

/*
	if (ret) {
		cleanup_xml_node((xmlNodePtr)ret);
	}
*/
	return ret;
}
Example #6
0
UT_Error UT_XML::parse (const char * buffer, UT_uint32 length)
{
  if (!m_bSniffing)
    {
      UT_ASSERT (m_pListener || m_pExpertListener);
      if ((m_pListener == 0) && (m_pExpertListener == 0)) return UT_ERROR;
    }
  UT_ASSERT (buffer);
  if (buffer == 0 || length == 0) return UT_ERROR;

  if (!reset_all ()) return UT_OUTOFMEM;

  UT_Error ret = UT_OK;

  xmlParserCtxtPtr ctxt;

  xmlSAXHandler hdl;
  memset(&hdl, 0, sizeof(hdl));

  hdl.getEntity    = _getEntity;
  hdl.startElement = _startElement;
  hdl.endElement   = _endElement;
  hdl.characters   = _charData;
  hdl.error        = _errorSAXFunc;
  hdl.fatalError   = _fatalErrorSAXFunc;
  hdl.processingInstruction = _processingInstruction;
  hdl.comment      = _comment;
  hdl.cdataBlock   = _cdata;

  ctxt = xmlCreateMemoryParserCtxt (buffer, static_cast<int>(length));
  if (ctxt == NULL)
    {
      UT_DEBUGMSG (("Unable to create libxml2 memory context!\n"));
      return UT_ERROR;
    }
  memcpy(ctxt->sax, &hdl, sizeof(hdl));
  ctxt->userData = static_cast<void *>(this);

  m_bStopped = false;

  xmlParseDocument (ctxt);

  if (!ctxt->wellFormed) ret = UT_IE_IMPORTERROR;

  xmlDocPtr myXmlDoc = ctxt->myDoc;
  xmlFreeParserCtxt (ctxt);
  xmlFreeDoc(myXmlDoc);

  return ret;
}
Example #7
0
bool XSLStyleSheet::parseString(const String& string, bool)
{
    // Parse in a single chunk into an xmlDocPtr
    const UChar BOM = 0xFEFF;
    const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM);
    setLoaderForLibXMLCallbacks(docLoader());
    if (!m_stylesheetDocTaken)
        xmlFreeDoc(m_stylesheetDoc);
    m_stylesheetDocTaken = false;

#if ENABLE(INSPECTOR)
    Console* console = 0;
    if (Frame* frame = ownerDocument()->frame())
        console = frame->domWindow()->console();
    xmlSetStructuredErrorFunc(console, XSLTProcessor::parseErrorFunc);
    xmlSetGenericErrorFunc(console, XSLTProcessor::genericErrorFunc);
#endif

    const char* buffer = reinterpret_cast<const char*>(string.characters());
    int size = string.length() * sizeof(UChar);

    xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(buffer, size);

    if (m_parentStyleSheet) {
        // The XSL transform may leave the newly-transformed document
        // with references to the symbol dictionaries of the style sheet
        // and any of its children. XML document disposal can corrupt memory
        // if a document uses more than one symbol dictionary, so we
        // ensure that all child stylesheets use the same dictionaries as their
        // parents.
        xmlDictFree(ctxt->dict);
        ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
        xmlDictReference(ctxt->dict);
    }

    m_stylesheetDoc = xmlCtxtReadMemory(ctxt, buffer, size,
                                        href().utf8().data(),
                                        BOMHighByte == 0xFF ? "UTF-16LE" : "UTF-16BE",
                                        XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NOCDATA);
    xmlFreeParserCtxt(ctxt);

    loadChildSheets();

    xmlSetStructuredErrorFunc(0, 0);
    xmlSetGenericErrorFunc(0, 0);

    setLoaderForLibXMLCallbacks(0);
    return m_stylesheetDoc;
}
	bool LibxmlSaxParser::parseBuffer( const char* uri, const char* buffer, int length )
	{
        mParserContext = xmlCreateMemoryParserCtxt( buffer, length );
        
        if ( !mParserContext )
        {
            ParserError error(ParserError::SEVERITY_CRITICAL,
                              ParserError::ERROR_COULD_NOT_OPEN_FILE,
                              0,
                              0,
                              0,
                              0,
                              uri);
            IErrorHandler* errorHandler = getParser()->getErrorHandler();
            if ( errorHandler )
            {
                errorHandler->handleError(error);
            }
            return false;
        }
        
        // We let libxml replace the entities
        mParserContext->replaceEntities = 1;

        if (mParserContext->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
        {
            xmlFree(mParserContext->sax);
        }
        
        mParserContext->sax = &SAXHANDLER;
        mParserContext->userData = (void*)this;
        
        initializeParserContext();
        xmlParseDocument(mParserContext);
        
        mParserContext->sax = 0;
        
        if ( mParserContext->myDoc )
        {
            xmlFreeDoc(mParserContext->myDoc);
            mParserContext->myDoc = 0;
        }
        
        xmlFreeParserCtxt(mParserContext);
        mParserContext = 0;
        
        return true;
	}
/* call-seq:
 *    XML::Parser::Context.string(string) -> XML::Parser::Context
 *
 * Creates a new parser context based on the specified string.
 *
 * Parameters:
 *
 *  string - A string that contains the data to parse.
*/
static VALUE rxml_parser_context_string(VALUE klass, VALUE string)
{
  xmlParserCtxtPtr ctxt;
  Check_Type(string, T_STRING);

  if (RSTRING_LEN(string) == 0)
    rb_raise(rb_eArgError, "Must specify a string with one or more characters");

  ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(string),
                                   RSTRING_LEN(string));

  if (!ctxt)
    rxml_raise(&xmlLastError);

  return rxml_parser_context_wrap(ctxt);
}
bool XSLStyleSheet::parseString(const String& string)
{
    // Parse in a single chunk into an xmlDocPtr
    const UChar BOM = 0xFEFF;
    const unsigned char BOMHighByte = *reinterpret_cast<const unsigned char*>(&BOM);
    if (!m_stylesheetDocTaken)
        xmlFreeDoc(m_stylesheetDoc);
    m_stylesheetDocTaken = false;

    PageConsoleClient* console = nullptr;
    Frame* frame = ownerDocument()->frame();
    if (frame && frame->page())
        console = &frame->page()->console();

    XMLDocumentParserScope scope(cachedResourceLoader(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);

    auto upconvertedCharacters = StringView(string).upconvertedCharacters();
    const char* buffer = reinterpret_cast<const char*>(upconvertedCharacters.get());
    int size = string.length() * sizeof(UChar);

    xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(buffer, size);
    if (!ctxt)
        return 0;

    if (m_parentStyleSheet) {
        // The XSL transform may leave the newly-transformed document
        // with references to the symbol dictionaries of the style sheet
        // and any of its children. XML document disposal can corrupt memory
        // if a document uses more than one symbol dictionary, so we
        // ensure that all child stylesheets use the same dictionaries as their
        // parents.
        xmlDictFree(ctxt->dict);
        ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
        xmlDictReference(ctxt->dict);
    }

    m_stylesheetDoc = xmlCtxtReadMemory(ctxt, buffer, size,
        finalURL().string().utf8().data(),
        BOMHighByte == 0xFF ? "UTF-16LE" : "UTF-16BE",
        XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NOCDATA);
    xmlFreeParserCtxt(ctxt);

    loadChildSheets();

    return m_stylesheetDoc;
}
Example #11
0
File: xml.cpp Project: BruceZu/hhvm
xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size,
                              bool skip_clean /*= true */) {
  xmlParserCtxtPtr ctxt = nullptr;
  xmlDocPtr ret;

/*
  xmlInitParser();
*/
  ctxt = xmlCreateMemoryParserCtxt((const char *)buf, buf_size);
  if (ctxt) {
    ctxt->keepBlanks = 0;
    ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
    ctxt->sax->comment = soap_Comment;
    ctxt->sax->warning = nullptr;
    ctxt->sax->error = nullptr;
    /*ctxt->sax->fatalError = nullptr;*/
    auto old = HHVM_FN(libxml_disable_entity_loader)(true);
    xmlParseDocument(ctxt);
    HHVM_FN(libxml_disable_entity_loader)(old);
    if (ctxt->wellFormed) {
      ret = ctxt->myDoc;
      if (ret->URL == nullptr && ctxt->directory != nullptr) {
        ret->URL = xmlCharStrdup(ctxt->directory);
      }
    } else {
      ret = nullptr;
      xmlFreeDoc(ctxt->myDoc);
      ctxt->myDoc = nullptr;
    }
    xmlFreeParserCtxt(ctxt);
  } else {
    ret = nullptr;
  }

/*
  xmlCleanupParser();
*/

  if (!skip_clean && ret) {
    cleanup_xml_node((xmlNodePtr)ret);
  }
  return ret;
}
Example #12
0
xmlDocPtr soap_xmlParseMemory(const void *buf, size_t buf_size) {
  xmlParserCtxtPtr ctxt = NULL;
  xmlDocPtr ret;

/*
  xmlInitParser();
*/
  ctxt = xmlCreateMemoryParserCtxt((const char *)buf, buf_size);
  if (ctxt) {
    ctxt->keepBlanks = 0;
    ctxt->sax->ignorableWhitespace = soap_ignorableWhitespace;
    ctxt->sax->comment = soap_Comment;
    ctxt->sax->warning = NULL;
    ctxt->sax->error = NULL;
    /*ctxt->sax->fatalError = NULL;*/
    xmlParseDocument(ctxt);
    if (ctxt->wellFormed) {
      ret = ctxt->myDoc;
      if (ret->URL == NULL && ctxt->directory != NULL) {
        ret->URL = xmlCharStrdup(ctxt->directory);
      }
    } else {
      ret = NULL;
      xmlFreeDoc(ctxt->myDoc);
      ctxt->myDoc = NULL;
    }
    xmlFreeParserCtxt(ctxt);
  } else {
    ret = NULL;
  }

/*
  xmlCleanupParser();
*/

/*
  if (ret) {
    cleanup_xml_node((xmlNodePtr)ret);
  }
*/
  return ret;
}
bool XSLStyleSheet::parseString(const String& source)
{
    // Parse in a single chunk into an xmlDocPtr
    if (!m_stylesheetDocTaken)
        xmlFreeDoc(m_stylesheetDoc);
    m_stylesheetDocTaken = false;

    PageConsole* console = 0;
    Frame* frame = ownerDocument()->frame();
    if (frame && frame->host())
        console = &frame->host()->console();

    XMLDocumentParserScope scope(fetcher(), XSLTProcessor::genericErrorFunc, XSLTProcessor::parseErrorFunc, console);
    XMLParserInput input(source);

    xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(input.data(), input.size());
    if (!ctxt)
        return 0;

    if (m_parentStyleSheet) {
        // The XSL transform may leave the newly-transformed document
        // with references to the symbol dictionaries of the style sheet
        // and any of its children. XML document disposal can corrupt memory
        // if a document uses more than one symbol dictionary, so we
        // ensure that all child stylesheets use the same dictionaries as their
        // parents.
        xmlDictFree(ctxt->dict);
        ctxt->dict = m_parentStyleSheet->m_stylesheetDoc->dict;
        xmlDictReference(ctxt->dict);
    }

    m_stylesheetDoc = xmlCtxtReadMemory(ctxt, input.data(), input.size(),
        finalURL().string().utf8().data(), input.encoding(),
        XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NOCDATA);

    xmlFreeParserCtxt(ctxt);
    loadChildSheets();
    return m_stylesheetDoc;
}
Example #14
0
 std::auto_ptr<document> dom_parser::parse_string(const char* str)
 {
     set_global_variables global_variables;
     // Create a libxml2 parser context for parsing the xml string.
     if (str == 0)
     {
         throw dom_error("fail to parse xml string: string is null");
     }
     int size = static_cast<int>(std::strlen(str));
     if (size == 0)
     {
         throw dom_error("fail to parse xml string: string is empty");
     }
     dom_parser_context_wrapper context( xmlCreateMemoryParserCtxt(str, size) );
     if (context.get() == 0)
     {
         throw dom_error("Fail to parse xml string: unable to create libxml2 parser context");
     }
     // Parse xml string under the constructed parser context.
     xmlDoc* px = parse_in_context(context.get());
     assert(px != 0);
     return std::auto_ptr<document>(new document(px));
 }
Example #15
0
bool HandlerBase::Parse( const char* buf, int size)
{
	context_ = xmlCreateMemoryParserCtxt( buf, size );
	if ( !context_ ) throw SaxError();

	xmlSAXHandler sh = 
	{
		0,0,0,0,0,
		0, //zGetEntity,
		0,0,0,0,0,0,
		zStartDocument,
		zEndDocument,
		zStartElement,
		zEndElement,
		0,
		zCharacters,
		0,0,
		zComment,
		zWarning,
		zzError,
		zFatalError,
		0,
		zCDATABlock,
		0
	};

	context_->sax = &sh;
	context_->userData = this;
	xmlSubstituteEntitiesDefault( 1 );
	xmlKeepBlanksDefault( 0 );
	xmlParseDocument( context_ );
	bool wellFormed = context_->wellFormed ? true : false;
	context_->sax = 0;
	xmlFreeParserCtxt( context_ );

	return wellFormed;
}