Fix for bug 65858 (Rework XSLT sorting). r=sicking, Pike, sr=jst.

This commit is contained in:
peterv%netscape.com 2001-09-28 12:58:57 +00:00
Родитель 0a24d79dc9
Коммит beb5f6d7a7
24 изменённых файлов: 904 добавлений и 84 удалений

Просмотреть файл

@ -45,7 +45,6 @@ MODULE_NAME = TransformiixModule
CPPSRCS = XSLTProcessorModule.cpp
LOBJS =../source/base/ArrayList.$(OBJ_SUFFIX) \
../source/base/DefaultStringComparator.$(OBJ_SUFFIX) \
../source/base/Double.$(OBJ_SUFFIX) \
../source/base/Integer.$(OBJ_SUFFIX) \
../source/base/List.$(OBJ_SUFFIX) \
@ -54,7 +53,6 @@ LOBJS =../source/base/ArrayList.$(OBJ_SUFFIX) \
../source/base/NamedMap.$(OBJ_SUFFIX) \
../source/base/SimpleErrorObserver.$(OBJ_SUFFIX) \
../source/base/Stack.$(OBJ_SUFFIX) \
../source/base/StringComparator.$(OBJ_SUFFIX) \
../source/base/StringList.$(OBJ_SUFFIX) \
../source/base/Tokenizer.$(OBJ_SUFFIX) \
../source/base/MozillaString.$(OBJ_SUFFIX) \
@ -132,8 +130,9 @@ LOBJS =../source/base/ArrayList.$(OBJ_SUFFIX) \
../source/xslt/functions/GenerateIdFunctionCall.$(OBJ_SUFFIX) \
../source/xslt/functions/txKeyFunctionCall.$(OBJ_SUFFIX) \
../source/xslt/functions/SystemPropertyFunctionCall.$(OBJ_SUFFIX) \
../source/xslt/util/NodeSorter.$(OBJ_SUFFIX) \
../source/xslt/util/NodeStack.$(OBJ_SUFFIX) \
../source/xslt/util/txNodeSorter.$(OBJ_SUFFIX) \
../source/xslt/util/txXPathResultComparator.$(OBJ_SUFFIX) \
$(NULL)
EXTRA_DSO_LDOPTS += \

Просмотреть файл

@ -41,7 +41,6 @@ CPPSRCS= \
CPP_OBJS= \
..\source\base\$(OBJDIR)\ArrayList.obj \
..\source\base\$(OBJDIR)\DefaultStringComparator.obj \
..\source\base\$(OBJDIR)\Double.obj \
..\source\base\$(OBJDIR)\Integer.obj \
..\source\base\$(OBJDIR)\List.obj \
@ -50,7 +49,6 @@ CPP_OBJS= \
..\source\base\$(OBJDIR)\NamedMap.obj \
..\source\base\$(OBJDIR)\SimpleErrorObserver.obj \
..\source\base\$(OBJDIR)\Stack.obj \
..\source\base\$(OBJDIR)\StringComparator.obj \
..\source\base\$(OBJDIR)\StringList.obj \
..\source\base\$(OBJDIR)\Tokenizer.obj \
..\source\base\$(OBJDIR)\MozillaString.obj \
@ -128,8 +126,9 @@ CPP_OBJS= \
..\source\xslt\functions\$(OBJDIR)\GenerateIdFunctionCall.obj \
..\source\xslt\functions\$(OBJDIR)\txKeyFunctionCall.obj \
..\source\xslt\functions\$(OBJDIR)\SystemPropertyFunctionCall.obj \
..\source\xslt\util\$(OBJDIR)\NodeSorter.obj \
..\source\xslt\util\$(OBJDIR)\NodeStack.obj \
..\source\xslt\util\$(OBJDIR)\txNodeSorter.obj \
..\source\xslt\util\$(OBJDIR)\txXPathResultComparator.obj \
.\$(OBJDIR)\XSLTProcessorModule.obj \
$(NULL)

Двоичный файл не отображается.

Просмотреть файл

Просмотреть файл

@ -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

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -32,7 +32,6 @@ DEFINES= $(DEFINES) -DTX_EXE
CPPSRCS= \
ArrayList.cpp \
DefaultStringComparator.cpp \
Double.cpp \
Integer.cpp \
List.cpp \
@ -41,7 +40,6 @@ CPPSRCS= \
SimpleErrorObserver.cpp \
Stack.cpp \
Map.cpp \
StringComparator.cpp \
StringList.cpp \
Tokenizer.cpp
@ -58,7 +56,6 @@ CPPSRCS = $(CPPSRCS) \
CPP_OBJS= \
.\$(OBJDIR)\ArrayList.obj \
.\$(OBJDIR)\DefaultStringComparator.obj \
.\$(OBJDIR)\Double.obj \
.\$(OBJDIR)\Integer.obj \
.\$(OBJDIR)\List.obj \
@ -67,7 +64,6 @@ CPP_OBJS= \
.\$(OBJDIR)\SimpleErrorObserver.obj \
.\$(OBJDIR)\Stack.obj \
.\$(OBJDIR)\Map.obj \
.\$(OBJDIR)\StringComparator.obj \
.\$(OBJDIR)\StringList.obj \
.\$(OBJDIR)\Tokenizer.obj

