Project

Profile

Help

Bug #4371 ยป php7_saxon.cpp

O'Neil Delpratt, 2020-05-12 15:49

 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php_saxon.h"

#ifdef COMPILE_DL_SAXON
extern "C" {
ZEND_GET_MODULE(saxon)
}
#endif

JNINativeMethod phpMethods[] =
{
{
"_phpCall",
"(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;",
(void *)&phpNativeCall
}
};

zend_object_handlers saxonProcessor_object_handlers;
zend_object_handlers xsltProcessor_object_handlers;
zend_object_handlers xslt30Processor_object_handlers;
zend_object_handlers xqueryProcessor_object_handlers;
zend_object_handlers xpathProcessor_object_handlers;
zend_object_handlers schemaValidator_object_handlers;
zend_object_handlers xdmValue_object_handlers;
zend_object_handlers xdmItem_object_handlers;
zend_object_handlers xdmNode_object_handlers;
zend_object_handlers xdmAtomicValue_object_handlers;

zend_class_entry *saxonProcessor_ce;
zend_class_entry *xsltProcessor_ce;
zend_class_entry *xslt30Processor_ce;
zend_class_entry *xqueryProcessor_ce;
zend_class_entry *xpathProcessor_ce;
zend_class_entry *schemaValidator_ce;
zend_class_entry *xdmValue_ce;
zend_class_entry *xdmItem_ce;
zend_class_entry *xdmNode_ce;
zend_class_entry *xdmAtomicValue_ce;

void SaxonProcessor_free_storage(zend_object *object)
{
saxonProcessor_object *obj;
obj = (saxonProcessor_object *)((char *)object - XtOffsetOf(saxonProcessor_object, std));

SaxonProcessor * saxonProc= obj->saxonProcessor;
if(saxonProc != NULL) {
delete saxonProc;
}
zend_object_std_dtor(object);

// efree(obj);
}

void SaxonProcessor_destroy_storage(zend_object *pobj)
{

//php_error(E_WARNING,"_destroy_Storage called php-saxonProc");
// saxonProcessor_object *obj;
//zend_object* pobj = Z_OBJ_P(getThis());
/* saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

SaxonProcessor * saxonProc= obj->saxonProcessor;
if(saxonProc != NULL) {
SaxonProcessor::release();
delete saxonProc;
}*/
zend_objects_destroy_object(pobj);

}

zend_object *saxonProcessor_create_handler(zend_class_entry *type)
{
zval *tmp;
zend_object retval;
saxonProcessor_object *obj = (saxonProcessor_object *)ecalloc(1, sizeof(saxonProcessor_object) + zend_object_properties_size(type));

zend_object_std_init(&obj->std, type); /* take care of the zend_object also ! */
object_properties_init(&obj->std, type);

obj->std.handlers = &saxonProcessor_object_handlers;

return &obj->std;
}

PHP_METHOD(SaxonProcessor, __construct)
{
if (ZEND_NUM_ARGS()>2) {
WRONG_PARAM_COUNT;
}

char * cwdi = NULL;
bool license = false;
size_t len1;
if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS(), "b", &license) == FAILURE) {
RETURN_NULL();
}


if (ZEND_NUM_ARGS()>1 && zend_parse_parameters(ZEND_NUM_ARGS(), "bs", &license, &cwdi, &len1) == FAILURE) {
RETURN_NULL();
}


zval *object = getThis();
SaxonProcessor * saxonProc = NULL;
zend_object * zobj = Z_OBJ_P(object);

saxonProcessor_object * obj = (saxonProcessor_object *)((char *)zobj - XtOffsetOf(saxonProcessor_object, std));

saxonProc = new SaxonProcessor(license); //TODO: add license flag to PHP function argument
obj->saxonProcessor = saxonProc;
//std::cerr<<"SaxonProcessorConstructor cp1"<<std::endl;
if(cwdi==NULL) {
//std::cerr<<"SaxonProcessorConstructor cp1-1"<<std::endl;
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
TCHAR s[256];

// --
DWORD a = GetCurrentDirectory(256, s);
const size_t newsize = wcslen(s)*2;
char* cwd = new char[newsize];
wcstombs_s(0, cwd, newsize, s, _TRUNCATE);
// -- code above returns the apache installtion directory as the CWD

char* cwd2;

//php_error(E_WARNING,cwd2);

saxonProc->setcwd(cwd);
// -- code above tries to use VCWD_GETCWD but there is a linkage error
#else
char cwd[256];

VCWD_GETCWD(cwd, sizeof(cwd));
if(cwd == NULL) {
//php_error(E_WARNING,"cwd is nullXXXXXXXXXXXXXXXXXXXXXXX");
}else {
// php_error(E_WARNING,cwd);
saxonProc->setcwd(cwd);

}
#endif

} else {
saxonProc->setcwd(cwdi);
}

}

PHP_METHOD(SaxonProcessor, __destruct)
{
//php_error(E_WARNING,"__destruct called php-saxonProc");
//std::cerr<<"__destruct call saxonProc"<<std::endl;
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

SaxonProcessor * saxonProc= obj->saxonProcessor;
if(saxonProc != NULL) {
//SaxonProcessor::release();
//delete saxonProc;
}
}


PHP_METHOD(SaxonProcessor, release)
{
//php_error(E_WARNING,"__destruct called php-saxonProc");
// std::cerr<<"__destruct call saxonProc"<<std::endl;
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

SaxonProcessor * saxonProc= obj->saxonProcessor;
if(saxonProc != NULL) {
SaxonProcessor::release();
}
}

PHP_METHOD(SaxonProcessor, setResourcesDirectory)
{
SaxonProcessor *saxonProcessor;
char * dirStr;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &dirStr, &len) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
if(dirStr != NULL) {
saxonProcessor->setResourcesDirectory(dirStr);
}
}
}


PHP_METHOD(SaxonProcessor, isSchemaAware)
{
SaxonProcessor *saxonProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
if(saxonProcessor->isSchemaAwareProcessor()){

RETURN_TRUE;
}
}
RETURN_FALSE;
}

PHP_METHOD(SaxonProcessor, setCatalog)
{
SaxonProcessor *saxonProcessor;
char * catalogFile;
size_t len;
bool trace = false;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "sb", &catalogFile, &len, &trace) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
if(catalogFile != NULL) {
saxonProcessor->setCatalog(catalogFile, trace);
}
}
}



PHP_METHOD(SaxonProcessor, setcwd)
{
SaxonProcessor *saxonProcessor;
char * cwdStr;
size_t len;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &cwdStr, &len) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
if(cwdStr != NULL) {
saxonProcessor->setcwd(cwdStr);
}
}
}


PHP_METHOD(SaxonProcessor, parseXmlFromString)
{
SaxonProcessor * saxonProcessor;
char * source;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &source, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
assert (obj != NULL);
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
XdmNode* node = saxonProcessor->parseXmlFromString(source);
if(node != NULL) {
if (object_init_ex(return_value, xdmNode_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object* vobj = Z_OBJ_P(return_value);
xdmNode_object * xdmNObj = (xdmNode_object *)((char *)vobj - XtOffsetOf(xdmNode_object, std));
assert (xdmNObj != NULL);
xdmNObj->xdmNode = node;
}
} else {
if(obj->saxonProcessor->exceptionOccurred()){
//TODO throw exception
}
}
} else {
RETURN_NULL();
}
}

PHP_METHOD(SaxonProcessor, parseXmlFromFile)
{
SaxonProcessor * saxonProcessor;
char * source;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &source, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
assert (obj != NULL);
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
XdmNode* node = saxonProcessor->parseXmlFromFile(source);//TODO this needs to be XdmNode object
if(node != NULL) {
if (object_init_ex(return_value, xdmNode_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object* vvobj = Z_OBJ_P(return_value);
xdmNode_object * vobj = (xdmNode_object *)((char *)vvobj - XtOffsetOf(xdmNode_object, std));
assert (vobj != NULL);
vobj->xdmNode = node;
}
} else {
// obj->xsltProcessor->checkException();//TODO
}
} else {
// std::cerr<<"parseXmlFromFile cp2"<<std::endl;
RETURN_NULL();
}
// std::cerr<<"parseXmlFromFile cp3"<<std::endl;
}


