Esempio n. 1
0
/**
 * exsltStrXpathCtxtRegister:
 *
 * Registers the EXSLT - Strings module for use outside XSLT
 */
int
exsltStrXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix)
{
    if (ctxt
        && prefix
        && !xmlXPathRegisterNs(ctxt,
                               prefix,
                               (const xmlChar *) EXSLT_STRINGS_NAMESPACE)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "encode-uri",
                                   (const xmlChar *) EXSLT_STRINGS_NAMESPACE,
                                   exsltStrEncodeUriFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "decode-uri",
                                   (const xmlChar *) EXSLT_STRINGS_NAMESPACE,
                                   exsltStrDecodeUriFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "padding",
                                   (const xmlChar *) EXSLT_STRINGS_NAMESPACE,
                                   exsltStrPaddingFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "align",
                                   (const xmlChar *) EXSLT_STRINGS_NAMESPACE,
                                   exsltStrAlignFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "concat",
                                   (const xmlChar *) EXSLT_STRINGS_NAMESPACE,
                                   exsltStrConcatFunction)) {
        return 0;
    }
    return -1;
}
Esempio n. 2
0
/**
 * @arg xpath XPath expression to evaluate
 * @doc document over which to evaluate
 * @arg pctxt can be NULL
 * @return list of matching nodes. The caller will have to free it using xmlXPathFreeNodeSet().
 */
