Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
nsXFormsXPathAnalyzer Class Reference

This class analyzes an XPath Expression parse tree (nsXFormsXPathNode), and returns all the nodes that the expression depends on. More...

#include <nsXFormsXPathAnalyzer.h>

Collaboration diagram for nsXFormsXPathAnalyzer:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsXFormsXPathAnalyzer (nsIXFormsXPathEvaluator *aEvaluator, nsIDOMNode *aResolver, nsIDOMNode *aOrigCtxt)
 ~nsXFormsXPathAnalyzer ()
nsresult Analyze (nsIDOMNode *aContextNode, const nsXFormsXPathNode *aNode, nsIDOMNSXPathExpression *aExpression, const nsAString *aExprString, nsCOMArray< nsIDOMNode > *aSet, PRUint32 aSize, PRUint32 aPosition, PRBool aIncludeRoot)
const nsStringArray & IndexesUsed () const

Private Member Functions

nsresult AnalyzeRecursively (nsIDOMNode *aContextNode, const nsXFormsXPathNode *aNode, PRUint32 aIndent, PRBool aCollect=PR_FALSE)

Private Attributes

nsCOMPtr< nsIXFormsXPathEvaluator > mEvaluator
nsCOMPtr< nsIDOMNodemResolver
nsCOMPtr< nsIDOMNodemOrigCtxt
nsCOMArray< nsIDOMNode > * mCurSet
nsCOMPtr< nsIDOMNSXPathExpressionmCurExpression
const nsAString * mCurExprString
PRUint32 mCurSize
PRUint32 mCurPosition
nsStringArray mIndexesUsed

Detailed Description

This class analyzes an XPath Expression parse tree (nsXFormsXPathNode), and returns all the nodes that the expression depends on.

Note:
Should be reimplemented and moved to Transformiix
This class is not thread safe (mCur.*)

Definition at line 59 of file nsXFormsXPathAnalyzer.h.


Constructor & Destructor Documentation

nsXFormsXPathAnalyzer::nsXFormsXPathAnalyzer ( nsIXFormsXPathEvaluator *  aEvaluator,
nsIDOMNode aResolver,
nsIDOMNode aOrigCtxt 
)

Definition at line 48 of file nsXFormsXPathAnalyzer.cpp.

  : mEvaluator(aEvaluator),
    mResolver(aResolver),
    mOrigCtxt(aOrigCtxt)
{
  MOZ_COUNT_CTOR(nsXFormsXPathAnalyzer);
}

Member Function Documentation

nsresult nsXFormsXPathAnalyzer::Analyze ( nsIDOMNode aContextNode,
const nsXFormsXPathNode aNode,
nsIDOMNSXPathExpression aExpression,
const nsAString *  aExprString,
nsCOMArray< nsIDOMNode > *  aSet,
PRUint32  aSize,
PRUint32  aPosition,
PRBool  aIncludeRoot 
)

Definition at line 64 of file nsXFormsXPathAnalyzer.cpp.

{
  NS_ENSURE_ARG(aContextNode);
  NS_ENSURE_ARG(aNode);
  NS_ENSURE_ARG(aExpression);
  NS_ENSURE_ARG(aExprString);
  NS_ENSURE_ARG(aSet);

  mCurExpression = aExpression;
  mCurExprString = aExprString;
  mCurSet = aSet;
  mCurSize = aSize;
  mCurPosition = aPosition;

#ifdef DEBUG_XF_ANALYZER
  printf("=====================================\n");
  printf("Analyzing: %s\n", NS_ConvertUTF16toUTF8(*mCurExprString).get());
  printf("=====================================\n");
#endif

  nsresult rv = AnalyzeRecursively(aContextNode, aNode->mChild, 0,
                                   aIncludeRoot);
#ifdef DEBUG_XF_ANALYZER
  printf("-------------------------------------\n");
#endif
  NS_ENSURE_SUCCESS(rv, rv);

  // Besides making the set a set, it also makes it sorted.
  nsXFormsUtils::MakeUniqueAndSort(aSet);

  return NS_OK;
}

Here is the call graph for this function:

nsresult nsXFormsXPathAnalyzer::AnalyzeRecursively ( nsIDOMNode aContextNode,
const nsXFormsXPathNode aNode,
PRUint32  aIndent,
PRBool  aCollect = PR_FALSE 
) [private]

Definition at line 106 of file nsXFormsXPathAnalyzer.cpp.

