static void XMLCALL endNamespaceHandler(void* userData, const char* prefix) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); assert(!stack->nodeStack.empty()); stack->nodeStack.pop(); }
static int pdb_xml_external_entity_ref_cb (XML_Parser xml_parser, const XML_Char *context, const XML_Char *base, const XML_Char *system_id, const XML_Char *public_id) { PdbXmlParser *parser = XML_GetUserData (xml_parser); PdbXmlStackEntry *stack_top; XML_Parser external_parser; GError *error = NULL; char *filename; gboolean ret; if (system_id == NULL) { g_set_error (&parser->abort_error, PDB_ERROR, PDB_ERROR_BAD_FORMAT, _("%s:%i:%i: An external entity was encountered " "without a system id"), pdb_xml_get_current_filename (parser), pdb_xml_get_current_line_number (parser), pdb_xml_get_current_column_number (parser)); return XML_STATUS_ERROR; } external_parser = XML_ExternalEntityParserCreate (xml_parser, context, NULL); pdb_xml_init_parser (parser, external_parser); if (base) filename = g_strconcat (base, "/", system_id, NULL); else filename = g_strdup (system_id); g_array_set_size (parser->stack, parser->stack->len + 1); stack_top = &g_array_index (parser->stack, PdbXmlStackEntry, parser->stack->len - 1); stack_top->parser = external_parser; stack_top->filename = NULL; ret = pdb_xml_parse (parser, filename, &error); g_free (filename); XML_ParserFree (external_parser); g_array_set_size (parser->stack, parser->stack->len - 1); if (ret) return XML_STATUS_OK; else { parser->abort_error = error; return XML_STATUS_ERROR; } }
static ERL_NIF_TERM parse(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { XML_Parser **parser; int is_final, res, errcode; ErlNifBinary stream; char *errstring; assert(argc == 3); if (!enif_get_resource(env, argv[0], PARSER_POINTER, (void **)&parser)) return enif_make_badarg(env); if (!enif_is_binary(env, argv[1])) return enif_make_badarg(env); enif_get_int(env, argv[2], &is_final); enif_inspect_binary(env, argv[1], &stream); expat_parser *parser_data = XML_GetUserData((XML_Parser)(*parser)); parser_data->result = enif_make_list(env, 0); parser_data->env = env; XML_SetUserData((XML_Parser)(*parser), parser_data); res = XML_Parse((XML_Parser)(*parser), (const char *)stream.data, stream.size, is_final); if(!res) { errcode = XML_GetErrorCode((XML_Parser)(*parser)); errstring = (char *)XML_ErrorString(errcode); return enif_make_tuple(env, 2, ERROR, enif_make_string(env, errstring, ERL_NIF_LATIN1)); } return enif_make_tuple(env, 2, OK, parser_data->result); };
int _68k_extentrefhandler(XML_Parser parser, const char *context, const char *base, const char *sysid, const char *pubid) { M68kXML_Parser p = XML_GetUserData(parser); void *arg = p->extenthandlerarg; return (int)call_68k_code(p->IExec, p->extentrefhandler, 5, arg ? arg : p, context, base, sysid, pubid); }
static void XMLCALL characterDataHandler(void* userData, const char* s, int len) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); if (!s || len <= 0) { return; } // See if we can just append the text to a previous text node. if (!stack->nodeStack.empty()) { Node* currentParent = stack->nodeStack.top(); if (!currentParent->children.empty()) { Node* lastChild = currentParent->children.back().get(); if (lastChild->type == NodeType::kText) { Text* text = static_cast<Text*>(lastChild); text->text += util::utf8ToUtf16(StringPiece(s, len)); return; } } } std::unique_ptr<Text> text = util::make_unique<Text>(); text->text = util::utf8ToUtf16(StringPiece(s, len)); addToStack(stack, parser, std::move(text)); }
END_TEST /* Regression test for SF bug #616863. */ static int XMLCALL external_entity_handler(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId) { int callno = 1 + (int)XML_GetUserData(parser); char *text; XML_Parser p2; if (callno == 1) text = ("<!ELEMENT doc (e+)>\n" "<!ATTLIST doc xmlns CDATA #IMPLIED>\n" "<!ELEMENT e EMPTY>\n"); else text = ("<?xml version='1.0' encoding='us-ascii'?>" "<e/>"); XML_SetUserData(parser, (void *) callno); p2 = XML_ExternalEntityParserCreate(parser, context, NULL); if (XML_Parse(p2, text, strlen(text), XML_TRUE) == XML_STATUS_ERROR) { xml_failure(p2); return 0; } XML_ParserFree(p2); return 1; }
void sb_parser_destroy(XML_Parser p) { void *x = XML_GetUserData(p); if (x) free(x); XML_ParserFree(p); }
void XMLCALL LoadPropertyTreeFromXml_OnEndRootElement( void *userData, const XML_Char *name) { XML_Parser parser = (XML_Parser)userData; Sys::PropertyTree* result = (Sys::PropertyTree*)XML_GetUserData(parser); std::string s = Sys::TrimSpaces(result->Text()); result->SetText(s.c_str()); }
static void XMLCALL endElementHandler(void* userData, const char* name) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); assert(!stack->nodeStack.empty()); stack->nodeStack.top()->comment = std::move(stack->pendingComment); stack->nodeStack.pop(); }
void XMLCALL LoadPropertyTreeFromXml_OnStartElement( void *userData, const XML_Char *name, const XML_Char **props) { XML_Parser parser = (XML_Parser)userData; Sys::PropertyTree* result = (Sys::PropertyTree*)XML_GetUserData(parser); result = result->CreateChild(name); CopyAllProps(result, props); XML_SetUserData(parser, result); }
void XMLCALL LoadPropertyTreeFromXml_OnCharacterData( void *userData, const XML_Char *text, int textLength) { XML_Parser parser = (XML_Parser)userData; Sys::PropertyTree* result = (Sys::PropertyTree*)XML_GetUserData(parser); std::string s = result->Text(); // (!) performance warning s.append(text, textLength); result->SetText(s.data(), (int)s.length()); }
static void XMLCALL commentDataHandler(void* userData, const char* comment) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); if (!stack->pendingComment.empty()) { stack->pendingComment += '\n'; } stack->pendingComment += util::utf8ToUtf16(comment); }
void parser_dtor(ErlNifEnv *env, void* obj) { XML_Parser* parser = (XML_Parser*) obj; if(!parser) return; expat_parser* parser_data = XML_GetUserData(*parser); enif_free(parser_data); XML_ParserFree(*parser); };
void XMLCALL LoadPropertyTreeFromXml_OnStartRootElement( void *userData, const XML_Char *name, const XML_Char **props) { XML_Parser parser = (XML_Parser)userData; Sys::PropertyTree* result = (Sys::PropertyTree*)XML_GetUserData(parser); result->SetName(name); CopyAllProps(result, props); XML_SetElementHandler(parser, LoadPropertyTreeFromXml_OnStartElement, LoadPropertyTreeFromXml_OnEndElement); }
int ParserEngine::handleExternalEntityRef(XML_Parser parser, const XML_Char* context, const XML_Char* base, const XML_Char* systemId, const XML_Char* publicId) { ParserEngine* pThis = reinterpret_cast<ParserEngine*>(XML_GetUserData(parser)); if (!context && !pThis->_externalParameterEntities) return XML_STATUS_ERROR; if (context && !pThis->_externalGeneralEntities) return XML_STATUS_ERROR; InputSource* pInputSource = 0; EntityResolver* pEntityResolver = 0; EntityResolverImpl defaultResolver; XMLString sysId(systemId); XMLString pubId; if (publicId) pubId.assign(publicId); URI uri(fromXMLString(pThis->_context.back()->getSystemId())); uri.resolve(fromXMLString(sysId)); if (pThis->_pEntityResolver) { pEntityResolver = pThis->_pEntityResolver; pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString())); } if (!pInputSource && pThis->_externalGeneralEntities) { pEntityResolver = &defaultResolver; pInputSource = pEntityResolver->resolveEntity(publicId ? &pubId : 0, toXMLString(uri.toString())); } if (pInputSource) { XML_Parser extParser = XML_ExternalEntityParserCreate(pThis->_parser, context, 0); if (!extParser) throw XMLException("Cannot create external entity parser"); try { pThis->parseExternal(extParser, pInputSource); } catch (XMLException&) { pEntityResolver->releaseInputSource(pInputSource); XML_ParserFree(extParser); throw; } pEntityResolver->releaseInputSource(pInputSource); XML_ParserFree(extParser); return XML_STATUS_OK; } else return XML_STATUS_ERROR; }
void HostileFactory::setFactoryCallbacks(XML_Parser p, XML_StartElementHandler start, XML_EndElementHandler end,RenderableAbstractFactory * renderableFactory) { currentHostile = new Hostile(); oldStart = start; oldEnd = end; oldUserData = XML_GetUserData(p); currentParer = p; g_renderableFactory = renderableFactory; //Put hostile factory handler XML_SetUserData(currentParer, currentHostile); XML_SetElementHandler(currentParer, hostile_xmlstart, hostile_xmlend); }
node_expat::node_expat(parser_expat* parser, node_expat* parent, int depth, node::_t_on_parsed_node on_parsed, const char* tag, const char**atts):node(tag, parent, depth, on_parsed),_parser(parser) { /* Implementar lectura de atts this->set_attribute(id, value); */ for (size_t i =0; atts[i]; i+=2) { this->set_attribute(atts[i], atts[i+1]); } __walker_depth = depth; _old_user_data = XML_GetUserData(_parser->get_parser()); __container = new expat_handler_container(this); XML_SetUserData(_parser->get_parser(), __container); }
bool my_parse_callback( const char *buff, void *opaque ) { struct user_data_struct *ds = (struct user_data_struct *)opaque; if ( XML_Parse( ds->parser, buff, strlen(buff), XML_FALSE ) == XML_STATUS_ERROR ) { std::cout << "Error parsing xml output:" << buff << std::endl; } else ds->parsed_xml = true; FeXMLParser *p = (FeXMLParser *)XML_GetUserData( ds->parser ); return p->get_continue_parse(); // return false to cancel callback }
void XMLCALL LoadPropertyTreeFromXml_OnEndElement( void *userData, const XML_Char *name) { XML_Parser parser = (XML_Parser)userData; Sys::PropertyTree* result = (Sys::PropertyTree*)XML_GetUserData(parser); std::string s = Sys::TrimSpaces(result->Text()); result->SetText(s.c_str()); result = result->Parent(); XML_SetUserData(parser, result); if (result == NULL) { // Expat reads first root tag only, so we do not need // protection from multiple root tags. } }
static void XMLCALL startNamespaceHandler(void* userData, const char* prefix, const char* uri) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); std::unique_ptr<Namespace> ns = util::make_unique<Namespace>(); if (prefix) { ns->namespacePrefix = util::utf8ToUtf16(prefix); } if (uri) { ns->namespaceUri = util::utf8ToUtf16(uri); } addToStack(stack, parser, std::move(ns)); }
/**************************************************************************************************************** * Entry end *****************************************************************************************************************/ static void XMLCALL _NFentryEnd (void *argPtr, const char *entry) { CMreturn ret; NFdata_t *data; NFobject_p object, parent; XML_Parser parser = (XML_Parser) argPtr; data = (NFdata_t *) XML_GetUserData (parser); if (data->Skip) { if (strcmp (data->SkipName,entry) == 0) { data->SkipLevel--; if (data->SkipLevel == 0) { data->Skip = false; free (data->SkipName); } } return; } data->Stack--; object = data->Objects [data->Stack]; parent = data->Stack > 0 ? data->Objects [data->Stack - 1] : (NFobject_p) NULL; switch (object->Type) { case NFcompAggregate: ret = NFparseCompAggregateFinalize (parent, object); break; case NFcompContainer: ret = NFparseCompContainerFinalize (parent, object); break; case NFcompInput: ret = NFparseCompInputFinalize (parent, object); break; case NFcompModel: ret = NFparseCompModelFinalize (object); break; case NFcompRegion: ret = NFparseCompRegionFinalize (parent, object); break; case NFnumInitial: ret = NFparseNumVariableFinalize (parent, object); break; case NFnumParameter: ret = NFparseNumParameterFinalize (parent, object); break; case NFnumVariable: ret = NFparseNumVariableFinalize (parent, object); break; case NFmodDerivative: ret = NFparseModDerivativeFinalize (parent, object); break; case NFmodInterface: ret = NFparseModInterfaceFinalize (parent, object); break; case NFmodEquation: ret = NFparseModEquationFinalize (parent, object); break; case NFmodProcess: ret = NFparseModProcessFinalize (parent, object); break; case NFmodRoute: ret = NFparseModRouteFinalize (parent, object); break; case NFobjAlias: ret = NFparseObjAliasFinalize (parent, object); break; case NFobjCategory: ret = NFparseObjCategoryFinalize (parent, object); break; case NFobjOutput: ret = NFparseObjOutputFinalize (parent, object); break; case NFobjPlugin: CMmsgPrint (CMmsgAppError, "This should never happen! %s:%d\n",__FILE__,__LINE__); break; } if (ret == CMfailed) goto Abort; data->Objects [data->Stack] = (NFobject_p) NULL; return; Abort: data->Abort = true; return; }
static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb, void *userp) { XML_Parser parser = (XML_Parser) userp; size_t real_size = length * nmemb; struct ParserStruct *state = (struct ParserStruct *) XML_GetUserData(parser); /* Only parse if we're not already in a failure state. */ if (state->ok && XML_Parse(parser, contents, real_size, 0) == 0) { int error_code = XML_GetErrorCode(parser); fprintf(stderr, "Parsing response buffer of length %lu failed with error code %d (%s).\n", real_size, error_code, XML_ErrorString(error_code)); state->ok = 0; } return real_size; }
static ERL_NIF_TERM free_parser(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { XML_Parser **parser; assert(argc == 1); if (!enif_get_resource(env, argv[0], PARSER_POINTER, (void **)&parser)) return enif_make_badarg(env); expat_parser *parser_data = XML_GetUserData((XML_Parser)(*parser)); enif_free(parser_data); XML_ParserFree((XML_Parser)(*parser)); return OK; };
BrokerMessage *sb_parser_process(XML_Parser p, char *buffer, int len) { BrokerParserData *bp = (BrokerParserData *) XML_GetUserData(p); bp->msg = calloc(1, sizeof(BrokerMessage)); XML_ParserReset(p, NULL); XML_SetUserData(p, bp); XML_SetElementHandler(p, sbx_start, sbx_end); XML_SetCharacterDataHandler(p, getText); if (!XML_Parse(p, buffer, len, 1)) { fprintf(stderr, "Parse error at line %d:\n%s\n", XML_GetCurrentLineNumber(p), XML_ErrorString(XML_GetErrorCode(p))); exit(-1); } return bp->msg; }
static ERL_NIF_TERM reset_parser(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { XML_Parser **parser; assert(argc == 1); if (!enif_get_resource(env, argv[0], PARSER_POINTER, (void **)&parser)) return enif_make_badarg(env); expat_parser *parser_data = XML_GetUserData((XML_Parser)(*parser)); parser_data->result = enif_make_list(env, 0); parser_data->xmlns = (ERL_NIF_TERM)NULL; parser_data->env = env; assert(XML_TRUE == XML_ParserReset((XML_Parser)(*parser), "UTF-8")); init_parser((XML_Parser)(*parser), parser_data); return OK; };
int _xml_externalEntityRefHandler(XML_ParserStruct* /* void* */ parserPtr, const XML_Char *openEntityNames, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId) { XmlParser *parser = (XmlParser*)XML_GetUserData((XML_Parser)parserPtr); int ret = 0; /* abort if no handler is set (should be configurable?) */ if (parser && parser->externalEntityRefHandler) { Array args = Array::Create(); args.append(parser); args.append(_xml_xmlchar_zval(openEntityNames, 0, parser->target_encoding)); args.append(_xml_xmlchar_zval(base, 0, parser->target_encoding)); args.append(_xml_xmlchar_zval(systemId, 0, parser->target_encoding)); args.append(_xml_xmlchar_zval(publicId, 0, parser->target_encoding)); ret = xml_call_handler(parser, parser->externalEntityRefHandler, args); } return ret; }
END_TEST static int external_entity_loader(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId) { char *text = (char *)XML_GetUserData(parser); XML_Parser extparser; extparser = XML_ExternalEntityParserCreate(parser, context, NULL); if (extparser == NULL) fail("Could not create external entity parser."); if (XML_Parse(extparser, text, strlen(text), 1) == XML_STATUS_ERROR) { xml_failure(parser); return 0; } return 1; }
static void XMLCALL startElementHandler(void* userData, const char* name, const char** attrs) { XML_Parser parser = reinterpret_cast<XML_Parser>(userData); Stack* stack = reinterpret_cast<Stack*>(XML_GetUserData(parser)); std::unique_ptr<Element> el = util::make_unique<Element>(); splitName(name, &el->namespaceUri, &el->name); while (*attrs) { Attribute attribute; splitName(*attrs++, &attribute.namespaceUri, &attribute.name); attribute.value = util::utf8ToUtf16(*attrs++); // Insert in sorted order. auto iter = std::lower_bound(el->attributes.begin(), el->attributes.end(), attribute, lessAttribute); el->attributes.insert(iter, std::move(attribute)); } el->comment = std::move(stack->pendingComment); addToStack(stack, parser, std::move(el)); }
int RS_XML(externalEntityHandler)(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId) { RS_XMLParserData *parserData = (RS_XMLParserData*)XML_GetUserData(parser); USER_OBJECT_ opArgs; int i, num; const XML_Char *xml_args[4]; num = sizeof(xml_args)/sizeof(xml_args[0]); xml_args[0] = context; xml_args[1] = base;xml_args[2] = systemId; xml_args[3] = publicId; opArgs = NEW_LIST(num); for(i =0;i < num; i++) { RECURSIVE_DATA(opArgs)[i] = NEW_CHARACTER(1); CHARACTER_DATA(RECURSIVE_DATA(opArgs)[i])[0] = ENC_COPY_TO_USER_STRING(xml_args[i] ? xml_args[i] : ""); } RS_XML(callUserFunction)("externalEntity", NULL, parserData, opArgs); return(1); /* ok to go on */ }
/**************************************************************************************************************** * Entry text *****************************************************************************************************************/ static void XMLCALL _NFentryText (void *argPtr, const char *str, int len) { char *whiteChars = " \t\n"; size_t i, start, end; bool white; NFdata_t *data; // NFobject_p object; XML_Parser parser = (XML_Parser) argPtr; data = (NFdata_t *) XML_GetUserData (parser); if (data->Skip) return; for (start = 0;start < len; start++) { for (i = 0;i < strlen (whiteChars); ++i) if (str [start] == whiteChars [i]) { white = true; break; } if (white == false) break; white = false; } if (start == len) return; for (end = len;end > 0; end--) { for (i = 0;i < strlen (whiteChars); ++i) if (str [end - 1] == whiteChars [i]) { white = true; break; } if (white == false) break; white = false; } if (end - start + 1 > data->BufferLen) { if ((data->Buffer = (char *) realloc (data->Buffer,end - start + 1)) == (char *) NULL) { CMmsgPrint (CMmsgSysError,"Memory allocation error in %s:%d\n\n",__FILE__,__LINE__); return; } data->BufferLen = end - start + 1; } strncpy (data->Buffer, str + start, end - start); data->Buffer [end - start] = '\0'; // TODO Initialize // object->Notes = CMstrAppend (object->Notes, data->Buffer, "\n"); }