示例#1
0
result_t XmlParser::parse(XmlDocument* doc, exlib::string source)
{
    XmlParser parser(doc, true);

    parser.m_now = doc;
    parser.m_list.push_back(doc);

    XML_Parser xml_parser = XML_ParserCreate(NULL);

    XML_SetParamEntityParsing(xml_parser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
    XML_SetUserData(xml_parser, &parser);

    XML_SetXmlDeclHandler(xml_parser, XmlDeclHandler);
    XML_SetElementHandler(xml_parser, StartElementHandler, EndElementHandler);
    XML_SetCharacterDataHandler(xml_parser, CharacterDataHandler);
    XML_SetProcessingInstructionHandler(xml_parser, ProcessingInstructionHandler);
    XML_SetCommentHandler(xml_parser, CommentHandler);
    XML_SetCdataSectionHandler(xml_parser, StartCdataSectionHandler, EndCdataSectionHandler);
    XML_SetStartDoctypeDeclHandler(xml_parser, StartDoctypeDeclHandler);

    if (XML_Parse(xml_parser, source.c_str(), (int32_t)source.length(), true) != XML_STATUS_OK) {
        char msg[128];
        sprintf(msg, "XmlParser: error on line %lu at column %lu: %s", XML_GetCurrentLineNumber(xml_parser),
            XML_GetCurrentColumnNumber(xml_parser) + 1,
            XML_ErrorString(XML_GetErrorCode(xml_parser)));

        XML_ParserFree(xml_parser);
        return CHECK_ERROR(Runtime::setError(msg));
    }

    XML_ParserFree(xml_parser);

    return 0;
}
示例#2
0
void xmlg_parser_error_abort(
      xmlGParser *xml_parser,
      HqBool fire_error_handler,
      uint8 *detail,
      int32  detail_len)
{
  xmlGParserCommon *c ;
  xmlGFilterChain *filter_chain ;
  uint32 line, column ;

  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL") ;
  c = xmlg_get_parser_common(xml_parser);
  XMLGASSERT(c != NULL, "common is NULL");

  filter_chain = c->filter_chain ;

  /* setting the callback functions to NULL ensures we stop */
  XML_SetElementHandler(xml_parser->ctxt, NULL, NULL) ;
  XML_SetStartNamespaceDeclHandler(xml_parser->ctxt, NULL) ;
  XML_SetCharacterDataHandler(xml_parser->ctxt, NULL) ;
  XML_SetXmlDeclHandler(xml_parser->ctxt, NULL) ;
  XML_SetStartDoctypeDeclHandler(xml_parser->ctxt, NULL) ;

  if (fire_error_handler) {
    xmlg_parser_line_and_column(xml_parser, &line, &column) ;

    /* only ever invoke the error handler once */
    if (c->parse_error_cb != NULL && fire_error_handler && ! c->success_abort)
      c->parse_error_cb(xml_parser, xmlg_fc_get_uri(filter_chain), line, column,
                        detail, detail_len) ;
  }

  if (! c->success_abort)
    c->error_abort = TRUE ;
}
XmlEncodingSpy::XmlEncodingSpy ( const char *encoding )
	: WrapExpat ( encoding )
	, d ( new EncodingData() )
{
	XML_SetUserData ( p, d.get() );
	XML_SetXmlDeclHandler ( p, xmldeclhandler );
	XML_SetStartElementHandler ( p, start );
	d->p = p;
}
示例#4
0
TinyDomElement* TinyDom::parse(std::istream& istr) {

  XML_Parser parser = XML_ParserCreate(0);
  TinyDomParser tdparser;

  XML_SetUserData(parser, &tdparser);
  XML_SetCharacterDataHandler(parser, characterDataHandler);
  XML_SetElementHandler(parser, startElementHandler, endElementHandler);
  XML_SetCommentHandler(parser, commentHandler);
  XML_SetProcessingInstructionHandler(parser, processingInstructionHandler);
  XML_SetXmlDeclHandler(parser, xmlDeclHandler);
  XML_SetDefaultHandler(parser, defaultHandler);
  XML_SetDoctypeDeclHandler(parser, startDoctypeDeclHandler, endDoctypeDeclHandler);

  try {

    while (1) {
      void* buf = XML_GetBuffer(parser, 1024);
      if (!buf) {
	throw std::runtime_error("out of memory!");
      }
      istr.read((char*)buf, 1024);
	  std::streamsize len = istr.gcount();
      if (istr.fail() && !istr.eof()) {
	throw std::runtime_error("failed IO");
      }
      bool isFinal = (istr.eof() || len < 1024);
      if (! XML_ParseBuffer(parser, len, isFinal)) {
	std::ostringstream ostr;
	ostr << "parse error at line " << XML_GetErrorLineNumber(parser)
	     << ", column " << XML_GetErrorColumnNumber(parser) << ": "
	     << XML_ErrorString(XML_GetErrorCode(parser));
	throw std::runtime_error(ostr.str());
				 
      }
      if (isFinal) {
	break;
      }
    }
	
    XML_ParserFree(parser);
	
  } catch (...) {

    //std::cerr << "Got exception: " << e.what() << "\n";

    if (parser) { XML_ParserFree(parser); }
    delete tdparser.rootElement;
    throw;

  }

  return tdparser.rootElement;

}
示例#5
0
/* ============================================================================
 * Abort and information functions.
 */
void xmlg_p_abort_parse(
      xmlGParser *xml_parser)
{
  xmlGParserCommon *c;
  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL");
  c = xmlg_get_parser_common(xml_parser);
  XMLGASSERT(c != NULL, "common is NULL");

  c->success_abort = TRUE;
  /* setting the callback functions to NULL ensures we stop */
  XML_SetElementHandler(xml_parser->ctxt, NULL, NULL);
  XML_SetStartNamespaceDeclHandler(xml_parser->ctxt, NULL);
  XML_SetCharacterDataHandler(xml_parser->ctxt, NULL);
  XML_SetXmlDeclHandler(xml_parser->ctxt, NULL) ;
  XML_SetStartDoctypeDeclHandler(xml_parser->ctxt, NULL) ;
}
示例#6
0
文件: lxplib.c 项目: JohnCrash/edu
static int lxp_make_parser (lua_State *L) {
  XML_Parser p;
  int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0);
  char sep = *luaL_optstring(L, 2, "");
  lxp_userdata *xpu = createlxp(L);
  xpu->bufferCharData = bufferCharData;
  p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) :
                                    XML_ParserCreateNS(NULL, sep);
  if (!p)
    luaL_error(L, "XML_ParserCreate failed");
  luaL_checktype(L, 1, LUA_TTABLE);
  checkcallbacks(L);
  lua_pushvalue(L, 1);
  xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX);
  XML_SetUserData(p, xpu);
  if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey))
    XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey);
  if (hasfield(L, CharDataKey))
    XML_SetCharacterDataHandler(p, f_CharData);
  if (hasfield(L, CommentKey))
    XML_SetCommentHandler(p, f_Comment);
  if (hasfield(L, DefaultKey))
    XML_SetDefaultHandler(p, f_Default);
  if (hasfield(L, DefaultExpandKey))
    XML_SetDefaultHandlerExpand(p, f_DefaultExpand);
  if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey))
    XML_SetElementHandler(p, f_StartElement, f_EndElement);
  if (hasfield(L, ExternalEntityKey))
    XML_SetExternalEntityRefHandler(p, f_ExternaEntity);
  if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey))
    XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl);
  if (hasfield(L, NotationDeclKey))
    XML_SetNotationDeclHandler(p, f_NotationDecl);
  if (hasfield(L, NotStandaloneKey))
    XML_SetNotStandaloneHandler(p, f_NotStandalone);
  if (hasfield(L, ProcessingInstructionKey))
    XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction);
  if (hasfield(L, UnparsedEntityDeclKey))
    XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl);
  if (hasfield(L, StartDoctypeDeclKey))
    XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl);
  if (hasfield(L, XmlDeclKey))
    XML_SetXmlDeclHandler(p, f_XmlDecl);
  return 1;
}
示例#7
0
文件: encoder.cpp 项目: isub/wbxml
int Encode (CFileReader &p_coFileReader)
{
	int iRetVal = 0;
	XML_Parser psoParser;

	psoParser = XML_ParserCreate ("UTF-8");
	if (NULL == psoParser) {
		iRetVal = ENOMEM;
		return iRetVal;
	}

	SDoc soDoc;

	/* регистрация обработчика данных */
	XML_SetElementHandler (psoParser, StartElementHandler, EndElementHandler);
	XML_SetXmlDeclHandler (psoParser, XmlDeclHandler);
	XML_SetDoctypeDeclHandler (psoParser, StartDoctypeDeclHandler, EndDoctypeDeclHandler);
	XML_SetUserData (psoParser, &soDoc);

	/* парсинг данных */
	char mcBuf[256];
	int iDataLen;
	int iIsFinal = 0;
	do {
		iDataLen = sizeof (mcBuf);
		if (p_coFileReader.ReadData ((unsigned char*)mcBuf, iDataLen))
			iIsFinal = 1;
		XML_Parse (psoParser, mcBuf, iDataLen, iIsFinal);
		if (iIsFinal)
			break;
	} while (1);

	Tokenize (soDoc);

	if (psoParser) {
		XML_ParserFree (psoParser);
		psoParser = NULL;
	}

	return iRetVal;
}
NS_IMETHODIMP
nsExpatDriver::WillBuildModel(const CParserContext& aParserContext,
                              nsITokenizer* aTokenizer,
                              nsIContentSink* aSink)
{
  mSink = do_QueryInterface(aSink);
  if (!mSink) {
    NS_ERROR("nsExpatDriver didn't get an nsIExpatSink");
    // Make sure future calls to us bail out as needed
    mInternalState = NS_ERROR_UNEXPECTED;
    return mInternalState;
  }

  mOriginalSink = aSink;

  static const XML_Memory_Handling_Suite memsuite =
    {
      (void *(*)(size_t))PR_Malloc,
      (void *(*)(void *, size_t))PR_Realloc,
      PR_Free
    };

  static const PRUnichar kExpatSeparator[] = { kExpatSeparatorChar, '\0' };

  mExpatParser = XML_ParserCreate_MM(kUTF16, &memsuite, kExpatSeparator);
  NS_ENSURE_TRUE(mExpatParser, NS_ERROR_FAILURE);

  XML_SetReturnNSTriplet(mExpatParser, XML_TRUE);

#ifdef XML_DTD
  XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS);
