2 Copyright (C) 2012 Modelon AB
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the BSD style license.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 FMILIB_License.txt file for more details.
12 You should have received a copy of the FMILIB_License.txt file
13 along with this program. If not, contact Modelon AB <http://www.modelon.com>.
19 /* For checking variable naming conventions */
20 #include <fmi1_xml_variable_name_parser.tab.h>
21 #define YYSTYPE YYFMI1STYPE
22 #include <fmi1_xml_variable_name_lex.h>
24 #include "fmi1_xml_model_description_impl.h"
25 #include "fmi1_xml_parser.h"
27 static const char * module = "FMI1XML";
29 #define ATTR_STR(attr) #attr
30 const char *fmi1_xmlAttrNames[] = {
31 FMI1_XML_ATTRLIST(ATTR_STR)
34 /* fmi1_xml_scheme_ defines give parent ID, the index in a sequence among siblings, flag if multiple elems are allowed */
35 #define fmi1_xml_scheme_fmiModelDescription {fmi1_xml_elmID_none, 0, 0}
36 #define fmi1_xml_scheme_UnitDefinitions {fmi1_xml_elmID_fmiModelDescription, 0, 0}
37 #define fmi1_xml_scheme_BaseUnit {fmi1_xml_elmID_UnitDefinitions, 0, 1}
38 #define fmi1_xml_scheme_DisplayUnitDefinition {fmi1_xml_elmID_BaseUnit, 0, 1}
39 #define fmi1_xml_scheme_TypeDefinitions {fmi1_xml_elmID_fmiModelDescription, 1, 0}
40 #define fmi1_xml_scheme_Type {fmi1_xml_elmID_TypeDefinitions, 0, 1}
41 #define fmi1_xml_scheme_RealType {fmi1_xml_elmID_Type, 0, 0}
42 #define fmi1_xml_scheme_IntegerType {fmi1_xml_elmID_Type, 0, 0}
43 #define fmi1_xml_scheme_BooleanType {fmi1_xml_elmID_Type, 0, 0}
44 #define fmi1_xml_scheme_StringType {fmi1_xml_elmID_Type, 0, 0}
45 #define fmi1_xml_scheme_EnumerationType {fmi1_xml_elmID_Type, 0, 0}
46 #define fmi1_xml_scheme_Item {fmi1_xml_elmID_EnumerationType, 0, 1}
47 #define fmi1_xml_scheme_DefaultExperiment {fmi1_xml_elmID_fmiModelDescription, 2, 0}
48 #define fmi1_xml_scheme_VendorAnnotations {fmi1_xml_elmID_fmiModelDescription, 3, 0}
49 #define fmi1_xml_scheme_Tool {fmi1_xml_elmID_VendorAnnotations, 0, 1}
50 #define fmi1_xml_scheme_Annotation {fmi1_xml_elmID_Tool, 0, 1}
51 #define fmi1_xml_scheme_ModelVariables {fmi1_xml_elmID_fmiModelDescription, 4, 0}
52 #define fmi1_xml_scheme_ScalarVariable {fmi1_xml_elmID_ModelVariables, 0, 1}
53 #define fmi1_xml_scheme_DirectDependency {fmi1_xml_elmID_ScalarVariable, 1, 0}
54 #define fmi1_xml_scheme_Name {fmi1_xml_elmID_DirectDependency, 0, 1}
55 #define fmi1_xml_scheme_Real {fmi1_xml_elmID_ScalarVariable, 0, 0}
56 #define fmi1_xml_scheme_Integer {fmi1_xml_elmID_ScalarVariable, 0, 0}
57 #define fmi1_xml_scheme_Boolean {fmi1_xml_elmID_ScalarVariable, 0, 0}
58 #define fmi1_xml_scheme_String {fmi1_xml_elmID_ScalarVariable, 0, 0}
59 #define fmi1_xml_scheme_Enumeration {fmi1_xml_elmID_ScalarVariable, 0, 0}
60 #define fmi1_xml_scheme_Implementation {fmi1_xml_elmID_fmiModelDescription, 5, 0}
61 #define fmi1_xml_scheme_CoSimulation_StandAlone {fmi1_xml_elmID_Implementation, 0, 0}
62 /* NOTE: Capabilities need special handling since it can appear both under
63 CoSimulation_StandAlone and CoSimulation_Tool
65 #define fmi1_xml_scheme_Capabilities {fmi1_xml_elmID_CoSimulation_StandAlone, 0, 0}
66 #define fmi1_xml_scheme_CoSimulation_Tool {fmi1_xml_elmID_Implementation, 0, 0}
67 #define fmi1_xml_scheme_Model {fmi1_xml_elmID_CoSimulation_Tool, 1, 0}
68 #define fmi1_xml_scheme_File {fmi1_xml_elmID_Model, 0, 1}
70 #define EXPAND_ELM_SCHEME(elm) fmi1_xml_scheme_##elm ,
72 fmi1_xml_scheme_info_t fmi1_xml_scheme_info[fmi1_xml_elm_number] = {
73 FMI1_XML_ELMLIST(EXPAND_ELM_SCHEME)
76 #define EXPAND_ELM_NAME(elm) { #elm, fmi1_xml_handle_##elm, fmi1_xml_elmID_##elm},
78 fmi1_xml_element_handle_map_t fmi1_element_handle_map[fmi1_xml_elm_number] = {
79 FMI1_XML_ELMLIST(EXPAND_ELM_NAME)
82 void fmi1_xml_parse_free_context(fmi1_xml_parser_context_t *context) {
84 if(context->modelDescription)
85 fmi1_xml_clear_model_description(context->modelDescription);
87 XML_ParserFree(context->parser);
90 fmi1_xml_free_parse_buffer(context);
91 if(context->attrMap) {
92 jm_vector_free(jm_named_ptr)(context->attrMap);
96 jm_vector_free(fmi1_xml_element_handle_map_t)(context->elmMap);
99 if(context->attrBuffer) {
100 jm_vector_free(jm_string)(context->attrBuffer);
101 context->attrBuffer = 0;
103 jm_stack_free_data(int)(& context->elmStack );
104 jm_vector_free_data(char)( &context->elmData );
106 jm_vector_free_data(jm_voidp)(&context->directDependencyBuf);
107 jm_vector_foreach(jm_string)(&context->directDependencyStringsStore, (void(*)(jm_string))context->callbacks->free);
108 jm_vector_free_data(jm_string)(&context->directDependencyStringsStore);
110 context->callbacks->free(context);
113 void fmi1_xml_parse_fatal(fmi1_xml_parser_context_t *context, const char* fmt, ...) {
115 va_start (args, fmt);
116 jm_log_fatal_v(context->callbacks, module, fmt, args);
118 XML_StopParser(context->parser,0);
121 void fmi1_xml_parse_error(fmi1_xml_parser_context_t *context, const char* fmt, ...) {
123 va_start (args, fmt);
124 if(context->parser) {
125 jm_log_error(context->callbacks, module, "Detected on line:%u of modelDescription.xml", XML_GetCurrentLineNumber(context->parser));
126 jm_log_error_v(context->callbacks, module, fmt, args);
129 jm_log_error_v(context->callbacks, module, fmt, args);
134 int fmi1_xml_is_attr_defined(fmi1_xml_parser_context_t *context, fmi1_xml_attr_enu_t attrID) {
135 return ( jm_vector_get_item(jm_string)(context->attrBuffer, attrID) != 0);
138 static int fmi1_get_attr_str(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required,const char** valp) {
140 jm_string elmName, attrName, value;
142 elmName = fmi1_element_handle_map[elmID].elementName;
143 attrName = fmi1_xmlAttrNames[attrID];
144 value = jm_vector_get_item(jm_string)(context->attrBuffer, attrID);
146 jm_vector_set_item(jm_string)(context->attrBuffer, attrID, 0);
149 fmi1_xml_parse_fatal(context, "Parsing XML element '%s': required attribute '%s' not found", elmName, attrName);
158 int fmi1_xml_set_attr_string(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, jm_vector(char)* field) {
160 jm_string elmName, attrName, val;
162 ret = fmi1_get_attr_str(context, elmID, attrID,required,&val);
164 if((!val || !val[0]) && !required) {
165 jm_vector_resize(char)(field, 1);
166 jm_vector_set_item(char)(field, 0, 0);
167 jm_vector_resize(char)(field, 0);
170 elmName = fmi1_element_handle_map[elmID].elementName;
171 attrName = fmi1_xmlAttrNames[attrID];
173 len = strlen(val) + 1;
174 if(jm_vector_resize(char)(field, len) < len) {
175 fmi1_xml_parse_fatal(context, "XML element '%s': could not allocate memory for setting '%s'='%s'", elmName, attrName, val);
178 /* copy terminating 0 as well but set vector size to be actual string length */
179 memcpy(jm_vector_get_itemp(char)(field,0), val, len);
180 jm_vector_resize(char)(field, len - 1);
184 int fmi1_xml_set_attr_uint(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, unsigned int* field, unsigned int defaultVal) {
186 jm_string elmName, attrName, strVal;
188 ret = fmi1_get_attr_str(context, elmID, attrID,required,&strVal);
190 if(!strVal && !required) {
195 elmName = fmi1_element_handle_map[elmID].elementName;
196 attrName = fmi1_xmlAttrNames[attrID];
198 if(sscanf(strVal, "%u", field) != 1) {
199 fmi1_xml_parse_error(context, "XML element '%s': could not parse value for attribute '%s'='%s'", elmName, attrName, strVal);
206 int fmi1_xml_set_attr_enum(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, unsigned int* field, unsigned int defaultVal, jm_name_ID_map_t* nameMap) {
208 jm_string elmName, attrName, strVal;
210 ret = fmi1_get_attr_str(context, elmID, attrID,required,&strVal);
212 if(!strVal && !required) {
217 elmName = fmi1_element_handle_map[elmID].elementName;
218 attrName = fmi1_xmlAttrNames[attrID];
221 while(nameMap[i].name && strcmp(nameMap[i].name, strVal)) i++;
222 if(!nameMap[i].name) {
223 fmi1_xml_parse_fatal(context, "XML element '%s': could not parse value for attribute '%s'='%s'", elmName, attrName, strVal);
226 *field = nameMap[i].ID;
230 int fmi1_xml_set_attr_boolean(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, int* field, int defaultVal) {
231 jm_name_ID_map_t fmi_boolean_i_dMap[] = {{"true", 1},{"false", 0}, {"1", 1},{"0", 0}, {0,0}};
232 return fmi1_xml_set_attr_enum(context,elmID, attrID,required, (unsigned*)field, (unsigned)defaultVal, fmi_boolean_i_dMap);
235 int fmi1_xml_set_attr_int(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, int* field, int defaultVal) {
237 jm_string elmName, attrName, strVal;
239 ret = fmi1_get_attr_str(context, elmID, attrID,required,&strVal);
241 if(!strVal && !required) {
246 elmName = fmi1_element_handle_map[elmID].elementName;
247 attrName = fmi1_xmlAttrNames[attrID];
249 if(sscanf(strVal, "%d", field) != 1) {
250 fmi1_xml_parse_error(context, "XML element '%s': could not parse value for attribute '%s'='%s'", elmName, attrName, strVal);
256 int fmi1_xml_set_attr_double(fmi1_xml_parser_context_t *context, fmi1_xml_elm_enu_t elmID, fmi1_xml_attr_enu_t attrID, int required, double* field, double defaultVal) {
259 jm_string elmName, attrName, strVal;
262 ret = fmi1_get_attr_str(context, elmID, attrID,required,&strVal);
264 if(!strVal && !required) {
269 elmName = fmi1_element_handle_map[elmID].elementName;
270 attrName = fmi1_xmlAttrNames[attrID];
272 if(sscanf(strVal, "%lf", field) != 1) {
273 fmi1_xml_parse_fatal(context, "XML element '%s': could not parse value for attribute '%s'='%s'", elmName, attrName, strVal);
279 int fmi1_xml_alloc_parse_buffer(fmi1_xml_parser_context_t *context, size_t items) {
281 jm_vector(jm_voidp)* parseBuffer = &context->parseBuffer;
283 if(jm_vector_init(jm_voidp)(parseBuffer,items,context->callbacks) < items) {
284 fmi1_xml_parse_fatal(context, "Could not allocate buffer for parsing XML");
287 jm_vector_zero(jm_voidp)(parseBuffer);
291 void fmi1_xml_free_parse_buffer(fmi1_xml_parser_context_t *context) {
293 jm_vector(jm_voidp)* parseBuffer = &context->parseBuffer;
295 for(i=0; i < jm_vector_get_size(jm_voidp)(parseBuffer); i++) {
296 jm_vector(char) * item = jm_vector_get_item(jm_voidp)(parseBuffer,i);
297 if(item) jm_vector_free(char)(item);
299 jm_vector_free_data(jm_voidp)(parseBuffer);
302 jm_vector(char) * fmi1_xml_reserve_parse_buffer(fmi1_xml_parser_context_t *context, size_t index, size_t size) {
304 jm_vector(jm_voidp)* parseBuffer = &context->parseBuffer;
305 jm_vector(char) * item = jm_vector_get_item(jm_voidp)(parseBuffer,index);
307 item = jm_vector_alloc(char)(size,size,context->callbacks);
308 jm_vector_set_item(jm_voidp)(parseBuffer,index,item);
310 fmi1_xml_parse_fatal(context, "Could not allocate a buffer for parsing XML");
315 if(jm_vector_resize(char)(item, size) < size ) {
316 fmi1_xml_parse_fatal(context, "Could not allocate a buffer for parsing XML");
323 jm_vector(char) * fmi1_xml_get_parse_buffer(fmi1_xml_parser_context_t *context, size_t index) {
324 jm_vector(jm_voidp)* parseBuffer = &context->parseBuffer;
325 return jm_vector_get_item(jm_voidp)(parseBuffer,index);
330 int fmi1_create_attr_map(fmi1_xml_parser_context_t* context) {
332 context->attrBuffer = jm_vector_alloc(jm_string)(fmi1_xml_attr_number, fmi1_xml_attr_number, context->callbacks);
333 if(!context->attrBuffer) return -1;
334 context->attrMap = jm_vector_alloc(jm_named_ptr)(fmi1_xml_attr_number, fmi1_xml_attr_number, context->callbacks);
335 if(!context->attrMap) return -1;
336 for(i = 0; i < fmi1_xml_attr_number; i++) {
338 jm_vector_set_item(jm_string)(context->attrBuffer, i, 0);
339 map.name = fmi1_xmlAttrNames[i];
340 map.ptr = (void*)(jm_vector_get_itemp(jm_string)(context->attrBuffer, i));
341 jm_vector_set_item(jm_named_ptr)(context->attrMap, i, map);
343 jm_vector_qsort(jm_named_ptr)(context->attrMap, jm_compare_named);
347 int fmi1_create_elm_map(fmi1_xml_parser_context_t* context) {
349 context->elmMap = jm_vector_alloc(fmi1_xml_element_handle_map_t)(fmi1_xml_elm_number, fmi1_xml_elm_number, context->callbacks);
350 if(!context->elmMap) return -1;
351 for(i = 0; i < fmi1_xml_elm_number; i++) {
352 fmi1_xml_element_handle_map_t item = fmi1_element_handle_map[i];
353 jm_vector_set_item(fmi1_xml_element_handle_map_t)(context->elmMap, i, item);
355 jm_vector_qsort(fmi1_xml_element_handle_map_t)(context->elmMap, fmi1_xml_compare_elmName);
359 static void XMLCALL fmi1_parse_element_start(void *c, const char *elm, const char **attr) {
361 fmi1_xml_element_handle_map_t keyEl;
362 fmi1_xml_element_handle_map_t* currentElMap;
363 jm_named_ptr* currentMap;
364 fmi1_xml_elm_enu_t currentID;
366 fmi1_xml_parser_context_t *context = c;
368 if(context->skipElementCnt) {
369 context->skipElementCnt++;
370 jm_log_warning(context->callbacks, module, "[Line:%u] Skipping nested XML element '%s'",
371 XML_GetCurrentLineNumber(context->parser), elm);
375 keyEl.elementName = elm;
376 /* find the element handle by name */
377 currentElMap = jm_vector_bsearch(fmi1_xml_element_handle_map_t)(context->elmMap, &keyEl, fmi1_xml_compare_elmName);
380 jm_log_error(context->callbacks, module, "[Line:%u] Unknown element '%s' in XML, skipping",
381 XML_GetCurrentLineNumber(context->parser), elm);
382 context->skipElementCnt = 1;
386 currentID = currentElMap->elemID;
387 /* Check that parent-child & siblings are fine */
389 fmi1_xml_elm_enu_t parentID = context->currentElmID;
390 fmi1_xml_elm_enu_t siblingID = context->lastElmID;
392 if((fmi1_xml_scheme_info[currentID].parentID != parentID) &&
393 ((currentID != fmi1_xml_elmID_Capabilities) || (parentID != fmi1_xml_elmID_CoSimulation_Tool))) {
394 jm_log_error(context->callbacks, module,
395 "[Line:%u] XML element '%s' cannot be placed inside '%s', skipping",
396 XML_GetCurrentLineNumber(context->parser), elm, fmi1_element_handle_map[parentID].elementName);
397 context->skipElementCnt = 1;
400 if(siblingID != fmi1_xml_elmID_none) {
401 if(siblingID == currentID) {
402 if(!fmi1_xml_scheme_info[currentID].multipleAllowed) {
403 jm_log_error(context->callbacks, module,
404 "[Line:%u] Multiple instances of XML element '%s' are not allowed, skipping",
405 XML_GetCurrentLineNumber(context->parser), elm);
406 context->skipElementCnt = 1;
411 int lastSiblingIndex = fmi1_xml_scheme_info[siblingID].siblingIndex;
412 int curSiblingIndex = fmi1_xml_scheme_info[currentID].siblingIndex;
414 if(lastSiblingIndex >= curSiblingIndex) {
415 jm_log_error(context->callbacks, module,
416 "[Line:%u] XML element '%s' cannot be placed after element '%s', skipping",
417 XML_GetCurrentLineNumber(context->parser), elm, fmi1_element_handle_map[siblingID].elementName);
418 context->skipElementCnt = 1;
423 context->lastElmID = fmi1_xml_elmID_none;
426 /* process the attributes */
430 /* find attribute by name */
431 currentMap = jm_vector_bsearch(jm_named_ptr)(context->attrMap, &key, jm_compare_named);
434 jm_log_error(context->callbacks, module, "Unknown attribute '%s' in XML", attr[i]);
437 /* save attr value (still as string) for further handling */
438 const char** mapItem = (const char**)currentMap->ptr;
439 *mapItem = attr[i+1];
444 /* handle the element */
445 if( currentElMap->elementHandle(context, 0) ) {
448 if(context->skipElementCnt) return;
449 /* check that the element handle had process all the attributes */
450 for(i = 0; i < fmi1_xml_attr_number; i++) {
451 if(jm_vector_get_item(jm_string)(context->attrBuffer, i)) {
452 if(!context->skipOneVariableFlag)
453 jm_log_warning(context->callbacks,module, "Attribute '%s' not processed by element '%s' handle", fmi1_xmlAttrNames[i], elm);
454 jm_vector_set_item(jm_string)(context->attrBuffer, i,0);
457 if(context -> currentElmID != fmi1_xml_elmID_none) { /* with nested elements: put the parent on the stack*/
458 jm_stack_push(int)(&context->elmStack, context -> currentElmID);
460 context -> currentElmID = currentID;
463 static void XMLCALL fmi1_parse_element_end(void* c, const char *elm) {
465 fmi1_xml_element_handle_map_t keyEl;
466 fmi1_xml_element_handle_map_t* currentElMap;
467 fmi1_xml_elm_enu_t currentID;
468 fmi1_xml_parser_context_t *context = c;
470 if(context->skipElementCnt) {
471 context->skipElementCnt--;
475 keyEl.elementName = elm;
476 currentElMap = jm_vector_bsearch(fmi1_xml_element_handle_map_t)(context->elmMap, &keyEl, fmi1_xml_compare_elmName);
479 fmi1_xml_parse_fatal(context, "Unknown element end in XML (element: %s)", elm);
482 currentID = currentElMap->elemID;
484 if(currentID != context -> currentElmID) {
486 fmi1_xml_parse_fatal(context, "Element end '%s' does not match element start '%s' in XML", elm,
487 fmi1_element_handle_map[context -> currentElmID].elementName);
491 jm_vector_push_back(char)(&context->elmData, 0);
493 if( currentElMap->elementHandle(context, jm_vector_get_itemp(char)(&context->elmData, 0) )) {
496 jm_vector_resize(char)(&context->elmData, 0);
498 /* record the last handle and pop the stack */
499 context->lastElmID = currentID;
501 if(jm_stack_is_empty(int)(&context->elmStack)) {
502 context -> currentElmID = fmi1_xml_elmID_none;
505 context -> currentElmID = (fmi1_xml_elm_enu_t)jm_stack_pop(int)(&context->elmStack);
510 * Called to handle element data, e.g. "xy" in <Name>xy</Name>
511 * Can be called many times, e.g. with "x" and then with "y" in the example above.
513 * For some reason, if the element data is the empty string (Eg. <a></a>)
514 * instead of an empty string with len == 0 we get "\n". The workaround is
515 * to replace this with the empty string whenever we encounter "\n".
517 static void XMLCALL fmi1_parse_element_data(void* c, const XML_Char *s, int len) {
518 fmi1_xml_parser_context_t *context = c;
520 jm_vector_reserve(char)(&context->elmData, len + jm_vector_get_size(char)(&context->elmData) + 1);
521 for(i = 0; i< len;i++) {
524 jm_vector_push_back(char)(&context->elmData, ch);
529 static int is_cs_fmu(fmi1_xml_model_description_t *md)
531 return md->fmuKind == fmi1_fmu_kind_enu_cs_tool ||
532 md->fmuKind == fmi1_fmu_kind_enu_cs_standalone;
535 static void fmi1_check_variable_naming_conventions(fmi1_xml_model_description_t *md) {
536 size_t n = jm_vector_get_size(jm_named_ptr)(&md->variablesByName);
541 /* check for duplicate variable names */
542 for (k = 1; k < n; k++) {
543 const char *v1 = jm_vector_get_item(jm_named_ptr)(&md->variablesByName, k - 1).name;
544 const char *v2 = jm_vector_get_item(jm_named_ptr)(&md->variablesByName, k).name;
545 if(strcmp(v1, v2) == 0) {
546 jm_log_error(md->callbacks, module,
547 "Two variables with the same name %s found. This is not allowed.",
552 /* check variable name syntax */
553 if (md->namingConvension == fmi1_naming_enu_structured) {
554 yyfmi1lex_init(&scanner);
555 for (k = 0; k < n; k++) {
556 char *name = ((fmi1_xml_variable_t *) jm_vector_get_item(jm_voidp)(
557 md->variablesOrigOrder, k))->name;
558 buf = yyfmi1_scan_string(name, scanner);
559 yyfmi1parse(scanner, md->callbacks, name);
560 yyfmi1_delete_buffer(buf, scanner);
562 yyfmi1lex_destroy(scanner);
566 int fmi1_xml_parse_model_description(fmi1_xml_model_description_t* md, const char* filename, int configuration) {
567 XML_Memory_Handling_Suite memsuite;
568 fmi1_xml_parser_context_t* context;
569 XML_Parser parser = NULL;
572 context = (fmi1_xml_parser_context_t*)md->callbacks->calloc(1, sizeof(fmi1_xml_parser_context_t));
574 jm_log_fatal(md->callbacks, module,
575 "Could not allocate memory for XML parser context");
577 context->callbacks = md->callbacks;
578 context->modelDescription = md;
579 if(fmi1_xml_alloc_parse_buffer(context, 16)) return -1;
580 if(fmi1_create_attr_map(context) || fmi1_create_elm_map(context)) {
581 fmi1_xml_parse_fatal(context, "Error in parsing initialization");
582 fmi1_xml_parse_free_context(context);
585 context->lastBaseUnit = 0;
586 jm_vector_init(jm_voidp)(&context->directDependencyBuf, 0, context->callbacks);
587 jm_vector_init(jm_string)(&context->directDependencyStringsStore, 0, context->callbacks);
588 context->skipOneVariableFlag = 0;
589 context->skipElementCnt = 0;
590 jm_stack_init(int)(&context->elmStack, context->callbacks);
591 jm_vector_init(char)(&context->elmData, 0, context->callbacks);
592 context->lastElmID = fmi1_xml_elmID_none;
593 context->currentElmID = fmi1_xml_elmID_none;
595 memsuite.malloc_fcn = context->callbacks->malloc;
596 memsuite.realloc_fcn = context->callbacks->realloc;
597 memsuite.free_fcn = context->callbacks->free;
598 context -> parser = parser = XML_ParserCreate_MM(0, &memsuite, 0);
601 fmi1_xml_parse_fatal(context, "Could not initialize XML parsing library.");
602 fmi1_xml_parse_free_context(context);
606 XML_SetUserData( parser, context);
608 XML_SetElementHandler(parser, fmi1_parse_element_start, fmi1_parse_element_end);
610 XML_SetCharacterDataHandler(parser, fmi1_parse_element_data);
612 file = fopen(filename, "rb");
614 fmi1_xml_parse_fatal(context, "Cannot open file '%s' for parsing", filename);
615 fmi1_xml_parse_free_context(context);
619 while (!feof(file)) {
620 char * text = jm_vector_get_itemp(char)(fmi1_xml_reserve_parse_buffer(context,0,XML_BLOCK_SIZE),0);
621 int n = (int)fread(text, sizeof(char), XML_BLOCK_SIZE, file);
623 fmi1_xml_parse_fatal(context, "Error reading from file %s", filename);
625 fmi1_xml_parse_free_context(context);
628 if (!XML_Parse(parser, text, n, feof(file))) {
629 fmi1_xml_parse_fatal(context, "Parse error at line %d:\n%s",
630 (int)XML_GetCurrentLineNumber(parser),
631 XML_ErrorString(XML_GetErrorCode(parser)));
633 fmi1_xml_parse_free_context(context);
634 return -1; /* failure */
638 /* done later XML_ParserFree(parser);*/
639 if(!jm_stack_is_empty(int)(&context->elmStack)) {
640 fmi1_xml_parse_fatal(context, "Unexpected end of file (not all elements ended) when parsing %s", filename);
641 fmi1_xml_parse_free_context(context);
645 /* Check if the 'capabilities' element was parsed in a CS FMU. If not,
646 * md->capabilities == NULL and default values should be set.
648 if (is_cs_fmu(md) && md->capabilities == NULL) {
649 jm_log_error(md->callbacks, module,
650 "No \"Capabilities\" element found, using default capabilities.");
652 md->capabilities = fmi1_xml_default_capabilities(md->callbacks);
653 if (md->capabilities == NULL) {
654 jm_log_fatal(md->callbacks, module, "Failed to allocate memory");
655 fmi1_xml_parse_free_context(context);
660 if (configuration & FMI1_XML_NAME_CHECK) {
661 fmi1_check_variable_naming_conventions(md);
664 md->status = fmi1_xml_model_description_enu_ok;
665 context->modelDescription = 0;
666 fmi1_xml_parse_free_context(context);
671 #define JM_TEMPLATE_INSTANCE_TYPE fmi1_xml_element_handle_map_t
672 #include "JM/jm_vector_template.h"