1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
/* $Id: LibXSLT.xs,v 1.63 2006/07/28 11:11:04 pajas Exp $ */
#ifdef __cplusplus
extern "C" {
#endif
#include <libxslt/xsltconfig.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libxslt/imports.h>
#include <libxslt/extensions.h>
#ifdef HAVE_EXSLT
#include <libexslt/exslt.h>
#include <libexslt/exsltconfig.h>
#endif
#include <libxml/xmlmemory.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/xpathInternals.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "perl-libxml-mm.h"
#include "ppport.h"
#ifdef __cplusplus
}
#endif
#define SET_CB(cb, fld) \
RETVAL = cb ? newSVsv(cb) : &PL_sv_undef;\
if (SvOK(fld)) {\
if (cb) {\
if (cb != fld) {\
sv_setsv(cb, fld);\
}\
}\
else {\
cb = newSVsv(fld);\
}\
}\
else {\
if (cb) {\
SvREFCNT_dec(cb);\
cb = NULL;\
}\
}
#define SET_CB2(cb, fld) cb=fld;
static SV * LibXSLT_debug_cb = NULL;
static HV * LibXSLT_HV_allCallbacks = NULL;
void
LibXSLT_free_all_callbacks(void)
{
if (LibXSLT_debug_cb) {
SvREFCNT_dec(LibXSLT_debug_cb);
LibXSLT_debug_cb = NULL;
}
}
int
LibXSLT_iowrite_scalar(void * context, const char * buffer, int len)
{
SV * scalar;
scalar = (SV *)context;
sv_catpvn(scalar, (const char*)buffer, len);
return len;
}
int
LibXSLT_ioclose_scalar(void * context)
{
return 0;
}
int
LibXSLT_iowrite_fh(void * context, const char * buffer, int len)
{
dSP;
SV * ioref;
SV * tbuff;
SV * results;
int cnt;
ENTER;
SAVETMPS;
ioref = (SV *)context;
tbuff = newSVpvn((char*)buffer, len);
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(ioref);
PUSHs(sv_2mortal(tbuff));
PUTBACK;
cnt = perl_call_method("print", G_SCALAR | G_EVAL);
SPAGAIN;
if (cnt != 1) {
croak("fh->print() method call failed");
}
results = POPs;
if (!SvOK(results)) {
croak("print to fh failed");
}
PUTBACK;
FREETMPS;
LEAVE;
return len;
}
int
LibXSLT_ioclose_fh(void * context)
{
return 0; /* we let Perl close the FH */
}
void
LibXSLT_error_handler(void * ctxt, const char * msg, ...)
{
va_list args;
SV * sv;
STRLEN n_a;
va_start(args, msg);
sv_vcatpvfn(ERRSV, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
}
void
LibXSLT_debug_handler(void * ctxt, const char * msg, ...)
{
dSP;
va_list args;
SV * sv;
STRLEN n_a;
sv = NEWSV(0,512);
va_start(args, msg);
sv_vsetpvfn(sv, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
int cnt = 0;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv);
PUTBACK;
cnt = perl_call_sv(LibXSLT_debug_cb, G_SCALAR | G_EVAL);
SPAGAIN;
if (cnt != 1) {
croak("debug handler call failed");
}
PUTBACK;
FREETMPS;
LEAVE;
}
SvREFCNT_dec(sv);
}
static void
LibXSLT_generic_function (xmlXPathParserContextPtr ctxt, int nargs) {
xmlXPathObjectPtr obj,ret;
xmlNodeSetPtr nodelist = NULL;
int count;
SV * perl_dispatch;
int i;
STRLEN len;
SV * perl_result;
ProxyNodePtr owner = NULL;
char * tmp_string;
STRLEN n_a;
double tmp_double;
int tmp_int;
AV * array_result;
xmlNodePtr tmp_node, tmp_node1;
SV *key;
char *strkey;
const char *function, *uri;
SV **perl_function;
AV *arguments;
int cnt = 0;
dSP;
function = ctxt->context->function;
uri = ctxt->context->functionURI;
key = newSVpvn("",0);
sv_catpv(key, "{");
sv_catpv(key, (const char*)uri);
sv_catpv(key, "}");
sv_catpv(key, (const char*)function);
strkey = SvPV(key, len);
perl_function = hv_fetch(LibXSLT_HV_allCallbacks, strkey, len, 0);
SvREFCNT_dec(key);
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(*perl_function);
/* set up call to perl dispatcher function */
for (i = 0; i < nargs; i++) {
obj = (xmlXPathObjectPtr)valuePop(ctxt);
switch (obj->type) {
case XPATH_NODESET:
case XPATH_XSLT_TREE:
nodelist = obj->nodesetval;
if ( nodelist ) {
XPUSHs(sv_2mortal(newSVpv("XML::LibXML::NodeList", 0)));
XPUSHs(sv_2mortal(newSViv(nodelist->nodeNr)));
if ( nodelist->nodeNr > 0 ) {
int i = 0 ;
const char * cls = "XML::LibXML::Node";
xmlNodePtr tnode;
SV * element;
len = nodelist->nodeNr;
for( i ; i < len; i++){
tnode = nodelist->nodeTab[i];
/*
if( tnode != NULL && tnode->doc != NULL) {
owner = SvPROXYNODE(sv_2mortal(x_PmmNodeToSv((xmlNodePtr)(tnode->doc), NULL)));
}
*/
if (tnode->type == XML_NAMESPACE_DECL) {
element = sv_newmortal();
cls = x_PmmNodeTypeName( tnode );
element = sv_setref_pv( element,
(const char *)cls,
(void *)xmlCopyNamespace((xmlNsPtr)tnode)
);
}
else {
/* need to copy the node as libxml2 will free it */
xmlNodePtr tnode_cpy = xmlCopyNode(tnode, 1);
element = x_PmmNodeToSv(tnode_cpy, owner);
}
XPUSHs( sv_2mortal(element) );
}
}
}
break;
case XPATH_BOOLEAN:
XPUSHs(sv_2mortal(newSVpv("XML::LibXML::Boolean", 0)));
XPUSHs(sv_2mortal(newSViv(obj->boolval)));
break;
case XPATH_NUMBER:
XPUSHs(sv_2mortal(newSVpv("XML::LibXML::Number", 0)));
XPUSHs(sv_2mortal(newSVnv(obj->floatval)));
break;
case XPATH_STRING:
XPUSHs(sv_2mortal(newSVpv("XML::LibXML::Literal", 0)));
XPUSHs(sv_2mortal(newSVpv(obj->stringval, 0)));
break;
default:
/* warn("Unknown XPath return type (%d) in call to {%s}%s - assuming string", obj->type, uri, function); */
XPUSHs(sv_2mortal(newSVpv("XML::LibXML::Literal", 0)));
XPUSHs(sv_2mortal(newSVpv((char*)xmlXPathCastToString(obj), 0)));
}
xmlXPathFreeObject(obj);
}
/* call perl dispatcher */
PUTBACK;
perl_dispatch = sv_2mortal(newSVpv("XML::LibXSLT::perl_dispatcher",0));
count = perl_call_sv(perl_dispatch, G_SCALAR|G_EVAL);
SPAGAIN;
if (SvTRUE(ERRSV)) {
POPs;
croak("LibXSLT: error coming back from perl-dispatcher in pm file. %s\n", SvPV(ERRSV, n_a));
}
if (count != 1) croak("LibXSLT: perl-dispatcher in pm file returned more than one argument!\n");
perl_result = POPs;
if (!SvOK(perl_result)) {
ret = (xmlXPathObjectPtr)xmlXPathNewCString("");
goto FINISH;
}
/* convert perl result structures to LibXML structures */
if (sv_isobject(perl_result) &&
(SvTYPE(SvRV(perl_result)) == SVt_PVMG ||
SvTYPE(SvRV(perl_result)) == SVt_PVAV))
{
if (sv_derived_from(perl_result, "XML::LibXML::NodeList")) {
ret = xmlXPathNewNodeSet(NULL);
ret->boolval = 1; /* free children */
array_result = (AV*)SvRV(perl_result);
while (av_len(array_result) >= 0) {
tmp_node1 = (xmlNodePtr)x_PmmSvNode(sv_2mortal(av_shift(array_result)));
tmp_node = xmlDocCopyNode(tmp_node1, ctxt->context->doc, 1);
xmlXPathNodeSetAdd(ret->nodesetval, tmp_node);
if (ret->user == NULL) {
ret->user = (void*)tmp_node;
}
else {
xmlNodePtr old = (xmlNodePtr)(ret->user);
old->prev = tmp_node;
tmp_node->next = old;
ret->user = (void*)tmp_node;
}
}
goto FINISH;
}
else if (sv_derived_from(perl_result, "XML::LibXML::Node")) {
ret = (xmlXPathObjectPtr)xmlXPathNewNodeSet(NULL);
ret->boolval = 1; /* free children */
tmp_node1 = (xmlNodePtr)x_PmmSvNode(perl_result);
tmp_node = xmlDocCopyNode(tmp_node1, ctxt->context->doc, 1);
xmlXPathNodeSetAdd(ret->nodesetval,tmp_node);
ret->user = (void*)tmp_node;
goto FINISH;
}
else if (sv_derived_from(perl_result, "XML::LibXML::Boolean")) {
tmp_int = SvIV(SvRV(perl_result));
ret = (xmlXPathObjectPtr)xmlXPathNewBoolean(tmp_int);
goto FINISH;
}
else if (sv_derived_from(perl_result, "XML::LibXML::Literal")) {
tmp_string = SvPV(SvRV(perl_result), len);
ret = (xmlXPathObjectPtr)xmlXPathNewCString(tmp_string);
goto FINISH;
}
else if (sv_derived_from(perl_result, "XML::LibXML::Number")) {
tmp_double = SvNV(SvRV(perl_result));
ret = (xmlXPathObjectPtr)xmlXPathNewFloat(tmp_double);
goto FINISH;
}
}
ret = (xmlXPathObjectPtr)xmlXPathNewCString(SvPV(perl_result, len));
FINISH:
valuePush(ctxt, ret);
PUTBACK;
FREETMPS;
LEAVE;
}
int
LibXSLT_input_match(char const * filename)
{
int results;
int count;
SV * res;
results = 0;
{
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv((char*)filename, 0)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_match",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("match callback must return a single value");
}
if (SvTRUE(ERRSV)) {
croak("input match callback died: %s", SvPV_nolen(ERRSV));
POPs ;
}
res = POPs;
if (SvTRUE(res)) {
results = 1;
}
PUTBACK;
FREETMPS;
LEAVE;
}
return results;
}
void *
LibXSLT_input_open(char const * filename)
{
SV * results;
int count;
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv((char*)filename, 0)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_open",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("open callback must return a single value");
}
if (SvTRUE(ERRSV)) {
croak("input callback died: %s", SvPV_nolen(ERRSV));
POPs ;
}
results = POPs;
SvREFCNT_inc(results);
PUTBACK;
FREETMPS;
LEAVE;
return (void *)results;
}
int
LibXSLT_input_read(void * context, char * buffer, int len)
{
STRLEN res_len;
const char * output;
SV * ctxt;
res_len = 0;
ctxt = (SV *)context;
{
int count;
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(ctxt);
PUSHs(sv_2mortal(newSViv(len)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_read",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("read callback must return a single value");
}
if (SvTRUE(ERRSV)) {
croak("read callback died: %s", SvPV_nolen(ERRSV));
POPs ;
}
output = POPp;
if (output != NULL) {
res_len = strlen(output);
if (res_len) {
strncpy(buffer, output, res_len);
}
else {
buffer[0] = 0;
}
}
PUTBACK;
FREETMPS;
LEAVE;
}
return res_len;
}
void
LibXSLT_input_close(void * context)
{
SV * ctxt;
ctxt = (SV *)context;
{
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(ctxt);
PUTBACK;
call_pv("XML::LibXML::InputCallback::_callback_close",
G_SCALAR | G_EVAL | G_DISCARD);
SvREFCNT_dec(ctxt);
if (SvTRUE(ERRSV)) {
croak("close callback died: %s", SvPV_nolen(ERRSV));
}
FREETMPS;
LEAVE;
}
}
MODULE = XML::LibXSLT PACKAGE = XML::LibXSLT
PROTOTYPES: DISABLE
BOOT:
LIBXML_TEST_VERSION
xsltMaxDepth = 250;
xsltSetXIncludeDefault(1);
LibXSLT_HV_allCallbacks = newHV();
#ifdef HAVE_EXSLT
exsltRegisterAll();
#endif
int
xinclude_default(self, ...)
SV * self
CODE:
RETVAL = xsltGetXIncludeDefault();
if (items > 1) {
xsltSetXIncludeDefault(SvIV(ST(1)));
}
OUTPUT:
RETVAL
int
max_depth(self, ...)
SV * self
CODE:
RETVAL = xsltMaxDepth;
if (items > 1) {
xsltMaxDepth = SvIV(ST(1));
}
OUTPUT:
RETVAL
void
register_function(self, uri, name, callback)
SV * self
char * uri
char * name
SV *callback
PPCODE:
{
SV *key;
STRLEN len;
char *strkey;
/* todo: Add checking of uri and name in here! */
xsltRegisterExtModuleFunction((const xmlChar *)name,
(const xmlChar *)uri,
LibXSLT_generic_function);
key = newSVpvn("",0);
sv_catpv(key, "{");
sv_catpv(key, (const char*)uri);
sv_catpv(key, "}");
sv_catpv(key, (const char*)name);
strkey = SvPV(key, len);
/* warn("Trying to store function '%s' in %d\n", strkey, LibXSLT_HV_allCallbacks); */
hv_store(LibXSLT_HV_allCallbacks, strkey, len, SvREFCNT_inc(callback), 0);
SvREFCNT_dec(key);
}
SV *
debug_callback(self, ...)
SV * self
CODE:
if (items > 1) {
SV * debug_cb = ST(1);
if (debug_cb && SvTRUE(debug_cb)) {
SET_CB(LibXSLT_debug_cb, ST(1));
}
else {
LibXSLT_debug_cb = NULL;
}
}
else {
RETVAL = LibXSLT_debug_cb ? sv_2mortal(LibXSLT_debug_cb) : &PL_sv_undef;
}
OUTPUT:
RETVAL
xsltStylesheetPtr
_parse_stylesheet(self, sv_doc)
SV * self
SV * sv_doc
PREINIT:
char * CLASS = "XML::LibXSLT::Stylesheet";
xmlDocPtr doc_copy;
xmlDocPtr doc;
CODE:
if (sv_doc == NULL) {
XSRETURN_UNDEF;
}
doc = (xmlDocPtr)x_PmmSvNode( sv_doc );
if (doc == NULL) {
XSRETURN_UNDEF;
}
doc_copy = xmlCopyDoc(doc, 1);
doc_copy->URL = xmlStrdup(doc->URL);
/* xmlNodeSetBase((xmlNodePtr)doc_copy, doc_copy->URL); */
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
RETVAL = xsltParseStylesheetDoc(doc_copy);
if (RETVAL == NULL) {
XSRETURN_UNDEF;
}
OUTPUT:
RETVAL
xsltStylesheetPtr
_parse_stylesheet_file(self, filename)
SV * self
const char * filename
PREINIT:
char * CLASS = "XML::LibXSLT::Stylesheet";
CODE:
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
RETVAL = xsltParseStylesheetFile(filename);
if (RETVAL == NULL) {
XSRETURN_UNDEF;
}
OUTPUT:
RETVAL
void
lib_init_callbacks( self )
SV * self
CODE:
xmlRegisterInputCallbacks((xmlInputMatchCallback) LibXSLT_input_match,
(xmlInputOpenCallback) LibXSLT_input_open,
(xmlInputReadCallback) LibXSLT_input_read,
(xmlInputCloseCallback) LibXSLT_input_close);
void
lib_cleanup_callbacks( self )
SV * self
CODE:
xmlCleanupInputCallbacks();
xmlRegisterDefaultInputCallbacks();
MODULE = XML::LibXSLT PACKAGE = XML::LibXSLT::Stylesheet
PROTOTYPES: DISABLE
SV *
transform(self, sv_doc, ...)
xsltStylesheetPtr self
SV * sv_doc
PREINIT:
char * CLASS = "XML::LibXML::Document";
# note really only 254 entries here - last one is NULL
const char *xslt_params[255];
xmlDocPtr real_dom;
xmlDocPtr doc;
STRLEN len;
CODE:
if (sv_doc == NULL) {
XSRETURN_UNDEF;
}
doc = (xmlDocPtr)x_PmmSvNode( sv_doc );
if (doc == NULL) {
XSRETURN_UNDEF;
}
xslt_params[0] = 0;
if (items > 256) {
croak("Too many parameters in transform()");
}
if (items % 2) {
croak("Odd number of parameters");
}
if (items > 2) {
int i;
for (i = 2; (i < items && i < 256); i++) {
xslt_params[i - 2] = (char *)SvPV(ST(i), PL_na);
}
# set last entry to NULL
xslt_params[i - 2] = 0;
}
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
real_dom = xsltApplyStylesheet(self, doc, xslt_params);
if (real_dom == NULL) {
if (SvTRUE(ERRSV)) {
croak("Exception occurred while applying stylesheet: %s", SvPV(ERRSV, len));
}
croak("Error applying stylesheet: %s", "(get error out of libxslt)");
}
if (real_dom->type == XML_HTML_DOCUMENT_NODE) {
if (self->method != NULL) {
xmlFree(self->method);
}
self->method = xmlMalloc(5);
strcpy(self->method, "html");
}
RETVAL = x_PmmNodeToSv((xmlNodePtr)real_dom, NULL);
OUTPUT:
RETVAL
SV *
transform_file(self, filename, ...)
xsltStylesheetPtr self
char * filename
PREINIT:
char * CLASS = "XML::LibXML::Document";
# note really only 254 entries here - last one is NULL
const char *xslt_params[255];
xmlDocPtr real_dom;
STRLEN len;
CODE:
xslt_params[0] = 0;
if (items > 256) {
croak("Too many parameters in transform()");
}
if (items % 2) {
croak("Odd number of parameters");
}
if (items > 2) {
int i;
for (i = 2; (i < items && i < 256); i++) {
xslt_params[i - 2] = (char *)SvPV(ST(i), PL_na);
}
# set last entry to NULL
xslt_params[i - 2] = 0;
}
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
real_dom = xsltApplyStylesheet(self, xmlParseFile(filename), xslt_params);
if (real_dom == NULL) {
if (SvTRUE(ERRSV)) {
croak("Error applying stylesheet: %s", SvPV(ERRSV, len));
}
croak("Error applying stylesheet: %s", "(get error out of libxslt)");
}
if (real_dom->type == XML_HTML_DOCUMENT_NODE) {
if (self->method != NULL) {
xmlFree(self->method);
}
self->method = xmlMalloc(5);
strcpy(self->method, "html");
}
RETVAL = x_PmmNodeToSv((xmlNodePtr)real_dom, NULL);
OUTPUT:
RETVAL
void
DESTROY(self)
xsltStylesheetPtr self
CODE:
if (self == NULL) {
XSRETURN_UNDEF;
}
xsltFreeStylesheet(self);
SV *
output_string(self, sv_doc)
xsltStylesheetPtr self
SV * sv_doc
PREINIT:
xmlOutputBufferPtr output;
SV * results = newSVpv("", 0);
const xmlChar *encoding = NULL;
xmlCharEncodingHandlerPtr encoder = NULL;
xmlDocPtr doc = (xmlDocPtr)x_PmmSvNode( sv_doc );
CODE:
XSLT_GET_IMPORT_PTR(encoding, self, encoding)
if (encoding != NULL) {
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
}
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
output = xmlOutputBufferCreateIO(
(xmlOutputWriteCallback) LibXSLT_iowrite_scalar,
(xmlOutputCloseCallback) LibXSLT_ioclose_scalar,
(void*)results,
encoder
);
if (xsltSaveResultTo(output, doc, self) == -1) {
croak("output to scalar failed");
}
xmlOutputBufferClose(output);
if (xmlStrEqual(encoding, (const xmlChar *) "UTF-8"))
SvUTF8_on( results );
RETVAL = results;
OUTPUT:
RETVAL
void
output_fh(self, sv_doc, fh)
xsltStylesheetPtr self
SV * sv_doc
SV * fh
PREINIT:
xmlOutputBufferPtr output;
const xmlChar *encoding = NULL;
xmlCharEncodingHandlerPtr encoder = NULL;
xmlDocPtr doc = (xmlDocPtr)x_PmmSvNode( sv_doc );
CODE:
XSLT_GET_IMPORT_PTR(encoding, self, encoding)
if (encoding != NULL) {
encoder = xmlFindCharEncodingHandler((char *)encoding);
if ((encoder != NULL) &&
(xmlStrEqual((const xmlChar *)encoder->name,
(const xmlChar *) "UTF-8")))
encoder = NULL;
}
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
output = xmlOutputBufferCreateIO(
(xmlOutputWriteCallback) LibXSLT_iowrite_fh,
(xmlOutputCloseCallback) LibXSLT_ioclose_fh,
(void*)fh,
encoder
);
if (xsltSaveResultTo(output, doc, self) == -1) {
croak("output to fh failed");
}
xmlOutputBufferClose(output);
void
output_file(self, sv_doc, filename)
xsltStylesheetPtr self
SV * sv_doc
char * filename
PREINIT:
xmlDocPtr doc = (xmlDocPtr)x_PmmSvNode( sv_doc );
CODE:
if (LibXSLT_debug_cb && SvTRUE(LibXSLT_debug_cb)) {
xsltSetGenericDebugFunc(PerlIO_stderr(), (xmlGenericErrorFunc)LibXSLT_debug_handler);
}
else {
xsltSetGenericDebugFunc(NULL, NULL);
}
xsltSaveResultToFilename(filename, doc, self, 0);
char *
media_type(self)
xsltStylesheetPtr self
CODE:
RETVAL = (char *)self->mediaType;
if (RETVAL == NULL) {
/* OK, that was borked. Try finding xsl:output tag manually... */
xmlNodePtr root = xmlDocGetRootElement(self->doc);
xmlNodePtr cld = root->children;
while ( cld != NULL ) {
if ( xmlStrcmp( "output", cld->name ) == 0
&& cld->ns != NULL
&& xmlStrcmp( "http://www.w3.org/1999/XSL/Transform", cld->ns->href ) == 0 )
{
break;
}
cld = cld->next;
}
if (cld != NULL) {
RETVAL = xmlGetProp(cld, "media-type");
}
if (RETVAL == NULL) {
RETVAL = "text/xml";
/* this below is rather simplistic, but should work for most cases */
if (self->method != NULL) {
if (strcmp(self->method, "html") == 0) {
RETVAL = "text/html";
}
else if (strcmp(self->method, "text") == 0) {
RETVAL = "text/plain";
}
}
}
}
OUTPUT:
RETVAL
char *
output_encoding(self)
xsltStylesheetPtr self
CODE:
RETVAL = (char *)self->encoding;
if (RETVAL == NULL) {
RETVAL = "UTF-8";
}
OUTPUT:
RETVAL
|