xml/xmldomandxpath/src/xmlenginedom/xmlengxpathexpression.cpp
changeset 0 e35f40988205
equal deleted inserted replaced
-1:000000000000 0:e35f40988205
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // XPath expression functions
       
    15 //
       
    16 
       
    17 #include <stdapis/libxml2/libxml2_globals.h>
       
    18 #include <stdapis/libxml2/libxml2_xpath.h>
       
    19 #include "libxml2_xpathinternals_private.h"
       
    20 #include <xml/dom/xmlengxpathexpression.h>
       
    21 #include <xml/dom/xmlengxformsinstancemap.h>
       
    22 #include <xml/dom/xmlengnode.h>
       
    23 #include "xmlengdomdefs.h"
       
    24 #include <xml/dom/xmlengxpatherrors.h>
       
    25 
       
    26 #include <xml/utils/xmlengmem.h>
       
    27 #include <xml/utils/xmlengxestrings.h>
       
    28 
       
    29 #define COMPILED_EXPR (static_cast<xmlXPathCompExprPtr>(iInternal))
       
    30 #define INSTANCE_MAP (iInternal2 ? \
       
    31                       static_cast<xmlHashTablePtr> \
       
    32                       (static_cast<CXmlEngXFormsInstanceMap*>(iInternal2)->iInternal) : NULL)
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // Default constructor
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 EXPORT_C RXmlEngXPathExpression::RXmlEngXPathExpression():iInternal(NULL), iInternal2(NULL), reserved1(0), reserved2(NULL)
       
    39     {
       
    40     }
       
    41 
       
    42 // --------------------------------------------------------------------------------------------
       
    43 // Resets the compiled expression and frees the allocated memory.
       
    44 // --------------------------------------------------------------------------------------------
       
    45 //
       
    46 EXPORT_C void RXmlEngXPathExpression::Destroy()
       
    47     {
       
    48     xmlXPathFreeCompExpr(COMPILED_EXPR);
       
    49     iInternal = NULL;
       
    50     iInternal2 = NULL;
       
    51     }
       
    52 
       
    53 // --------------------------------------------------------------------------------------------
       
    54 // Evaluates this expression.
       
    55 // @param[in] aContextNode The node relative to which the expression is evaluated.
       
    56 // @param[in]     aResolver     Namespace-resolver object
       
    57 // 
       
    58 // @return Result of evaluation as an RXmlEngXPathResult object.
       
    59 // May leave with KXmlEngErrXPathResult code if result is undefuned
       
    60 // --------------------------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C RXmlEngXPathResult RXmlEngXPathExpression::EvaluateL(
       
    63     TXmlEngNode aContextNode,
       
    64     const MXmlEngNamespaceResolver* /* aResolver */) const
       
    65     {
       
    66     xmlXPathContextPtr ctxt = xmlXPathNewContext(INTERNAL_NODEPTR(aContextNode)->doc);
       
    67     OOM_IF_NULL(ctxt);
       
    68     ctxt->node = INTERNAL_NODEPTR(aContextNode);
       
    69     ctxt->instanceDocs = INSTANCE_MAP;
       
    70     xmlXPathObjectPtr resultObj = xmlXPathCompiledEval(COMPILED_EXPR, ctxt);
       
    71     xmlXPathFreeContext(ctxt);
       
    72     if(OOM_FLAG)
       
    73         {
       
    74         xmlXPathFreeObject(resultObj);
       
    75         XmlEngLeaveL(KErrNoMemory);
       
    76         }
       
    77     if(!resultObj || resultObj->type == XPATH_UNDEFINED)
       
    78         {
       
    79         xmlXPathFreeObject(resultObj);
       
    80         XmlEngLeaveL(KXmlEngErrXPathResult);
       
    81         }
       
    82     return RXmlEngXPathResult(resultObj);
       
    83     }
       
    84 
       
    85 // --------------------------------------------------------------------------------------------
       
    86 // Does the same as #EvaluateL, but as a side-effect calculates the dependency list of the
       
    87 // expression. This is meant to be used by the XForms engine.
       
    88 //
       
    89 // @param[in]     aContextNode  The node relative to which the expression is evaluated.
       
    90 // @param[in,out] aDependents   After the method has returned, contains set of nodes that
       
    91 // the expression is dependent on.
       
    92 // @param[in]     aResolver     Namespace-resolver object
       
    93 // 
       
    94 // @return Result of evaluation as an RXmlEngXPathResult object.
       
    95 // May leave with KXmlEngErrXPathResult code if result is undefuned,
       
    96 // argument node set is not destroyed then.
       
    97 // --------------------------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C RXmlEngXPathResult RXmlEngXPathExpression::EvaluateWithDependenciesL(
       
   100     TXmlEngNode aContextNode,
       
   101     RXmlEngNodeSet& aDependents,
       
   102     const MXmlEngNamespaceResolver* /* aResolver */) const
       
   103     {
       
   104     xmlXPathContextPtr ctxt = xmlXPathNewContext(INTERNAL_NODEPTR(aContextNode)->doc);
       
   105     OOM_IF_NULL(ctxt);
       
   106     ctxt->node = INTERNAL_NODEPTR(aContextNode);
       
   107     ctxt->instanceDocs = INSTANCE_MAP;
       
   108     xmlXPathObjectPtr resultObj = xmlXPathCompiledEvalWithDependencies(
       
   109                                         COMPILED_EXPR,
       
   110                                         ctxt,
       
   111                                         static_cast<xmlNodeSetPtr>(aDependents.iInternal));
       
   112     xmlXPathFreeContext(ctxt);
       
   113     if(resultObj->type == XPATH_UNDEFINED)
       
   114         {
       
   115         xmlXPathFreeObject(resultObj);
       
   116         XmlEngLeaveL(KXmlEngErrXPathResult);
       
   117         }
       
   118 
       
   119     return RXmlEngXPathResult(resultObj);
       
   120     }
       
   121 
       
   122 EXPORT_C void RXmlEngXPathExpression::SetExtendedContext(
       
   123     void* aContext )
       
   124     {
       
   125     COMPILED_EXPR->extendedContext = aContext;
       
   126     }
       
   127 
       
   128 EXPORT_C void* RXmlEngXPathExpression::ExtendedContext() const
       
   129     {
       
   130     return COMPILED_EXPR->extendedContext;
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // Constructor
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 RXmlEngXPathExpression::RXmlEngXPathExpression(void* aData, void* aData2):iInternal(aData), iInternal2(aData2)
       
   138     {
       
   139     }