This commit is contained in:
rickg%netscape.com 1998-08-22 17:49:36 +00:00
Родитель b491633bcf
Коммит 6d71410d47
6 изменённых файлов: 438 добавлений и 180 удалений

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

@ -20,6 +20,7 @@
#define nsILoggingSink_h___ #define nsILoggingSink_h___
#include "nsIHTMLContentSink.h" #include "nsIHTMLContentSink.h"
#include "nsString.h"
// IID for nsILoggingSink // IID for nsILoggingSink
#define NS_ILOGGING_SINK_IID \ #define NS_ILOGGING_SINK_IID \
@ -31,7 +32,7 @@
class nsILoggingSink : public nsIHTMLContentSink { class nsILoggingSink : public nsIHTMLContentSink {
public: public:
NS_IMETHOD Init(FILE* fp) = 0; NS_IMETHOD SetOutputStream(ostream& aStream) =0;
}; };
extern "C" nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult); extern "C" nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult);

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

@ -18,6 +18,7 @@
*/ */
#include "nsILoggingSink.h" #include "nsILoggingSink.h"
#include "nsHTMLTags.h" #include "nsHTMLTags.h"
#include "nsString.h"
static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID); static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID);
static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID); static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID);
@ -34,6 +35,21 @@ static char gSkippedContentTags[] = {
0 0
}; };
/**
*
* @update gess8/8/98
* @param
* @return
*/
ostream& operator<<(ostream& os,nsAutoString& aString){
const PRUnichar* uc=aString.GetUnicode();
int len=aString.Length();
for(int i=0;i<len;i++)
os<<(char)uc[i];
return os;
}
class nsLoggingSink : public nsILoggingSink { class nsLoggingSink : public nsILoggingSink {
public: public:
nsLoggingSink(); nsLoggingSink();
@ -68,7 +84,7 @@ public:
NS_IMETHOD CloseFrameset(const nsIParserNode& aNode); NS_IMETHOD CloseFrameset(const nsIParserNode& aNode);
// nsILoggingSink // nsILoggingSink
NS_IMETHOD Init(FILE* fp); NS_IMETHOD SetOutputStream(ostream& aStream);
nsresult OpenNode(const char* aKind, const nsIParserNode& aNode); nsresult OpenNode(const char* aKind, const nsIParserNode& aNode);
nsresult CloseNode(const char* aKind); nsresult CloseNode(const char* aKind);
@ -78,7 +94,8 @@ public:
PRBool WillWriteAttributes(const nsIParserNode& aNode); PRBool WillWriteAttributes(const nsIParserNode& aNode);
protected: protected:
FILE* mFile; ostream* mOutput;
int mLevel;
}; };
nsresult nsresult
@ -98,14 +115,15 @@ NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult)
nsLoggingSink::nsLoggingSink() nsLoggingSink::nsLoggingSink()
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mFile = nsnull; mOutput = 0;
mLevel=-1;
} }
nsLoggingSink::~nsLoggingSink() nsLoggingSink::~nsLoggingSink()
{ {
if (nsnull != mFile) { if (0 != mOutput) {
fclose(mFile); mOutput->flush();
mFile = nsnull; mOutput = 0;
} }
} }
@ -143,27 +161,32 @@ nsLoggingSink::QueryInterface(const nsIID& aIID, void** aInstancePtr)
return NS_OK; return NS_OK;
} }
nsresult NS_IMETHODIMP
nsLoggingSink::Init(FILE* fp) nsLoggingSink::SetOutputStream(ostream& aStream) {
{ mOutput = &aStream;
if (nsnull == fp) {
return NS_ERROR_NULL_POINTER;
}
mFile = fp;
return NS_OK; return NS_OK;
} }
void WriteTabs(ostream& anOutputStream,int aTabCount) {
int tabs;
for(tabs=0;tabs<aTabCount;tabs++)
anOutputStream << " ";
}
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::WillBuildModel() nsLoggingSink::WillBuildModel()
{ {
fputs("<begin>\n", mFile); WriteTabs(*mOutput,++mLevel);
(*mOutput) << "<begin>" << endl;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel) nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel)
{ {
fputs("</begin>\n", mFile); WriteTabs(*mOutput,mLevel--);
(*mOutput) << "</begin>" << endl;
return NS_OK; return NS_OK;
} }
@ -188,7 +211,14 @@ nsLoggingSink::OpenContainer(const nsIParserNode& aNode)
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::CloseContainer(const nsIParserNode& aNode) nsLoggingSink::CloseContainer(const nsIParserNode& aNode)
{ {
return CloseNode("container");
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType);
return CloseNode(tag);
}
return CloseNode("???");
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -208,9 +238,9 @@ nsLoggingSink::SetTitle(const nsString& aValue)
{ {
nsAutoString tmp; nsAutoString tmp;
QuoteText(aValue, tmp); QuoteText(aValue, tmp);
fputs("<title value=\"", mFile); WriteTabs(*mOutput,++mLevel);
fputs(tmp, mFile); (*mOutput) << "<title value=\"" << tmp << "\"/>" << endl;
fputs("\"/>", mFile); --mLevel;
return NS_OK; return NS_OK;
} }
@ -286,31 +316,32 @@ nsLoggingSink::CloseFrameset(const nsIParserNode& aNode)
return CloseNode("frameset"); return CloseNode("frameset");
} }
nsresult nsresult
nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode) nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode)
{ {
fprintf(mFile, "<open kind=\"%s\" ", aKind); WriteTabs(*mOutput,++mLevel);
(*mOutput) << "<open container=";
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) && if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType); const char* tag = NS_EnumToTag(nodeType);
fprintf(mFile, "tag=\"%s\"", tag); (*mOutput) << "\"" << tag << "\"";
} }
else { else {
const nsString& text = aNode.GetText(); const nsString& text = aNode.GetText();
fputs("tag=\"", mFile); (*mOutput) << "\"" << text << " \"";
fputs(text, mFile);
fputs(" \"", mFile);
} }
if (WillWriteAttributes(aNode)) { if (WillWriteAttributes(aNode)) {
fputs(">\n", mFile); (*mOutput) << ">" << endl;
WriteAttributes(aNode); WriteAttributes(aNode);
fputs("</open>\n", mFile); (*mOutput) << "</open>" << endl;
} }
else { else {
fputs("/>\n", mFile); (*mOutput) << ">" << endl;
} }
return NS_OK; return NS_OK;
@ -319,7 +350,8 @@ nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode)
nsresult nsresult
nsLoggingSink::CloseNode(const char* aKind) nsLoggingSink::CloseNode(const char* aKind)
{ {
fprintf(mFile, "<close kind=\"%s\"/>\n", aKind); WriteTabs(*mOutput,mLevel--);
(*mOutput) << "<close container=\"" << aKind << "\">" << endl;
return NS_OK; return NS_OK;
} }
@ -332,10 +364,9 @@ nsLoggingSink::WriteAttributes(const nsIParserNode& aNode)
for (PRInt32 i = 0; i < ac; i++) { for (PRInt32 i = 0; i < ac; i++) {
const nsString& k = aNode.GetKeyAt(i); const nsString& k = aNode.GetKeyAt(i);
const nsString& v = aNode.GetValueAt(i); const nsString& v = aNode.GetValueAt(i);
fputs(" <attr key=\"", mFile);
fputs(k, mFile);
fputs("\" value=\"", mFile);
(*mOutput) << " <attr key=\"" << k << "\" value=\"";
tmp.Truncate(); tmp.Truncate();
tmp.Append(v); tmp.Append(v);
PRUnichar first = tmp.First(); PRUnichar first = tmp.First();
@ -351,18 +382,18 @@ nsLoggingSink::WriteAttributes(const nsIParserNode& aNode)
} }
} }
QuoteText(tmp, tmp2); QuoteText(tmp, tmp2);
fputs(tmp2, mFile);
fputs("\"/>\n", mFile); (*mOutput) << tmp2 << "\"/>" << endl;
} }
if (0 != strchr(gSkippedContentTags, aNode.GetNodeType())) { if (0 != strchr(gSkippedContentTags, aNode.GetNodeType())) {
const nsString& content = aNode.GetSkippedContent(); const nsString& content = aNode.GetSkippedContent();
if (content.Length() > 0) { if (content.Length() > 0) {
nsAutoString tmp; nsAutoString tmp;
fputs(" <content value=\"", mFile);
QuoteText(content, tmp); QuoteText(content, tmp);
fputs(tmp, mFile); (*mOutput) << " <content value=\"";
fputs("\"/>\n", mFile); (*mOutput) << tmp << "\"/>" << endl;
} }
} }
@ -388,18 +419,20 @@ nsLoggingSink::WillWriteAttributes(const nsIParserNode& aNode)
nsresult nsresult
nsLoggingSink::LeafNode(const nsIParserNode& aNode) nsLoggingSink::LeafNode(const nsIParserNode& aNode)
{ {
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); WriteTabs(*mOutput,1+mLevel);
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) && if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType); const char* tag = NS_EnumToTag(nodeType);
fprintf(mFile, "<leaf tag=\"%s\"", tag);
(*mOutput) << "<leaf tag=\"" << tag << "\"";
if (WillWriteAttributes(aNode)) { if (WillWriteAttributes(aNode)) {
fputs(">\n", mFile); (*mOutput) << ">" << endl;
WriteAttributes(aNode); WriteAttributes(aNode);
fputs("</leaf>\n", mFile); (*mOutput) << "</leaf>" << endl;
} }
else { else {
fputs("/>\n", mFile); (*mOutput) << "/>" << endl;
} }
} }
else { else {
@ -409,13 +442,11 @@ nsLoggingSink::LeafNode(const nsIParserNode& aNode)
case eHTMLTag_whitespace: case eHTMLTag_whitespace:
case eHTMLTag_text: case eHTMLTag_text:
QuoteText(aNode.GetText(), tmp); QuoteText(aNode.GetText(), tmp);
fputs("<text value=\"", mFile); (*mOutput) << "<text value=\"" << tmp << "\"/>" << endl;
fputs(tmp, mFile);
fputs("\"/>\n", mFile);
break; break;
case eHTMLTag_newline: case eHTMLTag_newline:
fputs("<newline/>\n", mFile); (*mOutput) << "<newline/>" << endl;
break; break;
case eHTMLTag_entity: case eHTMLTag_entity:
@ -425,9 +456,7 @@ nsLoggingSink::LeafNode(const nsIParserNode& aNode)
if (pos >= 0) { if (pos >= 0) {
tmp.Cut(pos, 1); tmp.Cut(pos, 1);
} }
fputs("<entity value=\"", mFile); (*mOutput) << "<entity value=\"" << tmp << "\"/>" << endl;
fputs(tmp, mFile);
fputs("\"/>\n", mFile);
break; break;
default: default:

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

@ -40,63 +40,162 @@ static NS_DEFINE_IID(kLoggingSinkCID, NS_LOGGING_SINK_IID);
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID);
//----------------------------------------------------------------------
static void SetupRegistry() static void SetupRegistry()
{ {
NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE); NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE);
NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE); NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE);
} }
//----------------------------------------------------------------------
static const char* kWorkingDir = "s:/mozilla/htmlparser/tests/logparse";
nsresult GenerateBaselineFile(const char* aSourceFilename,const char* aBaselineFilename) {
nsresult result=NS_OK;
if(aSourceFilename && aBaselineFilename) {
fstream theInputStream(aSourceFilename,ios::in | ios::nocreate);
// Create a parser
nsIParser* parser;
nsresult rv = NSRepository::CreateInstance(kParserCID,nsnull,kIParserIID,(void**)&parser);
if (NS_OK != rv) {
cout << "Unable to create a parser (" << rv << ")" <<endl;
return -1;
}
// Create a sink
nsILoggingSink* sink;
rv = NSRepository::CreateInstance(kLoggingSinkCID,nsnull,kILoggingSinkIID,(void**)&sink);
if (NS_OK != rv) {
cout << "Unable to create a sink (" << rv << ")" <<endl;
return -1;
}
{
fstream theOutputStream(aBaselineFilename,ios::out);
sink->SetOutputStream(theOutputStream);
// Parse the document, having the sink write the data to fp
nsIDTD* dtd = nsnull;
NS_NewNavHTMLDTD(&dtd);
parser->RegisterDTD(dtd);
parser->SetContentSink(sink);
result = parser->Parse(theInputStream);
NS_RELEASE(parser);
NS_RELEASE(sink);
}
}
return (NS_OK == result) ? 0 : -1;
}
//----------------------------------------------------------------------
PRBool CompareFiles(const char* aFilename1, const char* aFilename2) {
PRBool result=PR_TRUE;
fstream theFirstStream(aFilename1,ios::in | ios::nocreate);
fstream theSecondStream(aFilename2,ios::in | ios::nocreate);
PRBool done=PR_FALSE;
char ch1,ch2;
while(!done) {
theFirstStream >> ch1;
theSecondStream >> ch2;
if(ch1!=ch2) {
result=PR_FALSE;
break;
}
done=PRBool((theFirstStream.ipfx(1)==0) || (theSecondStream.ipfx(1)==0));
}
return result;
}
//----------------------------------------------------------------------
static const char* kAppName = "logparse ";
static const char* kOption1 = "Compare baseline file-set";
static const char* kOption2 = "Generate baseline ";
static const char* kResultMsg[2] = {" does not match baseline."," matches baseline."};
void ValidateBaselineFiles(const char* anIndexFilename) {
fstream theIndexFile(anIndexFilename,ios::in | ios::nocreate);
char theFilename[500];
char theBaselineFilename[500];
PRBool done=PR_FALSE;
while(!done) {
theIndexFile >> theFilename;
theIndexFile >> theBaselineFilename;
if(theFilename[0] && theBaselineFilename[0]) {
char theTempFile[500];
sprintf(theTempFile,theBaselineFilename);
strcat(theTempFile,"x");
if(0==GenerateBaselineFile(theFilename,theTempFile)) {
PRBool matches=CompareFiles(theTempFile,theBaselineFilename);
cout << theFilename << kResultMsg[matches] << endl;
}
}
theFilename[0]=0;
theBaselineFilename[0]=0;
done=PRBool(theIndexFile.ipfx(1)==0);
}
// Now it's time to compare our output to the baseline...
// if(!CompareFiles(aBaselineFilename,aBaselineFilename)){
// cout << "File: \"" << aSourceFilename << "\" does not match baseline." << endl;
// }
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 3) { if (argc < 2) {
fprintf(stderr, "Usage: logparse in out\n"); cout << "Usage: " << kAppName << " [options] [filename]" << endl;
cout << " -c [filelist] " << kOption1 << endl;
cout << " -g [in] [out] " << kOption2 << endl;
return -1; return -1;
} }
fstream *in = new fstream(); int result=0;
in->open(argv[1], ios::in | ios::nocreate);
FILE* fp = fopen(argv[2], "wb"); SetupRegistry();
if (nsnull == fp) {
fprintf(stderr, "can't create '%s'\n", argv[2]);
return -1;
}
SetupRegistry(); if(0==strcmp("-c",argv[1])) {
// Create a parser if(argc>2) {
nsIParser* parser; cout << kOption1 << "..." << endl;
nsresult rv = NSRepository::CreateInstance(kParserCID,
nsnull,
kIParserIID,
(void**)&parser);
if (NS_OK != rv) {
fprintf(stderr, "Unable to create a parser (%x)\n", rv);
return -1;
}
// Create a sink //Open the master filelist, and read the filenames.
nsILoggingSink* sink; //Each line contains a source filename and a baseline filename, separated by a space.
rv = NSRepository::CreateInstance(kLoggingSinkCID, ValidateBaselineFiles(argv[2]);
nsnull, }
kILoggingSinkIID, else {
(void**)&sink); cout << kAppName << ": Filelist missing for -c option -- nothing to do." << endl;
if (NS_OK != rv) { }
fprintf(stderr, "Unable to create a sink (%x)\n", rv);
return -1;
}
sink->Init(fp);
// Parse the document, having the sink write the data to fp }
nsIDTD* dtd = nsnull; else if(0==strcmp("-g",argv[1])) {
NS_NewNavHTMLDTD(&dtd); if(argc>3) {
parser->RegisterDTD(dtd); cout << kOption2 << argv[3] << " from " << argv[2] << "..." << endl;
parser->SetContentSink(sink); GenerateBaselineFile(argv[2],argv[3]);
PRInt32 status = parser->Parse(*in); }
NS_RELEASE(parser); else {
NS_RELEASE(sink); cout << kAppName << ": Filename(s) missing for -g option -- nothing to do." << endl;
}
return (NS_OK == status) ? 0 : -1; }
else {
cout << kAppName << ": Unknown options -- nothing to do." << endl;
}
return result;
} }

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