{
  nsXFormsXPathNode* t;
  nsAutoString xp;
  nsresult rv;
  nsCOMPtr<nsIDOMXPathResult> result;
  nsCOMPtr<nsIDOMNode> node;
#ifdef DEBUG_XF_ANALYZER
  char strbuf[100];
  char* strpos;
#endif

#ifdef DEBUG_beaufour_xxx
  printf("nsXFormsXPathParser::AnalyzeRecursively(%p)\n", (void*) aNode);
#endif
  
  for (; aNode; aNode = aNode->mSibling) {
#ifdef DEBUG_beaufour_xxx
    printf("\tChild: %p, Sibling: %p\n", (void*) aNode->mChild, (void*) aNode->mSibling);
    printf("\tIndex: %d - %d\n", aNode->mStartIndex, aNode->mEndIndex);
    printf("\tCon: %d, Predicate: %d, Literal: %d\n", aNode->mCon, aNode->mPredicate, aNode->mLiteral);
    printf("\tIsIndex: %d\n", aNode->mIsIndex);
#endif

    if (aNode->mEndIndex < 0 || aNode->mStartIndex >= aNode->mEndIndex) {
      continue;
    }

    PRBool hasContinue = PR_FALSE;
    
    // hasContinue == whether we have a child with a mCon
    t = aNode->mChild;
    while (t && !hasContinue) {
      hasContinue = t->mCon;
      t = t->mSibling;
    }

#ifdef DEBUG_XF_ANALYZER
    strpos = strbuf;
    *strpos = '\0';
    for (PRUint32 j = 0; j < aIndent; ++j) {
      strpos += sprintf(strpos, "  ");
    }
    
    strpos += sprintf(strpos, "<%s> ", hasContinue ? "C" : " ");
    
    if (aNode->mPredicate) {
      if (!(aNode->mChild)) {
        strpos += sprintf(strpos, "[PredicateVal], ");
      } else {
        strpos += sprintf(strpos, "[Predicate], ");
      }
    } else {
      if (!(aNode->mChild)) {
        strpos += sprintf(strpos, "[AxisStepsVal], ");
      } else {
        if (!hasContinue) {
          strpos += printf(strpos, "[AxisSteps], ");
        }
      }
    }
#endif

    if (aNode->mCon) {
      // Remove the leading /
      xp = Substring(*mCurExprString, aNode->mStartIndex + 1,
                     aNode->mEndIndex - aNode->mStartIndex - 1);
    } else {
      xp = Substring(*mCurExprString, aNode->mStartIndex,
                     aNode->mEndIndex - aNode->mStartIndex);
    }
    rv = mEvaluator->Evaluate(xp, aContextNode, mCurPosition, mCurSize,
                              mResolver, mOrigCtxt, nsIDOMXPathResult::ANY_TYPE,
                              nsnull, getter_AddRefs(result));
    if (NS_FAILED(rv)) {
      const PRUnichar *strings[] = { xp.get() };
      nsXFormsUtils::ReportError(NS_LITERAL_STRING("exprEvaluateError"),
                                 strings, 1, nsnull, nsnull);
      return rv;
    }

    PRUint16 type;
    rv = result->GetResultType(&type);
    NS_ENSURE_SUCCESS(rv, rv);

    if (aNode->mIsIndex) {
      // Extract index parameter, xp is "index(parameter)"
      const PRUint32 indexSize = sizeof("index(") - 1;
      nsDependentSubstring indexExpr = Substring(xp,
                                                 indexSize,
                                                 xp.Length() - indexSize - 1); // remove final ')' too
      nsCOMPtr<nsIDOMXPathResult> stringRes;
      rv = mEvaluator->Evaluate(indexExpr, aContextNode, mCurPosition, mCurSize,
                                mResolver, mOrigCtxt,
                                nsIDOMXPathResult::STRING_TYPE,
                                nsnull, getter_AddRefs(stringRes));
      NS_ENSURE_SUCCESS(rv, rv);

      nsAutoString indexId;
      rv = stringRes->GetStringValue(indexId);
      NS_ENSURE_SUCCESS(rv, rv);
      mIndexesUsed.AppendString(indexId);
    }

    // We are only interested in nodes
    if (   type != nsIDOMXPathResult::UNORDERED_NODE_ITERATOR_TYPE
        && type != nsIDOMXPathResult::ORDERED_NODE_ITERATOR_TYPE) {
      continue;
    }

    while (NS_SUCCEEDED(result->IterateNext(getter_AddRefs(node))) && node) {
      NS_ENSURE_SUCCESS(rv, rv);
#ifdef DEBUG_XF_ANALYZER
      printf(strbuf);
#endif
      if (!aCollect && (aNode->mChild || (!aNode->mChild && hasContinue))) {
#ifdef DEBUG_XF_ANALYZER
        printf("iterating '%s'\n", NS_ConvertUTF16toUTF8(xp).get());
#endif
      } else {
#ifdef DEBUG_XF_ANALYZER
        printf("collecting '%s'\n", NS_ConvertUTF16toUTF8(xp).get());
#endif
        mCurSet->AppendObject(node);
      }
      rv = AnalyzeRecursively(node, aNode->mChild, aIndent + 1);
    }

    NS_ENSURE_SUCCESS(rv, rv);
  }
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const nsStringArray & nsXFormsXPathAnalyzer::IndexesUsed ( ) const

Definition at line 243 of file nsXFormsXPathAnalyzer.cpp.

{
  return mIndexesUsed;
}

Member Data Documentation

Definition at line 66 of file nsXFormsXPathAnalyzer.h.

Definition at line 67 of file nsXFormsXPathAnalyzer.h.

Definition at line 69 of file nsXFormsXPathAnalyzer.h.

Definition at line 65 of file nsXFormsXPathAnalyzer.h.

Definition at line 68 of file nsXFormsXPathAnalyzer.h.

nsCOMPtr<nsIXFormsXPathEvaluator> nsXFormsXPathAnalyzer::mEvaluator [private]

Definition at line 61 of file nsXFormsXPathAnalyzer.h.

nsStringArray nsXFormsXPathAnalyzer::mIndexesUsed [private]

Definition at line 70 of file nsXFormsXPathAnalyzer.h.

Definition at line 63 of file nsXFormsXPathAnalyzer.h.

Definition at line 62 of file nsXFormsXPathAnalyzer.h.


The documentation for this class was generated from the following files: