From f46a1c678e7138eb0641ef6b337cfef92e8a3dda Mon Sep 17 00:00:00 2001 From: "sicking%bigfoot.com" Date: Thu, 27 Sep 2001 21:30:33 +0000 Subject: [PATCH] Cleanup the generate-id() xpath function implementation. b=101672 r=peterv, Pike sr=jst git-svn-id: svn://10.0.0.236/trunk@103978 18797224-902f-48f8-a5cc-f745e15eee43 --- .../source/xml/util/DOMHelper.cpp | 31 ------- .../transformiix/source/xml/util/DOMHelper.h | 6 -- .../source/xslt/ProcessorState.cpp | 10 +-- .../transformiix/source/xslt/ProcessorState.h | 6 -- .../xslt/functions/GenerateIdFunctionCall.cpp | 87 ++++++++++++------- .../source/xslt/functions/XSLTFunctions.h | 4 +- 6 files changed, 58 insertions(+), 86 deletions(-) diff --git a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp index 15288f2fbb3..42ee9d40387 100644 --- a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp +++ b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp @@ -66,37 +66,6 @@ Node* DOMHelper::appearsFirst(Node* node1, Node* node2) { } //-- compareDocumentOrders -/** - * Generates a unique ID for the given node and places the result in - * dest -**/ -void DOMHelper::generateId(Node* node, String& dest) { - - if (!node) { - dest.append(""); - return; - } - - dest.append("id"); - - if (node->getNodeType() == Node::DOCUMENT_NODE) { - Integer::toString(NS_PTR_TO_INT32(node),dest); - return; - } - - Integer::toString(NS_PTR_TO_INT32(node->getOwnerDocument()), dest); - - OrderInfo* orderInfo = getDocumentOrder(node); - - int i = 0; - while (i < orderInfo->size) { - dest.append('.'); - Integer::toString(orderInfo->order[i], dest); - ++i; - } - -} //-- generateId - //-------------------/ //- Private Methods -/ //-------------------/ diff --git a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h index 2ddd52d79bc..1aab7a31e61 100644 --- a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h +++ b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h @@ -80,12 +80,6 @@ public: **/ Node* appearsFirst(Node* node1, Node* node2); - /** - * Generates a unique ID for the given node and places the result in - * dest - **/ - void generateId(Node* node, String& dest); - /** * Returns the child number of the given node. Numbering * starts at 1 for all nodes except the Document node and diff --git a/mozilla/extensions/transformiix/source/xslt/ProcessorState.cpp b/mozilla/extensions/transformiix/source/xslt/ProcessorState.cpp index d6153e87b6a..5265c38b1e4 100644 --- a/mozilla/extensions/transformiix/source/xslt/ProcessorState.cpp +++ b/mozilla/extensions/transformiix/source/xslt/ProcessorState.cpp @@ -424,14 +424,6 @@ Element* ProcessorState::findTemplate(Node* node, Node* context, String* mode) { return matchTemplate; } //-- findTemplate -/** - * Generates a unique ID for the given node and places the result in - * dest -**/ -void ProcessorState::generateId(Node* node, String& dest) { - domHelper.generateId(node, dest); -} //-- generateId - /** * Returns the AttributeSet associated with the given name * or null if no AttributeSet is found @@ -866,7 +858,7 @@ FunctionCall* ProcessorState::resolveFunctionCall(const String& name) { err.append(name); } else if (GENERATE_ID_FN.isEqual(name)) { - return new GenerateIdFunctionCall(&domHelper); + return new GenerateIdFunctionCall(); } else if (SYSTEM_PROPERTY_FN.isEqual(name)) { return new SystemPropertyFunctionCall(); diff --git a/mozilla/extensions/transformiix/source/xslt/ProcessorState.h b/mozilla/extensions/transformiix/source/xslt/ProcessorState.h index 640163c89e8..95b2afb9362 100644 --- a/mozilla/extensions/transformiix/source/xslt/ProcessorState.h +++ b/mozilla/extensions/transformiix/source/xslt/ProcessorState.h @@ -96,12 +96,6 @@ public: **/ Node* copyNode(Node* node); - /** - * Generates a unique ID for the given node and places the result in - * dest - **/ - void generateId(Node* node, String& dest); - /** * Returns the AttributeSet associated with the given name * or null if no AttributeSet is found diff --git a/mozilla/extensions/transformiix/source/xslt/functions/GenerateIdFunctionCall.cpp b/mozilla/extensions/transformiix/source/xslt/functions/GenerateIdFunctionCall.cpp index 7e1777b03b3..8482458f285 100644 --- a/mozilla/extensions/transformiix/source/xslt/functions/GenerateIdFunctionCall.cpp +++ b/mozilla/extensions/transformiix/source/xslt/functions/GenerateIdFunctionCall.cpp @@ -24,18 +24,28 @@ #include "XSLTFunctions.h" #include "Names.h" +#ifdef TX_EXE +#include "stdio.h" +#else +#include "prprf.h" +#endif /* Implementation of XSLT 1.0 extension function: generate-id */ +#ifndef HAVE_64BIT_OS +char* GenerateIdFunctionCall::printfFmt = "id0x%08p"; +#else +char* GenerateIdFunctionCall::printfFmt = "id0x%016p"; +#endif + /** * Creates a new generate-id function call **/ -GenerateIdFunctionCall::GenerateIdFunctionCall(DOMHelper* domHelper) : FunctionCall(GENERATE_ID_FN) -{ - this->domHelper = domHelper; -} //-- GenerateIdFunctionCall +GenerateIdFunctionCall::GenerateIdFunctionCall() + : FunctionCall(GENERATE_ID_FN) +{} /** * Evaluates this Expr based on the given context node and processor state @@ -45,40 +55,53 @@ GenerateIdFunctionCall::GenerateIdFunctionCall(DOMHelper* domHelper) : FunctionC * @return the result of the evaluation * @see FunctionCall.h **/ -ExprResult* GenerateIdFunctionCall::evaluate(Node* context, ContextState* cs) { +ExprResult* GenerateIdFunctionCall::evaluate(Node* aContext, + ContextState* aCs) { - Node* node = context; + if (!requireParams(0, 1, aCs)) + return new StringResult(); - int argc = params.getLength(); + Node* node = 0; - StringResult* stringResult = 0; + // get node to generate id for + if (params.getLength() == 1) { + txListIterator iter(¶ms); + Expr* param = (Expr*)iter.next(); - if (argc > 0) { - ListIterator* iter = params.iterator(); - Expr* param = (Expr*) iter->next(); - delete iter; - ExprResult* exprResult = param->evaluate(context, cs); - if (!exprResult) return new StringResult(""); - if (exprResult->getResultType() == ExprResult::NODESET) { - NodeSet* nodes = (NodeSet*) exprResult; - if (nodes->size() == 0) - stringResult = new StringResult(""); - else - node = nodes->get(0); + ExprResult* exprResult = param->evaluate(aContext, aCs); + if (!exprResult) + return 0; + + if (exprResult->getResultType() != ExprResult::NODESET) { + String err("Invalid argument passed to generate-id(), " + "expecting NodeSet"); + aCs->recieveError(err); + delete exprResult; + return new StringResult(err); } - else { - String err("Invalid argument passed to generate-id(), expecting NodeSet"); - stringResult = new StringResult(err); + + NodeSet* nodes = (NodeSet*) exprResult; + if (nodes->size() > 0) { + aCs->sortByDocumentOrder(nodes); + node = nodes->get(0); } delete exprResult; } + else { + node = aContext; + } - if (stringResult) return stringResult; - - //-- generate id for selected node - String id; - domHelper->generateId(node, id); - return new StringResult(id); - -} //-- evaluate - + // generate id for selected node + char buf[22]; + if (node) { +#ifdef TX_EXE + sprintf(buf, printfFmt, node); +#else + PR_snprintf(buf, 21, printfFmt, node); +#endif + } + else { + buf[0] = 0; + } + return new StringResult(buf); +} diff --git a/mozilla/extensions/transformiix/source/xslt/functions/XSLTFunctions.h b/mozilla/extensions/transformiix/source/xslt/functions/XSLTFunctions.h index 60c36f204e9..7f1ed0127a6 100644 --- a/mozilla/extensions/transformiix/source/xslt/functions/XSLTFunctions.h +++ b/mozilla/extensions/transformiix/source/xslt/functions/XSLTFunctions.h @@ -265,7 +265,7 @@ public: /** * Creates a new generate-id() function call **/ - GenerateIdFunctionCall(DOMHelper* domHelper); + GenerateIdFunctionCall(); /** * Evaluates this Expr based on the given context node and processor state @@ -278,7 +278,7 @@ public: virtual ExprResult* evaluate(Node* context, ContextState* cs); private: - DOMHelper* domHelper; + static char* printfFmt; }; /**