Просмотреть файл

@ -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

Просмотреть файл

@ -28,7 +28,6 @@ PROGRAM = ..\..\transformiix.exe
CPP_OBJS= \
../base/$(OBJDIR)/ArrayList.obj \
../base/$(OBJDIR)/CommandLineUtils.obj \
../base/$(OBJDIR)/DefaultStringComparator.obj \
../base/$(OBJDIR)/Double.obj \
../base/$(OBJDIR)/Integer.obj \
../base/$(OBJDIR)/List.obj \
@ -37,7 +36,6 @@ CPP_OBJS= \
../base/$(OBJDIR)/NamedMap.obj \
../base/$(OBJDIR)/SimpleErrorObserver.obj \
../base/$(OBJDIR)/Stack.obj \
../base/$(OBJDIR)/StringComparator.obj \
../base/$(OBJDIR)/StringList.obj \
../base/$(OBJDIR)/Tokenizer.obj \
../base/$(OBJDIR)/TxString.obj \
@ -114,8 +112,9 @@ CPP_OBJS= \
../xslt/functions/$(OBJDIR)/GenerateIdFunctionCall.obj \
../xslt/functions/$(OBJDIR)/txKeyFunctionCall.obj \
../xslt/functions/$(OBJDIR)/SystemPropertyFunctionCall.obj \
../xslt/util/$(OBJDIR)/NodeSorter.obj \
../xslt/util/$(OBJDIR)/NodeStack.obj \
../xslt/util/$(OBJDIR)/txNodeSorter.obj \
../xslt/util/$(OBJDIR)/txXPathResultComparator.obj \
./$(OBJDIR)/transformiix.obj \
$(NULL)

Просмотреть файл

@ -78,8 +78,10 @@ const String WHEN = "when";
const String WITH_PARAM = "with-param";
//-- Attributes
const String CASE_ORDER_ATTR = "case-order";
const String CDATA_ELEMENTS_ATTR = "cdata-section-elements";
const String COUNT_ATTR = "count";
const String DATA_TYPE_ATTR = "data-type";
const String DEFAULT_SPACE_ATTR = "default-space";
const String DOCTYPE_PUBLIC_ATTR = "doctype-public";
const String DOCTYPE_SYSTEM_ATTR = "doctype-system";
@ -90,7 +92,7 @@ const String FORMAT_ATTR = "format";
const String FROM_ATTR = "from";
const String HREF_ATTR = "href";
const String INDENT_ATTR = "indent";
const String LANG_ATTR = "xml:lang";
const String LANG_ATTR = "lang";
const String LEVEL_ATTR = "level";
const String MATCH_ATTR = "match";
const String METHOD_ATTR = "method";
@ -108,14 +110,21 @@ const String USE_ATTR = "use";
const String USE_ATTRIBUTE_SETS_ATTR = "use-attribute-sets";
const String VALUE_ATTR = "value";
const String VERSION_ATTR = "version";
const String XML_LANG_ATTR = "xml:lang";
//-- Attribute Values
const String ANY_VALUE = "any";
const String ASCENDING_VALUE = "ascending";
const String DESCENDING_VALUE = "descending";
const String LOWER_FIRST_VALUE = "lower-first";
const String MULTIPLE_VALUE = "multiple";
const String NO_VALUE = "no";
const String NUMBER_VALUE = "number";
const String PRESERVE_VALUE = "preserve";
const String SINGLE_VALUE = "single";
const String STRIP_VALUE = "strip";
const String TEXT_VALUE = "text";
const String UPPER_FIRST_VALUE = "upper-first";
const String YES_VALUE = "yes";
//-- Stylesheet attributes

Просмотреть файл

