Beispiel #1
0
xmlParserCtxtPtr rsvg_create_xml_parser_from_stream (xmlSAXHandlerPtr sax,
						     void            *sax_user_data,
						     GInputStream    *stream,
						     GCancellable    *cancellable,
						     GError          **error)
{
    RsvgXmlInputStreamContext *context;
    xmlParserCtxtPtr parser;

    g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
    g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
    g_return_val_if_fail (error != NULL, NULL);

    context = g_slice_new (RsvgXmlInputStreamContext);
    context->stream = g_object_ref (stream);
    context->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
    context->error = error;

    parser = xmlCreateIOParserCtxt (sax,
                                    sax_user_data,
                                    context_read,
                                    context_close,
                                    context,
                                    XML_CHAR_ENCODING_NONE);

    if (!parser) {
        g_set_error (error, rsvg_error_quark (), 0, _("Error creating XML parser"));

        /* on error, xmlCreateIOParserCtxt() frees our context via the context_close function */
    }

    return parser;
}
static int
rxml_sax_parser_parse_io(VALUE self, VALUE input) {
  VALUE io = rb_ivar_get(input, IO_ATTR);
  VALUE encoding = rb_ivar_get(input, ENCODING_ATTR);
  xmlCharEncoding xmlEncoding = NUM2INT(encoding);
  xmlParserCtxtPtr ctxt = xmlCreateIOParserCtxt((xmlSAXHandlerPtr)&rxml_sax_hander_struct, (void *)self,
	 			                                        (xmlInputReadCallback) rxml_read_callback,
				                                         NULL, (void *)io, xmlEncoding);
  return xmlParseDocument(ctxt);
}
/*
 * call-seq:
 *  parse_io(io, encoding)
 *
 * Parse +io+ object with +encoding+
 */
static VALUE parse_io(VALUE klass, VALUE io, VALUE encoding)
{
  xmlCharEncoding enc = (xmlCharEncoding)NUM2INT(encoding); 

  xmlParserCtxtPtr ctxt = xmlCreateIOParserCtxt(
      NULL,
      NULL,
      (xmlInputReadCallback)io_read_callback,
      (xmlInputCloseCallback)io_close_callback,
      (void *)io,
      enc
  );

  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}
