not part of the build, r=peterv, fix 54490, whitespace stripping, +whitespace cleanup in XSLTProcessor.cpp

This commit is contained in:
axel%pike.org 2001-01-10 11:48:48 +00:00
Родитель 55b7b25d1f
Коммит 94df9a7e68
5 изменённых файлов: 66 добавлений и 133 удалений

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

@ -23,7 +23,7 @@
* Lidong, lidong520@263.net
* -- unicode bug fix
*
* $Id: XMLUtils.cpp,v 1.3 2000-03-23 10:17:14 kvisco%ziplink.net Exp $
* $Id: XMLUtils.cpp,v 1.4 2001-01-10 11:48:44 axel%pike.org Exp $
*/
/**
* An XML utility class
@ -207,73 +207,24 @@ void XMLUtils::normalizePIValue(String& piValue) {
} //-- noramlizePIValue
/**
* Strips whitespace from the given String.
* Newlines (#xD), tabs (#x9), and consecutive spaces (#x20) are
* converted to a single space (#x20).
* @param data the String to strip whitespace from
* @param dest the destination String to append the result to
* Is this a whitespace string to be stripped?
* Newlines (#xD), tabs (#x9), spaces (#x20), CRs (#xA) only?
* @param data the String to test for whitespace
**/
void XMLUtils::stripSpace (const String& data, String& dest) {
stripSpace(data,dest,MB_FALSE,MB_FALSE);
} //-- stripSpace
/**
* Strips whitespace from the given String.
* Newlines (#xD), tabs (#x9), and consecutive spaces (#x20) are
* converted to a single space (#x20).
* @param data the String to strip whitespace from
* @param dest the destination String to append the result to
* @param stripAllLeadSpace, a boolean indicating whether or not to
* strip all leading space. If true all whitespace from the start of the
* given String will be stripped. If false, all whitespace from the start
* of the given String will be converted to a single space.
* @param stripAllTrailSpace, a boolean indicating whether or not to
* strip all trailing space. If true all whitespace at the end of the
* given String will be stripped. If false, all whitespace at the end
* of the given String will be converted to a single space.
**/
void XMLUtils::stripSpace
( const String& data,
String& dest,
MBool stripAllLeadSpace,
MBool stripAllTrailSpace )
{
UNICODE_CHAR lastToken, token;
lastToken = 0x0000;
Int32 len = data.length();
Int32 oldLen = dest.length();
// indicates we have seen at least one
// non whitespace charater
MBool validChar = MB_FALSE;
for (Int32 i = 0; i < len; i++) {
token = data.charAt(i);
switch(token) {
case 0x0020: // space
case 0x0009: // tab
case 0x000A: // LF
case 0x000D: // CR
token = 0x0020;
if (stripAllLeadSpace && (!validChar)) break;
if (lastToken != token) dest.append(token);
break;
default:
dest.append(token);
validChar = MB_TRUE;
break;
MBool XMLUtils::shouldStripTextnode (const String& data){
MBool toStrip = MB_TRUE;
for (Int32 i=0;toStrip && i<data.length();i++){
switch(data.charAt(i)) {
case 0x0020: // space
case 0x0009: // tab
case 0x000A: // LF
case 0x000D: // CR
break;
default:
toStrip = MB_FALSE;
break;
}
lastToken = token;
}
//-- remove last trailing space if necessary
if (stripAllTrailSpace) {
len = dest.length();
if ( (len > oldLen) && (dest.charAt(len-1) == 0x0020) ) dest.setLength(len-1);
}
} //-- stripSpace
return toStrip;
} //-- shouldStripTextnode

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

@ -21,7 +21,7 @@
* Keith Visco, kvisco@ziplink.net
* -- original author.
*
* $Id: XMLUtils.h,v 1.3 2000-04-12 22:31:34 nisheeth%netscape.com Exp $
* $Id: XMLUtils.h,v 1.4 2001-01-10 11:48:45 axel%pike.org Exp $
*/
/**
@ -66,33 +66,11 @@ public:
static void normalizePIValue(String& attValue);
/**
* Strips whitespace from the given String.
* Newlines (#xD), tabs (#x9), and consecutive spaces (#x20) are
* converted to a single space (#x20).
* @param data the String to strip whitespace from
* @param dest the destination String to append the result to
* Is this a whitespace string to be stripped?
* Newlines (#xD), tabs (#x9), spaces (#x20), CRs (#xA) only?
* @param data the String to test for whitespace
**/
static void stripSpace (const String& data, String& dest);
/**
* Strips whitespace from the given String.
* Newlines (#xD), tabs (#x9), and consecutive spaces (#x20) are
* converted to a single space (#x20).
* @param data the String to strip whitespace from
* @param dest the destination String to append the result to
* @param stripAllLeadSpace, a boolean indicating whether or not to
* strip all leading space. If true all whitespace from the start of the
* given String will be stripped. If false, all whitespace from the start
* of the given String will be converted to a single space.
* @param stripAllTrailSpace, a boolean indicating whether or not to
* strip all trailing space. If true all whitespace at the end of the
* given String will be stripped. If false, all whitespace at the end
* of the given String will be converted to a single space.
**/
static void stripSpace (const String& data,
String& dest,
MBool stripAllLeadSpace,
MBool stripAllTrailSpace);
static MBool shouldStripTextnode (const String& data);
private:

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

@ -25,7 +25,7 @@
* Nisheeth Ranjan, nisheeth@netscape.com
* -- implemented rint function, which was not available on Windows.
*
* $Id: NumberFunctionCall.cpp,v 1.8 2000-09-16 20:53:01 Peter.VanderBeken%pandora.be Exp $
* $Id: NumberFunctionCall.cpp,v 1.9 2001-01-10 11:48:47 axel%pike.org Exp $
*/
/*
@ -141,20 +141,19 @@ ExprResult* NumberFunctionCall::evaluate(Node* context, ContextState* cs) {
}
else {
String resultStr;
String temp;
XMLDOMUtils::getNodeValue(context, &temp);
if ( cs->isStripSpaceAllowed(context) ) {
XMLUtils::stripSpace(temp, resultStr);
XMLDOMUtils::getNodeValue(context, &resultStr);
if ( cs->isStripSpaceAllowed(context) &&
XMLUtils::shouldStripTextnode(resultStr)) {
result->setValue(Double::NaN);
}
else {
resultStr.append(temp);
Double dbl(resultStr);
result->setValue(dbl.doubleValue());
}
Double dbl(resultStr);
result->setValue(dbl.doubleValue());
}
}
else {
result = new NumberResult(0.0);
result->setValue(Double::NaN);
}
break;
}

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

@ -21,14 +21,14 @@
* Keith Visco, kvisco@ziplink.net
* -- original author.
*
* $Id: StringFunctionCall.cpp,v 1.4 2000-06-11 12:29:29 Peter.VanderBeken%pandora.be Exp $
* $Id: StringFunctionCall.cpp,v 1.5 2001-01-10 11:48:47 axel%pike.org Exp $
*/
/**
* StringFunctionCall
* A representation of the XPath String funtions
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
* @version $Revision: 1.4 $ $Date: 2000-06-11 12:29:29 $
* @version $Revision: 1.5 $ $Date: 2001-01-10 11:48:47 $
**/
#include "FunctionLib.h"
@ -226,12 +226,10 @@ ExprResult* StringFunctionCall::evaluate(Node* context, ContextState* cs) {
evaluateToString((Expr*)iter->next(),context, cs, resultStr);
}
else {
String temp;
XMLDOMUtils::getNodeValue(context, &temp);
if ( cs->isStripSpaceAllowed(context) ) {
XMLUtils::stripSpace(temp, resultStr);
}
else resultStr.append(temp);
XMLDOMUtils::getNodeValue(context, &resultStr);
if ( cs->isStripSpaceAllowed(context) &&
XMLUtils::shouldStripTextnode(resultStr))
resultStr = "";
}
result = new StringResult(resultStr);
}

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

@ -38,7 +38,7 @@
* Olivier Gerardin
* -- Changed behavior of passing parameters to templates
*
* $Id: XSLTProcessor.cpp,v 1.26 2000-11-16 22:08:58 axel%pike.org Exp $
* $Id: XSLTProcessor.cpp,v 1.27 2001-01-10 11:48:48 axel%pike.org Exp $
*/
#include "XSLTProcessor.h"
@ -53,7 +53,7 @@
/**
* XSLTProcessor is a class for Processing XSL styelsheets
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
* @version $Revision: 1.26 $ $Date: 2000-11-16 22:08:58 $
* @version $Revision: 1.27 $ $Date: 2001-01-10 11:48:48 $
**/
/**
@ -142,7 +142,7 @@ void XSLTProcessor::print
MBool indent = MB_FALSE;
if (format->isMethodExplicit()) {
if (format->isHTMLOutput()) xmlPrinter = new HTMLPrinter(*target);
else if (format->isTextOutput()) xmlPrinter = new TEXTPrinter(*target);
else if (format->isTextOutput()) xmlPrinter = new TEXTPrinter(*target);
else xmlPrinter = new XMLPrinter(*target);
indent = format->getIndent();
}
@ -836,15 +836,23 @@ void XSLTProcessor::processAction
const String curValue = ((Text*)xslAction)->getData();
//-- set leading + trailing whitespace stripping flags
MBool stripLWS = (MBool) (xslAction->getPreviousSibling());
MBool stripTWS = (MBool) (xslAction->getNextSibling());
XMLUtils::stripSpace(curValue,textValue, stripLWS, stripTWS);
//-- create new text node and add it to the result tree
//-- if necessary
#ifdef DEBUG_ah
Node* priorNode = xslAction->getPreviousSibling();
Node* nextNode = xslAction->getNextSibling();
if (priorNode && (priorNode->getNodeType()==Node::TEXT_NODE))
printf("Textnode found in prior in whitespace strip\n");
if (nextNode && (nextNode->getNodeType()==Node::TEXT_NODE))
printf("Textnode found in next in whitespace strip\n");
#endif
if (XMLUtils::shouldStripTextnode(curValue))
textValue="";
else textValue=curValue;
}
else {
textValue = ((Text*)xslAction)->getData();
}
//-- create new text node and add it to the result tree
//-- if necessary
if ( textValue.length() > 0)
ps->addToResultTree(resultDoc->createTextNode(textValue));
return;
@ -1270,14 +1278,13 @@ void XSLTProcessor::processAction
NodeSet* nodes = (NodeSet*)exprResult;
if ( nodes->size() > 0) {
Node* node = nodes->get(0);
if ( ps->isStripSpaceAllowed(node) ) {
const String temp = value;
if ( ps->isStripSpaceAllowed(node) &&
XMLUtils::shouldStripTextnode(value))
value.clear();
XMLUtils::stripSpace(temp, value);
}
}
}
ps->addToResultTree(resultDoc->createTextNode(value));
if (value.length()>0)
ps->addToResultTree(resultDoc->createTextNode(value));
delete exprResult;
break;
}
@ -1465,7 +1472,7 @@ NamedMap* XSLTProcessor::processParameters(Element* xslAction, Node* context, Pr
VariableBinding* binding = new VariableBinding(name, exprResult);
params->put((const String&)name, binding);
}
}
}
}
}
}
@ -1753,16 +1760,16 @@ XSLTProcessor::TransformDocument(nsIDOMNode* aSourceDOM,
process(sourceNode, sourceNode, ps);
if (aObserver) {
nsresult res = NS_OK;
nsAutoString topic; topic.Assign(NS_LITERAL_STRING("xslt-done"));
nsresult res = NS_OK;
nsAutoString topic; topic.Assign(NS_LITERAL_STRING("xslt-done"));
nsCOMPtr<nsIObserverService> anObserverService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
nsIDOMNode* docElement = (resultDocument->getDocumentElement())->getNSObj();
nsCOMPtr<nsIObserverService> anObserverService = do_GetService(NS_OBSERVERSERVICE_CONTRACTID, &res);
if (NS_SUCCEEDED(res)) {
nsIDOMNode* docElement = (resultDocument->getDocumentElement())->getNSObj();
anObserverService->AddObserver(aObserver, topic.GetUnicode());
anObserverService->Notify(docElement, topic.GetUnicode(), nsnull);
}
anObserverService->AddObserver(aObserver, topic.GetUnicode());
anObserverService->Notify(docElement, topic.GetUnicode(), nsnull);
}
}
delete ps;