PHP_METHOD(SaxonProcessor, createAtomicValue)
{
XdmAtomicValue * xdmValue = NULL;
SaxonProcessor * proc;
char * source;
int len1;
zval *zvalue;
bool bVal;
char * sVal;
int len;
long iVal;
double dVal;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "z",&zvalue) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
assert (obj != NULL);
proc = obj->saxonProcessor;
assert (proc != NULL);
if (proc != NULL) {
switch (Z_TYPE_P(zvalue)) {
case IS_FALSE:
xdmValue = proc->makeBooleanValue(false);
case IS_TRUE:
xdmValue = proc->makeBooleanValue(true);
break;
case IS_LONG:
iVal = Z_LVAL_P(zvalue);
xdmValue = proc->makeIntegerValue((int)iVal);
break;
case IS_STRING:
sVal = Z_STRVAL_P(zvalue);
len = Z_STRLEN_P(zvalue);
xdmValue = proc->makeStringValue((const char*)sVal);
//efree(sVal);
break;
case IS_NULL:
xdmValue = new XdmAtomicValue();
break;
case IS_DOUBLE:
dVal = (double)Z_DVAL_P(zvalue);
xdmValue = proc->makeDoubleValue((double)iVal);
break;
case IS_ARRAY:
// TODO: Should not be expected. Do this some other way
//break;
case IS_OBJECT:
// TODO: implement this
//break;
default:
obj = NULL;
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "unknown type specified in XdmValue", 0 TSRMLS_CC);
RETURN_NULL();
}
//std::cerr<<"createAtomicValue cp0"<<std::endl;
if(xdmValue == NULL) {
RETURN_NULL();
}
if (object_init_ex(return_value, xdmAtomicValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct xdmAtomicValue_object* vobj = (struct xdmAtomicValue_object *)Z_OBJ_P(return_value);
zend_object* vvobj = Z_OBJ_P(return_value);
xdmAtomicValue_object * vobj = (xdmAtomicValue_object *)((char *)vvobj - XtOffsetOf(xdmAtomicValue_object, std));
assert (vobj != NULL);
vobj->xdmAtomicValue = xdmValue;
}
} else {
RETURN_NULL();
}
}


PHP_METHOD(SaxonProcessor, newXPathProcessor)
{
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
SaxonProcessor * proc;
XPathProcessor * xpathProcessor = NULL;

zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

assert (obj != NULL);
proc = obj->saxonProcessor;
assert (proc != NULL);
if (proc != NULL) {
if (object_init_ex(return_value, xpathProcessor_ce) != SUCCESS) {
RETURN_NULL();
} else {
// struct xpathProcessor_object* vobji = (struct xpathProcessor_object *)Z_OBJ_P(return_value TSRMLS_CC);
zend_object* vvobj = Z_OBJ_P(return_value);
xpathProcessor_object * vobj = (xpathProcessor_object *)((char *)vvobj - XtOffsetOf(xpathProcessor_object, std));
assert (vobj != NULL);
xpathProcessor = proc->newXPathProcessor();
vobj->xpathProcessor = xpathProcessor;
}
} else {
RETURN_NULL();
}
}

PHP_METHOD(SaxonProcessor, newXsltProcessor)
{
//std::cerr<<"SaxonProcessor newXsltproc point 1"<<std::endl;
//php_error(E_WARNING,"new xsltProc 1");
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
SaxonProcessor * proc;
XsltProcessor * xsltProcessor = NULL;

zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

// saxonProcessor_object *obj = (saxonProcessor_object *)Z_OBJ_P(getThis());
assert (obj != NULL);
proc = obj->saxonProcessor;

assert (proc != NULL);
if (proc != NULL) {
if (object_init_ex(return_value, xsltProcessor_ce) != SUCCESS) {
RETURN_NULL();
} else {
xsltProcessor = proc->newXsltProcessor();
zend_object* vobj = Z_OBJ_P(return_value);
xsltProcessor_object * xproc_object = (xsltProcessor_object *)((char *)vobj - XtOffsetOf(xsltProcessor_object, std));
assert (vobj != NULL);
xproc_object->xsltProcessor = xsltProcessor;

}
} else {
RETURN_NULL();
}
}


PHP_METHOD(SaxonProcessor, newXslt30Processor)
{
//std::cerr<<"SaxonProcessor newXslt30proc point 1"<<std::endl;
// php_error(E_WARNING,"new xslt30Proc 1");
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
SaxonProcessor * proc;
Xslt30Processor * xslt30Processor = NULL;

zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

// saxonProcessor_object *obj = (saxonProcessor_object *)Z_OBJ_P(getThis());
assert (obj != NULL);
proc = obj->saxonProcessor;

assert (proc != NULL);
if (proc != NULL) {
if (object_init_ex(return_value, xslt30Processor_ce) != SUCCESS) {
RETURN_NULL();
} else {
xslt30Processor = proc->newXslt30Processor();
zend_object* vobj = Z_OBJ_P(return_value);
xslt30Processor_object * xproc_object = (xslt30Processor_object *)((char *)vobj - XtOffsetOf(xslt30Processor_object, std));
assert (vobj != NULL);

xproc_object->xslt30Processor = xslt30Processor;

}
} else {

RETURN_NULL();
}
}

PHP_METHOD(SaxonProcessor, newXQueryProcessor)
{
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
SaxonProcessor * proc;
XQueryProcessor * xqueryProcessor = NULL;
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
assert (obj != NULL);
proc = obj->saxonProcessor;
assert (proc != NULL);
if (proc != NULL) {
if (object_init_ex(return_value, xqueryProcessor_ce) != SUCCESS) {
RETURN_NULL();
} else {
xqueryProcessor = proc->newXQueryProcessor();
zend_object* vvobj = Z_OBJ_P(return_value);
xqueryProcessor_object * vobj = (xqueryProcessor_object *)((char *)vvobj - XtOffsetOf(xqueryProcessor_object, std));
assert (vvobj != NULL);
vobj->xqueryProcessor = xqueryProcessor;
}
} else {
RETURN_NULL();
}
}

PHP_METHOD(SaxonProcessor, newSchemaValidator)
{
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
SaxonProcessor * proc;
SchemaValidator * schemaValidator = NULL;
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
assert (obj != NULL);
proc = obj->saxonProcessor;
assert (proc != NULL);
if (proc != NULL) {
if (object_init_ex(return_value, schemaValidator_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct schemaValidator_object* vobj = (struct schemaValidator_object *)Z_OBJ_P(return_value TSRMLS_CC);
zend_object* vvobj = Z_OBJ_P(return_value);
schemaValidator_object * vobj = (schemaValidator_object *)((char *)vvobj - XtOffsetOf(schemaValidator_object, std));
assert (vobj != NULL);
schemaValidator = proc->newSchemaValidator();
if(schemaValidator == NULL){
RETURN_NULL();
}
vobj->schemaValidator = schemaValidator;
}
} else {
RETURN_NULL();
}
}


PHP_METHOD(SaxonProcessor, version)
{
SaxonProcessor *saxonProcessor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));
saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL) {
const char * verStr = saxonProcessor->version();
//char *str = estrdup(saxonProcessor->version());
_RETURN_STRING(verStr);
//efree(verStr);
}
RETURN_NULL();
}



PHP_METHOD(SaxonProcessor, setConfigurationProperty)
{
SaxonProcessor *saxonProcessor;
char * name;
char * value;
size_t len1, len2;
if (ZEND_NUM_ARGS()!= 2) {
WRONG_PARAM_COUNT;
}

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &name, &len1, &value, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL && name != NULL && value != NULL) {
saxonProcessor->setConfigurationProperty(name, value);
}
}

PHP_METHOD(SaxonProcessor, registerPHPFunctions)
{
SaxonProcessor *saxonProcessor;
char * libName;
size_t len1;
//std::cerr<<"checkpoint in registerPHPFunction start"<<std::endl;
if (ZEND_NUM_ARGS()!= 1) {
WRONG_PARAM_COUNT;
}

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &libName, &len1) == FAILURE) {
RETURN_NULL();
}
//std::cerr<<"checkpoint in registerPHPFunction cp2"<<std::endl;
zend_object* pobj = Z_OBJ_P(getThis());
saxonProcessor_object * obj = (saxonProcessor_object *)((char *)pobj - XtOffsetOf(saxonProcessor_object, std));

saxonProcessor = obj->saxonProcessor;
if (saxonProcessor != NULL && libName != NULL) {
saxonProcessor->setConfigurationProperty("extc", libName);
}
saxonProcessor->registerNativeMethods(SaxonProcessor::sxn_environ->env, "com/saxonica/functions/extfn/cpp/PHPFunctionSet$PHPFunction",
phpMethods, sizeof(phpMethods) / sizeof(phpMethods[0]));
// std::cerr<<"checkpoint in registerPHPFunction end"<<std::endl;
}

/* ============== XSLT10/20/30/31: PHP Interface of XsltProcessor class =============== */

void XsltProcessor_free_storage(zend_object *object)
{

zend_object_std_dtor(object);
}

void XsltProcessor_destroy_storage(zend_object *object)
{
xsltProcessor_object *obj;

zend_objects_destroy_object(object);
}

zend_object * xsltProcessor_create_handler(zend_class_entry *type)
{

xsltProcessor_object *obj = (xsltProcessor_object *)ecalloc(1, sizeof(xsltProcessor_object)+ zend_object_properties_size(type));
zend_object_std_init(&obj->std,type);
object_properties_init(&obj->std, type);
obj->std.handlers = &xsltProcessor_object_handlers;

return &obj->std;
}



PHP_METHOD(XsltProcessor, __destruct)
{

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));


XsltProcessor * xsltProcessor= obj->xsltProcessor;
if(xsltProcessor != NULL){
delete xsltProcessor;
}
}

