| 1 |
|
| #include "xh_config.h" |
| 2 |
|
| #include "xh_core.h" |
| 3 |
|
| |
| 4 |
|
| XH_INLINE void |
| 5 |
27 |
| _xh_h2x_lx_write_complex_node(xh_h2x_ctx_t *ctx, xh_char_t *key, I32 key_len, SV *value) |
| 6 |
|
| { |
| 7 |
|
| /* '<tag' */ |
| 8 |
27 |
| xh_xml_write_start_tag(&ctx->writer, key, key_len); |
| 9 |
|
| /* ' attr1="..." attr2="..."' */ |
| 10 |
27 |
| xh_h2x_lx(ctx, value, key, key_len, XH_H2X_F_ATTR_ONLY); |
| 11 |
|
| /* '>' */ |
| 12 |
27 |
| xh_xml_write_end_tag(&ctx->writer); |
| 13 |
|
| |
| 14 |
27 |
| xh_h2x_lx(ctx, value, key, key_len, XH_H2X_F_NONE); |
| 15 |
|
| |
| 16 |
27 |
| xh_xml_write_end_node(&ctx->writer, key, key_len); |
| 17 |
27 |
| } |
| 18 |
|
| |
| 19 |
|
| XH_INLINE void |
| 20 |
49 |
| _xh_h2x_lx(xh_h2x_ctx_t *ctx, xh_char_t *key, I32 key_len, SV *value, xh_int_t flag) |
| 21 |
|
| { |
| 22 |
|
| xh_uint_t type; |
| 23 |
|
| |
| 24 |
49 |
| value = xh_h2x_resolve_value(ctx, value, &type); |
| 25 |
|
| |
| 26 |
49 |
▶ | if (ctx->opts.cdata[0] != '\0' && xh_strcmp(key, ctx->opts.cdata) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->opts.cdata[0] != '\0' && xh_strcmp(key, ctx->opts.cdata) == 0) { |
8 |
41 |
49 |
| if (ctx->opts.cdata[0] != '\0' && xh_strcmp(key, ctx->opts.cdata) == 0) { |
4 |
4 |
8 |
|
| 27 |
19 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
2 |
2 |
4 |
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
1 |
1 |
2 |
|
| 28 |
1 |
| xh_xml_write_cdata(&ctx->writer, value); |
| 29 |
|
| } |
| 30 |
45 |
▶ | else if (ctx->opts.text[0] != '\0' && xh_strcmp(key, ctx->opts.text) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ctx->opts.text[0] != '\0' && xh_strcmp(key, ctx->opts.text) == 0) { |
45 |
0 |
45 |
| else if (ctx->opts.text[0] != '\0' && xh_strcmp(key, ctx->opts.text) == 0) { |
4 |
41 |
45 |
|
| 31 |
4 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
2 |
2 |
4 |
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
0 |
2 |
2 |
|
| 32 |
2 |
| xh_xml_write_content(&ctx->writer, value); |
| 33 |
|
| } |
| 34 |
41 |
▶ | else if (ctx->opts.comm[0] != '\0' && xh_strcmp(key, ctx->opts.comm) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| else if (ctx->opts.comm[0] != '\0' && xh_strcmp(key, ctx->opts.comm) == 0) { |
8 |
33 |
41 |
| else if (ctx->opts.comm[0] != '\0' && xh_strcmp(key, ctx->opts.comm) == 0) { |
4 |
4 |
8 |
|
| 35 |
4 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY) return; |
2 |
2 |
4 |
|
| 36 |
|
| |
| 37 |
2 |
▶ | if (type & XH_H2X_T_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type & XH_H2X_T_SCALAR) { |
1 |
1 |
2 |
|
| 38 |
1 |
| xh_xml_write_comment(&ctx->writer, value); |
| 39 |
|
| } |
| 40 |
|
| else { |
| 41 |
1 |
| xh_xml_write_comment(&ctx->writer, NULL); |
| 42 |
|
| } |
| 43 |
|
| } |
| 44 |
37 |
▶ | else if (ctx->opts.attr[0] != '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (ctx->opts.attr[0] != '\0') { |
37 |
0 |
37 |
|
| 45 |
37 |
▶ | if (xh_strncmp(key, ctx->opts.attr, ctx->opts.attr_len) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (xh_strncmp(key, ctx->opts.attr, ctx->opts.attr_len) == 0) { |
8 |
29 |
37 |
|
| 46 |
8 |
▶ | if (!(flag & XH_H2X_F_ATTR_ONLY)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(flag & XH_H2X_F_ATTR_ONLY)) return; |
4 |
4 |
8 |
|
| 47 |
|
| |
| 48 |
4 |
| key += ctx->opts.attr_len; |
| 49 |
4 |
| key_len -= ctx->opts.attr_len; |
| 50 |
|
| |
| 51 |
4 |
▶ | if (type & XH_H2X_T_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type & XH_H2X_T_SCALAR) { |
3 |
1 |
4 |
|
| 52 |
3 |
| xh_xml_write_attribute(&ctx->writer, key, key_len, value); |
| 53 |
|
| } |
| 54 |
|
| else { |
| 55 |
1 |
| xh_xml_write_attribute(&ctx->writer, key, key_len, NULL); |
| 56 |
|
| } |
| 57 |
|
| } |
| 58 |
|
| else { |
| 59 |
29 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY) return; |
7 |
22 |
29 |
|
| 60 |
|
| |
| 61 |
22 |
▶ | if (type & XH_H2X_T_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type & XH_H2X_T_ARRAY) { |
1 |
21 |
22 |
|
| 62 |
1 |
| xh_h2x_lx(ctx, value, key, key_len, XH_H2X_F_NONE); |
| 63 |
|
| } |
| 64 |
21 |
▶ | else if (type & XH_H2X_T_NOT_NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (type & XH_H2X_T_NOT_NULL) { |
20 |
1 |
21 |
|
| 65 |
20 |
| _xh_h2x_lx_write_complex_node(ctx, key, key_len, value); |
| 66 |
|
| } |
| 67 |
|
| else { |
| 68 |
1 |
| xh_xml_write_empty_node(&ctx->writer, key, key_len); |
| 69 |
|
| } |
| 70 |
|
| } |
| 71 |
|
| } |
| 72 |
|
| else { |
| 73 |
0 |
▶ | if (type & XH_H2X_T_NOT_NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type & XH_H2X_T_NOT_NULL) { |
0 |
0 |
0 |
|
| 74 |
0 |
| xh_h2x_lx(ctx, value, key, key_len, XH_H2X_F_NONE); |
| 75 |
|
| } |
| 76 |
|
| else { |
| 77 |
0 |
| xh_xml_write_empty_node(&ctx->writer, key, key_len); |
| 78 |
|
| } |
| 79 |
|
| } |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| void |
| 83 |
70 |
| xh_h2x_lx(xh_h2x_ctx_t *ctx, SV *value, xh_char_t *key, I32 key_len, xh_int_t flag) |
| 84 |
|
| { |
| 85 |
|
| SV *hash_value; |
| 86 |
|
| size_t len, i; |
| 87 |
|
| xh_uint_t type; |
| 88 |
|
| xh_sort_hash_t *sorted_hash; |
| 89 |
|
| |
| 90 |
70 |
| value = xh_h2x_resolve_value(ctx, value, &type); |
| 91 |
|
| |
| 92 |
70 |
▶ | if (type & XH_H2X_T_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (type & XH_H2X_T_SCALAR) { |
20 |
50 |
70 |
|
| 93 |
20 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
10 |
10 |
20 |
|
| 94 |
10 |
| xh_xml_write_content(&ctx->writer, value); |
| 95 |
|
| } |
| 96 |
50 |
▶ | else if (type & XH_H2X_T_HASH) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (type & XH_H2X_T_HASH) { |
47 |
3 |
50 |
|
| 97 |
47 |
▶ | len = HvUSEDKEYS((HV *) value); |
Branch: 1 branch
| Branch | True | False | Total |
| len = HvUSEDKEYS((HV *) value); |
0 |
47 |
47 |
|
| 98 |
|
| |
| 99 |
47 |
▶ | if (len > 1 && ctx->opts.canonical) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 1 && ctx->opts.canonical) { |
2 |
45 |
47 |
| if (len > 1 && ctx->opts.canonical) { |
0 |
2 |
2 |
|
| 100 |
0 |
| ENTER; |
| 101 |
0 |
| sorted_hash = xh_sort_hash((HV *) value, len); |
| 102 |
0 |
| SAVEDESTRUCTOR_X(xh_sort_hash_free, sorted_hash); |
| 103 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 104 |
0 |
| _xh_h2x_lx(ctx, sorted_hash[i].key, sorted_hash[i].key_len, sorted_hash[i].value, flag); |
| 105 |
|
| } |
| 106 |
0 |
| LEAVE; |
| 107 |
|
| } |
| 108 |
|
| else { |
| 109 |
47 |
| hv_iterinit((HV *) value); |
| 110 |
96 |
▶ | while ((hash_value = hv_iternextsv((HV *) value, (char **) &key, &key_len))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((hash_value = hv_iternextsv((HV *) value, (char **) &key, &key_len))) { |
49 |
47 |
96 |
|
| 111 |
49 |
| _xh_h2x_lx(ctx, key, key_len, hash_value, flag); |
| 112 |
|
| } |
| 113 |
|
| } |
| 114 |
|
| } |
| 115 |
3 |
▶ | else if (type & XH_H2X_T_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (type & XH_H2X_T_ARRAY) { |
0 |
3 |
3 |
|
| 116 |
3 |
▶ | if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
1 |
2 |
3 |
|
| 117 |
2 |
| len = av_len((AV *) value) + 1; |
| 118 |
9 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
7 |
2 |
9 |
|
| 119 |
7 |
| _xh_h2x_lx_write_complex_node(ctx, key, key_len, *av_fetch((AV *) value, i, 0)); |
| 120 |
|
| } |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
2 |
| FINISH: |
| 124 |
70 |
| ctx->depth--; |
| 125 |
70 |
| } |
| 126 |
|
| |
| 127 |
|
| #ifdef XH_HAVE_DOM |
| 128 |
|
| XH_INLINE void |
| 129 |
|
| _xh_h2d_lx_write_complex_node(xh_h2x_ctx_t *ctx, xmlNodePtr rootNode, xh_char_t *key, I32 key_len, SV *value) |
| 130 |
|
| { |
| 131 |
|
| //rootNode = xh_dom_new_node(ctx, rootNode, key, key_len, NULL, type & XH_H2X_T_RAW); |
| 132 |
|
| rootNode = xh_dom_new_node(ctx, rootNode, key, key_len, NULL, XH_H2X_T_RAW); |
| 133 |
|
| |
| 134 |
|
| xh_h2d_lx(ctx, rootNode, value, key, key_len, XH_H2X_F_ATTR_ONLY); |
| 135 |
|
| xh_h2d_lx(ctx, rootNode, value, key, key_len, XH_H2X_F_NONE); |
| 136 |
|
| } |
| 137 |
|
| |
| 138 |
|
| XH_INLINE void |
| 139 |
|
| _xh_h2d_lx(xh_h2x_ctx_t *ctx, xmlNodePtr rootNode, xh_char_t *key, I32 key_len, SV *value, xh_int_t flag) |
| 140 |
|
| { |
| 141 |
|
| xh_uint_t type; |
| 142 |
|
| |
| 143 |
|
| value = xh_h2x_resolve_value(ctx, value, &type); |
| 144 |
|
| |
| 145 |
|
| if (ctx->opts.cdata[0] != '\0' && xh_strcmp(key, ctx->opts.cdata) == 0) { |
| 146 |
|
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
| 147 |
|
| xh_dom_new_cdata(ctx, rootNode, value); |
| 148 |
|
| } |
| 149 |
|
| else if (ctx->opts.text[0] != '\0' && xh_strcmp(key, ctx->opts.text) == 0) { |
| 150 |
|
| if (flag & XH_H2X_F_ATTR_ONLY || !(type & XH_H2X_T_SCALAR)) return; |
| 151 |
|
| xh_dom_new_content(ctx, rootNode, value); |
| 152 |
|
| } |
| 153 |
|
| else if (ctx->opts.comm[0] != '\0' && xh_strcmp(key, ctx->opts.comm) == 0) { |
| 154 |
|
| if (flag & XH_H2X_F_ATTR_ONLY) return; |
| 155 |
|
| |
| 156 |
|
| if (!type) { |
| 157 |
|
| xh_dom_new_comment(ctx, rootNode, NULL); |
| 158 |
|
| } |
| 159 |
|
| else if (type & XH_H2X_T_SCALAR) { |
| 160 |
|
| xh_dom_new_comment(ctx, rootNode, value); |
| 161 |
|
| } |
| 162 |
|
| } |
| 163 |
|
| else if (ctx->opts.attr[0] != '\0') { |
| 164 |
|
| if (xh_strncmp(key, ctx->opts.attr, ctx->opts.attr_len) == 0) { |
| 165 |
|
| if (!(flag & XH_H2X_F_ATTR_ONLY)) return; |
| 166 |
|
| |
| 167 |
|
| key += ctx->opts.attr_len; |
| 168 |
|
| key_len -= ctx->opts.attr_len; |
| 169 |
|
| |
| 170 |
|
| if (type & XH_H2X_T_SCALAR) { |
| 171 |
|
| xh_dom_new_attribute(ctx, rootNode, key, key_len, value); |
| 172 |
|
| } |
| 173 |
|
| else { |
| 174 |
|
| xh_dom_new_attribute(ctx, rootNode, key, key_len, NULL); |
| 175 |
|
| } |
| 176 |
|
| } |
| 177 |
|
| else { |
| 178 |
|
| if (flag & XH_H2X_F_ATTR_ONLY) return; |
| 179 |
|
| |
| 180 |
|
| if (type & XH_H2X_T_ARRAY) { |
| 181 |
|
| xh_h2d_lx(ctx, rootNode, value, key, key_len, XH_H2X_F_NONE); |
| 182 |
|
| } |
| 183 |
|
| else if (type & XH_H2X_T_NOT_NULL) { |
| 184 |
|
| _xh_h2d_lx_write_complex_node(ctx, rootNode, key, key_len, value); |
| 185 |
|
| } |
| 186 |
|
| else { |
| 187 |
|
| xh_dom_new_node(ctx, rootNode, key, key_len, NULL, type & XH_H2X_T_RAW); |
| 188 |
|
| } |
| 189 |
|
| } |
| 190 |
|
| } |
| 191 |
|
| else { |
| 192 |
|
| rootNode = xh_dom_new_node(ctx, rootNode, key, key_len, NULL, type & XH_H2X_T_RAW); |
| 193 |
|
| if (type & XH_H2X_T_NOT_NULL) { |
| 194 |
|
| xh_h2d_lx(ctx, rootNode, value, key, key_len, XH_H2X_F_NONE); |
| 195 |
|
| } |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
|
| void |
| 200 |
|
| xh_h2d_lx(xh_h2x_ctx_t *ctx, xmlNodePtr rootNode, SV *value, xh_char_t *key, I32 key_len, xh_int_t flag) |
| 201 |
|
| { |
| 202 |
|
| SV *hash_value; |
| 203 |
|
| size_t len, i; |
| 204 |
|
| xh_uint_t type; |
| 205 |
|
| xh_sort_hash_t *sorted_hash; |
| 206 |
|
| |
| 207 |
|
| value = xh_h2x_resolve_value(ctx, value, &type); |
| 208 |
|
| |
| 209 |
|
| if (type & XH_H2X_T_SCALAR) { |
| 210 |
|
| if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
| 211 |
|
| xh_dom_new_content(ctx, rootNode, value); |
| 212 |
|
| } |
| 213 |
|
| else if (type & XH_H2X_T_HASH) { |
| 214 |
|
| len = HvUSEDKEYS((HV *) value); |
| 215 |
|
| hv_iterinit((HV *) value); |
| 216 |
|
| |
| 217 |
|
| if (len > 1 && ctx->opts.canonical) { |
| 218 |
|
| ENTER; |
| 219 |
|
| sorted_hash = xh_sort_hash((HV *) value, len); |
| 220 |
|
| SAVEDESTRUCTOR_X(xh_sort_hash_free, sorted_hash); |
| 221 |
|
| for (i = 0; i < len; i++) { |
| 222 |
|
| _xh_h2d_lx(ctx, rootNode, sorted_hash[i].key, sorted_hash[i].key_len, sorted_hash[i].value, flag); |
| 223 |
|
| } |
| 224 |
|
| LEAVE; |
| 225 |
|
| } |
| 226 |
|
| else { |
| 227 |
|
| while ((hash_value = hv_iternextsv((HV *) value, (char **) &key, &key_len))) { |
| 228 |
|
| _xh_h2d_lx(ctx, rootNode, key, key_len, hash_value, flag); |
| 229 |
|
| } |
| 230 |
|
| } |
| 231 |
|
| } |
| 232 |
|
| else if (type & XH_H2X_T_ARRAY) { |
| 233 |
|
| if (flag & XH_H2X_F_ATTR_ONLY) goto FINISH; |
| 234 |
|
| len = av_len((AV *) value) + 1; |
| 235 |
|
| for (i = 0; i < len; i++) { |
| 236 |
|
| _xh_h2d_lx_write_complex_node(ctx, rootNode, key, key_len, *av_fetch((AV *) value, i, 0)); |
| 237 |
|
| } |
| 238 |
|
| } |
| 239 |
|
| |
| 240 |
|
| FINISH: |
| 241 |
|
| ctx->depth--; |
| 242 |
|
| } |
| 243 |
|
| #endif |