#endif

  mURISpec = aParserContext.mScanner->GetFilename();

  XML_SetBase(mExpatParser, mURISpec.get());

  nsCOMPtr<nsIDocument> doc = do_QueryInterface(mOriginalSink->GetTarget());
  if (doc) {
    nsCOMPtr<nsPIDOMWindow> win = doc->GetWindow();
    if (!win) {
      PRBool aHasHadScriptHandlingObject;
      nsIScriptGlobalObject *global =
        doc->GetScriptHandlingObject(aHasHadScriptHandlingObject);
      if (global) {
        win = do_QueryInterface(global);
      }
    }
    if (win && !win->IsInnerWindow()) {
      win = win->GetCurrentInnerWindow();
    }
    if (win) {
      mInnerWindowID = win->WindowID();
    }
  }

  // Set up the callbacks
  XML_SetXmlDeclHandler(mExpatParser, Driver_HandleXMLDeclaration); 
  XML_SetElementHandler(mExpatParser, Driver_HandleStartElement,
                        Driver_HandleEndElement);
  XML_SetCharacterDataHandler(mExpatParser, Driver_HandleCharacterData);
  XML_SetProcessingInstructionHandler(mExpatParser,
                                      Driver_HandleProcessingInstruction);
  XML_SetDefaultHandlerExpand(mExpatParser, Driver_HandleDefault);
  XML_SetExternalEntityRefHandler(mExpatParser,
                                  (XML_ExternalEntityRefHandler)
                                          Driver_HandleExternalEntityRef);
  XML_SetExternalEntityRefHandlerArg(mExpatParser, this);
  XML_SetCommentHandler(mExpatParser, Driver_HandleComment);
  XML_SetCdataSectionHandler(mExpatParser, Driver_HandleStartCdataSection,
                             Driver_HandleEndCdataSection);

  XML_SetParamEntityParsing(mExpatParser,
                            XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE);
  XML_SetDoctypeDeclHandler(mExpatParser, Driver_HandleStartDoctypeDecl,
                            Driver_HandleEndDoctypeDecl);

  // If the sink is an nsIExtendedExpatSink,
  // register some addtional handlers.
  mExtendedSink = do_QueryInterface(mSink);
  if (mExtendedSink) {
    XML_SetNamespaceDeclHandler(mExpatParser,
                                Driver_HandleStartNamespaceDecl,
                                Driver_HandleEndNamespaceDecl);
    XML_SetUnparsedEntityDeclHandler(mExpatParser,
                                     Driver_HandleUnparsedEntityDecl);
    XML_SetNotationDeclHandler(mExpatParser,
                               Driver_HandleNotationDecl);
  }

  // Set up the user data.
  XML_SetUserData(mExpatParser, this);

  // XML must detect invalid character convertion
  aParserContext.mScanner->OverrideReplacementCharacter(0xffff);

  return mInternalState;
}
示例#9
0
WBXML_DECLARE(WBXMLError) wbxml_tree_from_xml(WB_UTINY *xml, WB_ULONG xml_len, WBXMLTree **tree)
{
#if defined( HAVE_EXPAT )

    const XML_Feature *feature_list = NULL;
    XML_Parser         xml_parser   = NULL;
    WBXMLError         ret          = WBXML_OK;
    WB_BOOL            expat_utf16  = FALSE;
    WBXMLTreeClbCtx    wbxml_tree_clb_ctx;

    /* First Check if Expat is outputing UTF-16 strings */
    feature_list = (const XML_Feature *)XML_GetFeatureList();

    if ((feature_list != NULL) && (feature_list[0].value != sizeof(WB_TINY))) {
#if !defined( HAVE_ICONV )
        /* Ouch, can't convert from UTF-16 to UTF-8 */
        return WBXML_ERROR_XMLPARSER_OUTPUT_UTF16;
#else
        /* Expat returns UTF-16 encoded strings in its callbacks */
        expat_utf16 = TRUE;
#endif /* !HAVE_ICONV */
    }

    if (tree != NULL)
        *tree = NULL;

    /* Create Expat XML Parser */
    if ((xml_parser = XML_ParserCreateNS(NULL, WBXML_NAMESPACE_SEPARATOR)) == NULL)
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;

    /* Init context */
    wbxml_tree_clb_ctx.current = NULL;
    wbxml_tree_clb_ctx.error = WBXML_OK;
    wbxml_tree_clb_ctx.skip_lvl = 0;
    wbxml_tree_clb_ctx.skip_start = 0;
    wbxml_tree_clb_ctx.xml_parser = xml_parser;
    wbxml_tree_clb_ctx.input_buff = xml;
    wbxml_tree_clb_ctx.expat_utf16 = expat_utf16;

    /* Create WBXML Tree */
    if ((wbxml_tree_clb_ctx.tree = wbxml_tree_create(WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN)) == NULL) {
        XML_ParserFree(xml_parser);
        WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Tree"));
        return WBXML_ERROR_NOT_ENOUGH_MEMORY;
    }
    
    /* Set Handlers Callbacks */
    XML_SetXmlDeclHandler(xml_parser, wbxml_tree_clb_xml_decl);
    XML_SetStartDoctypeDeclHandler(xml_parser, wbxml_tree_clb_xml_doctype_decl);
    XML_SetElementHandler(xml_parser, wbxml_tree_clb_xml_start_element, wbxml_tree_clb_xml_end_element);
    XML_SetCdataSectionHandler(xml_parser, wbxml_tree_clb_xml_start_cdata, wbxml_tree_clb_xml_end_cdata);
    XML_SetProcessingInstructionHandler(xml_parser , wbxml_tree_clb_xml_pi);
    XML_SetCharacterDataHandler(xml_parser, wbxml_tree_clb_xml_characters);
    XML_SetUserData(xml_parser, (void*)&wbxml_tree_clb_ctx);

    /* Parse the XML Document to WBXML Tree */
    if (XML_Parse(xml_parser, (WB_TINY*) xml, xml_len, TRUE) == 0)
    {
        WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - expat error %i\n"
            "\tdescription: %s\n"
            "\tline: %i\n"
            "\tcolumn: %i\n"
            "\tbyte index: %i\n"
            "\ttotal bytes: %i\n%s",
            XML_GetErrorCode(xml_parser), 
            XML_ErrorString(XML_GetErrorCode(xml_parser)), 
            XML_GetCurrentLineNumber(xml_parser), 
            XML_GetCurrentColumnNumber(xml_parser), 
            XML_GetCurrentByteIndex(xml_parser), 
            XML_GetCurrentByteCount(xml_parser), xml));

        wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);

        ret = WBXML_ERROR_XML_PARSING_FAILED;
    }
    else {
        if ((ret = wbxml_tree_clb_ctx.error) != WBXML_OK)
        {
            WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - context error %i", ret));
            wbxml_tree_destroy(wbxml_tree_clb_ctx.tree);
        }
        else
            *tree = wbxml_tree_clb_ctx.tree;
    }

    /* Clean-up */
    XML_ParserFree(xml_parser);

    return ret;

