| 1 |
#include "xh_config.h" |
| 2 |
#include "xh_core.h" |
| 3 |
|
| 4 |
void |
| 5 |
xh_h2x_native(xh_h2x_ctx_t *ctx, xh_char_t *key, I32 key_len, SV *value) |
| 6 |
{ |
| 7 |
xh_uint_t type; |
| 8 |
size_t i, len; |
| 9 |
SV *item_value; |
| 10 |
xh_char_t *item; |
| 11 |
I32 item_len; |
| 12 |
xh_sort_hash_t *sorted_hash; |
| 13 |
GV *method; |
| 14 |
|
| 15 |
value = xh_h2x_resolve_value(ctx, value, &type); |
| 16 |
|
| 17 |
if (type & XH_H2X_T_BLESSED && (method = gv_fetchmethod_autoload(SvSTASH(value), "iternext", 0)) != NULL) { |
| 18 |
while (1) { |
| 19 |
item_value = xh_h2x_call_method(value, method); |
| 20 |
if (!SvOK(item_value)) break; |
| 21 |
(void) xh_h2x_native(ctx, key, key_len, item_value); |
| 22 |
SvREFCNT_dec(item_value); |
| 23 |
} |
| 24 |
goto FINISH; |
| 25 |
} |
| 26 |
|
| 27 |
if (type & XH_H2X_T_SCALAR) { |
| 28 |
xh_xml_write_node(&ctx->writer, key, key_len, value, type & XH_H2X_T_RAW); |
| 29 |
} |
| 30 |
else if (type & XH_H2X_T_HASH) { |
| 31 |
len = HvUSEDKEYS((HV *) value); |
| 32 |
if (len == 0) goto ADD_EMPTY_NODE; |
| 33 |
|
| 34 |
if (key != NULL) |
| 35 |
xh_xml_write_start_node(&ctx->writer, key, key_len); |
| 36 |
|
| 37 |
if (len > 1 && ctx->opts.canonical) { |
| 38 |
ENTER; |
| 39 |
sorted_hash = xh_sort_hash((HV *) value, len); |
| 40 |
SAVEDESTRUCTOR_X(xh_sort_hash_free, sorted_hash); |
| 41 |
for (i = 0; i < len; i++) { |
| 42 |
xh_h2x_native(ctx, sorted_hash[i].key, sorted_hash[i].key_len, sorted_hash[i].value); |
| 43 |
} |
| 44 |
LEAVE; |
| 45 |
} |
| 46 |
else { |
| 47 |
hv_iterinit((HV *) value); |
| 48 |
while ((item_value = hv_iternextsv((HV *) value, (char **) &item, &item_len))) { |
| 49 |
xh_h2x_native(ctx, item, item_len, item_value); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if (key != NULL) |
| 54 |
xh_xml_write_end_node(&ctx->writer, key, key_len); |
| 55 |
} |
| 56 |
else if (type & XH_H2X_T_ARRAY) { |
| 57 |
len = av_len((AV *) value) + 1; |
| 58 |
for (i = 0; i < len; i++) { |
| 59 |
xh_h2x_native(ctx, key, key_len, *av_fetch((AV *) value, i, 0)); |
| 60 |
} |
| 61 |
} |
| 62 |
else { |
| 63 |
ADD_EMPTY_NODE: |
| 64 |
if (key != NULL) |
| 65 |
xh_xml_write_empty_node(&ctx->writer, key, key_len); |
| 66 |
} |
| 67 |
|
| 68 |
FINISH: |
| 69 |
ctx->depth--; |
| 70 |
} |
| 71 |
|
| 72 |
#ifdef XH_HAVE_DOM |
| 73 |
void |
| 74 |
xh_h2d_native(xh_h2x_ctx_t *ctx, xmlNodePtr rootNode, xh_char_t *key, I32 key_len, SV *value) |
| 75 |
{ |
| 76 |
xh_uint_t type; |
| 77 |
size_t i, len; |
| 78 |
SV *item_value; |
| 79 |
xh_char_t *item; |
| 80 |
I32 item_len; |
| 81 |
xh_sort_hash_t *sorted_hash; |
| 82 |
GV *method; |
| 83 |
|
| 84 |
value = xh_h2x_resolve_value(ctx, value, &type); |
| 85 |
|
| 86 |
if (type & XH_H2X_T_BLESSED && (method = gv_fetchmethod_autoload(SvSTASH(value), "iternext", 0)) != NULL) { |
| 87 |
while (1) { |
| 88 |
item_value = xh_h2x_call_method(value, method); |
| 89 |
if (!SvOK(item_value)) break; |
| 90 |
(void) xh_h2d_native(ctx, rootNode, key, key_len, item_value); |
| 91 |
SvREFCNT_dec(item_value); |
| 92 |
} |
| 93 |
goto FINISH; |
| 94 |
} |
| 95 |
|
| 96 |
if (type & XH_H2X_T_SCALAR) { |
| 97 |
(void) xh_dom_new_node(ctx, rootNode, key, key_len, value, type & XH_H2X_T_RAW); |
| 98 |
} |
| 99 |
else if (type & XH_H2X_T_HASH) { |
| 100 |
len = HvUSEDKEYS((HV *) value); |
| 101 |
if (len == 0) goto ADD_EMPTY_NODE; |
| 102 |
|
| 103 |
rootNode = xh_dom_new_node(ctx, rootNode, key, key_len, NULL, FALSE); |
| 104 |
|
| 105 |
if (len > 1 && ctx->opts.canonical) { |
| 106 |
ENTER; |
| 107 |
sorted_hash = xh_sort_hash((HV *) value, len); |
| 108 |
SAVEDESTRUCTOR_X(xh_sort_hash_free, sorted_hash); |
| 109 |
for (i = 0; i < len; i++) { |
| 110 |
xh_h2d_native(ctx, rootNode, sorted_hash[i].key, sorted_hash[i].key_len, sorted_hash[i].value); |
| 111 |
} |
| 112 |
LEAVE; |
| 113 |
} |
| 114 |
else { |
| 115 |
hv_iterinit((HV *) value); |
| 116 |
while ((item_value = hv_iternextsv((HV *) value, (char **) &item, &item_len))) { |
| 117 |
xh_h2d_native(ctx, rootNode, item, item_len, item_value); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
else if (type & XH_H2X_T_ARRAY) { |
| 122 |
len = av_len((AV *) value) + 1; |
| 123 |
for (i = 0; i < len; i++) { |
| 124 |
(void) xh_h2d_native(ctx, rootNode, key, key_len, *av_fetch((AV *) value, i, 0)); |
| 125 |
} |
| 126 |
} |
| 127 |
else { |
| 128 |
ADD_EMPTY_NODE: |
| 129 |
xh_dom_new_node(ctx, rootNode, key, key_len, NULL, FALSE); |
| 130 |
} |
| 131 |
|
| 132 |
FINISH: |
| 133 |
ctx->depth--; |
| 134 |
} |
| 135 |
#endif |