- 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
This commit is contained in:
nisheeth%netscape.com
1999-03-11 05:29:36 +00:00
parent 840ff7b218
commit 1f80f4903a
4 changed files with 232 additions and 18 deletions

View File

@@ -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:
*
* <ParserError>
* XML Error: "contents of aError->description"
* Line Number: "contents of aError->lineNumber"
* <SourceText>
* "Contents of aError->sourceLine"
* "^ pointing at the error location"
* </SourceText>
* </ParserError>
*
*/
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

View File

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