#else /* HAVE_EXPAT */

#if defined( HAVE_LIBXML )

    /** @todo Use LibXML2 SAX interface ! */
    return WBXML_ERROR_NO_XMLPARSER;

#else /* HAVE_LIBXML */
    
    /** @note You can add here another XML Parser support */
    return WBXML_ERROR_NO_XMLPARSER;

#endif /* HAVE_LIBXML */

#endif /* HAVE_EXPAT */
}
示例#10
0
/* ============================================================================
 * Create/Destroy XML parse handler.
 */
HqBool xmlg_p_new(
      xmlGContext *xml_ctxt,
      xmlGParser **xml_parser,
      xmlGMemoryHandler *memory_handler,
      xmlGFilterChain *filter_chain)
{
  xmlGParser *new_xml_parser;

  XMLGASSERT(xml_ctxt != NULL, "xml_ctxt is NULL");
  XMLGASSERT(xml_parser != NULL, "xml_parser is NULL");
  XMLGASSERT(filter_chain != NULL, "filter_chain is NULL");

  *xml_parser = NULL;

  /* Although this code is going to be common to all back end XML parsers,
     we need to do it here because only this file knows about the size of
     xmlGParser - which is back end specific. */
  if (memory_handler != NULL) {
    if (memory_handler->f_malloc == NULL ||
        memory_handler->f_realloc == NULL ||
        memory_handler->f_free == NULL) {

      XMLGASSERT(FALSE, "incomplete memory handler") ;
      return FALSE ;
    }

    /* Use the pluged memory handler to allocate this structure */
    if ((new_xml_parser = memory_handler->f_malloc(sizeof(xmlGParser))) == NULL)
      return FALSE;
  } else {
    /* use the XML sub-system memory management */
    if ((new_xml_parser = xmlg_subsystem_malloc(xml_ctxt, sizeof(xmlGParser))) == NULL)
      return FALSE;
  }

  if (! xmlg_parser_common_init(new_xml_parser,
                                xml_ctxt,
                                memory_handler,
                                filter_chain)) {
    /* We need to do this as we don't know if the memory handlers have
       been plugged successfuly. Better safe than sorry. */
    if (memory_handler != NULL) {
      memory_handler->f_free(new_xml_parser);
    } else {
      xmlg_subsystem_free(xml_ctxt, new_xml_parser);
    }
    return FALSE;
  }
  /* xmlg_parser_malloc and friends are now available for this
     handler. */

  /* Do backend XML parser specific stuff. */

  /* setup expat memory handler structure */
  if (memory_handler != NULL) {
    new_xml_parser->expat_mem.malloc_fcn = memory_handler->f_malloc ;
    new_xml_parser->expat_mem.realloc_fcn = memory_handler->f_realloc ;
    new_xml_parser->expat_mem.free_fcn = memory_handler->f_free ;
  } else {
    new_xml_parser->expat_mem.malloc_fcn = xml_ctxt->memory_handler.f_malloc ;
    new_xml_parser->expat_mem.realloc_fcn = xml_ctxt->memory_handler.f_realloc ;
    new_xml_parser->expat_mem.free_fcn = xml_ctxt->memory_handler.f_free ;
  }

  new_xml_parser->ctxt = XML_ParserCreate_MM(NULL, &new_xml_parser->expat_mem, &sep);
  if (new_xml_parser->ctxt == NULL) {
    xmlg_parser_free(new_xml_parser, new_xml_parser);
    return FALSE;
  }

  /* Turn prefix mapping on. */
  XML_SetReturnNSTriplet(new_xml_parser->ctxt, 1);

  XML_SetUserData(new_xml_parser->ctxt, (void *)(new_xml_parser)) ;
  XML_SetElementHandler(new_xml_parser->ctxt, expat_start_element_ns_cb,
                        expat_end_element_ns_cb) ;
  XML_SetStartNamespaceDeclHandler(new_xml_parser->ctxt, expat_start_namespace_cb) ;
  XML_SetCharacterDataHandler(new_xml_parser->ctxt, expat_characters_data_cb) ;
  XML_SetXmlDeclHandler(new_xml_parser->ctxt, expat_xml_decl_cb) ;

  XML_SetStartDoctypeDeclHandler(new_xml_parser->ctxt, expat_xml_dtd_cb) ;

  if (filter_chain != NULL) {
    xmlg_p_set_parse_error_cb(new_xml_parser, filter_chain->parse_error_cb) ;
  }

  *xml_parser = new_xml_parser;

  return TRUE;
}
void _Expat_XML_SetXmlDeclHandler(struct ExpatIFace * Self, XML_Parser parser, XML_XmlDeclHandler handler)
{
	XML_SetXmlDeclHandler(parser, handler);
}
示例#12
0
void XMLParser::EnableXmlDeclHandler(bool enable)
{
	assert(m_parser != NULL);
	XML_SetXmlDeclHandler(m_parser, enable ? XmlDeclHandler : NULL);
}
示例#13
0
/*
 * Initialize an IterParser object
 *
 * The Python arguments are:
 *
 *    *fd*: A Python file object or a callable object
 *    *buffersize*: The size of the read buffer
 */