PHP_METHOD(XsltProcessor, transformFileToFile)
{
XsltProcessor *xsltProcessor;
char * outfileName;
char * infilename;
char * styleFileName;
size_t len1, len2, len3;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "sss", &infilename, &len1, &styleFileName, &len2, &outfileName, &len3) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {

xsltProcessor->transformFileToFile(infilename, styleFileName, outfileName);
if(xsltProcessor->exceptionOccurred()) {
// TODO: throw exception
}
}
}

PHP_METHOD(XsltProcessor, transformFileToValue)
{
XsltProcessor *xsltProcessor;
char * infilename;
char * styleFileName;
size_t len1, len2;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &infilename, &len1, &styleFileName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {

XdmValue * node = xsltProcessor->transformFileToValue(infilename, styleFileName);
if(node != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object *ooth = Z_OBJ_P(return_value);
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value TSRMLS_CC);
xdmValue_object* vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = node;
}
} else {
if(obj->xsltProcessor->exceptionOccurred()){
//TODO
}
}
}else {
RETURN_NULL();
}
}


PHP_METHOD(XsltProcessor, transformFileToString)
{
XsltProcessor *xsltProcessor;
char * infilename;
char * styleFileName;
size_t len1, len2;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &infilename, &len1, &styleFileName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;

if (xsltProcessor != NULL) {

const char * result = xsltProcessor->transformFileToString(infilename, styleFileName);
if(result != NULL) {
//char *str = estrdup(result);
_RETURN_STRING(result);
} else if(xsltProcessor->exceptionOccurred()){
//TODO: xsltProcessor->checkException();
const char * errStr = xsltProcessor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xsltProcessor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}
}

/*enum saxonTypeEnum
{
enumNode,
enumString,
enumInteger,
enumDouble,
enumFloat,
enumBool,
enumArrXdmValue
};*/

jobject JNICALL phpNativeCall
(JNIEnv *env, jobject object, jstring funcName, jobjectArray arguments, jobjectArray argTypes){
JNIEnv *senv = SaxonProcessor::sxn_environ->env;
// std::cerr<<"phpNative called"<<std::endl;
char *nativeString = (char *)senv->GetStringUTFChars(funcName, NULL);
std::string nativeString2 = std::string(nativeString);
if(nativeString == NULL) {
return NULL;
}
int nativeStrLen = strlen(nativeString);
//std::cerr<<"phpNative called. nativeString="<<nativeString<<", length="<<nativeStrLen<<std::endl;
zval function_name;
zval retval;
int argLength = 0;
zvalArr * php_argv= NULL;
if(arguments != NULL) {
argLength = (int)senv->GetArrayLength(arguments);
php_argv = new zvalArr[argLength];
}
zval *params;
if(argLength>0) {
//(*params) = (zval**)malloc(sizeof(zval*) * argLength);
params = new zval[argLength];
} else {
params = NULL;
}
std::map<std::string, saxonTypeEnum> typeMap;
typeMap["node"] = enumNode;
typeMap["string"] = enumString;
typeMap["integer"] = enumInteger;
typeMap["double"] = enumDouble;
typeMap["float"] = enumFloat;
typeMap["boolean"] = enumBool;
typeMap["[xdmvalue"] = enumArrXdmValue;
sxnc_value* sresult = (sxnc_value *)malloc(sizeof(sxnc_value));
SaxonProcessor * nprocessor = new SaxonProcessor(true); //processor object created for XdmNode
jclass xdmValueForCppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp");
jmethodID procForNodeMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmValueForCppClass, "getProcessor", "(Ljava/lang/Object;)Lnet/sf/saxon/s9api/Processor;");
//std::cerr<<"phpNative called cp2"<<std::endl;

for(int i=0; i<argLength;i++){

jstring argType = (jstring)senv->GetObjectArrayElement(argTypes, i);
jobject argObj = senv->GetObjectArrayElement(arguments, i);

const char * str = senv->GetStringUTFChars(argType,NULL);
const char *stri = NULL;
double dnumber = 0;
long lnumber = 0;
bool bvalue = false;
float fnumber = 0;

struct xdmNode_object* vobj;
zend_object* zend_vobj;
XdmNode * node = NULL;
std::map<std::string, saxonTypeEnum>::iterator it = typeMap.find(str);
if (it != typeMap.end()){
switch (it->second)
{
case enumNode:
if(!nprocessor->proc){
nprocessor->proc = (jobject)SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmValueForCppClass, procForNodeMID, argObj);
}
if (object_init_ex(php_argv[i]._val, xdmNode_ce) != SUCCESS) {
//error
//php_error(E_WARNING,"error phpNative xdmNode creation failed");
break;
}
node = new XdmNode(argObj);
node->setProcessor(nprocessor);

//MAKE_STD_ZVAL(php_argv[i]._val);
zend_vobj = Z_OBJ_P(php_argv[i]._val);
vobj = (xdmNode_object *)((char *)zend_vobj - XtOffsetOf(xdmNode_object, std));
assert (vobj != NULL);
vobj->xdmNode = node;
break;
case enumString:
stri = senv->GetStringUTFChars((jstring)argObj, NULL);
//ZVAL_STRING(php_argv[i]._val);
_ZVAL_STRING(php_argv[i]._val, stri);
break;
case enumInteger:
sresult->xdmvalue = argObj;
lnumber = getLongValue(SaxonProcessor::sxn_environ, *sresult, 0);

//MAKE_STD_ZVAL(php_argv[i]._val);
ZVAL_LONG(php_argv[i]._val, lnumber);
break;
case enumDouble:
sresult->xdmvalue = argObj;
dnumber = getDoubleValue(SaxonProcessor::sxn_environ, *sresult, 0);
//MAKE_STD_ZVAL(php_argv[i]._val);
ZVAL_DOUBLE(php_argv[i]._val, dnumber);
break;
case enumFloat:
sresult->xdmvalue = argObj;
fnumber = getFloatValue(SaxonProcessor::sxn_environ, *sresult, 0);
//MAKE_STD_ZVAL(php_argv[i]._val);
ZVAL_DOUBLE(php_argv[i]._val, fnumber);
break;
case enumBool:
sresult->xdmvalue = argObj;
bvalue = getBooleanValue(SaxonProcessor::sxn_environ, *sresult);
//MAKE_STD_ZVAL(php_argv[i]._val);
ZVAL_BOOL(php_argv[i]._val, bvalue);
break;
case enumArrXdmValue:
//TODO - not currently supported
argLength--;
break;
}
senv->ReleaseStringUTFChars(argType, str);
}

}

//TODO should free sresult but it causes memory corruption
// array of zvals to execute
//std::cerr<<"phpNative called cp3"<<std::endl;
// convert all the values
for(int i = 0; i < argLength; i++) {

params[i] = *php_argv[i]._val; }

//std::cerr<<"phpNative called cp3-1"<<std::endl;
//note: no implicit type conversion.

zval *argvv = NULL;//xparams;
zval* callOnObj = NULL;
//MAKE_STD_ZVAL(function_name);
//nativeString[nativeStrLen] = '\0';

ZVAL_STRING(&function_name, nativeString);
//std::cerr<<"phpNative called cp3-2, argumentLen="<<argLength<<std::endl;
if(call_user_function_ex(CG(function_table), (zval*)callOnObj, &function_name, &retval, argLength, params, 0, NULL) != SUCCESS)
{
zend_error(E_ERROR, "Function call failed");
}

if(Z_TYPE(retval) ==0){
zend_error(E_ERROR, "Function returned null");
}

char * sVal = NULL;
int len = 0;
jobject obj = NULL;
//TODO handle XdmValue wrapped object
const char * objName = NULL;
xdmNode_object* ooth = NULL;
zend_object* zend_vobj2;
bool bVal;
switch (Z_TYPE_P(&retval)) {
case IS_FALSE:
obj= booleanValue(SaxonProcessor::sxn_environ, false);
break;
case IS_TRUE:
obj= booleanValue(SaxonProcessor::sxn_environ, true);
break;
case IS_LONG:
obj= longValue(SaxonProcessor::sxn_environ, Z_LVAL_P(&retval));
break;
case IS_STRING:
sVal = Z_STRVAL_P(&retval);
len = Z_STRLEN_P(&retval);
obj = getJavaStringValue(SaxonProcessor::sxn_environ,estrndup(sVal, len));
break;
break;
case IS_NULL:
break;
case IS_DOUBLE:
obj = doubleValue(SaxonProcessor::sxn_environ, (double)Z_DVAL_P(&retval));
break;
case IS_ARRAY:
//break;
case IS_OBJECT:
objName =ZSTR_VAL(Z_OBJCE_P(&retval)->name);

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_vobj2 = Z_OBJ_P(&retval);
ooth = (xdmNode_object *)((char *)zend_vobj2 - XtOffsetOf(xdmNode_object, std));

if(ooth != NULL) {
obj = ooth->xdmNode->getUnderlyingValue();
}
}
break;
default:
obj = NULL;
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unknown type specified in extension function", 0 TSRMLS_CC);
}

