xml/xmldomandxpath/src/xmlenginedom/xmlengxpathevaluationcontext_impl.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 evaluation context implementation
       
    15 //
       
    16 
       
    17 #include "xmlengdomdefs.h"
       
    18 #include "xmlengxpathevaluationcontext_impl.h"
       
    19 #include <stdapis/libxml2/libxml2_globals.h>
       
    20 #include <xml/dom/xmlengnode.h>
       
    21 #include <xml/dom/xmlengnamespace.h>
       
    22 #include <xml/dom/xmlengelement.h>
       
    23 
       
    24 TUint TXmlEngXPathEvaluationContextImpl::ArgCount()
       
    25     {
       
    26     return iArgCount;
       
    27     }
       
    28 
       
    29 const RXmlEngXPathResult TXmlEngXPathEvaluationContextImpl::Argument(
       
    30     TUint aIndex )
       
    31     {
       
    32     XE_ASSERT_DEBUG(aIndex < iArgCount);
       
    33     // "Peeking" a value from a XPath-expression evaluation stack
       
    34     return RXmlEngXPathResult(iContext->valueTab[iContext->valueNr - (iArgCount - aIndex)]);
       
    35     }
       
    36 
       
    37 const RXmlEngXPathResult TXmlEngXPathEvaluationContextImpl::Result()
       
    38     {
       
    39     XE_ASSERT_DEBUG(iCurrentResult);
       
    40     if (iCurrentResult->type != XPATH_NODESET &&
       
    41         iCurrentResult->nodesetval)
       
    42         {
       
    43         xmlXPathFreeNodeSet(iCurrentResult->nodesetval);
       
    44         iCurrentResult->nodesetval = NULL;
       
    45         }
       
    46     if (iCurrentResult->type != XPATH_STRING && iCurrentResult->stringval)
       
    47         {
       
    48         xmlFree(iCurrentResult->stringval);
       
    49         iCurrentResult->stringval = NULL;
       
    50         }
       
    51     return RXmlEngXPathResult(iCurrentResult);
       
    52     }
       
    53 
       
    54 void TXmlEngXPathEvaluationContextImpl::InitializeNodeSetResult()
       
    55     {
       
    56     XE_ASSERT_DEBUG(iCurrentResult);
       
    57     iCurrentResult->nodesetval = xmlXPathNodeSetCreate(NULL);
       
    58     if(iCurrentResult->nodesetval)
       
    59         {
       
    60         iCurrentResult->type = XPATH_NODESET;
       
    61         }
       
    62     // else OOM happened and will be handled later
       
    63     }
       
    64 
       
    65 void TXmlEngXPathEvaluationContextImpl::AppendToResult(
       
    66     const TXmlEngNode aNode )
       
    67     {
       
    68     if(OOM_FLAG)
       
    69         return; // Do nothing in OOM
       
    70     if (aNode.NodeType() != TXmlEngNode::ENamespaceDeclaration)
       
    71         {
       
    72         xmlXPathNodeSetAdd(iCurrentResult->nodesetval, INTERNAL_NODEPTR(*const_cast<TXmlEngNode*>(&aNode)));
       
    73         }
       
    74     }
       
    75 void TXmlEngXPathEvaluationContextImpl::AppendToResult(
       
    76     const TXmlEngNamespace aAppendedNsNode,
       
    77     const TXmlEngElement aNsParentNode)
       
    78     {
       
    79     XE_ASSERT_DEBUG(iCurrentResult);
       
    80 
       
    81     if(OOM_FLAG)
       
    82         return; // Do nothing in OOM
       
    83     // Add a copy of namespace node with special function
       
    84     xmlXPathNodeSetAddNs(iCurrentResult->nodesetval,
       
    85                          INTERNAL_NODEPTR(*const_cast<TXmlEngElement*>(&aNsParentNode)),
       
    86                          INTERNAL_NSPTR(*const_cast<TXmlEngNamespace*>(&aAppendedNsNode)));
       
    87     }
       
    88 
       
    89 void TXmlEngXPathEvaluationContextImpl::AppendToResult(
       
    90     const RXmlEngNodeSet aNodeSet )
       
    91     {
       
    92     if(OOM_FLAG)
       
    93         return; // Do nothing in OOM
       
    94     xmlXPathNodeSetMerge(iCurrentResult->nodesetval, INTERNAL_NODESETPTR(aNodeSet));
       
    95     iCurrentResult->type = XPATH_NODESET;
       
    96     }
       
    97 
       
    98 void TXmlEngXPathEvaluationContextImpl::SetResult(
       
    99     TReal aNumber )
       
   100     {
       
   101     XE_ASSERT_DEBUG(iCurrentResult);
       
   102     iCurrentResult->type = XPATH_NUMBER;
       
   103     iCurrentResult->floatval = aNumber;
       
   104     }
       
   105 
       
   106 void TXmlEngXPathEvaluationContextImpl::SetResult(
       
   107     TBool aBoolean )
       
   108     {
       
   109     XE_ASSERT_DEBUG(iCurrentResult);
       
   110     iCurrentResult->type = XPATH_BOOLEAN;
       
   111     iCurrentResult->boolval = (aBoolean != 0);
       
   112     }
       
   113 
       
   114 void TXmlEngXPathEvaluationContextImpl::SetResultL(
       
   115     const TDesC8& aString )
       
   116     {
       
   117     // Note: OOM flag is checked later (otherwise, there are two checks in the function)
       
   118     xmlChar* copy = xmlCharFromDesC8L(aString);
       
   119    
       
   120     iCurrentResult->type = XPATH_STRING;
       
   121     if(iCurrentResult->stringval)
       
   122         xmlFree(iCurrentResult->stringval);
       
   123     iCurrentResult->stringval = copy;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------           Impementation-specific methods
       
   127 /**
       
   128 Allocates memory for function result
       
   129 
       
   130 @return TRUE is succeeded, FALSE if OOM has happened
       
   131 */
       
   132 TBool TXmlEngXPathEvaluationContextImpl::Initialize()
       
   133     {
       
   134     iCurrentResult = (xmlXPathObjectPtr) xmlMalloc(sizeof(xmlXPathObject));
       
   135     if(iCurrentResult)
       
   136         memset(iCurrentResult, 0, (size_t)sizeof(xmlXPathObject));
       
   137     iCurrentResult->type = XPATH_STRING;
       
   138     return (TBool)iCurrentResult;
       
   139     }
       
   140 
       
   141 void* TXmlEngXPathEvaluationContextImpl::ExtendedContext()
       
   142     {
       
   143     return iContext->comp->extendedContext;
       
   144     }