| 1 |
#include "xh_config.h" |
| 2 |
#include "xh_core.h" |
| 3 |
|
| 4 |
SV * |
| 5 |
xh_h2x(xh_h2x_ctx_t *ctx) |
| 6 |
{ |
| 7 |
SV *result; |
| 8 |
|
| 9 |
/* run */ |
| 10 |
dXCPT; |
| 11 |
XCPT_TRY_START |
| 12 |
{ |
| 13 |
xh_stack_init(&ctx->stash, XH_H2X_STASH_SIZE, sizeof(SV *)); |
| 14 |
xh_writer_init(&ctx->writer, ctx->opts.encoding, ctx->opts.output, ctx->opts.output_cb, ctx->opts.buf_size, ctx->opts.indent, ctx->opts.trim, ctx->opts.utf8); |
| 15 |
|
| 16 |
if (ctx->opts.xml_decl) { |
| 17 |
xh_xml_write_xml_declaration(&ctx->writer, ctx->opts.version, ctx->opts.encoding); |
| 18 |
} |
| 19 |
|
| 20 |
switch (ctx->opts.method) { |
| 21 |
case XH_METHOD_NATIVE: |
| 22 |
if (ctx->opts.keep_root) { |
| 23 |
xh_h2x_native(ctx, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash)); |
| 24 |
} |
| 25 |
else |
| 26 |
{ |
| 27 |
xh_h2x_native(ctx, NULL, 0, SvRV(ctx->hash)); |
| 28 |
} |
| 29 |
break; |
| 30 |
case XH_METHOD_NATIVE_ATTR_MODE: |
| 31 |
(void) xh_h2x_native_attr(ctx, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash), XH_H2X_F_COMPLEX); |
| 32 |
break; |
| 33 |
case XH_METHOD_LX: |
| 34 |
xh_h2x_lx(ctx, ctx->hash, NULL, 0, XH_H2X_F_NONE); |
| 35 |
break; |
| 36 |
default: |
| 37 |
croak("Invalid method"); |
| 38 |
} |
| 39 |
|
| 40 |
result = xh_writer_flush(&ctx->writer); |
| 41 |
} XCPT_TRY_END |
| 42 |
|
| 43 |
XCPT_CATCH |
| 44 |
{ |
| 45 |
xh_stash_clean(&ctx->stash); |
| 46 |
if (ctx->opts.output_cb == NULL) { |
| 47 |
result = xh_writer_flush(&ctx->writer); |
| 48 |
if (result != NULL && result != &PL_sv_undef) { |
| 49 |
SvREFCNT_dec(result); |
| 50 |
} |
| 51 |
} |
| 52 |
xh_writer_destroy(&ctx->writer); |
| 53 |
XCPT_RETHROW; |
| 54 |
} |
| 55 |
|
| 56 |
xh_stash_clean(&ctx->stash); |
| 57 |
if (result != NULL && result != &PL_sv_undef && ctx->opts.utf8) { |
| 58 |
#ifdef XH_HAVE_ENCODER |
| 59 |
if (ctx->writer.encoder == NULL) { |
| 60 |
SvUTF8_on(result); |
| 61 |
} |
| 62 |
#else |
| 63 |
SvUTF8_on(result); |
| 64 |
#endif |
| 65 |
} |
| 66 |
xh_writer_destroy(&ctx->writer); |
| 67 |
|
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
#ifdef XH_HAVE_DOM |
| 72 |
SV * |
| 73 |
xh_h2d(xh_h2x_ctx_t *ctx) |
| 74 |
{ |
| 75 |
dXCPT; |
| 76 |
|
| 77 |
xmlDocPtr doc = xmlNewDoc(BAD_CAST ctx->opts.version); |
| 78 |
if (doc == NULL) { |
| 79 |
croak("Can't create new document"); |
| 80 |
} |
| 81 |
if (ctx->opts.encoding[0] == '\0') { |
| 82 |
doc->encoding = (const xmlChar*) xmlStrdup((const xmlChar*) XH_INTERNAL_ENCODING); |
| 83 |
} |
| 84 |
else { |
| 85 |
doc->encoding = (const xmlChar*) xmlStrdup((const xmlChar*) ctx->opts.encoding); |
| 86 |
} |
| 87 |
|
| 88 |
XCPT_TRY_START |
| 89 |
{ |
| 90 |
xh_stack_init(&ctx->stash, XH_H2X_STASH_SIZE, sizeof(SV *)); |
| 91 |
switch (ctx->opts.method) { |
| 92 |
case XH_METHOD_NATIVE: |
| 93 |
xh_h2d_native(ctx, (xmlNodePtr) doc, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash)); |
| 94 |
break; |
| 95 |
case XH_METHOD_NATIVE_ATTR_MODE: |
| 96 |
(void) xh_h2d_native_attr(ctx, (xmlNodePtr) doc, ctx->opts.root, xh_strlen(ctx->opts.root), SvRV(ctx->hash), XH_H2X_F_COMPLEX); |
| 97 |
break; |
| 98 |
case XH_METHOD_LX: |
| 99 |
xh_h2d_lx(ctx, (xmlNodePtr) doc, ctx->hash, NULL, 0, XH_H2X_F_NONE); |
| 100 |
break; |
| 101 |
default: |
| 102 |
croak("Invalid method"); |
| 103 |
} |
| 104 |
} XCPT_TRY_END |
| 105 |
|
| 106 |
XCPT_CATCH |
| 107 |
{ |
| 108 |
xh_stash_clean(&ctx->stash); |
| 109 |
XCPT_RETHROW; |
| 110 |
} |
| 111 |
|
| 112 |
xh_stash_clean(&ctx->stash); |
| 113 |
|
| 114 |
return x_PmmNodeToSv((xmlNodePtr) doc, NULL); |
| 115 |
} |
| 116 |
#endif |