//zend_printf("We have %i as type<br>", retval->type);
//*return_value = *retval;
//zval_copy_ctor(return_value);
//zval_ptr_dtor(&retval);
/*int cstrlen = Z_STRLEN_P(retval);
char * str = estrndup(Z_STRVAL_P(retval), cstrlen);
jstring jstrBuf = SaxonProcessor::sxn_environ->env->NewStringUTF(str);*/
zval_ptr_dtor(&retval);
return obj;
}



PHP_METHOD(XsltProcessor, transformToString)
{
XsltProcessor *xsltProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}


zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {

const char * result = xsltProcessor->transformToString();
if(result != NULL) {
_RETURN_STRING(result);
} else if(xsltProcessor->exceptionOccurred()){
xsltProcessor->checkException();
const char * errStr = xsltProcessor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xsltProcessor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}
RETURN_NULL();
}

PHP_METHOD(XsltProcessor, transformToValue)
{
XsltProcessor *xsltProcessor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;

if (xsltProcessor != NULL) {

XdmValue * node = xsltProcessor->transformToValue();
if(node != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value);
zend_object *vvobj = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = node;
}
} else if(xsltProcessor->exceptionOccurred()){
xsltProcessor->checkException();
RETURN_NULL();
}
} else {
RETURN_NULL();
}
}

PHP_METHOD(XsltProcessor, transformToFile)
{
XsltProcessor *xsltProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;

if (xsltProcessor != NULL) {

xsltProcessor->transformToFile();
if(xsltProcessor->exceptionOccurred()) {
//TODO
const char * exStr = xsltProcessor->checkException();
}
} else {
RETURN_NULL();
}
}

PHP_METHOD(XsltProcessor, compileFromFile)
{
XsltProcessor *xsltProcessor;
char * name;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->compileFromFile(name);
}
}

PHP_METHOD(XsltProcessor, compileFromString)
{
XsltProcessor *xsltProcessor;
char * stylesheetStr;
size_t len1, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &stylesheetStr, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->compileFromString(stylesheetStr);
}
}

PHP_METHOD(XsltProcessor, compileFromStringAndSave)
{
XsltProcessor *xsltProcessor;
char * stylesheetStr;
char * filename;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &stylesheetStr, &filename, &len1, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->compileFromStringAndSave(stylesheetStr, filename);
}
}

PHP_METHOD(XsltProcessor, compileFromFileAndSave)
{
XsltProcessor *xsltProcessor;
char * stylesheetFilename;
char * filename;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &stylesheetFilename, &filename, &len1, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->compileFromStringAndSave(stylesheetFilename, filename);
}
}



PHP_METHOD(XsltProcessor, compileFromValue)
{
XsltProcessor *xsltProcessor;
zval* oth;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "O", &oth, xdmValue_ce) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
zend_object* vvobj = Z_OBJ_P(oth);
xdmValue_object* ooth = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
if(ooth != NULL) {
XdmValue * value = ooth->xdmValue;
if(value != NULL && value->size() == 1 && (value->getHead())->getType() == 3) {
xsltProcessor->compileFromXdmNode((XdmNode*)(value->getHead()));
}
}
}
}




PHP_METHOD(XsltProcessor, setSourceFromXdmValue)
{
XsltProcessor *xsltProcessor;
zval* oth = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {

if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value");
return;
}
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
xsltProcessor->setSourceFromXdmNode(value);

}
}
} /*else if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object* vvobj = Z_OBJ_P(oth);
xdmValue_object* ooth = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
if(ooth != NULL) {
XdmValue * value = ooth->xdmValue;
if(value != NULL && value) {
xsltProcessor->setSourceFromXdmNode((XdmNode*)value);
}
}
} */

}
}
}


PHP_METHOD(XsltProcessor, setJustInTimeCompilation)
{
XsltProcessor *xsltProcessor;
bool jit = false;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "b", &jit) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->setJustInTimeCompilation(jit);
}
}

PHP_METHOD(XsltProcessor, setOutputFile)
{
XsltProcessor *xsltProcessor;
char * outputFilename;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &outputFilename, &len1) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL && outputFilename != NULL) {
xsltProcessor->setOutputFile(outputFilename);
}
}

int size_t2int(size_t val) {
return (val <= INT_MAX) ? (int)((ssize_t)val) : -1;
}

PHP_METHOD(XsltProcessor, setSourceFromFile)
{
XsltProcessor *xsltProcessor;
char * inFilename;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &inFilename, &len1) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL && inFilename != NULL) {
xsltProcessor->setSourceFromFile(inFilename);
}
}


PHP_METHOD(XsltProcessor, setProperty)
{
XsltProcessor *xsltProcessor;
char * name;
char * value;
size_t len1, len2, myint;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &name, &len1, &value, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->setProperty(name, value);
}
}

PHP_METHOD(XsltProcessor, setParameter)
{

XsltProcessor *xsltProcessor;
char * name;
zval* oth;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "sz", &name, &len2, &oth) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value - value is null");
return;
}

const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(oth);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmNode * value = nobj->xdmNode;
if(value != NULL) {
xsltProcessor->setParameter(name, (XdmValue *)value);

}
}
} else if(strcmp(objName, "Saxon\\XdmValue")==0){
zend_object* ooth = Z_OBJ_P(oth);
xdmValue_object * vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(vobj != NULL) {
XdmValue * value = vobj->xdmValue;
if(value != NULL) {
xsltProcessor->setParameter(name, value);
}
}



} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0){
zend_object* ooth = Z_OBJ_P(oth);
xdmAtomicValue_object * aobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(aobj != NULL) {
XdmAtomicValue * value = aobj->xdmAtomicValue;
if(value != NULL) {
xsltProcessor->setParameter(name, (XdmValue *)value);
}
}



}

}
}

PHP_METHOD(XsltProcessor, clearParameters)
{
XsltProcessor *xsltProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->clearParameters(true);
}
}

PHP_METHOD(XsltProcessor, clearProperties)
{
XsltProcessor *xsltProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->clearProperties();
}
}



PHP_METHOD(XsltProcessor, getXslMessages)
{
XsltProcessor *xsltProcessor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;

if (xsltProcessor != NULL) {

XdmValue * values = xsltProcessor->getXslMessages();
if(values != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value);
zend_object *vvobj = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));


assert (vobj != NULL);
vobj->xdmValue = values;
}
} else if(xsltProcessor->exceptionOccurred()){
xsltProcessor->checkException();
RETURN_NULL();
}
} else {
RETURN_NULL();
}
}



PHP_METHOD(XsltProcessor, exceptionOccurred)
{
XsltProcessor *xsltProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
bool result = xsltProcessor->exceptionOccurred();
RETURN_BOOL(result);
}
RETURN_BOOL(false);
}

PHP_METHOD(XsltProcessor, getExceptionCount)
{
XsltProcessor *xsltProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
int count = xsltProcessor->exceptionCount();
RETURN_LONG(count);
}
RETURN_LONG(0);
}

PHP_METHOD(XsltProcessor, getErrorCode)
{
XsltProcessor *xsltProcessor;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
const char * errCode = xsltProcessor->getErrorCode((int)index);
if(errCode != NULL) {
//char *str = estrdup(errCode);
_RETURN_STRING(errCode);
}
}
RETURN_NULL();
}

PHP_METHOD(XsltProcessor, getErrorMessage)
{
XsltProcessor *xsltProcessor;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
const char * errStr = xsltProcessor->getErrorMessage((int)index);
if(errStr != NULL) {
//char *str = estrdup(errStr);
_RETURN_STRING(errStr);
}
}
RETURN_NULL();
}
PHP_METHOD(XsltProcessor, exceptionClear)
{
XsltProcessor *xsltProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xsltProcessor_object *obj = (xsltProcessor_object *)((char *)pobj - XtOffsetOf(xsltProcessor_object, std));
xsltProcessor = obj->xsltProcessor;
if (xsltProcessor != NULL) {
xsltProcessor->exceptionClear();
}
}



/* ============== XSLT30: PHP Interface of Xslt30Processor class =============== */

void Xslt30Processor_free_storage(zend_object *object)
{

zend_object_std_dtor(object);

}

void Xslt30Processor_destroy_storage(zend_object *object)
{
xslt30Processor_object *obj;

zend_objects_destroy_object(object);
}

zend_object * xslt30Processor_create_handler(zend_class_entry *type)
{


xslt30Processor_object *obj = (xslt30Processor_object *)ecalloc(1, sizeof(xslt30Processor_object)+ zend_object_properties_size(type));


zend_object_std_init(&obj->std,type);
object_properties_init(&obj->std, type);

obj->std.handlers = &xslt30Processor_object_handlers;

return &obj->std;
}



PHP_METHOD(Xslt30Processor, __destruct)
{

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));


Xslt30Processor * xslt30Processor= obj->xslt30Processor;
if(xslt30Processor != NULL){
delete xslt30Processor;
}


}



