2004-10-13 13:51:52 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla XForms support.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Novell, Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Allan Beaufour <abeaufour@novell.com>
|
|
|
|
* David Landwehr <dlandwehr@novell.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsXFormsXPathAnalyzer.h"
|
|
|
|
#include "nsIDOMXPathResult.h"
|
2005-02-24 17:35:27 +03:00
|
|
|
#include "nsXFormsUtils.h"
|
2004-10-13 13:51:52 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2005-01-19 19:05:31 +03:00
|
|
|
//#define DEBUG_XF_ANALYZER
|
2004-10-13 13:51:52 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_DECL_CTOR_COUNTER(nsXFormsXPathAnalyzer)
|
|
|
|
|
2005-04-28 11:17:49 +04:00
|
|
|
nsXFormsXPathAnalyzer::nsXFormsXPathAnalyzer(nsIXFormsXPathEvaluator *aEvaluator,
|
|
|
|
nsIDOMNode *aResolver)
|
|
|
|
: mEvaluator(aEvaluator), mResolver(aResolver)
|
2004-10-13 13:51:52 +04:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsXFormsXPathAnalyzer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXFormsXPathAnalyzer::~nsXFormsXPathAnalyzer()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsXFormsXPathAnalyzer);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2005-06-02 11:00:11 +04:00
|
|
|
nsXFormsXPathAnalyzer::Analyze(nsIDOMNode *aContextNode,
|
|
|
|
const nsXFormsXPathNode *aNode,
|
|
|
|
nsIDOMNSXPathExpression *aExpression,
|
|
|
|
const nsAString *aExprString,
|
|
|
|
nsCOMArray<nsIDOMNode> *aSet,
|
2005-06-02 11:19:32 +04:00
|
|
|
PRUint32 aPosition,
|
|
|
|
PRUint32 aSize)
|
2004-10-13 13:51:52 +04:00
|
|
|
{
|
2005-01-19 19:05:31 +03:00
|
|
|
NS_ENSURE_ARG(aContextNode);
|
|
|
|
NS_ENSURE_ARG(aNode);
|
|
|
|
NS_ENSURE_ARG(aExpression);
|
|
|
|
NS_ENSURE_ARG(aExprString);
|
|
|
|
NS_ENSURE_ARG(aSet);
|
2004-10-13 13:51:52 +04:00
|
|
|
|
|
|
|
mCurExpression = aExpression;
|
|
|
|
mCurExprString = aExprString;
|
|
|
|
mCurSet = aSet;
|
2005-06-02 11:00:11 +04:00
|
|
|
mCurSize = aSize;
|
|
|
|
mCurPosition = aPosition;
|
2004-10-13 13:51:52 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_XF_ANALYZER
|
|
|
|
printf("=====================================\n");
|
2006-02-03 17:18:39 +03:00
|
|
|
printf("Analyzing: %s\n", NS_ConvertUTF16toUTF8(*mCurExprString).get());
|
2004-10-13 13:51:52 +04:00
|
|
|
printf("=====================================\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult rv = AnalyzeRecursively(aContextNode, aNode->mChild, 0);
|
|
|
|
#ifdef DEBUG_XF_ANALYZER
|
|
|
|
printf("-------------------------------------\n");
|
|
|
|
#endif
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-01-19 19:05:31 +03:00
|
|
|
// Besides making the set a set, it also makes it sorted.
|
2005-02-24 17:35:27 +03:00
|
|
|
nsXFormsUtils::MakeUniqueAndSort(aSet);
|
2005-01-19 19:05:31 +03:00
|
|
|
|
2004-10-13 13:51:52 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2005-01-19 19:05:31 +03:00
|
|
|
nsXFormsXPathAnalyzer::AnalyzeRecursively(nsIDOMNode *aContextNode,
|
|
|
|
const nsXFormsXPathNode *aNode,
|
|
|
|
PRUint32 aIndent)
|
2004-10-13 13:51:52 +04:00
|
|
|
{
|
|
|
|
nsXFormsXPathNode* t;
|
|
|
|
nsAutoString xp;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDOMXPathResult> result;
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
char strbuf[100];
|
|
|
|
char* strpos;
|
|
|
|
|
|
|
|
#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);
|
2005-04-28 11:17:49 +04:00
|
|
|
printf("\tIsIndex: %d\n", aNode->mIsIndex);
|
2004-10-13 13:51:52 +04:00
|
|
|
#endif
|
|
|
|
|
2005-04-11 12:04:51 +04:00
|
|
|
if (aNode->mEndIndex < 0 || aNode->mStartIndex >= aNode->mEndIndex) {
|
2004-10-13 13:51:52 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool hasContinue = PR_FALSE;
|
|
|
|
strpos = strbuf;
|
|
|
|
*strpos = '\0';
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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 /
|
2005-06-08 15:10:47 +04:00
|
|
|
xp = Substring(*mCurExprString, aNode->mStartIndex + 1,
|
2005-07-20 11:49:26 +04:00
|
|
|
aNode->mEndIndex - aNode->mStartIndex - 1);
|
2004-10-13 13:51:52 +04:00
|
|
|
} else {
|
2005-06-08 15:10:47 +04:00
|
|
|
xp = Substring(*mCurExprString, aNode->mStartIndex,
|
|
|
|
aNode->mEndIndex - aNode->mStartIndex);
|
2004-10-13 13:51:52 +04:00
|
|
|
}
|
2005-06-02 11:00:11 +04:00
|
|
|
rv = mEvaluator->Evaluate(xp, aContextNode, mCurPosition, mCurSize,
|
|
|
|
mResolver, nsIDOMXPathResult::ANY_TYPE,
|
2004-10-13 13:51:52 +04:00
|
|
|
nsnull, getter_AddRefs(result));
|
2005-06-08 15:10:47 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
const PRUnichar *strings[] = { xp.get() };
|
|
|
|
nsXFormsUtils::ReportError(NS_LITERAL_STRING("exprEvaluateError"),
|
|
|
|
strings, 1, nsnull, nsnull);
|
|
|
|
return rv;
|
|
|
|
}
|
2004-10-13 13:51:52 +04:00
|
|
|
|
|
|
|
PRUint16 type;
|
|
|
|
rv = result->GetResultType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2005-04-28 11:17:49 +04:00
|
|
|
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;
|
2005-06-02 11:00:11 +04:00
|
|
|
rv = mEvaluator->Evaluate(indexExpr, aContextNode, mCurPosition, mCurSize,
|
|
|
|
mResolver, nsIDOMXPathResult::STRING_TYPE,
|
2005-04-28 11:17:49 +04:00
|
|
|
nsnull, getter_AddRefs(stringRes));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString indexId;
|
|
|
|
rv = stringRes->GetStringValue(indexId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mIndexesUsed.AppendString(indexId);
|
|
|
|
}
|
|
|
|
|
2004-10-13 13:51:52 +04:00
|
|
|
// 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 (aNode->mChild || (!aNode->mChild && hasContinue)) {
|
|
|
|
#ifdef DEBUG_XF_ANALYZER
|
2006-02-03 17:18:39 +03:00
|
|
|
printf("iterating '%s'\n", NS_ConvertUTF16toUTF8(xp).get());
|
2004-10-13 13:51:52 +04:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
#ifdef DEBUG_XF_ANALYZER
|
2006-02-03 17:18:39 +03:00
|
|
|
printf("collecting '%s'\n", NS_ConvertUTF16toUTF8(xp).get());
|
2004-10-13 13:51:52 +04:00
|
|
|
#endif
|
2005-02-24 17:35:27 +03:00
|
|
|
mCurSet->AppendObject(node);
|
2004-10-13 13:51:52 +04:00
|
|
|
}
|
|
|
|
rv = AnalyzeRecursively(node, aNode->mChild, aIndent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-04-28 11:17:49 +04:00
|
|
|
|
|
|
|
const nsStringArray&
|
|
|
|
nsXFormsXPathAnalyzer::IndexesUsed() const
|
|
|
|
{
|
|
|
|
return mIndexesUsed;
|
|
|
|
}
|