static int
IterParser_init(IterParser *self, PyObject *args, PyObject *kwds)
{
    PyObject* fd              = NULL;
    PyObject* read            = NULL;
    ssize_t   buffersize      = 1 << 14;

    static char *kwlist[] = {"fd", "buffersize", NULL};
    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:IterParser.__init__", kwlist,
                                     &fd, &buffersize)) {
        return -1;
    }

    /* Keep the buffersize within a reasonable range */
    self->buffersize = CLAMP(buffersize, (ssize_t)(1 << 10), (ssize_t)(1 << 24));
#ifdef __clang__
    /* Clang can't handle the file descriptors Python gives us,
       so in that case, we just call the object's read method. */
    read = PyObject_GetAttrString(fd, "read");
    if (read != NULL) {
        fd = read;
    }
#else
    self->file = PyObject_AsFileDescriptor(fd);
    if (self->file != -1) {
        /* This is a real C file handle or descriptor.  We therefore
           need to allocate our own read buffer, and get the real C
           object. */
        self->buffer = malloc((size_t)self->buffersize);
        if (self->buffer == NULL) {
            PyErr_SetString(PyExc_MemoryError, "Out of memory");
            goto fail;
        }
        self->fd = fd;   Py_INCREF(self->fd);
        lseek(self->file, 0, SEEK_SET);
    } else