@ -77,8 +77,10 @@ extern const String WITH_PARAM;
//-- Attributes
extern const String CASE_ORDER_ATTR;
extern const String CDATA_ELEMENTS;
extern const String COUNT_ATTR;
extern const String DATA_TYPE_ATTR;
extern const String DEFAULT_SPACE_ATTR;
extern const String DOCTYPE_PUBLIC_ATTR;
extern const String DOCTYPE_SYSTEM_ATTR;
@ -107,14 +109,21 @@ extern const String USE_ATTR;
extern const String USE_ATTRIBUTE_SETS_ATTR;
extern const String VALUE_ATTR;
extern const String VERSION_ATTR;
extern const String XML_LANG_ATTR;
//-- Attribute Values
extern const String ANY_VALUE;
extern const String ASCENDING_VALUE;
extern const String DESCENDING_VALUE;
extern const String LOWER_FIRST_VALUE;
extern const String MULTIPLE_VALUE;
extern const String NO_VALUE;
extern const String NUMBER_VALUE;
extern const String PRESERVE_VALUE;
extern const String SINGLE_VALUE;
extern const String STRIP_VALUE;
extern const String TEXT_VALUE;
extern const String UPPER_FIRST_VALUE;
extern const String YES_VALUE;
//-- Stylesheet attributes

Просмотреть файл

@ -620,6 +620,29 @@ Node* ProcessorState::popCurrentNode() {
return currentNodeStack.pop();
} //-- popCurrentNode
void ProcessorState::processAttrValueTemplate(const String& aAttValue,
Node* aContext,
String& aResult)
{
aResult.clear();
AttributeValueTemplate* avt =
exprParser.createAttributeValueTemplate(aAttValue);
if (!avt) {
// XXX ErrorReport: out of memory
return;
}
ExprResult* exprResult = avt->evaluate(aContext, this);
delete avt;
if (!exprResult) {
// XXX ErrorReport: out of memory
return;
}
exprResult->stringValue(aResult);
delete exprResult;
}
/**
* Adds the given XSLT action to the top of the action stack
**/

Просмотреть файл

@ -238,6 +238,10 @@ public:
**/
Node* popCurrentNode();
void processAttrValueTemplate(const String& aAttValue,
Node* aContext,
String& aResult);
/**
* Adds the given XSLT action to the top of the action stack
**/

Просмотреть файл