PHP_METHOD(Xslt30Processor, callFunctionReturningValue)
{
//arguments: const char * stylesheetFilename, const char* functionName, XdmValue * arguments, int argument_length

Xslt30Processor *xslt30Processor;
HashTable *arr_hash;
char* functionName;
zval * arguments_zval;
zval * val;
long num_key;
XdmValue ** arguments = NULL;
int argument_length=0;
zend_string *key;

char * infilename;
char * styleFileName = NULL;
size_t len1, len2;

if (ZEND_NUM_ARGS()==2 && zend_parse_parameters(ZEND_NUM_ARGS() , "sa", &functionName, &len2, &arguments_zval) == FAILURE) {
RETURN_NULL();
} else if (ZEND_NUM_ARGS()==3 && zend_parse_parameters(ZEND_NUM_ARGS() , "ssa", &styleFileName, &len1, &functionName, &len2, &arguments_zval) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
arr_hash = Z_ARRVAL_P(arguments_zval);
argument_length = zend_hash_num_elements(arr_hash);
arguments = new XdmValue*[argument_length];
if (xslt30Processor != NULL) {
ZEND_HASH_FOREACH_KEY_VAL(arr_hash, num_key, key, val) {
if(Z_TYPE_P(val) != NULL) {
const char * objName =ZSTR_VAL(Z_OBJCE_P(val)->name);
/*std::cerr<<"test type:"<<(Z_OBJCE_P(val)->name)<<std::endl;
php_printf("num_key %d =>", num_key);
php_printf("key %d =>", key);
*/
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmNode;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmAtomicValue_object * nobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmAtomicValue;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmValue_object * nobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(nobj != NULL) {
XdmValue * value = nobj->xdmValue;
arguments[num_key] = value;
}
}
else {//TODO error warning}
}}else {
//TODO error warning
}
} ZEND_HASH_FOREACH_END();


//php_printf(" argument_length= %d",argument_length);
XdmValue * result = xslt30Processor->callFunctionReturningValue(styleFileName, functionName, arguments, argument_length);
if(result != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object *ooth = Z_OBJ_P(return_value);
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value TSRMLS_CC);
xdmValue_object* vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = result;
}
} else {
if(obj->xslt30Processor->exceptionOccurred()){
//TODO
}
RETURN_NULL();
}
}


}

PHP_METHOD(Xslt30Processor, callFunctionReturningString){
Xslt30Processor *xslt30Processor;
HashTable *arr_hash;
char* functionName;
zval * arguments_zval;
zval * val;
long num_key;
XdmValue ** arguments = NULL;
int argument_length=0;
zend_string *key;

char * infilename;
char * styleFileName = NULL;
size_t len1, len2;

if (ZEND_NUM_ARGS()==2 && zend_parse_parameters(ZEND_NUM_ARGS() , "sa", &functionName, &len2, &arguments_zval) == FAILURE) {
RETURN_NULL();
}else if (ZEND_NUM_ARGS()==3 && zend_parse_parameters(ZEND_NUM_ARGS() , "ssa", &styleFileName, &len1, &functionName, &len2, &arguments_zval) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
arr_hash = Z_ARRVAL_P(arguments_zval);
argument_length = zend_hash_num_elements(arr_hash);
arguments = new XdmValue*[argument_length];
if (xslt30Processor != NULL) {
ZEND_HASH_FOREACH_KEY_VAL(arr_hash, num_key, key, val) {
if(Z_TYPE_P(val) != NULL) {
const char * objName =ZSTR_VAL(Z_OBJCE_P(val)->name);
/*std::cerr<<"test type:"<<(Z_OBJCE_P(val)->name)<<std::endl;
php_printf("num_key %d =>", num_key);
php_printf("key %d =>", key);
*/
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmNode;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmAtomicValue_object * nobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmAtomicValue;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmValue_object * nobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(nobj != NULL) {
XdmValue * value = nobj->xdmValue;
arguments[num_key] = value;
}
}
else {//TODO error warning}
}}else {
//TODO error warning
}
} ZEND_HASH_FOREACH_END();

const char * result = xslt30Processor->callFunctionReturningString(styleFileName, functionName, arguments, argument_length);
if(result != NULL) {
//char *str = estrdup(result);
_RETURN_STRING(result);
} else if(xslt30Processor->exceptionOccurred()){
//TODO: xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}



}


PHP_METHOD(Xslt30Processor, callFunctionReturningFile){
HashTable *arr_hash;
Xslt30Processor *xslt30Processor;
char* functionName;
zval * arguments_zval;
zval * val;
long num_key;
XdmValue ** arguments = NULL;
int argument_length=0;
zend_string *key;

char * outfilename;
char * styleFileName = NULL;
size_t len1, len2, len3;


if (ZEND_NUM_ARGS()==3 && zend_parse_parameters(ZEND_NUM_ARGS() , "sas", &functionName, &len2, &arguments_zval, &outfilename, &len3) == FAILURE) {
RETURN_NULL();
} else if (ZEND_NUM_ARGS()==4 && zend_parse_parameters(ZEND_NUM_ARGS() , "ssas", &styleFileName, &len1, &functionName, &len2, &arguments_zval, &outfilename, &len3) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
arr_hash = Z_ARRVAL_P(arguments_zval);
argument_length = zend_hash_num_elements(arr_hash);
arguments = new XdmValue*[argument_length];
if (xslt30Processor != NULL) {
ZEND_HASH_FOREACH_KEY_VAL(arr_hash, num_key, key, val) {
if(Z_TYPE_P(val) != NULL) {
const char * objName =ZSTR_VAL(Z_OBJCE_P(val)->name);
/*std::cerr<<"test type:"<<(Z_OBJCE_P(val)->name)<<std::endl;
php_printf("num_key %d =>", num_key);
php_printf("key %d =>", key);
*/
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmNode;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmAtomicValue_object * nobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmAtomicValue;
arguments[num_key] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmValue_object * nobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(nobj != NULL) {
XdmValue * value = nobj->xdmValue;
arguments[num_key] = value;
}
}
else {//TODO error warning}
}}else {
//TODO error warning
}
} ZEND_HASH_FOREACH_END();


xslt30Processor->callFunctionReturningFile(styleFileName, functionName, arguments, argument_length, outfilename);
if(xslt30Processor->exceptionOccurred()){
//TODO: xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
}
}
}

}

PHP_METHOD(Xslt30Processor, callTemplateReturningValue){

Xslt30Processor *xslt30Processor;
char* templateName = NULL;
char * styleFileName = NULL;
size_t len1, len2;


if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS() , "s", &templateName, &len2) == FAILURE) {
RETURN_NULL();
} else if (ZEND_NUM_ARGS()>1 && zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &styleFileName, &len1, &templateName, &len2) == FAILURE) {
RETURN_NULL();
}


zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {

//php_printf(" argument_length= %d",argument_length);
XdmValue * result = xslt30Processor->callTemplateReturningValue(styleFileName, templateName);
if(result != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object *ooth = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = result;
}
} else {
if(obj->xslt30Processor->exceptionOccurred()){
//TODO
}
RETURN_NULL();
}
}
}


PHP_METHOD(Xslt30Processor, callTemplateReturningString){
Xslt30Processor *xslt30Processor;
char* templateName = NULL;
char * styleFileName = NULL;
size_t len1, len2;

if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS() , "s", &templateName, &len2) == FAILURE) {
RETURN_NULL();
}

if (ZEND_NUM_ARGS()>1 && zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &styleFileName, &len1, &templateName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {

//php_printf(" argument_length= %d",argument_length);
const char * result = xslt30Processor->callTemplateReturningString(styleFileName, templateName);
if(result != NULL) {
_RETURN_STRING(result);
} else {
if(xslt30Processor->exceptionOccurred()){
xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}
}

RETURN_NULL();
}

PHP_METHOD(Xslt30Processor, callTemplateReturningFile){
Xslt30Processor *xslt30Processor;
char* templateName = NULL;
char * filename = NULL;
char * styleFileName = NULL;
size_t len1, len2, len3;

if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS() , "s", &templateName, &len2) == FAILURE) {
RETURN_NULL();
}

if (ZEND_NUM_ARGS()>1 && zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &styleFileName, &len1, &templateName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->callTemplateReturningFile(styleFileName, templateName, filename);
}

}


PHP_METHOD(Xslt30Processor, applyTemplatesReturningValue){
Xslt30Processor *xslt30Processor;
char * styleFileName = NULL;
size_t len1;

if (ZEND_NUM_ARGS()>0) {

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &styleFileName, &len1) == FAILURE) {
RETURN_NULL();
}
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
XdmValue * result = xslt30Processor->applyTemplatesReturningValue(styleFileName);
if(result != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object *ooth = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = result;
}
} else {
if(obj->xslt30Processor->exceptionOccurred()){
//TODO
}
RETURN_NULL();
}



} else {
RETURN_NULL();
}
}