@ -20,6 +20,7 @@
#define nsILoggingSink_h___ #define nsILoggingSink_h___
#include "nsIHTMLContentSink.h" #include "nsIHTMLContentSink.h"
#include "nsString.h"
// IID for nsILoggingSink // IID for nsILoggingSink
#define NS_ILOGGING_SINK_IID \ #define NS_ILOGGING_SINK_IID \
@ -31,7 +32,7 @@
class nsILoggingSink : public nsIHTMLContentSink { class nsILoggingSink : public nsIHTMLContentSink {
public: public:
NS_IMETHOD Init(FILE* fp) = 0; NS_IMETHOD SetOutputStream(ostream& aStream) =0;
}; };
extern "C" nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult); extern "C" nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult);

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

@ -18,6 +18,7 @@
*/ */
#include "nsILoggingSink.h" #include "nsILoggingSink.h"
#include "nsHTMLTags.h" #include "nsHTMLTags.h"
#include "nsString.h"
static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID); static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID);
static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID); static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID);
@ -34,6 +35,21 @@ static char gSkippedContentTags[] = {
0 0
}; };
/**
*
* @update gess8/8/98
* @param
* @return
*/
ostream& operator<<(ostream& os,nsAutoString& aString){
const PRUnichar* uc=aString.GetUnicode();
int len=aString.Length();
for(int i=0;i<len;i++)
os<<(char)uc[i];
return os;
}
class nsLoggingSink : public nsILoggingSink { class nsLoggingSink : public nsILoggingSink {
public: public:
nsLoggingSink(); nsLoggingSink();
@ -68,7 +84,7 @@ public:
NS_IMETHOD CloseFrameset(const nsIParserNode& aNode); NS_IMETHOD CloseFrameset(const nsIParserNode& aNode);
// nsILoggingSink // nsILoggingSink
NS_IMETHOD Init(FILE* fp); NS_IMETHOD SetOutputStream(ostream& aStream);
nsresult OpenNode(const char* aKind, const nsIParserNode& aNode); nsresult OpenNode(const char* aKind, const nsIParserNode& aNode);
nsresult CloseNode(const char* aKind); nsresult CloseNode(const char* aKind);
@ -78,7 +94,8 @@ public:
PRBool WillWriteAttributes(const nsIParserNode& aNode); PRBool WillWriteAttributes(const nsIParserNode& aNode);
protected: protected:
FILE* mFile; ostream* mOutput;
int mLevel;
}; };
nsresult nsresult
@ -98,14 +115,15 @@ NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult)
nsLoggingSink::nsLoggingSink() nsLoggingSink::nsLoggingSink()
{ {
NS_INIT_REFCNT(); NS_INIT_REFCNT();
mFile = nsnull; mOutput = 0;
mLevel=-1;
} }
nsLoggingSink::~nsLoggingSink() nsLoggingSink::~nsLoggingSink()
{ {
if (nsnull != mFile) { if (0 != mOutput) {
fclose(mFile); mOutput->flush();
mFile = nsnull; mOutput = 0;
} }
} }
@ -143,27 +161,32 @@ nsLoggingSink::QueryInterface(const nsIID& aIID, void** aInstancePtr)
return NS_OK; return NS_OK;
} }
nsresult NS_IMETHODIMP
nsLoggingSink::Init(FILE* fp) nsLoggingSink::SetOutputStream(ostream& aStream) {
{ mOutput = &aStream;
if (nsnull == fp) {
return NS_ERROR_NULL_POINTER;
}
mFile = fp;
return NS_OK; return NS_OK;
} }
void WriteTabs(ostream& anOutputStream,int aTabCount) {
int tabs;
for(tabs=0;tabs<aTabCount;tabs++)
anOutputStream << " ";
}
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::WillBuildModel() nsLoggingSink::WillBuildModel()
{ {
fputs("<begin>\n", mFile); WriteTabs(*mOutput,++mLevel);
(*mOutput) << "<begin>" << endl;
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel) nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel)
{ {
fputs("</begin>\n", mFile); WriteTabs(*mOutput,mLevel--);
(*mOutput) << "</begin>" << endl;
return NS_OK; return NS_OK;
} }
@ -188,7 +211,14 @@ nsLoggingSink::OpenContainer(const nsIParserNode& aNode)
NS_IMETHODIMP NS_IMETHODIMP
nsLoggingSink::CloseContainer(const nsIParserNode& aNode) nsLoggingSink::CloseContainer(const nsIParserNode& aNode)
{ {
return CloseNode("container");
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType);
return CloseNode(tag);
}
return CloseNode("???");
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -208,9 +238,9 @@ nsLoggingSink::SetTitle(const nsString& aValue)
{ {
nsAutoString tmp; nsAutoString tmp;
QuoteText(aValue, tmp); QuoteText(aValue, tmp);
fputs("<title value=\"", mFile); WriteTabs(*mOutput,++mLevel);
fputs(tmp, mFile); (*mOutput) << "<title value=\"" << tmp << "\"/>" << endl;
fputs("\"/>", mFile); --mLevel;
return NS_OK; return NS_OK;
} }
@ -286,31 +316,32 @@ nsLoggingSink::CloseFrameset(const nsIParserNode& aNode)
return CloseNode("frameset"); return CloseNode("frameset");
} }
nsresult nsresult
nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode) nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode)
{ {
fprintf(mFile, "<open kind=\"%s\" ", aKind); WriteTabs(*mOutput,++mLevel);
(*mOutput) << "<open container=";
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) && if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType); const char* tag = NS_EnumToTag(nodeType);
fprintf(mFile, "tag=\"%s\"", tag); (*mOutput) << "\"" << tag << "\"";
} }
else { else {
const nsString& text = aNode.GetText(); const nsString& text = aNode.GetText();
fputs("tag=\"", mFile); (*mOutput) << "\"" << text << " \"";
fputs(text, mFile);
fputs(" \"", mFile);
} }
if (WillWriteAttributes(aNode)) { if (WillWriteAttributes(aNode)) {
fputs(">\n", mFile); (*mOutput) << ">" << endl;
WriteAttributes(aNode); WriteAttributes(aNode);
fputs("</open>\n", mFile); (*mOutput) << "</open>" << endl;
} }
else { else {
fputs("/>\n", mFile); (*mOutput) << ">" << endl;
} }
return NS_OK; return NS_OK;
@ -319,7 +350,8 @@ nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode)
nsresult nsresult
nsLoggingSink::CloseNode(const char* aKind) nsLoggingSink::CloseNode(const char* aKind)
{ {
fprintf(mFile, "<close kind=\"%s\"/>\n", aKind); WriteTabs(*mOutput,mLevel--);
(*mOutput) << "<close container=\"" << aKind << "\">" << endl;
return NS_OK; return NS_OK;
} }
@ -332,10 +364,9 @@ nsLoggingSink::WriteAttributes(const nsIParserNode& aNode)
for (PRInt32 i = 0; i < ac; i++) { for (PRInt32 i = 0; i < ac; i++) {
const nsString& k = aNode.GetKeyAt(i); const nsString& k = aNode.GetKeyAt(i);
const nsString& v = aNode.GetValueAt(i); const nsString& v = aNode.GetValueAt(i);
fputs(" <attr key=\"", mFile);
fputs(k, mFile);
fputs("\" value=\"", mFile);
(*mOutput) << " <attr key=\"" << k << "\" value=\"";
tmp.Truncate(); tmp.Truncate();
tmp.Append(v); tmp.Append(v);
PRUnichar first = tmp.First(); PRUnichar first = tmp.First();
@ -351,18 +382,18 @@ nsLoggingSink::WriteAttributes(const nsIParserNode& aNode)
} }
} }
QuoteText(tmp, tmp2); QuoteText(tmp, tmp2);
fputs(tmp2, mFile);
fputs("\"/>\n", mFile); (*mOutput) << tmp2 << "\"/>" << endl;
} }
if (0 != strchr(gSkippedContentTags, aNode.GetNodeType())) { if (0 != strchr(gSkippedContentTags, aNode.GetNodeType())) {
const nsString& content = aNode.GetSkippedContent(); const nsString& content = aNode.GetSkippedContent();
if (content.Length() > 0) { if (content.Length() > 0) {
nsAutoString tmp; nsAutoString tmp;
fputs(" <content value=\"", mFile);
QuoteText(content, tmp); QuoteText(content, tmp);
fputs(tmp, mFile); (*mOutput) << " <content value=\"";
fputs("\"/>\n", mFile); (*mOutput) << tmp << "\"/>" << endl;
} }
} }
@ -388,18 +419,20 @@ nsLoggingSink::WillWriteAttributes(const nsIParserNode& aNode)
nsresult nsresult
nsLoggingSink::LeafNode(const nsIParserNode& aNode) nsLoggingSink::LeafNode(const nsIParserNode& aNode)
{ {
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); WriteTabs(*mOutput,1+mLevel);
nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType());
if ((nodeType >= eHTMLTag_unknown) && if ((nodeType >= eHTMLTag_unknown) &&
(nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) {
const char* tag = NS_EnumToTag(nodeType); const char* tag = NS_EnumToTag(nodeType);
fprintf(mFile, "<leaf tag=\"%s\"", tag);
(*mOutput) << "<leaf tag=\"" << tag << "\"";
if (WillWriteAttributes(aNode)) { if (WillWriteAttributes(aNode)) {
fputs(">\n", mFile); (*mOutput) << ">" << endl;
WriteAttributes(aNode); WriteAttributes(aNode);
fputs("</leaf>\n", mFile); (*mOutput) << "</leaf>" << endl;
} }
else { else {
fputs("/>\n", mFile); (*mOutput) << "/>" << endl;
} }
} }
else { else {
@ -409,13 +442,11 @@ nsLoggingSink::LeafNode(const nsIParserNode& aNode)
case eHTMLTag_whitespace: case eHTMLTag_whitespace:
case eHTMLTag_text: case eHTMLTag_text:
QuoteText(aNode.GetText(), tmp); QuoteText(aNode.GetText(), tmp);
fputs("<text value=\"", mFile); (*mOutput) << "<text value=\"" << tmp << "\"/>" << endl;
fputs(tmp, mFile);
fputs("\"/>\n", mFile);
break; break;
case eHTMLTag_newline: case eHTMLTag_newline:
fputs("<newline/>\n", mFile); (*mOutput) << "<newline/>" << endl;
break; break;
case eHTMLTag_entity: case eHTMLTag_entity:
@ -425,9 +456,7 @@ nsLoggingSink::LeafNode(const nsIParserNode& aNode)
if (pos >= 0) { if (pos >= 0) {
tmp.Cut(pos, 1); tmp.Cut(pos, 1);
} }
fputs("<entity value=\"", mFile); (*mOutput) << "<entity value=\"" << tmp << "\"/>" << endl;
fputs(tmp, mFile);
fputs("\"/>\n", mFile);
break; break;
default: default:

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

@ -40,63 +40,162 @@ static NS_DEFINE_IID(kLoggingSinkCID, NS_LOGGING_SINK_IID);
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID);
//----------------------------------------------------------------------
static void SetupRegistry() static void SetupRegistry()
{ {
NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE); NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE);
NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE); NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE);
} }
//----------------------------------------------------------------------
static const char* kWorkingDir = "s:/mozilla/htmlparser/tests/logparse";
nsresult GenerateBaselineFile(const char* aSourceFilename,const char* aBaselineFilename) {
nsresult result=NS_OK;
if(aSourceFilename && aBaselineFilename) {
fstream theInputStream(aSourceFilename,ios::in | ios::nocreate);
// Create a parser
nsIParser* parser;
nsresult rv = NSRepository::CreateInstance(kParserCID,nsnull,kIParserIID,(void**)&parser);
if (NS_OK != rv) {
cout << "Unable to create a parser (" << rv << ")" <<endl;
return -1;
}
// Create a sink
nsILoggingSink* sink;
rv = NSRepository::CreateInstance(kLoggingSinkCID,nsnull,kILoggingSinkIID,(void**)&sink);
if (NS_OK != rv) {
cout << "Unable to create a sink (" << rv << ")" <<endl;
return -1;
}
{
fstream theOutputStream(aBaselineFilename,ios::out);
sink->SetOutputStream(theOutputStream);
// Parse the document, having the sink write the data to fp
nsIDTD* dtd = nsnull;
NS_NewNavHTMLDTD(&dtd);
parser->RegisterDTD(dtd);
parser->SetContentSink(sink);
result = parser->Parse(theInputStream);
NS_RELEASE(parser);
NS_RELEASE(sink);
}
}
return (NS_OK == result) ? 0 : -1;
}
//----------------------------------------------------------------------
PRBool CompareFiles(const char* aFilename1, const char* aFilename2) {
PRBool result=PR_TRUE;
fstream theFirstStream(aFilename1,ios::in | ios::nocreate);
fstream theSecondStream(aFilename2,ios::in | ios::nocreate);
PRBool done=PR_FALSE;
char ch1,ch2;
while(!done) {
theFirstStream >> ch1;
theSecondStream >> ch2;
if(ch1!=ch2) {
result=PR_FALSE;
break;
}
done=PRBool((theFirstStream.ipfx(1)==0) || (theSecondStream.ipfx(1)==0));
}
return result;
}
//----------------------------------------------------------------------
static const char* kAppName = "logparse ";
static const char* kOption1 = "Compare baseline file-set";
static const char* kOption2 = "Generate baseline ";
static const char* kResultMsg[2] = {" does not match baseline."," matches baseline."};
void ValidateBaselineFiles(const char* anIndexFilename) {
fstream theIndexFile(anIndexFilename,ios::in | ios::nocreate);
char theFilename[500];
char theBaselineFilename[500];
PRBool done=PR_FALSE;
while(!done) {
theIndexFile >> theFilename;
theIndexFile >> theBaselineFilename;
if(theFilename[0] && theBaselineFilename[0]) {
char theTempFile[500];
sprintf(theTempFile,theBaselineFilename);
strcat(theTempFile,"x");
if(0==GenerateBaselineFile(theFilename,theTempFile)) {
PRBool matches=CompareFiles(theTempFile,theBaselineFilename);
cout << theFilename << kResultMsg[matches] << endl;
}
}
theFilename[0]=0;
theBaselineFilename[0]=0;
done=PRBool(theIndexFile.ipfx(1)==0);
}
// Now it's time to compare our output to the baseline...
// if(!CompareFiles(aBaselineFilename,aBaselineFilename)){
// cout << "File: \"" << aSourceFilename << "\" does not match baseline." << endl;
// }
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 3) { if (argc < 2) {
fprintf(stderr, "Usage: logparse in out\n"); cout << "Usage: " << kAppName << " [options] [filename]" << endl;
cout << " -c [filelist] " << kOption1 << endl;
cout << " -g [in] [out] " << kOption2 << endl;
return -1; return -1;
} }
fstream *in = new fstream(); int result=0;
in->open(argv[1], ios::in | ios::nocreate);
FILE* fp = fopen(argv[2], "wb"); SetupRegistry();
if (nsnull == fp) {
fprintf(stderr, "can't create '%s'\n", argv[2]);
return -1;
}
SetupRegistry(); if(0==strcmp("-c",argv[1])) {
// Create a parser if(argc>2) {
nsIParser* parser; cout << kOption1 << "..." << endl;
nsresult rv = NSRepository::CreateInstance(kParserCID,
nsnull,
kIParserIID,
(void**)&parser);
if (NS_OK != rv) {
fprintf(stderr, "Unable to create a parser (%x)\n", rv);
return -1;
}
// Create a sink //Open the master filelist, and read the filenames.
nsILoggingSink* sink; //Each line contains a source filename and a baseline filename, separated by a space.
rv = NSRepository::CreateInstance(kLoggingSinkCID, ValidateBaselineFiles(argv[2]);
nsnull, }
kILoggingSinkIID, else {
(void**)&sink); cout << kAppName << ": Filelist missing for -c option -- nothing to do." << endl;
if (NS_OK != rv) { }
fprintf(stderr, "Unable to create a sink (%x)\n", rv);
return -1;
}
sink->Init(fp);
// Parse the document, having the sink write the data to fp }
nsIDTD* dtd = nsnull; else if(0==strcmp("-g",argv[1])) {
NS_NewNavHTMLDTD(&dtd); if(argc>3) {
parser->RegisterDTD(dtd); cout << kOption2 << argv[3] << " from " << argv[2] << "..." << endl;
parser->SetContentSink(sink); GenerateBaselineFile(argv[2],argv[3]);
PRInt32 status = parser->Parse(*in); }
NS_RELEASE(parser); else {
NS_RELEASE(sink); cout << kAppName << ": Filename(s) missing for -g option -- nothing to do." << endl;
}
return (NS_OK == status) ? 0 : -1; }
else {
cout << kAppName << ": Unknown options -- nothing to do." << endl;
}
return result;
} }