@ -46,7 +46,7 @@
#include "VariableBinding.h"
#include "XMLUtils.h"
#include "XMLDOMUtils.h"
#include "NodeSorter.h"
#include "txNodeSorter.h"
#include "Numbering.h"
#include "Tokenizer.h"
#include "URIUtils.h"
@ -1022,24 +1022,24 @@ void XSLTProcessor::processAction
//-- make sure nodes are in DocumentOrder
ps->sortByDocumentOrder(nodeSet);
//-- look for xsl:sort elements
//-- push nodeSet onto context stack
ps->getNodeSetStack()->push(nodeSet);
// Look for xsl:sort elements
txNodeSorter sorter(ps);
Node* child = actionElement->getFirstChild();
while (child) {
if (child->getNodeType() == Node::ELEMENT_NODE) {
String nodeName = child->getNodeName();
if (getElementType(nodeName, ps) == XSLType::SORT) {
NodeSorter::sort(nodeSet, (Element*)child, node, ps);
break;
}
if ((child->getNodeType() == Node::ELEMENT_NODE) &&
(getElementType(child->getNodeName(), ps) == XSLType::SORT)) {
sorter.addSortElement((Element*)child, node);
}
child = child->getNextSibling();
}
sorter.sortNodeSet(nodeSet);
// Process xsl:with-param elements
NamedMap* actualParams = processParameters(actionElement, node, ps);
//-- push nodeSet onto context stack
ps->getNodeSetStack()->push(nodeSet);
for (int i = 0; i < nodeSet->size(); i++) {
Node* currNode = nodeSet->get(i);
Element* xslTemplate = ps->findTemplate(currNode, node, mode);
@ -1076,7 +1076,7 @@ void XSLTProcessor::processAction
String ns = actionElement->getAttribute(NAMESPACE_ATTR);
//-- process name as an AttributeValueTemplate
String name;
processAttrValueTemplate(attr->getValue(),name,node,ps);
ps->processAttrValueTemplate(attr->getValue(), node, name);
Attr* newAttr = 0;
//-- check name validity
if ( XMLUtils::isValidQName(name)) {
@ -1210,7 +1210,7 @@ void XSLTProcessor::processAction
String ns = actionElement->getAttribute(NAMESPACE_ATTR);
//-- process name as an AttributeValueTemplate
String name;
processAttrValueTemplate(attr->getValue(),name,node,ps);
ps->processAttrValueTemplate(attr->getValue(), node, name);
Element* element = 0;
//-- check name validity
if ( XMLUtils::isValidQName(name)) {
@ -1267,27 +1267,40 @@ void XSLTProcessor::processAction
//-- make sure nodes are in DocumentOrder
ps->sortByDocumentOrder(nodeSet);
//-- look for xsl:sort elements
//-- push nodeSet onto context stack
ps->getNodeSetStack()->push(nodeSet);
// Look for xsl:sort elements
txNodeSorter sorter(ps);
Node* child = actionElement->getFirstChild();
while (child) {
if (child->getNodeType() == Node::ELEMENT_NODE) {
String nodeName = child->getNodeName();
if (getElementType(nodeName, ps) == XSLType::SORT) {
NodeSorter::sort(nodeSet, (Element*)child, node, ps);
int nodeType = child->getNodeType();
if (nodeType == Node::ELEMENT_NODE) {
if (getElementType(child->getNodeName(), ps) == XSLType::SORT) {
sorter.addSortElement((Element*)child, node);
}
else {
// xsl:sort must occur first
break;
}
}
else if ((nodeType == Node::TEXT_NODE ||
nodeType == Node::CDATA_SECTION_NODE) &&
!XMLUtils::isWhitespace(child->getNodeValue())) {
break;
}
child = child->getNextSibling();
}
sorter.sortNodeSet(nodeSet);
//-- push nodeSet onto context stack
ps->getNodeSetStack()->push(nodeSet);
for (int i = 0; i < nodeSet->size(); i++) {
Node* currNode = nodeSet->get(i);
ps->pushCurrentNode(currNode);
processChildren(currNode, actionElement, ps);
ps->popCurrentNode();
}
//-- remove nodeSet from context stack
ps->getNodeSetStack()->pop();
}
@ -1368,7 +1381,7 @@ void XSLTProcessor::processAction
String ns = actionElement->getAttribute(NAMESPACE_ATTR);
//-- process name as an AttributeValueTemplate
String name;
processAttrValueTemplate(attr->getValue(),name,node,ps);
ps->processAttrValueTemplate(attr->getValue(), node, name);
//-- check name validity
if ( !XMLUtils::isValidQName(name)) {
String err("error processing xsl:");
@ -1532,7 +1545,7 @@ void XSLTProcessor::processAction
Attr* newAttr = resultDoc->createAttribute(attr->getName());
//-- process Attribute Value Templates
String value;
processAttrValueTemplate(attr->getValue(), value, node, ps);
ps->processAttrValueTemplate(attr->getValue(), node, value);
newAttr->setValue(value);
ps->addToResultTree(newAttr);
}
@ -1586,32 +1599,6 @@ void XSLTProcessor::processAttributeSets
}
} //-- processAttributeSets
/**
* Processes the given attribute value as an AttributeValueTemplate
* @param attValue the attribute value to process
* @param result, the String in which to store the result
* @param context the current context node
* @param ps the current ProcessorState
**/
void XSLTProcessor::processAttrValueTemplate
(const String& attValue, String& result, Node* context, ProcessorState* ps)
{
AttributeValueTemplate* avt = 0;
avt = exprParser.createAttributeValueTemplate(attValue);
if (!avt) {
return;
}
ExprResult* exprResult = avt->evaluate(context,ps);
if (!exprResult) {
return;
}
exprResult->stringValue(result);
delete exprResult;
delete avt;
} //-- processAttributeValueTemplate
/**
* Processes the xsl:with-param child elements of the given xsl action.
* A VariableBinding is created for each actual parameter, and

Просмотреть файл

@ -218,11 +218,6 @@ private:
**/
SimpleErrorObserver fatalObserver;
/**
* An expression parser for creating AttributeValueTemplates
**/
ExprParser exprParser;
/**
* The version of XSL which this Processes
**/
@ -313,16 +308,6 @@ private:
**/
void processAttributeSets(const String& names, Node* node, ProcessorState* ps);
/**
* Processes the given attribute value as an AttributeValueTemplate
* @param attValue the attribute value to process
* @param result, the String in which to store the result
* @param context the current context node
* @param ps the current ProcessorState
**/
void processAttrValueTemplate
(const String& attValue, String& result, Node* context, ProcessorState* ps);
/**
* Processes the children of the specified element using the given context node
* and ProcessorState

Просмотреть файл

@ -34,10 +34,11 @@ REQUIRES = string \
layout \
content \
widget \
locale \
$(NULL)
endif
CPPSRCS = NodeSorter.cpp NodeStack.cpp
CPPSRCS = NodeStack.cpp txNodeSorter.cpp txXPathResultComparator.cpp
include $(topsrcdir)/config/rules.mk

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -27,6 +27,7 @@ REQUIRES = string \
layout \
widget \
content \
locale \
$(NULL)
include <$(DEPTH)/config/config.mak>
@ -36,12 +37,14 @@ DEFINES=$(DEFINES) -DTX_EXE
CPPSRCS= \
NodeStack.cpp \
NodeSorter.cpp \
txNodeSorter.cpp \
txXPathResultComparator.cpp \
$(NULL)
CPP_OBJS= \
.\$(OBJDIR)\NodeStack.obj \
.\$(OBJDIR)\NodeSorter.obj \
.\$(OBJDIR)\txNodeSorter.obj \
.\$(OBJDIR)\txXPathResultComparator.obj \
$(NULL)
EXPORTS = \

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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