Beispiel #4
0
HarvestResult processHarvestFromFd(int fd, HarvestProcessor* harvestProcessor,
                                   bool shouldProcessData) {
    MwsHarvest_SaxUserData user_data;
    xmlSAXHandler saxHandler;
    xmlParserCtxtPtr ctxtPtr;
    HarvestResult result;
    result.status = -1;

    user_data.harvestProcessor = harvestProcessor;
    user_data.shouldProcessData = shouldProcessData;
    memset(&saxHandler, 0, sizeof(xmlSAXHandler));

    // Registering Sax callbacks
    saxHandler.endDocument = my_endDocument;
    saxHandler.startElement = my_startElement;
    saxHandler.endElement = my_endElement;
    saxHandler.characters = my_characters;
    saxHandler.warning = my_warning;
    saxHandler.error = my_error;
    saxHandler.fatalError = my_fatalError;

    // Locking libXML -- to allow multi-threaded use
    xmlLockLibrary();
    // Creating the IOParser context
    if ((ctxtPtr = xmlCreateIOParserCtxt(&saxHandler, &user_data,
                                         fdXmlInputReadCallback, nullptr, &fd,
                                         XML_CHAR_ENCODING_UTF8)) ==
        nullptr) {
        PRINT_WARN("Error while creating the ParserContext\n");
    }
        // Parsing the document
        else if ((result.status = xmlParseDocument(ctxtPtr)) == -1) {
        PRINT_WARN("Parsing XML document failed\n");
    }

    // Freeing the parser context
    if (ctxtPtr) {
        xmlFreeParserCtxt(ctxtPtr);
    }

    // Unlocking libXML -- to allow multi-threaded use
    xmlUnlockLibrary();

    result.numExpressions = user_data.parsedExpr;

    return result;
}
Beispiel #5
0
TEG_STATUS metaserver_get_servers( void )
{
	int s = -1;
	int ret;
	xmlParserCtxtPtr ctxt;
	xmlDocPtr doc = NULL;

	metaserver_flush_list();

	s = net_connect_tcp("teg.game-server.cc",2002);
	if( s < 0 )
		goto error;

	if( net_printf( s, "GET /listServers_xml HTTP/1.1\r\n\r\n") < 0 )
		goto error;


	ctxt = xmlCreateIOParserCtxt(NULL, NULL,
		    (xmlInputReadCallback) net_read,
		    (xmlInputCloseCallback) net_close,
		    (void *)s, XML_CHAR_ENCODING_NONE);
	xmlParseDocument(ctxt);

	ret = ctxt->wellFormed;
	doc = ctxt->myDoc;
	xmlFreeParserCtxt(ctxt);

	if (!ret)
		goto error;

	if( metaserver_parse_xml( doc ) != TEG_STATUS_SUCCESS )
		goto error;

	return TEG_STATUS_SUCCESS;

error:
	if( doc )
		xmlFreeDoc(doc);

	if( s >= 0 )
		net_close( s );

	return TEG_STATUS_ERROR;
}
Beispiel #6
0
int main() {
    int fd;
    xmlParserCtxtPtr ctxt;
    xmlSAXHandler sax = { 0 };

    // Affectation des fonctions de rappels
    sax.startElement = debut_element;
    // Ouverture du flux
    if ((fd = open("catalogue.xml", O_RDONLY)) == -1) {
        fprintf(stderr, "Echec sur open\n");
        return EXIT_FAILURE;
    }
    // Création du contexte
    if ((ctxt = xmlCreateIOParserCtxt(&sax, NULL, sax_read, sax_close, &fd, XML_CHAR_ENCODING_NONE)) == NULL) {
        fprintf(stderr, "Erreur lors de la création du contexte\n");
        return EXIT_FAILURE;
    }
    // Parsing du document
    xmlParseDocument(ctxt);
    // Libération de la mémoire
    xmlFreeParserCtxt(ctxt);

    return EXIT_SUCCESS;
}
Beispiel #7
0
MwsQuery* readMwsQueryFromFd(int fd)
{
    MwsQuery_SaxUserData user_data;
    xmlSAXHandler        saxHandler;
    xmlParserCtxtPtr     ctxtPtr;
    int                  ret;

    // Initializing the SAX Handler
    memset(&saxHandler, 0, sizeof(xmlSAXHandler));

    // Registering Sax callbacks with defined ones

    //internalSubsetSAXFunc        internalSubset;
    //isStandaloneSAXFunc          isStandalone;
    //hasInternalSubsetSAXFunc     hasInternalSubset;
    //hasExternalSubsetSAXFunc     hasExternalSubset;
    //resolveEntitySAXFunc         resolveEntity;
    saxHandler.getEntity     = my_getEntity;               // STUB
    //entityDeclSAXFunc            entityDecl;
    //notationDeclSAXFunc          notationDecl;
    //attributeDeclSAXFunc         attributeDecl;
    //elementDeclSAXFunc           elementDecl;
    //unparsedEntityDeclSAXFunc    unparsedEntityDecl;
    //setDocumentLocatorSAXFunc    setDocumentLocator;
    saxHandler.startDocument = my_startDocument;
    saxHandler.endDocument   = my_endDocument;
    saxHandler.startElement  = my_startElement;
    saxHandler.endElement    = my_endElement;
    //referenceSAXFunc             reference;
    saxHandler.characters    = my_characters;
    //ignorableWhitespaceSAXFunc   ignorableWhitespace;
    //processingInstructionSAXFunc processingInstruction;
    //commentSAXFunc               comment;
    saxHandler.warning       = my_warning;
    saxHandler.error         = my_error;
    saxHandler.fatalError    = my_fatalError;

    // Locking libXML -- to allow multi-threaded use
    xmlLockLibrary();
    
    // Creating the IOParser context
    ctxtPtr = xmlCreateIOParserCtxt(&saxHandler,
                                    &user_data,
                                    fdXmlInputReadCallback,
                                    NULL,
                                    &fd,
                                    XML_CHAR_ENCODING_UTF8);
    if (ctxtPtr == NULL)
    {
        fprintf(stderr, "Error while creating the ParserContext\n");
        xmlUnlockLibrary();
        return NULL;
    }
  
    ret = xmlParseDocument(ctxtPtr);
    if (ret == -1)
    {
        fprintf(stderr, "Parsing failed\n");
    }

    if (!ctxtPtr->wellFormed)
    {
        fprintf(stderr, "Bad XML document\n");
    }

    // Freeing the parser context
    xmlFreeParserCtxt(ctxtPtr);

    // Unlocking libXML -- to allow multi-threaded use
    xmlUnlockLibrary();

    return user_data.result;
}
string
verifyMwsMessageTypeFromFd(int fd)
{
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_IN;
#endif

    MwsMessage_SaxUserData user_data;
    xmlSAXHandler          saxHandler;
    xmlParserCtxtPtr       ctxtPtr;
    int                    ret;

    // Initializing the SAX Handler
    memset(&saxHandler, 0, sizeof(xmlSAXHandler));

    // Registering Sax callbacks with defined ones

    //internalSubsetSAXFunc        internalSubset;
    //isStandaloneSAXFunc          isStandalone;
    //hasInternalSubsetSAXFunc     hasInternalSubset;
    //hasExternalSubsetSAXFunc     hasExternalSubset;
    //resolveEntitySAXFunc         resolveEntity;
    //saxHandler.getEntity     = my_getEntity;               // STUB
    //entityDeclSAXFunc            entityDecl;
    //notationDeclSAXFunc          notationDecl;
    //attributeDeclSAXFunc         attributeDecl;
    //elementDeclSAXFunc           elementDecl;
    //unparsedEntityDeclSAXFunc    unparsedEntityDecl;
    //setDocumentLocatorSAXFunc    setDocumentLocator;
    saxHandler.startDocument = my_startDocument;
    //saxHandler.endDocument   = my_endDocument;
    saxHandler.startElement  = my_startElement;
    //saxHandler.endElement    = my_endElement;
    //referenceSAXFunc             reference;
    //saxHandler.characters    = my_characters;
    //ignorableWhitespaceSAXFunc   ignorableWhitespace;
    //processingInstructionSAXFunc processingInstruction;
    //commentSAXFunc               comment;
    //saxHandler.warning       = my_warning;
    //saxHandler.error         = my_error;
    //saxHandler.fatalError    = my_fatalError;

    // Locking libXML -- to allow multi-threaded use
    xmlLockLibrary();
    
    // Creating the IOParser context
    if ((ctxtPtr = xmlCreateIOParserCtxt(&saxHandler,
                                         &user_data,
                                         fdXmlInputReadCallback,
                                         NULL,
                                         &fd,
                                         XML_CHAR_ENCODING_UTF8))
            == NULL)
    {
        fprintf(stderr, "Error while creating the ParserContext\n");
    }
    // Parsing the document
    else if ((ret = xmlParseDocument(ctxtPtr))
            == -1)
    {
        fprintf(stderr, "Parsing XML document failed\n");
    }

    // Freeing the parser context
    if (ctxtPtr)
        xmlFreeParserCtxt(ctxtPtr);

    // Unlocking libXML -- to allow multi-threaded use
    xmlUnlockLibrary();

    // Checking for found messages
    if (!user_data.messageTypeFound)
    {
      fprintf(stderr, "The message is not a known MWS message type\n");
    }

#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_OUT;
#endif

    return user_data.messageType;
}