PHP_METHOD(Xslt30Processor, applyTemplatesReturningString){
Xslt30Processor *xslt30Processor;

char * styleFileName = NULL;
size_t len1;


if (ZEND_NUM_ARGS()>0) {
if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &styleFileName, &len1) == FAILURE) {
RETURN_NULL();
}
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
const char * result = xslt30Processor->applyTemplatesReturningString(styleFileName);
if(result != NULL) {
_RETURN_STRING(result);
} else {
if(xslt30Processor->exceptionOccurred()){
xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}
}

RETURN_NULL();


}



PHP_METHOD(Xslt30Processor, applyTemplatesReturningFile){
Xslt30Processor *xslt30Processor;
char * filename = NULL;
char * styleFileName = NULL;
size_t len1, len2;

if (ZEND_NUM_ARGS()==2 && zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &styleFileName, &len1, &filename, &len2) == FAILURE) {
RETURN_NULL();
}

if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS() , "s", &filename, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->applyTemplatesReturningFile(styleFileName, filename);
}



}


PHP_METHOD(Xslt30Processor, addPackages){
HashTable *arr_hash;
Xslt30Processor *xslt30Processor;
char* functionName = NULL;
zval * arguments_zval;
zval * val;
long num_key;
const char ** arguments = NULL;
int argument_length=0;
zend_string *key;

char * infilename = NULL;
char * styleFileName = NULL;
size_t len1, len2;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "a", &arguments_zval) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
arr_hash = Z_ARRVAL_P(arguments_zval);
argument_length = zend_hash_num_elements(arr_hash);
arguments = new const char*[argument_length];

if (xslt30Processor != NULL) {
ZEND_HASH_FOREACH_KEY_VAL(arr_hash, num_key, key, val) {
if(Z_TYPE_P(val) != NULL && Z_TYPE_P(val)== IS_STRING) {
/*std::cerr<<"test type:"<<(Z_OBJCE_P(val)->name)<<std::endl;
php_printf("num_key %d =>", num_key);
php_printf("key %d =>", key);
*/
arguments[num_key] = (const char*)Z_STRVAL_P(val);
}
} ZEND_HASH_FOREACH_END();

xslt30Processor->addPackages(arguments, argument_length);
if(xslt30Processor->exceptionOccurred()){
//TODO: xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
}
}
}
}


PHP_METHOD(Xslt30Processor,setInitialTemplateParameters){

Xslt30Processor *xslt30Processor;
HashTable *arr_hash;
zval * arguments_zval;
zval * val;
long num_key;
XdmValue ** arguments;
int argument_length=0;
zend_string *key;

bool tunnel = false;
if (ZEND_NUM_ARGS()==1 && zend_parse_parameters(ZEND_NUM_ARGS() , "a", &arguments_zval) == FAILURE) {
RETURN_NULL();
}

if (ZEND_NUM_ARGS()==2 && zend_parse_parameters(ZEND_NUM_ARGS() , "ab", &arguments_zval, &tunnel) == FAILURE) {
RETURN_NULL();
}


zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
arr_hash = Z_ARRVAL_P(arguments_zval);
argument_length = zend_hash_num_elements(arr_hash);
arguments = new XdmValue*[argument_length];
std::map<std::string,XdmValue*> parameters;
if (xslt30Processor != NULL) {
ZEND_HASH_FOREACH_KEY_VAL(arr_hash, num_key, key, val) {
if(Z_TYPE_P(val) != NULL) {
const char * objName =ZSTR_VAL(Z_OBJCE_P(val)->name);
/*std::cerr<<"test type:"<<(Z_OBJCE_P(val)->name)<<std::endl;
php_printf("num_key %d =>", num_key);
php_printf("key %d =>", key);
*/
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmNode;
parameters[ZSTR_VAL(key)] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmAtomicValue_object * nobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(nobj != NULL) {
XdmValue * value = (XdmValue *) nobj->xdmAtomicValue;
parameters[ZSTR_VAL(key)] = value;
}
}
else if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object* ooth = Z_OBJ_P(val);
xdmValue_object * nobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(nobj != NULL) {
XdmValue * value = nobj->xdmValue;
parameters[ZSTR_VAL(key)] = value;
}
}
else {//TODO error warning}
}}else {
//TODO error warning
}
} ZEND_HASH_FOREACH_END();


//php_printf(" argument_length= %d",argument_length);
xslt30Processor->setInitialTemplateParameters(parameters, tunnel);
}

}


PHP_METHOD(Xslt30Processor, setInitialMatchSelection){
Xslt30Processor *xslt30Processor;
zval* oth = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {

if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value - NULL found");
return;
}

const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;

if(strcmp(objName, "Saxon\\XdmValue")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmValue_object* ooth = (xdmValue_object *)((char *)nobj - XtOffsetOf(xdmValue_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmValue * value = ooth->xdmValue;
if(value != NULL) {
xslt30Processor->setInitialMatchSelection(value);

}
}
} else if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
XdmValue *valueX = (XdmValue*)value;
xslt30Processor->setInitialMatchSelection(valueX);

} else {

php_error(E_WARNING, "XdmNode - seInitialMatchSelection - cp1");
}
}
} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* vvobj = Z_OBJ_P(oth);

xdmAtomicValue_object* ooth = (xdmAtomicValue_object *)((char *)vvobj - XtOffsetOf(xdmAtomicValue_object, std));
if(ooth != NULL) {
XdmAtomicValue * value = ooth->xdmAtomicValue;
if(value != NULL) {
xslt30Processor->setInitialMatchSelection((XdmValue*)value);
}
}
} else {
//TODO raise warning
}


}
}




}

PHP_METHOD(Xslt30Processor, setGlobalContextItem)
{
Xslt30Processor *xslt30Processor;
zval* oth = NULL;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {

if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value");
return;
}

const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
XdmItem *valueX = (XdmItem*)value;
xslt30Processor->setGlobalContextItem(valueX);

}
}
} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0) {
zend_object* vvobj = Z_OBJ_P(oth);
xdmAtomicValue_object* ooth = (xdmAtomicValue_object *)((char *)vvobj - XtOffsetOf(xdmAtomicValue_object, std));
if(ooth != NULL) {
XdmAtomicValue * value = ooth->xdmAtomicValue;
if(value != NULL) {
xslt30Processor->setGlobalContextItem((XdmItem*)value);
}
}
} else {
//TODO raise warning
}


}
}
}


PHP_METHOD(Xslt30Processor, setGlobalContextFromFile)
{
Xslt30Processor *xslt30Processor;
char * inFilename = NULL;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &inFilename, &len1) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL && inFilename != NULL) {
xslt30Processor->setGlobalContextFromFile(inFilename);


}
}


PHP_METHOD(Xslt30Processor, setInitialMatchSelectionAsFile)
{
Xslt30Processor *xslt30Processor;
char * inFilename = NULL;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &inFilename, &len1) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL && inFilename != NULL) {
xslt30Processor->setInitialMatchSelectionAsFile(inFilename);


}
}


PHP_METHOD(Xslt30Processor, transformFileToFile)
{
Xslt30Processor *xslt30Processor;
char * outfileName = NULL;
char * infilename = NULL;
char * styleFileName = NULL;
size_t len1, len2, len3;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "sss", &infilename, &len1, &styleFileName, &len2, &outfileName, &len3) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {

xslt30Processor->transformFileToFile(infilename, styleFileName, outfileName);
if(xslt30Processor->exceptionOccurred()) {
// TODO: throw exception
}
}
}

PHP_METHOD(Xslt30Processor, transformFileToValue)
{
Xslt30Processor *xslt30Processor;
char * infilename = NULL;
char * styleFileName = NULL;
size_t len1, len2;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &infilename, &len1, &styleFileName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {

XdmValue * node = xslt30Processor->transformFileToValue(infilename, styleFileName);
if(node != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value TSRMLS_CC);
zend_object *ooth = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = node;
}
} else {
if(obj->xslt30Processor->exceptionOccurred()){
//TODO
}
}
}else {
RETURN_NULL();
}
}


PHP_METHOD(Xslt30Processor, transformFileToString)
{
Xslt30Processor *xslt30Processor;
char * infilename = NULL;
char * styleFileName = NULL;
size_t len1, len2;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &infilename, &len1, &styleFileName, &len2) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {

const char * result = xslt30Processor->transformFileToString(infilename, styleFileName);
if(result != NULL) {
//char *str = estrdup(result);
_RETURN_STRING(result);
} else if(xslt30Processor->exceptionOccurred()){
//TODO: xslt30Processor->checkException();
const char * errStr = xslt30Processor->getErrorMessage(0);
if(errStr != NULL) {
const char * errorCode = xslt30Processor->getErrorCode(0);
if(errorCode!=NULL) {
// TODO: throw exception
}
}
}
}
}



PHP_METHOD(Xslt30Processor, transformToString)
{
Xslt30Processor *xslt30Processor;

if (ZEND_NUM_ARGS()>1) {
WRONG_PARAM_COUNT;
}


zval* oth;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}


zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {

if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value");
return;
}
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
const char * result = xslt30Processor->transformToString(value);
if(result != NULL) {
_RETURN_STRING(result);
} else if(xslt30Processor->exceptionOccurred()){
xslt30Processor->checkException();
}
}

}
}
}}
RETURN_NULL();
}

PHP_METHOD(Xslt30Processor, transformToValue)
{
Xslt30Processor *xslt30Processor;

if (ZEND_NUM_ARGS()>1) {
WRONG_PARAM_COUNT;
}


zval* oth;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {
if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value");
return;
}
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
XdmValue * node = xslt30Processor->transformToValue(value);
if(node != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
zend_object *vvobj = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = node;
}
} else if(xslt30Processor->exceptionOccurred()){
xslt30Processor->checkException();
RETURN_NULL();
}
}
}
}

} } else {
RETURN_NULL();
}
}

PHP_METHOD(Xslt30Processor, transformToFile)
{
Xslt30Processor *xslt30Processor;

if (ZEND_NUM_ARGS()>1) {
WRONG_PARAM_COUNT;
}


zval* oth;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "z", &oth) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {
if(!oth) {
php_error(E_WARNING, "Error setting source value");
return;
} else {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value");
return;
}
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object * nobj = Z_OBJ_P(oth);

xdmNode_object* ooth = (xdmNode_object *)((char *)nobj - XtOffsetOf(xdmNode_object, std));//(xdmNode_object*)Z_OBJ_P(oth);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
xslt30Processor->transformToFile(value);
if(xslt30Processor->exceptionOccurred()) {
//TODO
const char * exStr = xslt30Processor->checkException();
}
}
}
}

}
}
else {
RETURN_NULL();
}
}

PHP_METHOD(Xslt30Processor, compileFromFile)
{
Xslt30Processor *xslt30Processor;
char * name = NULL;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->compileFromFile(name);
}
}


PHP_METHOD(Xslt30Processor, compileFromAssociatedFile)
{
Xslt30Processor *xslt30Processor;
char * name = NULL;
size_t len1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->compileFromAssociatedFile(name);
}
}


PHP_METHOD(Xslt30Processor, compileFromString)
{
Xslt30Processor *xslt30Processor;
char * stylesheetStr;
size_t len1, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &stylesheetStr, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->compileFromString(stylesheetStr);
}
}

PHP_METHOD(Xslt30Processor, compileFromStringAndSave)
{
Xslt30Processor *xslt30Processor;
char * stylesheetStr;
char * filename = NULL;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &stylesheetStr, filename, &len1, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->compileFromStringAndSave(stylesheetStr, filename);
}
}

PHP_METHOD(Xslt30Processor, compileFromFileAndSave)
{
Xslt30Processor *xslt30Processor;
char * stylesheetFilename = NULL;
char * filename = NULL;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &stylesheetFilename, filename, &len1, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->compileFromStringAndSave(stylesheetFilename, filename);
}
}



PHP_METHOD(Xslt30Processor, compileFromValue)
{
Xslt30Processor *xslt30Processor;
zval* oth;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "O", &oth, xdmValue_ce) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
zend_object* vvobj = Z_OBJ_P(oth);
xdmValue_object* ooth = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
if(ooth != NULL) {
XdmValue * value = ooth->xdmValue;
if(value != NULL && value->size() == 1 && (value->getHead())->getType() == 3) {
xslt30Processor->compileFromXdmNode((XdmNode*)(value->getHead()));
}
}
}
}



PHP_METHOD(Xslt30Processor, setResultAsRawValue)
{
Xslt30Processor *xslt30Processor;
bool raw = false;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "b", &raw) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->setResultAsRawValue(raw);
}

}



PHP_METHOD(Xslt30Processor, setJustInTimeCompilation)
{
Xslt30Processor *xslt30Processor;
bool jit = false;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "b", &jit) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->setJustInTimeCompilation(jit);
}
}

PHP_METHOD(Xslt30Processor, setOutputFile)
{
Xslt30Processor *xslt30Processor;
char * outputFilename = NULL;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "s", &outputFilename, &len1) == FAILURE) {
RETURN_NULL();
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL && outputFilename != NULL) {

xslt30Processor->setOutputFile(outputFilename);


}
}




PHP_METHOD(Xslt30Processor, setProperty)
{
Xslt30Processor *xslt30Processor;
char * name = NULL;
char * value;
size_t len1, len2, myint;

if (zend_parse_parameters(ZEND_NUM_ARGS() , "ss", &name, &len1, &value, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->setProperty(name, value);
}
}

PHP_METHOD(Xslt30Processor, setParameter)
{

Xslt30Processor *xslt30Processor;
char * name;
zval* oth;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "sz", &name, &len2, &oth) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value - value is null");
return;
}

const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;

if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object* ooth = Z_OBJ_P(oth);
xdmNode_object * nobj = (xdmNode_object *)((char *)ooth - XtOffsetOf(xdmNode_object, std));
if(nobj != NULL) {
XdmNode * value = nobj->xdmNode;
if(value != NULL) {
xslt30Processor->setParameter(name, (XdmValue *)value);

}
}
} else if(strcmp(objName, "Saxon\\XdmValue")==0){
zend_object* ooth = Z_OBJ_P(oth);
xdmValue_object * vobj = (xdmValue_object *)((char *)ooth - XtOffsetOf(xdmValue_object, std));
if(vobj != NULL) {
XdmValue * value = vobj->xdmValue;
if(value != NULL) {

xslt30Processor->setParameter(name, value);
}
}



} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0){
zend_object* ooth = Z_OBJ_P(oth);
xdmAtomicValue_object * aobj = (xdmAtomicValue_object *)((char *)ooth - XtOffsetOf(xdmAtomicValue_object, std));
if(aobj != NULL) {
XdmAtomicValue * value = aobj->xdmAtomicValue;
if(value != NULL) {
xslt30Processor->setParameter(name, (XdmValue *)value);
}
}



}

}
}

PHP_METHOD(Xslt30Processor, clearParameters)
{
Xslt30Processor *xslt30Processor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->clearParameters(true);
}
}

PHP_METHOD(Xslt30Processor, clearProperties)
{
Xslt30Processor *xslt30Processor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->clearProperties();
}
}



PHP_METHOD(Xslt30Processor, getXslMessages)
{
Xslt30Processor *xslt30Processor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;

if (xslt30Processor != NULL) {

XdmValue * values = xslt30Processor->getXslMessages();
if(values != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
} else {
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value);
zend_object *vvobj = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));


assert (vobj != NULL);
vobj->xdmValue = values;
}
} else if(xslt30Processor->exceptionOccurred()){
xslt30Processor->checkException();
RETURN_NULL();
}
} else {
RETURN_NULL();
}
}



PHP_METHOD(Xslt30Processor, exceptionOccurred)
{
Xslt30Processor *xslt30Processor;
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
bool result = xslt30Processor->exceptionOccurred();
RETURN_BOOL(result);
}
RETURN_BOOL(false);
}

PHP_METHOD(Xslt30Processor, getExceptionCount)
{
Xslt30Processor *xslt30Processor;
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
int count = xslt30Processor->exceptionCount();
RETURN_LONG(count);
}
RETURN_LONG(0);
}

PHP_METHOD(Xslt30Processor, getErrorCode)
{
Xslt30Processor *xslt30Processor;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
const char * errCode = xslt30Processor->getErrorCode((int)index);
if(errCode != NULL) {
//char *str = estrdup(errCode);
_RETURN_STRING(errCode);
}
}
RETURN_NULL();
}

PHP_METHOD(Xslt30Processor, getErrorMessage)
{
Xslt30Processor *xslt30Processor;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
const char * errStr = xslt30Processor->getErrorMessage((int)index);
if(errStr != NULL) {
//char *str = estrdup(errStr);
_RETURN_STRING(errStr);
}
}
RETURN_NULL();
}
PHP_METHOD(Xslt30Processor, exceptionClear)
{
Xslt30Processor *xslt30Processor;
zend_object* pobj = Z_OBJ_P(getThis());
xslt30Processor_object *obj = (xslt30Processor_object *)((char *)pobj - XtOffsetOf(xslt30Processor_object, std));
xslt30Processor = obj->xslt30Processor;
if (xslt30Processor != NULL) {
xslt30Processor->exceptionClear();
}
}

/* ============== XQuery10/30/31: PHP Interface of XQueryProcessor =============== */

void xqueryProcessor_free_storage(zend_object *object)
{
zend_object_std_dtor(object);
}

void xqueryProcessor_destroy_storage(zend_object *object)
{
zend_objects_destroy_object(object);
}

zend_object *xqueryProcessor_create_handler(zend_class_entry *type)
{

xqueryProcessor_object *obj = (xqueryProcessor_object *)ecalloc(1, sizeof(xqueryProcessor_object)+ zend_object_properties_size(type));
zend_object_std_init(&obj->std, type);
object_properties_init(&obj->std, type);
obj->std.handlers = &xqueryProcessor_object_handlers;

return &obj->std;
}

PHP_METHOD(XQueryProcessor, __destruct)
{
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
XQueryProcessor * xqueryProcessor= obj->xqueryProcessor;
if(xqueryProcessor != NULL){
delete xqueryProcessor;
}
}


PHP_METHOD(XQueryProcessor, runQueryToValue)
{
XQueryProcessor *xqueryProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xqueryProcessor = obj->xqueryProcessor;

if (xqueryProcessor != NULL) {
XdmValue * node = xqueryProcessor->runQueryToValue();
if(node != NULL) {
if (object_init_ex(return_value, xdmValue_ce) != SUCCESS) {
RETURN_NULL();
return;
} else {
//struct xdmValue_object* vobj = (struct xdmValue_object *)Z_OBJ_P(return_value TSRMLS_CC);
zend_object *vvobj = Z_OBJ_P(return_value);
xdmValue_object* vobj = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
assert (vobj != NULL);
vobj->xdmValue = node;
return;
}
}
xqueryProcessor->checkException();//TODO
} else {
RETURN_NULL();
}
}

PHP_METHOD(XQueryProcessor, runQueryToString)
{
XQueryProcessor *xqueryProcessor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;

if (xqueryProcessor != NULL) {
const char * result = xqueryProcessor->runQueryToString();
if(result != NULL) {
//char *str = estrdup(result);
_RETURN_STRING(result);
return;
} else {
xqueryProcessor->checkException(); //TODO
}
}
RETURN_NULL();
}

PHP_METHOD(XQueryProcessor, runQueryToFile)
{

char * ofilename;
size_t len1 =0;
if (ZEND_NUM_ARGS()!= 1) {
WRONG_PARAM_COUNT;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &ofilename, &len1) == FAILURE) {
RETURN_NULL();
}
XQueryProcessor *xqueryProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));



xqueryProcessor = obj->xqueryProcessor;

if (xqueryProcessor != NULL) {
if(ofilename != NULL) {
xqueryProcessor->setOutputFile(ofilename);
}
xqueryProcessor->runQueryToFile();
}

}

PHP_METHOD(XQueryProcessor, setQueryContent)
{
char * queryStr;
size_t len1;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &queryStr, &len1) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
if(queryStr != NULL) {
obj->xqueryProcessor->setProperty("qs", queryStr);
}
}

PHP_METHOD(XQueryProcessor, setQueryFile)
{
char * fileName;
size_t len1;
XQueryProcessor *xqueryProcessor;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fileName, &len1) == FAILURE) {
RETURN_NULL();
}
if(fileName != NULL) {
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
xqueryProcessor->setQueryFile(fileName);
}
}

PHP_METHOD(XQueryProcessor, setQueryBaseURI)
{
char * base;
size_t len1;
XQueryProcessor *xqueryProcessor;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &base, &len1) == FAILURE) {
RETURN_NULL();
}
if(base != NULL) {
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
xqueryProcessor->setQueryBaseURI(base);
}
}

PHP_METHOD(XQueryProcessor, declareNamespace)
{
char * prefix;
char * ns;
size_t len1, len2;
XQueryProcessor *xqueryProcessor;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &prefix, &len1, &ns, &len2) == FAILURE) {
RETURN_NULL();
}
if(prefix != NULL && ns != NULL) {
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
xqueryProcessor->declareNamespace(prefix, ns);
}
}



PHP_METHOD(XQueryProcessor, setContextItem)
{
char * context;
int len1;
zval* oth;
XQueryProcessor *xqueryProcessor;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &oth) == FAILURE) {
RETURN_NULL();
}
if(oth != NULL) {
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object *vvobj = Z_OBJ_P(oth);
xdmNode_object* ooth = (xdmNode_object *)((char *)vvobj - XtOffsetOf(xdmNode_object, std));
//xdmNode_object* ooth = (xdmNode_object*)Z_OBJ_P(oth TSRMLS_CC);
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
xqueryProcessor->setContextItem((XdmItem *)value);
return;
}
}
} else if(strcmp(objName, "Saxon\\XdmItem")==0){
zend_object *vvobj = Z_OBJ_P(oth);
xdmItem_object* ooth = (xdmItem_object *)((char *)vvobj - XtOffsetOf(xdmItem_object, std));
if(ooth != NULL) {
XdmItem * value = ooth->xdmItem;
if(value != NULL) {
xqueryProcessor->setContextItem(value);
return;
}
}



} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0){
zend_object *vvobj = Z_OBJ_P(oth);
xdmAtomicValue_object* ooth = (xdmAtomicValue_object *)((char *)vvobj - XtOffsetOf(xdmAtomicValue_object, std));
if(ooth != NULL) {
XdmAtomicValue * value = ooth->xdmAtomicValue;
if(value != NULL) {
xqueryProcessor->setContextItem((XdmItem *)value);
return;
}
}



}


}
//throw exception
}

PHP_METHOD(XQueryProcessor, setContextItemFromFile)
{
char * cfilename;
size_t len1;
XQueryProcessor *xqueryProcessor;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &cfilename, &len1) == FAILURE) {
RETURN_NULL();
}
if(cfilename != NULL) {
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
xqueryProcessor->setContextItemFromFile(cfilename);
}
}


PHP_METHOD(XQueryProcessor, setProperty)
{
XQueryProcessor *xqueryProcessor;
char * name;
char * value;
size_t len1, len2, myint;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &name, &len1, &value, &len2) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
xqueryProcessor->setProperty(name, value);
}
}

PHP_METHOD(XQueryProcessor, setParameter)
{

XQueryProcessor *xqueryProcessor;
char * name;
zval* oth;
size_t len1, len2, myint;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "sz", &name, &len2, &oth) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
if(Z_TYPE_P(oth) ==IS_NULL){
php_error(E_WARNING, "Error setting source value - value is null");
return;
}
const char * objName =ZSTR_VAL(Z_OBJCE_P(oth)->name);
//std::cout<<"test type:"<<(Z_OBJCE_P(oth)->name)<<std::endl;
//Z_ADDREF_P(oth);
if(strcmp(objName, "Saxon\\XdmNode")==0) {
zend_object *vvobj = Z_OBJ_P(oth);
xdmNode_object* ooth = (xdmNode_object *)((char *)vvobj - XtOffsetOf(xdmNode_object, std));
if(ooth != NULL) {
XdmNode * value = ooth->xdmNode;
if(value != NULL) {
xqueryProcessor->setParameter(name, (XdmValue *)value);

}
}
} else if(strcmp(objName, "Saxon\\XdmValue")==0){
zend_object *vvobj = Z_OBJ_P(oth);
xdmValue_object* ooth = (xdmValue_object *)((char *)vvobj - XtOffsetOf(xdmValue_object, std));
if(ooth != NULL) {
XdmValue * value = ooth->xdmValue;
if(value != NULL) {
xqueryProcessor->setParameter(name, value);
}
}



} else if(strcmp(objName, "Saxon\\XdmAtomicValue")==0){
zend_object *vvobj = Z_OBJ_P(oth);
xdmAtomicValue_object* ooth = (xdmAtomicValue_object *)((char *)vvobj - XtOffsetOf(xdmAtomicValue_object, std));
if(ooth != NULL) {
XdmAtomicValue * value = ooth->xdmAtomicValue;
if(value != NULL) {
xqueryProcessor->setParameter(name, (XdmValue *)value);
}
}



}

}
}




PHP_METHOD(XQueryProcessor, clearParameters)
{
XQueryProcessor *xqueryProcessor;
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
xqueryProcessor->clearParameters(true);
}
}

PHP_METHOD(XQueryProcessor, clearProperties)
{
XQueryProcessor *xqueryProcessor;

if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
xqueryProcessor->clearProperties();
}
}

PHP_METHOD(XQueryProcessor, exceptionOccurred)
{
XQueryProcessor *xqueryProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
bool result = xqueryProcessor->exceptionOccurred();
RETURN_BOOL(result);
}
RETURN_BOOL(false);
}

PHP_METHOD(XQueryProcessor, getExceptionCount)
{
XQueryProcessor *xqueryProcessor;
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
if (ZEND_NUM_ARGS()>0) {
WRONG_PARAM_COUNT;
}

xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
int count = xqueryProcessor->exceptionCount();
RETURN_LONG(count);
}
RETURN_LONG(0);
}

PHP_METHOD(XQueryProcessor, getErrorCode)
{
XQueryProcessor *xqueryProcessor;
long index;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
RETURN_NULL();
}
zend_object* pobj = Z_OBJ_P(getThis());
xqueryProcessor_object *obj = (xqueryProcessor_object *)((char *)pobj - XtOffsetOf(xqueryProcessor_object, std));
xqueryProcessor = obj->xqueryProcessor;
if (xqueryProcessor != NULL) {
const char * errCode = xqueryProcessor->getErrorCode((int)index);
if(errCode != NULL) {
//char *str = estrdup(errCode);
_RETURN_STRING(errCode);
}
}
RETURN_NULL();
}