#endif
    if (PyCallable_Check(fd)) {
        /* fd is a Python callable */
        self->fd = fd;   Py_INCREF(self->fd);
        self->read = fd; Py_INCREF(self->read);
    } else {
        PyErr_SetString(
            PyExc_TypeError,
            "Arg 1 to iterparser must be a file object or callable object");
        goto fail;
    }

    PyErr_Clear();

    self->queue_read_idx  = 0;
    self->queue_write_idx = 0;
    self->done            = 0;

    self->text = malloc((size_t)buffersize * sizeof(XML_Char));
    self->text_alloc = buffersize;
    if (self->text == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Out of memory");
        goto fail;
    }
    text_clear(self);

    self->read_args = Py_BuildValue("(n)", buffersize);
    if (self->read_args == NULL) {
        goto fail;
    }

    self->dict_singleton = PyDict_New();
    if (self->dict_singleton == NULL) {
        goto fail;
    }

    self->td_singleton = PyUnicode_FromString("TD");
    if (self->td_singleton == NULL) {
        goto fail;
    }

    if (queue_realloc(self, buffersize)) {
        goto fail;
    }

    /* Set up an expat parser with our callbacks */
    self->parser = XML_ParserCreate(NULL);
    if (self->parser == NULL) {
        PyErr_SetString(PyExc_MemoryError, "Out of memory");
        goto fail;
    }
    XML_SetUserData(self->parser, self);
    XML_SetElementHandler(
        self->parser,
        (XML_StartElementHandler)startElement,
        (XML_EndElementHandler)endElement);
    XML_SetCharacterDataHandler(
        self->parser,
        (XML_CharacterDataHandler)characterData);
    XML_SetXmlDeclHandler(
        self->parser,
        (XML_XmlDeclHandler)xmlDecl);

    Py_XDECREF(read);

    return 0;

 fail:
    Py_XDECREF(read);
    Py_XDECREF(self->fd);
    Py_XDECREF(self->read);
    free(self->text);
    Py_XDECREF(self->dict_singleton);
    Py_XDECREF(self->td_singleton);
    Py_XDECREF(self->read_args);
    free(self->queue);

    return -1;
}
示例#14
0
void bmx_expat_XML_SetXmlDeclHandler(XML_Parser parser) {
	XML_SetXmlDeclHandler(parser, bmx_expat_XmlDeclHandler);
}