xmlNodeSetPtr find_node_set(const char *xpath, const xmlDocPtr doc, xmlXPathParserContextPtr *pctxt)
{
  xmlXPathContextPtr ctxt = xmlXPathNewContext(doc);
  if(!ctxt)
  {
    g_printerr(G_STRLOC ": Failed to allocate XPathContext!\n");
    return NULL;
  }

  if(xmlXPathRegisterNs(ctxt, (xmlChar *) FREEDICT_EDITOR_NAMESPACE_PREFIX, (xmlChar *) FREEDICT_EDITOR_NAMESPACE))
  {
    g_printerr("Warning: Unable to register XSLT-Namespace prefix \"%s\""
	" for URI \"%s\"\n", FREEDICT_EDITOR_NAMESPACE_PREFIX, FREEDICT_EDITOR_NAMESPACE);
  }

  if(xmlXPathRegisterFuncNS(ctxt, (xmlChar *) "unbalanced-braces",
	(xmlChar *) FREEDICT_EDITOR_NAMESPACE, freedict_xpath_extension_unbalanced_braces))
    g_printerr("Warning: Unable to register XPath extension function "
	"\"unbalanced-braces\" for URI \"%s\"\n", FREEDICT_EDITOR_NAMESPACE);

  xmlXPathParserContextPtr pctxt2;
  if(!pctxt) pctxt = &pctxt2;
  xmlXPathObjectPtr xpobj = my_xmlXPathEvalExpression((xmlChar *) xpath, ctxt, pctxt);
  if(!xpobj)
  {
    g_printerr(G_STRLOC ": No XPathObject!\n");
    xmlXPathFreeContext(ctxt);
    return NULL;
  }

  if(!(xpobj->nodesetval))
  {
    g_printerr(G_STRLOC ": No nodeset!\n");
    xmlXPathFreeObject(xpobj);
    xmlXPathFreeContext(ctxt);
    return NULL;
  }

  if(!(xpobj->nodesetval->nodeNr))
  {
    //g_printerr("0 nodes!\n");
    xmlXPathFreeObject(xpobj);
    xmlXPathFreeContext(ctxt);
    return NULL;
  }

  xmlXPathFreeContext(ctxt);

  xmlNodeSetPtr nodes = xmlMalloc(sizeof(xmlNodeSet));
  // XXX copying is slow...
  memcpy(nodes, xpobj->nodesetval, sizeof(xmlNodeSet));

  // I don't understand the naming of this function.  According to the
  // documentation, it frees xpobj, but not its nodelist, if it
  // contained one. So it should be called xmlXPathFreeObjectButNotNodeSetList().
  xmlXPathFreeNodeSetList(xpobj);

  return nodes;
}
Esempio n. 3
0
/* {{{ proto void DOMXPath::__construct(DOMDocument doc) U */
PHP_METHOD(domxpath, __construct)
{
	zval *id, *doc;
	xmlDocPtr docp = NULL;
	dom_object *docobj;
	dom_xpath_object *intern;
	xmlXPathContextPtr ctx, oldctx;
	zend_error_handling error_handling;

	zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "OO", &id, dom_xpath_class_entry, &doc, dom_document_class_entry) == FAILURE) {
		zend_restore_error_handling(&error_handling TSRMLS_CC);
		return;
	}

	zend_restore_error_handling(&error_handling TSRMLS_CC);
	DOM_GET_OBJ(docp, doc, xmlDocPtr, docobj);

	ctx = xmlXPathNewContext(docp);
	if (ctx == NULL) {
		php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
		RETURN_FALSE;
	}

	intern = (dom_xpath_object *)zend_object_store_get_object(id TSRMLS_CC);
	if (intern != NULL) {
		oldctx = (xmlXPathContextPtr)intern->ptr;
		if (oldctx != NULL) {
			php_libxml_decrement_doc_ref((php_libxml_node_object *)intern TSRMLS_CC);
			xmlXPathFreeContext(oldctx);
		}

		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",
					   (const xmlChar *) "http://php.net/xpath",
					   dom_xpath_ext_function_string_php);
		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "function",
					   (const xmlChar *) "http://php.net/xpath",
					   dom_xpath_ext_function_object_php);

		intern->ptr = ctx;
		ctx->userData = (void *)intern;
		intern->document = docobj->document;
		php_libxml_increment_doc_ref((php_libxml_node_object *)intern, docp TSRMLS_CC);
	}
}
Esempio n. 4
0
/* {{{ proto void DOMXPath::__construct(DOMDocument doc) U */
PHP_METHOD(domxpath, __construct)
{
	zval *id = getThis(), *doc;
	xmlDocPtr docp = NULL;
	dom_object *docobj;
	dom_xpath_object *intern;
	xmlXPathContextPtr ctx, oldctx;

	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "O", &doc, dom_document_class_entry) == FAILURE) {
		return;
	}

	DOM_GET_OBJ(docp, doc, xmlDocPtr, docobj);

	ctx = xmlXPathNewContext(docp);
	if (ctx == NULL) {
		php_dom_throw_error(INVALID_STATE_ERR, 1);
		RETURN_FALSE;
	}

	intern = Z_XPATHOBJ_P(id);
	if (intern != NULL) {
		oldctx = (xmlXPathContextPtr)intern->dom.ptr;
		if (oldctx != NULL) {
			php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
			xmlXPathFreeContext(oldctx);
		}

		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",
					   (const xmlChar *) "http://php.net/xpath",
					   dom_xpath_ext_function_string_php);
		xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "function",
					   (const xmlChar *) "http://php.net/xpath",
					   dom_xpath_ext_function_object_php);

		intern->dom.ptr = ctx;
		ctx->userData = (void *)intern;
		intern->dom.document = docobj->document;
		php_libxml_increment_doc_ref((php_libxml_node_object *) &intern->dom, docp);
	}
}
Esempio n. 5
0
int
exsltMathXpathCtxtRegister (xmlXPathContextPtr ctxt, const xmlChar *prefix)
{
    if (ctxt
        && prefix
        && !xmlXPathRegisterNs(ctxt,
                               prefix,
                               (const xmlChar *) EXSLT_MATH_NAMESPACE)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "min",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathMinFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "max",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathMaxFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "highest",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathHighestFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "lowest",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathLowestFunction)
#ifdef HAVE_STDLIB_H
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "random",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathRandomFunction)
#endif
#if HAVE_MATH_H
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "abs",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathAbsFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "sqrt",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathSqrtFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "power",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathPowerFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "log",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathLogFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "sin",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathSinFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "cos",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathCosFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "tan",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathTanFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "asin",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathAsinFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "acos",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathAcosFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "atan",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathAtanFunction)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "atan2",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathAtan2Function)
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "exp",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathExpFunction)
#endif
        && !xmlXPathRegisterFuncNS(ctxt,
                                   (const xmlChar *) "constant",
                                   (const xmlChar *) EXSLT_MATH_NAMESPACE,
                                   exsltMathConstantFunction)) {
        return 0;
    }
    return -1;
}