mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-01-26 14:46:02 +00:00
Fix for bug 65858 (Rework XSLT sorting). r=sicking, Pike, sr=jst.
This commit is contained in:
parent
5ab951d60b
commit
009fdc44d4
@ -34,7 +34,6 @@ REQUIRES = string \
|
||||
endif
|
||||
|
||||
CPPSRCS = ArrayList.cpp \
|
||||
DefaultStringComparator.cpp \
|
||||
Double.cpp \
|
||||
Integer.cpp \
|
||||
List.cpp \
|
||||
@ -43,7 +42,6 @@ CPPSRCS = ArrayList.cpp \
|
||||
NamedMap.cpp \
|
||||
SimpleErrorObserver.cpp \
|
||||
Stack.cpp \
|
||||
StringComparator.cpp \
|
||||
StringList.cpp \
|
||||
Tokenizer.cpp
|
||||
|
||||
|
@ -30,7 +30,6 @@ PROGRAM = ../transformiix
|
||||
|
||||
OBJS =../base/ArrayList.$(OBJ_SUFFIX) \
|
||||
../base/CommandLineUtils.$(OBJ_SUFFIX) \
|
||||
../base/DefaultStringComparator.$(OBJ_SUFFIX) \
|
||||
../base/Double.$(OBJ_SUFFIX) \
|
||||
../base/Integer.$(OBJ_SUFFIX) \
|
||||
../base/List.$(OBJ_SUFFIX) \
|
||||
@ -39,7 +38,6 @@ OBJS =../base/ArrayList.$(OBJ_SUFFIX) \
|
||||
../base/NamedMap.$(OBJ_SUFFIX) \
|
||||
../base/SimpleErrorObserver.$(OBJ_SUFFIX) \
|
||||
../base/Stack.$(OBJ_SUFFIX) \
|
||||
../base/StringComparator.$(OBJ_SUFFIX) \
|
||||
../base/StringList.$(OBJ_SUFFIX) \
|
||||
../base/Tokenizer.$(OBJ_SUFFIX) \
|
||||
../base/TxString.$(OBJ_SUFFIX) \
|
||||
@ -120,8 +118,9 @@ OBJS =../base/ArrayList.$(OBJ_SUFFIX) \
|
||||
../xslt/functions/GenerateIdFunctionCall.$(OBJ_SUFFIX) \
|
||||
../xslt/functions/txKeyFunctionCall.$(OBJ_SUFFIX) \
|
||||
../xslt/functions/SystemPropertyFunctionCall.$(OBJ_SUFFIX) \
|
||||
../xslt/util/NodeSorter.$(OBJ_SUFFIX) \
|
||||
../xslt/util/NodeStack.$(OBJ_SUFFIX) \
|
||||
../xslt/util/txNodeSorter.$(OBJ_SUFFIX) \
|
||||
../xslt/util/txXPathResultComparator.$(OBJ_SUFFIX) \
|
||||
transformiix.$(OBJ_SUFFIX)
|
||||
CPP_PROG_LINK = 1
|
||||
include $(topsrcdir)/config/rules.mk
|
||||
|
294
content/xslt/src/xslt/txNodeSorter.cpp
Normal file
294
content/xslt/src/xslt/txNodeSorter.cpp
Normal file
@ -0,0 +1,294 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/* ***** 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 the TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Jonas Sicking.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2001
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <sicking@bigfoot.com>
|
||||
* Peter Van der Beken <peterv@netscape.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 "txNodeSorter.h"
|
||||
#include <string.h>
|
||||
#include "Names.h"
|
||||
#include "ProcessorState.h"
|
||||
#include "txXPathResultComparator.h"
|
||||
|
||||
/*
|
||||
* Sorts Nodes as specified by the W3C XSLT 1.0 Recommendation
|
||||
*/
|
||||
|
||||
#define DEFAULT_LANG "en"
|
||||
|
||||
txNodeSorter::txNodeSorter(ProcessorState* aPs) : mNKeys(0),
|
||||
mPs(aPs)
|
||||
{
|
||||
}
|
||||
|
||||
txNodeSorter::~txNodeSorter()
|
||||
{
|
||||
txListIterator iter(&mSortKeys);
|
||||
while (iter.hasNext()) {
|
||||
SortKey* key = (SortKey*)iter.next();
|
||||
delete key->mComparator;
|
||||
delete key;
|
||||
}
|
||||
}
|
||||
|
||||
MBool txNodeSorter::addSortElement(Element* aSortElement,
|
||||
Node* aContext)
|
||||
{
|
||||
SortKey* key = new SortKey;
|
||||
if (!key) {
|
||||
// XXX ErrorReport: out of memory
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
// Get common attributes
|
||||
String attrValue;
|
||||
|
||||
// Select
|
||||
Attr* attr = aSortElement->getAttributeNode(SELECT_ATTR);
|
||||
if (attr)
|
||||
attrValue = attr->getValue();
|
||||
else
|
||||
attrValue = ".";
|
||||
|
||||
key->mExpr = mPs->getExpr(attrValue);
|
||||
if (!key->mExpr) {
|
||||
// XXX ErrorReport: Out of memory
|
||||
delete key;
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
// Order
|
||||
MBool ascending;
|
||||
MBool hasAttr = getAttrAsAVT(aSortElement, ORDER_ATTR, aContext, attrValue);
|
||||
if (!hasAttr || attrValue.isEqual(ASCENDING_VALUE)) {
|
||||
ascending = MB_TRUE;
|
||||
}
|
||||
else if (attrValue.isEqual(DESCENDING_VALUE)) {
|
||||
ascending = MB_FALSE;
|
||||
}
|
||||
else {
|
||||
delete key;
|
||||
// XXX ErrorReport: unknown value for order attribute
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
|
||||
// Create comparator depending on datatype
|
||||
String dataType;
|
||||
hasAttr = getAttrAsAVT(aSortElement, DATA_TYPE_ATTR, aContext, dataType);
|
||||
if (!hasAttr || dataType.isEqual(TEXT_VALUE)) {
|
||||
// Text comparator
|
||||
|
||||
// Language
|
||||
String lang;
|
||||
if (!getAttrAsAVT(aSortElement, LANG_ATTR, aContext, lang))
|
||||
lang = DEFAULT_LANG;
|
||||
|
||||
// Case-order
|
||||
MBool upperFirst;
|
||||
hasAttr = getAttrAsAVT(aSortElement, CASE_ORDER_ATTR, aContext, attrValue);
|
||||
if (!hasAttr || attrValue.isEqual(UPPER_FIRST_VALUE)) {
|
||||
upperFirst = MB_TRUE;
|
||||
}
|
||||
else if (attrValue.isEqual(LOWER_FIRST_VALUE)) {
|
||||
upperFirst = MB_FALSE;
|
||||
}
|
||||
else {
|
||||
// XXX ErrorReport: unknown value for case-order attribute
|
||||
delete key;
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
key->mComparator = new txResultStringComparator(ascending,
|
||||
upperFirst,
|
||||
lang);
|
||||
}
|
||||
else if (dataType.isEqual(NUMBER_VALUE)) {
|
||||
// Number comparator
|
||||
key->mComparator = new txResultNumberComparator(ascending);
|
||||
}
|
||||
else {
|
||||
// XXX ErrorReport: unknown data-type
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
if (!key->mComparator) {
|
||||
// XXX ErrorReport: out of memory
|
||||
return MB_FALSE;
|
||||
}
|
||||
|
||||
mSortKeys.add(key);
|
||||
mNKeys++;
|
||||
return MB_TRUE;
|
||||
}
|
||||
|
||||
MBool txNodeSorter::sortNodeSet(NodeSet* aNodes)
|
||||
{
|
||||
if (mNKeys == 0)
|
||||
return MB_TRUE;
|
||||
|
||||
txList sortedNodes;
|
||||
txListIterator iter(&sortedNodes);
|
||||
|
||||
int len = aNodes->size();
|
||||
|
||||
// Step through each node in NodeSet...
|
||||
int i;
|
||||
for (i = len - 1; i >= 0; i--) {
|
||||
SortableNode* currNode = new SortableNode(aNodes->get(i), mNKeys);
|
||||
if (!currNode) {
|
||||
// XXX ErrorReport: out of memory
|
||||
iter.reset();
|
||||
while (iter.hasNext()) {
|
||||
SortableNode* sNode = (SortableNode*)iter.next();
|
||||
sNode->clear(mNKeys);
|
||||
delete sNode;
|
||||
}
|
||||
return MB_FALSE;
|
||||
}
|
||||
iter.reset();
|
||||
SortableNode* compNode = (SortableNode*)iter.next();
|
||||
while (compNode && (compareNodes(currNode, compNode) > 0)) {
|
||||
compNode = (SortableNode*)iter.next();
|
||||
}
|
||||
// ... and insert in sorted list
|
||||
iter.addBefore(currNode);
|
||||
}
|
||||
|
||||
// Clean up and set nodes in NodeSet
|
||||
// Note that the nodeset shouldn't be changed until the sort is done
|
||||
// since it's the current-nodeset used during xpath evaluation
|
||||
aNodes->clear();
|
||||
aNodes->setDuplicateChecking(MB_FALSE);
|
||||
iter.reset();
|
||||
while (iter.hasNext()) {
|
||||
SortableNode* sNode = (SortableNode*)iter.next();
|
||||
aNodes->add(sNode->mNode);
|
||||
sNode->clear(mNKeys);
|
||||
delete sNode;
|
||||
}
|
||||
aNodes->setDuplicateChecking(MB_TRUE);
|
||||
|
||||
return MB_TRUE;
|
||||
}
|
||||
|
||||
int txNodeSorter::compareNodes(SortableNode* aSNode1,
|
||||
SortableNode* aSNode2)
|
||||
{
|
||||
txListIterator iter(&mSortKeys);
|
||||
int i;
|
||||
|
||||
// Step through each key until a difference is found
|
||||
for (i = 0; i < mNKeys; i++) {
|
||||
SortKey* key = (SortKey*)iter.next();
|
||||
// Lazy create sort values
|
||||
if (!aSNode1->mSortValues[i]) {
|
||||
mPs->pushCurrentNode(aSNode1->mNode);
|
||||
ExprResult* res = key->mExpr->evaluate(aSNode1->mNode, mPs);
|
||||
mPs->popCurrentNode();
|
||||
if (!res) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
aSNode1->mSortValues[i] = key->mComparator->createSortableValue(res);
|
||||
if (!aSNode1->mSortValues[i]) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
delete res;
|
||||
}
|
||||
if (!aSNode2->mSortValues[i]) {
|
||||
mPs->pushCurrentNode(aSNode2->mNode);
|
||||
ExprResult* res = key->mExpr->evaluate(aSNode2->mNode, mPs);
|
||||
mPs->popCurrentNode();
|
||||
if (!res) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
aSNode2->mSortValues[i] = key->mComparator->createSortableValue(res);
|
||||
if (!aSNode2->mSortValues[i]) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
delete res;
|
||||
}
|
||||
|
||||
// Compare node values
|
||||
int compRes = key->mComparator->compareValues(aSNode1->mSortValues[i],
|
||||
aSNode2->mSortValues[i]);
|
||||
if (compRes != 0)
|
||||
return compRes;
|
||||
}
|
||||
// All keys have the same value for these nodes
|
||||
return 0;
|
||||
}
|
||||
|
||||
MBool txNodeSorter::getAttrAsAVT(Element* aSortElement,
|
||||
const String& aAttrName,
|
||||
Node* aContext,
|
||||
String& aResult)
|
||||
{
|
||||
aResult.clear();
|
||||
Node* tempNode = aSortElement->getAttributeNode(aAttrName);
|
||||
|
||||
if (!tempNode)
|
||||
return MB_FALSE;
|
||||
|
||||
const String& attValue = tempNode->getNodeValue();
|
||||
mPs->processAttrValueTemplate(attValue, aContext, aResult);
|
||||
return MB_TRUE;
|
||||
}
|
||||
|
||||
txNodeSorter::SortableNode::SortableNode(Node* aNode, int aNValues)
|
||||
{
|
||||
mNode = aNode;
|
||||
mSortValues = new TxObject*[aNValues];
|
||||
if (!mSortValues) {
|
||||
// XXX ErrorReport: out of memory
|
||||
return;
|
||||
}
|
||||
memset(mSortValues, 0, aNValues * sizeof(void *));
|
||||
}
|
||||
|
||||
void txNodeSorter::SortableNode::clear(int aNValues)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < aNValues; i++) {
|
||||
delete mSortValues[i];
|
||||
}
|
||||
|
||||
delete [] mSortValues;
|
||||
}
|
97
content/xslt/src/xslt/txNodeSorter.h
Normal file
97
content/xslt/src/xslt/txNodeSorter.h
Normal file
@ -0,0 +1,97 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/* ***** 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 the TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Jonas Sicking.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2001
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <sicking@bigfoot.com>
|
||||
* Peter Van der Beken <peterv@netscape.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 ***** */
|
||||
|
||||
#ifndef TRANSFRMX_NODESORTER_H
|
||||
#define TRANSFRMX_NODESORTER_H
|
||||
|
||||
#include "baseutils.h"
|
||||
#include "List.h"
|
||||
|
||||
class Element;
|
||||
class Expr;
|
||||
class Node;
|
||||
class NodeSet;
|
||||
class ProcessorState;
|
||||
class String;
|
||||
class TxObject;
|
||||
class txXPathResultComparator;
|
||||
|
||||
/*
|
||||
* Sorts Nodes as specified by the W3C XSLT 1.0 Recommendation
|
||||
*/
|
||||
|
||||
class txNodeSorter
|
||||
{
|
||||
public:
|
||||
txNodeSorter(ProcessorState* aPs);
|
||||
~txNodeSorter();
|
||||
|
||||
MBool addSortElement(Element* aSortElement,
|
||||
Node* aContext);
|
||||
MBool sortNodeSet(NodeSet* aNodes);
|
||||
|
||||
private:
|
||||
class SortableNode
|
||||
{
|
||||
public:
|
||||
SortableNode(Node* aNode, int aNValues);
|
||||
void clear(int aNValues);
|
||||
TxObject** mSortValues;
|
||||
Node* mNode;
|
||||
};
|
||||
struct SortKey
|
||||
{
|
||||
Expr* mExpr;
|
||||
txXPathResultComparator* mComparator;
|
||||
};
|
||||
|
||||
int compareNodes(SortableNode* sNode1,
|
||||
SortableNode* sNode2);
|
||||
|
||||
MBool getAttrAsAVT(Element* aSortElement,
|
||||
const String& aAttrName,
|
||||
Node* aContext,
|
||||
String& aResult);
|
||||
|
||||
txList mSortKeys;
|
||||
ProcessorState* mPs;
|
||||
int mNKeys;
|
||||
};
|
||||
|
||||
#endif
|
288
content/xslt/src/xslt/txXPathResultComparator.cpp
Normal file
288
content/xslt/src/xslt/txXPathResultComparator.cpp
Normal file
@ -0,0 +1,288 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/* ***** 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 the TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Jonas Sicking.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2001
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <sicking@bigfoot.com>
|
||||
* Peter Van der Beken <peterv@netscape.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 "txXPathResultComparator.h"
|
||||
#include "Expr.h"
|
||||
#include "txNodeSorter.h"
|
||||
#ifndef TX_EXE
|
||||
#include "nsCollationCID.h"
|
||||
#include "nsILocale.h"
|
||||
#include "nsILocaleService.h"
|
||||
#include "nsIServiceManager.h"
|
||||
#include "nsLocaleCID.h"
|
||||
#include "prmem.h"
|
||||
|
||||
static NS_DEFINE_CID(kCollationFactoryCID, NS_COLLATIONFACTORY_CID);
|
||||
#endif
|
||||
|
||||
#define kAscending (1<<0)
|
||||
#define kUpperFirst (1<<1)
|
||||
|
||||
txResultStringComparator::txResultStringComparator(MBool aAscending,
|
||||
MBool aUpperFirst,
|
||||
const String& aLanguage)
|
||||
{
|
||||
mSorting = 0;
|
||||
if (aAscending)
|
||||
mSorting |= kAscending;
|
||||
if (aUpperFirst)
|
||||
mSorting |= kUpperFirst;
|
||||
#ifndef TX_EXE
|
||||
nsresult rv = init(aLanguage);
|
||||
if (NS_FAILED(rv))
|
||||
NS_ERROR("Failed to initialize txResultStringComparator");
|
||||
#endif
|
||||
}
|
||||
|
||||
txResultStringComparator::~txResultStringComparator()
|
||||
{
|
||||
}
|
||||
|
||||
#ifndef TX_EXE
|
||||
nsresult txResultStringComparator::init(const String& aLanguage)
|
||||
{
|
||||
nsresult rv;
|
||||
|
||||
nsCOMPtr<nsILocaleService> localeService =
|
||||
do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsILocale> locale;
|
||||
rv = localeService->NewLocale(aLanguage.getConstNSString().get(),
|
||||
getter_AddRefs(locale));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsICollationFactory> colFactory =
|
||||
do_CreateInstance(kCollationFactoryCID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = colFactory->CreateCollation(locale, getter_AddRefs(mCollation));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
TxObject* txResultStringComparator::createSortableValue(ExprResult* aExprRes)
|
||||
{
|
||||
StringValue* val = new StringValue;
|
||||
|
||||
if (!val)
|
||||
return 0;
|
||||
|
||||
#ifdef TX_EXE
|
||||
aExprRes->stringValue(val->mStr);
|
||||
// We don't support case-order on standalone
|
||||
val->mStr.toLowerCase();
|
||||
#else
|
||||
if (!mCollation)
|
||||
return 0;
|
||||
|
||||
val->mCaseKey = new String;
|
||||
if (!val->mCaseKey) {
|
||||
delete val;
|
||||
return 0;
|
||||
}
|
||||
|
||||
aExprRes->stringValue(*(String *)val->mCaseKey);
|
||||
const nsString& nsCaseKey = ((String *)val->mCaseKey)->getConstNSString();
|
||||
if (nsCaseKey.IsEmpty()) {
|
||||
return val;
|
||||
}
|
||||
nsresult rv = createRawSortKey(kCollationCaseInSensitive,
|
||||
nsCaseKey,
|
||||
&val->mKey,
|
||||
&val->mLength);
|
||||
if (NS_FAILED(rv)) {
|
||||
NS_ERROR("Failed to create raw sort key");
|
||||
delete val;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
return val;
|
||||
}
|
||||
|
||||
int txResultStringComparator::compareValues(TxObject* aVal1, TxObject* aVal2)
|
||||
{
|
||||
StringValue* strval1 = (StringValue*)aVal1;
|
||||
StringValue* strval2 = (StringValue*)aVal2;
|
||||
#ifdef TX_EXE
|
||||
PRInt32 len1 = strval1->mStr.length();
|
||||
PRInt32 len2 = strval2->mStr.length();
|
||||
PRInt32 minLength = (len1 < len2) ? len1 : len2;
|
||||
|
||||
PRInt32 c = 0;
|
||||
while (c < minLength) {
|
||||
UNICODE_CHAR ch1 = strval1->mStr.charAt(c);
|
||||
UNICODE_CHAR ch2 = strval2->mStr.charAt(c);
|
||||
if (ch1 < ch2)
|
||||
return ((mSorting & kAscending) ? 1 : -1) * -1;
|
||||
if (ch2 < ch1)
|
||||
return ((mSorting & kAscending) ? 1 : -1) * 1;
|
||||
c++;
|
||||
}
|
||||
|
||||
if (len1 == len2)
|
||||
return 0;
|
||||
|
||||
return ((mSorting & kAscending) ? 1 : -1) * ((len1 < len2) ? -1 : 1);
|
||||
#else
|
||||
if (!mCollation)
|
||||
return -1;
|
||||
|
||||
if (strval1->mLength == 0) {
|
||||
if (strval2->mLength == 0)
|
||||
return 0;
|
||||
return ((mSorting & kAscending) ? -1 : 1);
|
||||
}
|
||||
|
||||
if (strval2->mLength == 0)
|
||||
return ((mSorting & kAscending) ? 1 : -1);
|
||||
|
||||
nsresult rv;
|
||||
PRInt32 result = -1;
|
||||
rv = mCollation->CompareRawSortKey(strval1->mKey, strval1->mLength,
|
||||
strval2->mKey, strval2->mLength,
|
||||
&result);
|
||||
if (NS_FAILED(rv)) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (result != 0)
|
||||
return ((mSorting & kAscending) ? 1 : -1) * result;
|
||||
|
||||
if (strval1->mCaseLength < 0) {
|
||||
String* caseString = (String *)strval1->mCaseKey;
|
||||
rv = createRawSortKey(kCollationCaseSensitive,
|
||||
caseString->getConstNSString(),
|
||||
(PRUint8**)&strval1->mCaseKey,
|
||||
&strval1->mCaseLength);
|
||||
if (NS_FAILED(rv)) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
delete caseString;
|
||||
}
|
||||
if (strval2->mCaseLength < 0) {
|
||||
String* caseString = (String *)strval2->mCaseKey;
|
||||
rv = createRawSortKey(kCollationCaseSensitive,
|
||||
caseString->getConstNSString(),
|
||||
(PRUint8**)&strval2->mCaseKey,
|
||||
&strval2->mCaseLength);
|
||||
if (NS_FAILED(rv)) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
delete caseString;
|
||||
}
|
||||
rv = mCollation->CompareRawSortKey((PRUint8*)strval1->mCaseKey, strval1->mCaseLength,
|
||||
(PRUint8*)strval2->mCaseKey, strval2->mCaseLength,
|
||||
&result);
|
||||
if (NS_FAILED(rv)) {
|
||||
// XXX ErrorReport
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ((mSorting & kAscending) ? 1 : -1) * ((mSorting & kUpperFirst) ? 1 : -1) * result;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef TX_EXE
|
||||
nsresult txResultStringComparator::createRawSortKey(const nsCollationStrength aStrength,
|
||||
const nsString& aString,
|
||||
PRUint8** aKey,
|
||||
PRUint32* aLength)
|
||||
{
|
||||
nsresult rv = mCollation->GetSortKeyLen(aStrength, aString, aLength);
|
||||
*aKey = (PRUint8*)PR_MALLOC(*aLength);
|
||||
|
||||
if (!*aKey)
|
||||
return NS_ERROR_OUT_OF_MEMORY;
|
||||
|
||||
rv = mCollation->CreateRawSortKey(aStrength, aString, *aKey, aLength);
|
||||
return rv;
|
||||
}
|
||||
|
||||
txResultStringComparator::StringValue::StringValue() : mKey(0),
|
||||
mLength(0),
|
||||
mCaseKey(0),
|
||||
mCaseLength(-1)
|
||||
{
|
||||
}
|
||||
|
||||
txResultStringComparator::StringValue::~StringValue()
|
||||
{
|
||||
PR_Free(mKey);
|
||||
if (mCaseLength >= 0)
|
||||
PR_Free((PRUint8*)mCaseKey);
|
||||
else
|
||||
delete (String*)mCaseKey;
|
||||
}
|
||||
#endif
|
||||
|
||||
txResultNumberComparator::txResultNumberComparator(MBool aAscending)
|
||||
{
|
||||
mAscending = aAscending ? 1 : -1;
|
||||
}
|
||||
|
||||
TxObject* txResultNumberComparator::createSortableValue(ExprResult* aExprRes)
|
||||
{
|
||||
NumberValue* numval = new NumberValue;
|
||||
if (numval)
|
||||
numval->mVal = aExprRes->numberValue();
|
||||
return numval;
|
||||
}
|
||||
|
||||
int txResultNumberComparator::compareValues(TxObject* aVal1, TxObject* aVal2)
|
||||
{
|
||||
double dval1 = ((NumberValue*)aVal1)->mVal;
|
||||
double dval2 = ((NumberValue*)aVal2)->mVal;
|
||||
|
||||
if (Double::isNaN(dval1))
|
||||
return Double::isNaN(dval2) ? 0 : -mAscending;
|
||||
|
||||
if (Double::isNaN(dval2))
|
||||
return mAscending;
|
||||
|
||||
if (dval1 == dval2)
|
||||
return 0;
|
||||
|
||||
return (dval1 < dval2) ? -mAscending : mAscending;
|
||||
}
|
130
content/xslt/src/xslt/txXPathResultComparator.h
Normal file
130
content/xslt/src/xslt/txXPathResultComparator.h
Normal file
@ -0,0 +1,130 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/* ***** 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 the TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Jonas Sicking.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2001
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Jonas Sicking <sicking@bigfoot.com>
|
||||
* Peter Van der Beken <peterv@netscape.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 ***** */
|
||||
|
||||
#ifndef TRANSFRMX_XPATHRESULTCOMPARATOR_H
|
||||
#define TRANSFRMX_XPATHRESULTCOMPARATOR_H
|
||||
|
||||
#include "TxObject.h"
|
||||
#include "TxString.h"
|
||||
#ifndef TX_EXE
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsICollation.h"
|
||||
#endif
|
||||
|
||||
class ExprResult;
|
||||
|
||||
/*
|
||||
* Result comparators
|
||||
*/
|
||||
class txXPathResultComparator
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Compares two XPath results. Returns -1 if val1 < val2,
|
||||
* 1 if val1 > val2 and 0 if val1 == val2.
|
||||
*/
|
||||
virtual int compareValues(TxObject* val1, TxObject* val2) = 0;
|
||||
|
||||
/*
|
||||
* Create a sortable value.
|
||||
*/
|
||||
virtual TxObject* createSortableValue(ExprResult* exprRes) = 0;
|
||||
};
|
||||
|
||||
/*
|
||||
* Compare results as stings (data-type="text")
|
||||
*/
|
||||
class txResultStringComparator : public txXPathResultComparator
|
||||
{
|
||||
public:
|
||||
txResultStringComparator(MBool aAscending, MBool aUpperFirst,
|
||||
const String& aLanguage);
|
||||
~txResultStringComparator();
|
||||
|
||||
int compareValues(TxObject* aVal1, TxObject* aVal2);
|
||||
TxObject* createSortableValue(ExprResult* aExprRes);
|
||||
private:
|
||||
#ifndef TX_EXE
|
||||
nsCOMPtr<nsICollation> mCollation;
|
||||
nsresult init(const String& aLanguage);
|
||||
nsresult createRawSortKey(const nsCollationStrength aStrength,
|
||||
const nsString& aString,
|
||||
PRUint8** aKey,
|
||||
PRUint32* aLength);
|
||||
#endif
|
||||
int mSorting;
|
||||
|
||||
class StringValue : public TxObject
|
||||
{
|
||||
public:
|
||||
#ifdef TX_EXE
|
||||
String mStr;
|
||||
#else
|
||||
StringValue();
|
||||
~StringValue();
|
||||
|
||||
PRUint8* mKey;
|
||||
void* mCaseKey;
|
||||
PRUint32 mLength, mCaseLength;
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* Compare results as numbers (data-type="number")
|
||||
*/
|
||||
class txResultNumberComparator : public txXPathResultComparator
|
||||
{
|
||||
public:
|
||||
txResultNumberComparator(MBool aAscending);
|
||||
|
||||
int compareValues(TxObject* aVal1, TxObject* aVal2);
|
||||
TxObject* createSortableValue(ExprResult* aExprRes);
|
||||
|
||||
private:
|
||||
int mAscending;
|
||||
|
||||
class NumberValue : public TxObject
|
||||
{
|
||||
public:
|
||||
double mVal;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user