From 1f80f4903a11e73b2a4d5b873cbc84e7645b7e2b Mon Sep 17 00:00:00 2001 From: "nisheeth%netscape.com" Date: Thu, 11 Mar 1999 05:29:36 +0000 Subject: [PATCH] - Beautified error reporting of expat errors. We now don't just dump the error text out as nodes. We create the parsererror and sourcetext HTML nodes that can be styled via ua.css. git-svn-id: svn://10.0.0.236/trunk@23665 18797224-902f-48f8-a5cc-f745e15eee43 --- .../xml/document/src/nsXMLContentSink.cpp | 123 ++++++++++++++++-- .../xml/document/src/nsXMLContentSink.h | 2 + .../xml/document/src/nsXMLContentSink.cpp | 123 ++++++++++++++++-- .../xml/document/src/nsXMLContentSink.h | 2 + 4 files changed, 232 insertions(+), 18 deletions(-) diff --git a/mozilla/content/xml/document/src/nsXMLContentSink.cpp b/mozilla/content/xml/document/src/nsXMLContentSink.cpp index 9d9c1f95bbd..65613d9a293 100644 --- a/mozilla/content/xml/document/src/nsXMLContentSink.cpp +++ b/mozilla/content/xml/document/src/nsXMLContentSink.cpp @@ -71,6 +71,8 @@ static NS_DEFINE_IID(kIDOMCommentIID, NS_IDOMCOMMENT_IID); static NS_DEFINE_IID(kIScrollableViewIID, NS_ISCROLLABLEVIEW_IID); static NS_DEFINE_IID(kIDOMNodeIID, NS_IDOMNODE_IID); +static void SetTextStringOnTextNode(const nsString& aTextString, nsIContent* aTextNode); + #define XML_PSEUDO_ELEMENT 0 // XXX Open Issues: @@ -719,22 +721,125 @@ nsXMLContentSink::AddLeaf(const nsIParserNode& aNode) return NS_OK; } -NS_IMETHODIMP -nsXMLContentSink::NotifyError(const nsParserError* aError) -{ - nsString errorText("XML Parser Error: '"); +nsresult nsXMLContentSink::CreateErrorText(const nsParserError* aError, nsString& aErrorString) +{ + nsString errorText("XML Parsing Error: "); if (aError) { errorText.Append(aError->description); - errorText.Append("', Line: "); + errorText.Append("\nLine Number "); errorText.Append(aError->lineNumber, 10); - errorText.Append(", Column: "); + errorText.Append(", Column "); errorText.Append(aError->colNumber, 10); + errorText.Append(":"); + } + + aErrorString = errorText; + + return NS_OK; +} + +nsresult nsXMLContentSink::CreateSourceText(const nsParserError* aError, nsString& aSourceString) +{ + nsString sourceText; + PRInt32 errorPosition = aError->colNumber; + + sourceText.Append(aError->sourceLine); + sourceText.Append("\n"); + for (int i = 0; i < errorPosition - 1; i++) + sourceText.Append("-"); + sourceText.Append("^"); + + aSourceString = sourceText; + + return NS_OK; +} + +static void SetTextStringOnTextNode(const nsString& aTextString, nsIContent* aTextNode) +{ + nsITextContent* text = nsnull; + PRUnichar *tempUnicode = aTextString.ToNewUnicode(); + static NS_DEFINE_IID(kITextContentIID, NS_ITEXT_CONTENT_IID); + + aTextNode->QueryInterface(kITextContentIID, (void**) &text); + text->SetText(tempUnicode, aTextString.Length(), PR_FALSE); + delete [] tempUnicode; + NS_RELEASE(text); +} + + +/* + * We create the following XML snippet programmatically and + * insert it into the content model: + * + * + * XML Error: "contents of aError->description" + * Line Number: "contents of aError->lineNumber" + * + * "Contents of aError->sourceLine" + * "^ pointing at the error location" + * + * + * + */ +NS_IMETHODIMP +nsXMLContentSink::NotifyError(const nsParserError* aError) +{ + nsresult result = NS_OK; + nsAutoString parserErrorTag = "parsererror"; + nsAutoString sourceTextTag = "sourcetext"; + nsString errorText; + nsString sourceText; + nsIHTMLContent* errorContainerNode = nsnull; + nsIHTMLContent* sourceContainerNode = nsnull; + nsIContent* errorTextNode = nsnull; + nsIContent* sourceTextNode = nsnull; + + /* Create container and text content nodes */ + result = NS_CreateHTMLElement(&errorContainerNode, parserErrorTag); + if (NS_OK == result) { + result = NS_NewTextNode(&errorTextNode); + if (NS_OK == result) { + result = NS_CreateHTMLElement(&sourceContainerNode, sourceTextTag); + if (NS_OK == result) { + result = NS_NewTextNode(&sourceTextNode); + } + } } - AddText(errorText); - FlushText(); - return NS_OK; + /* Create the error text string and source text string + and set the text strings into the text nodes. */ + result = CreateErrorText(aError, errorText); + if (NS_OK == result) { + SetTextStringOnTextNode(errorText, errorTextNode); + } + + result = CreateSourceText(aError, sourceText); + if (NS_OK == result) { + SetTextStringOnTextNode(sourceText, sourceTextNode); + } + + /* Hook the content nodes up to the document and to each other */ + if (NS_OK == result) { + errorContainerNode->SetDocument(mDocument, PR_FALSE); + errorTextNode->SetDocument(mDocument, PR_FALSE); + sourceContainerNode->SetDocument(mDocument, PR_FALSE); + sourceTextNode->SetDocument(mDocument, PR_FALSE); + + if (nsnull == mDocElement) { + mDocElement = errorContainerNode; + NS_ADDREF(mDocElement); + mDocument->SetRootContent(mDocElement); + } + else { + mDocElement->AppendChildTo(errorContainerNode, PR_FALSE); + } + errorContainerNode->AppendChildTo(errorTextNode, PR_FALSE); + errorContainerNode->AppendChildTo(sourceContainerNode, PR_FALSE); + sourceContainerNode->AppendChildTo(sourceTextNode, PR_FALSE); + } + + return result; } // nsIXMLContentSink diff --git a/mozilla/content/xml/document/src/nsXMLContentSink.h b/mozilla/content/xml/document/src/nsXMLContentSink.h index f9a820ffeb1..ee2c0f97655 100644 --- a/mozilla/content/xml/document/src/nsXMLContentSink.h +++ b/mozilla/content/xml/document/src/nsXMLContentSink.h @@ -121,6 +121,8 @@ protected: #endif nsresult AddText(const nsString& aString); + nsresult CreateErrorText(const nsParserError* aError, nsString& aErrorString); + nsresult CreateSourceText(const nsParserError* aError, nsString& aSourceString); nsIDocument* mDocument; nsIURL* mDocumentURL; diff --git a/mozilla/layout/xml/document/src/nsXMLContentSink.cpp b/mozilla/layout/xml/document/src/nsXMLContentSink.cpp index 9d9c1f95bbd..65613d9a293 100644 --- a/mozilla/layout/xml/document/src/nsXMLContentSink.cpp +++ b/mozilla/layout/xml/document/src/nsXMLContentSink.cpp @@ -71,6 +71,8 @@ static NS_DEFINE_IID(kIDOMCommentIID, NS_IDOMCOMMENT_IID); static NS_DEFINE_IID(kIScrollableViewIID, NS_ISCROLLABLEVIEW_IID); static NS_DEFINE_IID(kIDOMNodeIID, NS_IDOMNODE_IID); +static void SetTextStringOnTextNode(const nsString& aTextString, nsIContent* aTextNode); + #define XML_PSEUDO_ELEMENT 0 // XXX Open Issues: @@ -719,22 +721,125 @@ nsXMLContentSink::AddLeaf(const nsIParserNode& aNode) return NS_OK; } -NS_IMETHODIMP -nsXMLContentSink::NotifyError(const nsParserError* aError) -{ - nsString errorText("XML Parser Error: '"); +nsresult nsXMLContentSink::CreateErrorText(const nsParserError* aError, nsString& aErrorString) +{ + nsString errorText("XML Parsing Error: "); if (aError) { errorText.Append(aError->description); - errorText.Append("', Line: "); + errorText.Append("\nLine Number "); errorText.Append(aError->lineNumber, 10); - errorText.Append(", Column: "); + errorText.Append(", Column "); errorText.Append(aError->colNumber, 10); + errorText.Append(":"); + } + + aErrorString = errorText; + + return NS_OK; +} + +nsresult nsXMLContentSink::CreateSourceText(const nsParserError* aError, nsString& aSourceString) +{ + nsString sourceText; + PRInt32 errorPosition = aError->colNumber; + + sourceText.Append(aError->sourceLine); + sourceText.Append("\n"); + for (int i = 0; i < errorPosition - 1; i++) + sourceText.Append("-"); + sourceText.Append("^"); + + aSourceString = sourceText; + + return NS_OK; +} + +static void SetTextStringOnTextNode(const nsString& aTextString, nsIContent* aTextNode) +{ + nsITextContent* text = nsnull; + PRUnichar *tempUnicode = aTextString.ToNewUnicode(); + static NS_DEFINE_IID(kITextContentIID, NS_ITEXT_CONTENT_IID); + + aTextNode->QueryInterface(kITextContentIID, (void**) &text); + text->SetText(tempUnicode, aTextString.Length(), PR_FALSE); + delete [] tempUnicode; + NS_RELEASE(text); +} + + +/* + * We create the following XML snippet programmatically and + * insert it into the content model: + * + * + * XML Error: "contents of aError->description" + * Line Number: "contents of aError->lineNumber" + * + * "Contents of aError->sourceLine" + * "^ pointing at the error location" + * + * + * + */ +NS_IMETHODIMP +nsXMLContentSink::NotifyError(const nsParserError* aError) +{ + nsresult result = NS_OK; + nsAutoString parserErrorTag = "parsererror"; + nsAutoString sourceTextTag = "sourcetext"; + nsString errorText; + nsString sourceText; + nsIHTMLContent* errorContainerNode = nsnull; + nsIHTMLContent* sourceContainerNode = nsnull; + nsIContent* errorTextNode = nsnull; + nsIContent* sourceTextNode = nsnull; + + /* Create container and text content nodes */ + result = NS_CreateHTMLElement(&errorContainerNode, parserErrorTag); + if (NS_OK == result) { + result = NS_NewTextNode(&errorTextNode); + if (NS_OK == result) { + result = NS_CreateHTMLElement(&sourceContainerNode, sourceTextTag); + if (NS_OK == result) { + result = NS_NewTextNode(&sourceTextNode); + } + } } - AddText(errorText); - FlushText(); - return NS_OK; + /* Create the error text string and source text string + and set the text strings into the text nodes. */ + result = CreateErrorText(aError, errorText); + if (NS_OK == result) { + SetTextStringOnTextNode(errorText, errorTextNode); + } + + result = CreateSourceText(aError, sourceText); + if (NS_OK == result) { + SetTextStringOnTextNode(sourceText, sourceTextNode); + } + + /* Hook the content nodes up to the document and to each other */ + if (NS_OK == result) { + errorContainerNode->SetDocument(mDocument, PR_FALSE); + errorTextNode->SetDocument(mDocument, PR_FALSE); + sourceContainerNode->SetDocument(mDocument, PR_FALSE); + sourceTextNode->SetDocument(mDocument, PR_FALSE); + + if (nsnull == mDocElement) { + mDocElement = errorContainerNode; + NS_ADDREF(mDocElement); + mDocument->SetRootContent(mDocElement); + } + else { + mDocElement->AppendChildTo(errorContainerNode, PR_FALSE); + } + errorContainerNode->AppendChildTo(errorTextNode, PR_FALSE); + errorContainerNode->AppendChildTo(sourceContainerNode, PR_FALSE); + sourceContainerNode->AppendChildTo(sourceTextNode, PR_FALSE); + } + + return result; } // nsIXMLContentSink diff --git a/mozilla/layout/xml/document/src/nsXMLContentSink.h b/mozilla/layout/xml/document/src/nsXMLContentSink.h index f9a820ffeb1..ee2c0f97655 100644 --- a/mozilla/layout/xml/document/src/nsXMLContentSink.h +++ b/mozilla/layout/xml/document/src/nsXMLContentSink.h @@ -121,6 +121,8 @@ protected: #endif nsresult AddText(const nsString& aString); + nsresult CreateErrorText(const nsParserError* aError, nsString& aErrorString); + nsresult CreateSourceText(const nsParserError* aError, nsString& aSourceString); nsIDocument* mDocument; nsIURL* mDocumentURL;