Exemple #1
0
int
dump_data_element(int depth, char **buffer, int *max, int *offset, const char *prefix,
                  xmlNode * data, gboolean formatted)
{
    int printed = 0;
    int has_children = 0;
    xmlNode *child = NULL;
    const char *name = NULL;

    CRM_CHECK(data != NULL, return 0);

    name = crm_element_name(data);

    CRM_CHECK(name != NULL, return 0);
    CRM_CHECK(buffer != NULL && *buffer != NULL, return 0);

    crm_trace("Dumping %s...", name);

    if (prefix) {
        printed = snprintf(bhead(buffer, offset), bremain(max, offset), "%s", prefix);
        update_buffer_head(printed);
    }

    if (formatted) {
        printed = print_spaces(bhead(buffer, offset), depth, bremain(max, offset));
        update_buffer_head(printed);
    }

    printed = snprintf(bhead(buffer, offset), bremain(max, offset), "<%s", name);
    update_buffer_head(printed);

    if(data) {
        xmlAttrPtr xIter = NULL;
        for(xIter = data->properties; xIter; xIter = xIter->next) {
            const char *prop_name = (const char *)xIter->name;
            const char *prop_value = crm_element_value(data, prop_name);
            crm_trace("Dumping <%s %s=\"%s\"...",
                        name, prop_name, prop_value);
            printed = snprintf(bhead(buffer, offset), bremain(max, offset), " %s=\"%s\"", prop_name, prop_value);
            update_buffer_head(printed);
        }
    }

    has_children = xml_has_children(data);
    printed = snprintf(bhead(buffer, offset), bremain(max, offset), "%s>%s",
                       has_children == 0 ? "/" : "", formatted ? "\n" : "");
    update_buffer_head(printed);

    if (has_children == 0) {
        return 0;
    }

    for (child = __xml_first_child(data); child != NULL; child = __xml_next(child)) {
        if (dump_data_element(depth + 1, buffer, max, offset, prefix, child, formatted) < 0) {
            return -1;
        }
    }

    if (prefix) {
        printed = snprintf(bhead(buffer, offset), bremain(max, offset), "%s", prefix);
        update_buffer_head(printed);
    }

    if (formatted) {
        printed = print_spaces(bhead(buffer, offset), depth, bremain(max, offset));
        update_buffer_head(printed);
    }

    printed =
        snprintf(bhead(buffer, offset), bremain(max, offset), "</%s>%s", name,
                 formatted ? "\n" : "");
    update_buffer_head(printed);
    crm_trace("Dumped %s...", name);

    return has_children;
}
Exemple #2
0
int
struct_display_as_xml(
	int log_level, int depth, struct ha_msg *data,
	const char *prefix, gboolean formatted) 
{
	int lpc = 0;
	int printed = 0;
	gboolean has_children = FALSE;
	char print_buffer[1000];
	char *buffer = print_buffer;
	const char *name = cl_get_string(data, F_XML_TAGNAME);

	if(data == NULL) {
		return 0;

	} else if(name == NULL) {
		cl_log(LOG_WARNING, "Struct at depth %d had no name", depth);
		cl_log_message(log_level, data);
		return 0;
	}
	
	if(formatted) {
		printed = struct_display_print_spaces(buffer, depth);
		update_buffer_head(buffer, printed);
	}
	
	printed = sprintf(buffer, "<%s", name);
	update_buffer_head(buffer, printed);
	
	for (lpc = 0; lpc < data->nfields; lpc++) {
		const char *prop_name = data->names[lpc];
		const char *prop_value = data->values[lpc];
		if(data->types[lpc] != FT_STRING) {
			continue;
		} else if(prop_name == NULL) {
			continue;
		} else if(prop_name[0] == '_' && prop_name[1] == '_') {
			continue;
		}
		printed = sprintf(buffer, " %s=\"%s\"", prop_name, prop_value);
		update_buffer_head(buffer, printed);
	}

	for (lpc = 0; lpc < data->nfields; lpc++) {
		if(data->types[lpc] == FT_STRUCT) {
			has_children = TRUE;
			break;
		}
	}

	printed = sprintf(buffer, "%s>", has_children==0?"/":"");
	update_buffer_head(buffer, printed);
	cl_log(log_level, "%s%s", prefix?prefix:"", print_buffer);
	buffer = print_buffer;
	
	if(has_children == FALSE) {
		return 0;
	}
	
	for (lpc = 0; lpc < data->nfields; lpc++) {
		if(data->types[lpc] != FT_STRUCT) {
			continue;
		} else if(0 > struct_display_as_xml(
				  log_level, depth+1, data->values[lpc],
				  prefix, formatted)) {
			return -1;
		}
	}

	if(formatted) {
		printed = struct_display_print_spaces(buffer, depth);
		update_buffer_head(buffer, printed);
	}
	cl_log(log_level, "%s%s</%s>", prefix?prefix:"", print_buffer, name);

	return 0;
}