diff --git a/mozilla/htmlparser/src/nsILoggingSink.h b/mozilla/htmlparser/src/nsILoggingSink.h new file mode 100644 index 00000000000..eb2a772ae43 --- /dev/null +++ b/mozilla/htmlparser/src/nsILoggingSink.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#ifndef nsILoggingSink_h___ +#define nsILoggingSink_h___ + +#include "nsIHTMLContentSink.h" + +// IID for nsILoggingSink +#define NS_ILOGGING_SINK_IID \ + {0xa6cf9061, 0x15b3, 0x11d2,{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}} + +// Class IID for the logging sink +#define NS_LOGGING_SINK_IID \ + {0xa6cf9060, 0x15b3, 0x11d2,{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}} + +class nsILoggingSink : public nsIHTMLContentSink { +public: + NS_IMETHOD Init(FILE* fp) = 0; +}; + +extern nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult); + +#endif /* nsILoggingSink_h___ */ diff --git a/mozilla/htmlparser/src/nsLoggingSink.cpp b/mozilla/htmlparser/src/nsLoggingSink.cpp new file mode 100644 index 00000000000..e5b33efc5da --- /dev/null +++ b/mozilla/htmlparser/src/nsLoggingSink.cpp @@ -0,0 +1,391 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#include "nsILoggingSink.h" +#include "nsHTMLTags.h" + +static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID); +static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID); +static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +class nsLoggingSink : public nsILoggingSink { +public: + nsLoggingSink(); + ~nsLoggingSink(); + + // nsISupports + NS_DECL_ISUPPORTS + + // nsIContentSink + NS_IMETHOD WillBuildModel(); + NS_IMETHOD DidBuildModel(PRInt32 aQualityLevel); + NS_IMETHOD WillInterrupt(); + NS_IMETHOD WillResume(); + NS_IMETHOD OpenContainer(const nsIParserNode& aNode); + NS_IMETHOD CloseContainer(const nsIParserNode& aNode); + NS_IMETHOD AddLeaf(const nsIParserNode& aNode); + + // nsIHTMLContentSink + NS_IMETHOD PushMark(); + NS_IMETHOD SetTitle(const nsString& aValue); + NS_IMETHOD OpenHTML(const nsIParserNode& aNode); + NS_IMETHOD CloseHTML(const nsIParserNode& aNode); + NS_IMETHOD OpenHead(const nsIParserNode& aNode); + NS_IMETHOD CloseHead(const nsIParserNode& aNode); + NS_IMETHOD OpenBody(const nsIParserNode& aNode); + NS_IMETHOD CloseBody(const nsIParserNode& aNode); + NS_IMETHOD OpenForm(const nsIParserNode& aNode); + NS_IMETHOD CloseForm(const nsIParserNode& aNode); + NS_IMETHOD OpenMap(const nsIParserNode& aNode); + NS_IMETHOD CloseMap(const nsIParserNode& aNode); + NS_IMETHOD OpenFrameset(const nsIParserNode& aNode); + NS_IMETHOD CloseFrameset(const nsIParserNode& aNode); + + // nsILoggingSink + NS_IMETHOD Init(FILE* fp); + + nsresult OpenNode(const char* aKind, const nsIParserNode& aNode); + nsresult CloseNode(const char* aKind); + nsresult LeafNode(const nsIParserNode& aNode); + nsresult WriteAttributes(const nsIParserNode& aNode); + nsresult QuoteText(const nsString& aValue, nsString& aResult); + +protected: + FILE* mFile; +}; + +nsresult +NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult) +{ + NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr"); + if (nsnull == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + nsLoggingSink* it = new nsLoggingSink(); + if (nsnull == it) { + return NS_ERROR_OUT_OF_MEMORY; + } + return it->QueryInterface(kIContentSinkIID, (void**) aInstancePtrResult); +} + +nsLoggingSink::nsLoggingSink() +{ + NS_INIT_REFCNT(); + mFile = nsnull; +} + +nsLoggingSink::~nsLoggingSink() +{ + if (nsnull != mFile) { + fclose(mFile); + mFile = nsnull; + } +} + +NS_IMPL_ADDREF(nsLoggingSink) +NS_IMPL_RELEASE(nsLoggingSink) + +nsresult +nsLoggingSink::QueryInterface(const nsIID& aIID, void** aInstancePtr) +{ + NS_PRECONDITION(nsnull != aInstancePtr, "null ptr"); + if (nsnull == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + if (aIID.Equals(kISupportsIID)) { + nsISupports* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kIContentSinkIID)) { + nsIContentSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kIHTMLContentSinkIID)) { + nsIHTMLContentSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kILoggingSinkIID)) { + nsILoggingSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else { + *aInstancePtr = nsnull; + return NS_NOINTERFACE; + } + NS_ADDREF(this); + return NS_OK; +} + +nsresult +nsLoggingSink::Init(FILE* fp) +{ + if (nsnull == fp) { + return NS_ERROR_NULL_POINTER; + } + mFile = fp; + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillBuildModel() +{ + fputs("\n", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel) +{ + fputs("\n", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillInterrupt() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillResume() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::OpenContainer(const nsIParserNode& aNode) +{ + return OpenNode("container", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseContainer(const nsIParserNode& aNode) +{ + return CloseNode("container"); +} + +NS_IMETHODIMP +nsLoggingSink::AddLeaf(const nsIParserNode& aNode) +{ + return LeafNode(aNode); +} + +NS_IMETHODIMP +nsLoggingSink::PushMark() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::SetTitle(const nsString& aValue) +{ + nsAutoString tmp; + QuoteText(aValue, tmp); + fputs("", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::OpenHTML(const nsIParserNode& aNode) +{ + return OpenNode("html", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseHTML(const nsIParserNode& aNode) +{ + return CloseNode("html"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenHead(const nsIParserNode& aNode) +{ + return OpenNode("head", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseHead(const nsIParserNode& aNode) +{ + return CloseNode("head"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenBody(const nsIParserNode& aNode) +{ + return OpenNode("body", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseBody(const nsIParserNode& aNode) +{ + return CloseNode("body"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenForm(const nsIParserNode& aNode) +{ + return OpenNode("form", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseForm(const nsIParserNode& aNode) +{ + return CloseNode("form"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenMap(const nsIParserNode& aNode) +{ + return OpenNode("map", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseMap(const nsIParserNode& aNode) +{ + return CloseNode("map"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenFrameset(const nsIParserNode& aNode) +{ + return OpenNode("frameset", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseFrameset(const nsIParserNode& aNode) +{ + return CloseNode("frameset"); +} + +nsresult +nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode) +{ + fprintf(mFile, "<open kind=\"%s\">\n", aKind); + + nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); + if ((nodeType >= eHTMLTag_unknown) && + (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { + const char* tag = NS_EnumToTag(nodeType); + fprintf(mFile, " <tag tag=\"%s\"/>\n", tag); + } + else { + const nsString& text = aNode.GetText(); + fputs(" <tag tag=\"", mFile); + fputs(text, mFile); + fputs(" \"/>", mFile); + } + + WriteAttributes(aNode); + + fputs("</open>\n", mFile); + return NS_OK; +} + +nsresult +nsLoggingSink::CloseNode(const char* aKind) +{ + fprintf(mFile, "<close kind=\"%s\"/>\n", aKind); + return NS_OK; +} + +nsresult +nsLoggingSink::WriteAttributes(const nsIParserNode& aNode) +{ + PRInt32 ac = aNode.GetAttributeCount(); + for (PRInt32 i = 0; i < ac; i++) { + const nsString& k = aNode.GetKeyAt(i); + const nsString& v = aNode.GetValueAt(i); + fputs(" <attr key=\"", mFile); + fputs(k, mFile); + fputs("\" value=", mFile); + fputs(v, mFile); + fputs("/>\n", mFile); + } + return NS_OK; +} + +nsresult +nsLoggingSink::LeafNode(const nsIParserNode& aNode) +{ + nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); + if ((nodeType >= eHTMLTag_unknown) && + (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { + const char* tag = NS_EnumToTag(nodeType); + fprintf(mFile, "<leaf tag=\"%s\">\n", tag); + WriteAttributes(aNode); + fputs("</leaf>\n", mFile); + } + else { + PRInt32 pos; + nsAutoString tmp; + switch (nodeType) { + case eHTMLTag_whitespace: + case eHTMLTag_newline: + case eHTMLTag_text: + QuoteText(aNode.GetText(), tmp); + fputs("<text value=\"", mFile); + fputs(tmp, mFile); + fputs("\"/>\n", mFile); + break; + + case eHTMLTag_entity: + tmp.Append(aNode.GetText()); + tmp.Cut(0, 1); + pos = tmp.Length() - 1; + if (pos >= 0) { + tmp.Cut(pos, 1); + } + fputs("<entity value=\"", mFile); + fputs(tmp, mFile); + fputs("\"/>\n", mFile); + break; + + default: + NS_NOTREACHED("unsupported leaf node type"); + } + } + return NS_OK; +} + +nsresult +nsLoggingSink::QuoteText(const nsString& aValue, nsString& aResult) +{ + const PRUnichar* cp = aValue.GetUnicode(); + const PRUnichar* end = cp + aValue.Length(); + while (cp < end) { + PRUnichar ch = *cp++; + if (ch == '"') { + aResult.Append("""); + } + else if (ch == '&') { + aResult.Append("&"); + } + else if ((ch < 32) || (ch >= 127)) { + aResult.Append("&#"); + aResult.Append(PRInt32(ch), 10); + aResult.Append(';'); + } + else { + aResult.Append(ch); + } + } + return NS_OK; +} diff --git a/mozilla/htmlparser/tests/logparse/logparse.cpp b/mozilla/htmlparser/tests/logparse/logparse.cpp new file mode 100644 index 00000000000..7e2d0e5d056 --- /dev/null +++ b/mozilla/htmlparser/tests/logparse/logparse.cpp @@ -0,0 +1,102 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#include "nsRepository.h" +#include "nsParserCIID.h" +#include "nsIParser.h" +#include "nsILoggingSink.h" +#include "CNavDTD.h" +#include <fstream.h> + +#ifdef XP_PC +#define PARSER_DLL "raptorhtmlpars.dll" +#endif +#ifdef XP_MAC +#endif +#ifdef XP_UNIX +#define PARSER_DLL "libraptorhtmlpars.so" +#endif + +// Class IID's +static NS_DEFINE_IID(kParserCID, NS_PARSER_IID); +static NS_DEFINE_IID(kLoggingSinkCID, NS_LOGGING_SINK_IID); + +// Interface IID's +static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); +static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); + +static void SetupRegistry() +{ + NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE); +} + +//---------------------------------------------------------------------- + +int main(int argc, char** argv) +{ + if (argc != 3) { + fprintf(stderr, "Usage: logparse in out\n"); + return -1; + } + + fstream *in = new fstream(); + in->open(argv[1], ios::in | ios::nocreate); + + FILE* fp = fopen(argv[2], "wb"); + if (nsnull == fp) { + fprintf(stderr, "can't create '%s'\n", argv[2]); + return -1; + } + + SetupRegistry(); + + // Create a parser + nsIParser* parser; + 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 + nsILoggingSink* sink; + rv = NSRepository::CreateInstance(kLoggingSinkCID, + nsnull, + kILoggingSinkIID, + (void**)&sink); + 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; + NS_NewNavHTMLDTD(&dtd); + parser->RegisterDTD(dtd); + parser->SetContentSink(sink); + PRInt32 status = parser->Parse(*in); + NS_RELEASE(parser); + NS_RELEASE(sink); + + return (NS_OK == status) ? 0 : -1; +} diff --git a/mozilla/htmlparser/tests/logparse/makefile.win b/mozilla/htmlparser/tests/logparse/makefile.win new file mode 100644 index 00000000000..95b779c3311 --- /dev/null +++ b/mozilla/htmlparser/tests/logparse/makefile.win @@ -0,0 +1,56 @@ +#!nmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=..\..\.. +IGNORE_MANIFEST=1 + +MAKE_OBJ_TYPE = EXE +PROGRAM = .\$(OBJDIR)\logparse.exe + +MISCDEP= \ + $(DIST)\lib\raptorhtmlpars.lib \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) + +OBJS = \ + .\$(OBJDIR)\logparse.obj \ + $(NULL) + +LINCS= \ + -I$(PUBLIC)\raptor \ + -I$(PUBLIC)\xpcom \ + -I$(PUBLIC)\netlib + +MYLIBS= \ + $(DIST)\lib\raptorhtmlpars.lib \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) \ + $(NULL) + +LLIBS= $(MYLIBS) -SUBSYSTEM:CONSOLE + +include <$(DEPTH)\config\rules.mak> + +!ifdef MOZ_NO_DEBUG_RTL +OS_CFLAGS = $(OS_CFLAGS) -DMOZ_NO_DEBUG_RTL +!endif + +install:: $(PROGRAM) + $(MAKE_INSTALL) $(PROGRAM) $(DIST)\bin + +clobber:: + rm -f $(DIST)\bin\logparse.exe diff --git a/mozilla/parser/htmlparser/src/nsILoggingSink.h b/mozilla/parser/htmlparser/src/nsILoggingSink.h new file mode 100644 index 00000000000..eb2a772ae43 --- /dev/null +++ b/mozilla/parser/htmlparser/src/nsILoggingSink.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#ifndef nsILoggingSink_h___ +#define nsILoggingSink_h___ + +#include "nsIHTMLContentSink.h" + +// IID for nsILoggingSink +#define NS_ILOGGING_SINK_IID \ + {0xa6cf9061, 0x15b3, 0x11d2,{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}} + +// Class IID for the logging sink +#define NS_LOGGING_SINK_IID \ + {0xa6cf9060, 0x15b3, 0x11d2,{0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32}} + +class nsILoggingSink : public nsIHTMLContentSink { +public: + NS_IMETHOD Init(FILE* fp) = 0; +}; + +extern nsresult NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult); + +#endif /* nsILoggingSink_h___ */ diff --git a/mozilla/parser/htmlparser/src/nsLoggingSink.cpp b/mozilla/parser/htmlparser/src/nsLoggingSink.cpp new file mode 100644 index 00000000000..e5b33efc5da --- /dev/null +++ b/mozilla/parser/htmlparser/src/nsLoggingSink.cpp @@ -0,0 +1,391 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#include "nsILoggingSink.h" +#include "nsHTMLTags.h" + +static NS_DEFINE_IID(kIContentSinkIID, NS_ICONTENT_SINK_IID); +static NS_DEFINE_IID(kIHTMLContentSinkIID, NS_IHTML_CONTENT_SINK_IID); +static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + +class nsLoggingSink : public nsILoggingSink { +public: + nsLoggingSink(); + ~nsLoggingSink(); + + // nsISupports + NS_DECL_ISUPPORTS + + // nsIContentSink + NS_IMETHOD WillBuildModel(); + NS_IMETHOD DidBuildModel(PRInt32 aQualityLevel); + NS_IMETHOD WillInterrupt(); + NS_IMETHOD WillResume(); + NS_IMETHOD OpenContainer(const nsIParserNode& aNode); + NS_IMETHOD CloseContainer(const nsIParserNode& aNode); + NS_IMETHOD AddLeaf(const nsIParserNode& aNode); + + // nsIHTMLContentSink + NS_IMETHOD PushMark(); + NS_IMETHOD SetTitle(const nsString& aValue); + NS_IMETHOD OpenHTML(const nsIParserNode& aNode); + NS_IMETHOD CloseHTML(const nsIParserNode& aNode); + NS_IMETHOD OpenHead(const nsIParserNode& aNode); + NS_IMETHOD CloseHead(const nsIParserNode& aNode); + NS_IMETHOD OpenBody(const nsIParserNode& aNode); + NS_IMETHOD CloseBody(const nsIParserNode& aNode); + NS_IMETHOD OpenForm(const nsIParserNode& aNode); + NS_IMETHOD CloseForm(const nsIParserNode& aNode); + NS_IMETHOD OpenMap(const nsIParserNode& aNode); + NS_IMETHOD CloseMap(const nsIParserNode& aNode); + NS_IMETHOD OpenFrameset(const nsIParserNode& aNode); + NS_IMETHOD CloseFrameset(const nsIParserNode& aNode); + + // nsILoggingSink + NS_IMETHOD Init(FILE* fp); + + nsresult OpenNode(const char* aKind, const nsIParserNode& aNode); + nsresult CloseNode(const char* aKind); + nsresult LeafNode(const nsIParserNode& aNode); + nsresult WriteAttributes(const nsIParserNode& aNode); + nsresult QuoteText(const nsString& aValue, nsString& aResult); + +protected: + FILE* mFile; +}; + +nsresult +NS_NewHTMLLoggingSink(nsIContentSink** aInstancePtrResult) +{ + NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr"); + if (nsnull == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + nsLoggingSink* it = new nsLoggingSink(); + if (nsnull == it) { + return NS_ERROR_OUT_OF_MEMORY; + } + return it->QueryInterface(kIContentSinkIID, (void**) aInstancePtrResult); +} + +nsLoggingSink::nsLoggingSink() +{ + NS_INIT_REFCNT(); + mFile = nsnull; +} + +nsLoggingSink::~nsLoggingSink() +{ + if (nsnull != mFile) { + fclose(mFile); + mFile = nsnull; + } +} + +NS_IMPL_ADDREF(nsLoggingSink) +NS_IMPL_RELEASE(nsLoggingSink) + +nsresult +nsLoggingSink::QueryInterface(const nsIID& aIID, void** aInstancePtr) +{ + NS_PRECONDITION(nsnull != aInstancePtr, "null ptr"); + if (nsnull == aInstancePtr) { + return NS_ERROR_NULL_POINTER; + } + if (aIID.Equals(kISupportsIID)) { + nsISupports* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kIContentSinkIID)) { + nsIContentSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kIHTMLContentSinkIID)) { + nsIHTMLContentSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else if (aIID.Equals(kILoggingSinkIID)) { + nsILoggingSink* tmp = this; + *aInstancePtr = (void*) tmp; + } + else { + *aInstancePtr = nsnull; + return NS_NOINTERFACE; + } + NS_ADDREF(this); + return NS_OK; +} + +nsresult +nsLoggingSink::Init(FILE* fp) +{ + if (nsnull == fp) { + return NS_ERROR_NULL_POINTER; + } + mFile = fp; + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillBuildModel() +{ + fputs("<begin>\n", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::DidBuildModel(PRInt32 aQualityLevel) +{ + fputs("</begin>\n", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillInterrupt() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::WillResume() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::OpenContainer(const nsIParserNode& aNode) +{ + return OpenNode("container", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseContainer(const nsIParserNode& aNode) +{ + return CloseNode("container"); +} + +NS_IMETHODIMP +nsLoggingSink::AddLeaf(const nsIParserNode& aNode) +{ + return LeafNode(aNode); +} + +NS_IMETHODIMP +nsLoggingSink::PushMark() +{ + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::SetTitle(const nsString& aValue) +{ + nsAutoString tmp; + QuoteText(aValue, tmp); + fputs("<title value=\"", mFile); + fputs(tmp, mFile); + fputs("\"/>", mFile); + return NS_OK; +} + +NS_IMETHODIMP +nsLoggingSink::OpenHTML(const nsIParserNode& aNode) +{ + return OpenNode("html", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseHTML(const nsIParserNode& aNode) +{ + return CloseNode("html"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenHead(const nsIParserNode& aNode) +{ + return OpenNode("head", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseHead(const nsIParserNode& aNode) +{ + return CloseNode("head"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenBody(const nsIParserNode& aNode) +{ + return OpenNode("body", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseBody(const nsIParserNode& aNode) +{ + return CloseNode("body"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenForm(const nsIParserNode& aNode) +{ + return OpenNode("form", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseForm(const nsIParserNode& aNode) +{ + return CloseNode("form"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenMap(const nsIParserNode& aNode) +{ + return OpenNode("map", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseMap(const nsIParserNode& aNode) +{ + return CloseNode("map"); +} + +NS_IMETHODIMP +nsLoggingSink::OpenFrameset(const nsIParserNode& aNode) +{ + return OpenNode("frameset", aNode); +} + +NS_IMETHODIMP +nsLoggingSink::CloseFrameset(const nsIParserNode& aNode) +{ + return CloseNode("frameset"); +} + +nsresult +nsLoggingSink::OpenNode(const char* aKind, const nsIParserNode& aNode) +{ + fprintf(mFile, "<open kind=\"%s\">\n", aKind); + + nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); + if ((nodeType >= eHTMLTag_unknown) && + (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { + const char* tag = NS_EnumToTag(nodeType); + fprintf(mFile, " <tag tag=\"%s\"/>\n", tag); + } + else { + const nsString& text = aNode.GetText(); + fputs(" <tag tag=\"", mFile); + fputs(text, mFile); + fputs(" \"/>", mFile); + } + + WriteAttributes(aNode); + + fputs("</open>\n", mFile); + return NS_OK; +} + +nsresult +nsLoggingSink::CloseNode(const char* aKind) +{ + fprintf(mFile, "<close kind=\"%s\"/>\n", aKind); + return NS_OK; +} + +nsresult +nsLoggingSink::WriteAttributes(const nsIParserNode& aNode) +{ + PRInt32 ac = aNode.GetAttributeCount(); + for (PRInt32 i = 0; i < ac; i++) { + const nsString& k = aNode.GetKeyAt(i); + const nsString& v = aNode.GetValueAt(i); + fputs(" <attr key=\"", mFile); + fputs(k, mFile); + fputs("\" value=", mFile); + fputs(v, mFile); + fputs("/>\n", mFile); + } + return NS_OK; +} + +nsresult +nsLoggingSink::LeafNode(const nsIParserNode& aNode) +{ + nsHTMLTag nodeType = nsHTMLTag(aNode.GetNodeType()); + if ((nodeType >= eHTMLTag_unknown) && + (nodeType <= nsHTMLTag(NS_HTML_TAG_MAX))) { + const char* tag = NS_EnumToTag(nodeType); + fprintf(mFile, "<leaf tag=\"%s\">\n", tag); + WriteAttributes(aNode); + fputs("</leaf>\n", mFile); + } + else { + PRInt32 pos; + nsAutoString tmp; + switch (nodeType) { + case eHTMLTag_whitespace: + case eHTMLTag_newline: + case eHTMLTag_text: + QuoteText(aNode.GetText(), tmp); + fputs("<text value=\"", mFile); + fputs(tmp, mFile); + fputs("\"/>\n", mFile); + break; + + case eHTMLTag_entity: + tmp.Append(aNode.GetText()); + tmp.Cut(0, 1); + pos = tmp.Length() - 1; + if (pos >= 0) { + tmp.Cut(pos, 1); + } + fputs("<entity value=\"", mFile); + fputs(tmp, mFile); + fputs("\"/>\n", mFile); + break; + + default: + NS_NOTREACHED("unsupported leaf node type"); + } + } + return NS_OK; +} + +nsresult +nsLoggingSink::QuoteText(const nsString& aValue, nsString& aResult) +{ + const PRUnichar* cp = aValue.GetUnicode(); + const PRUnichar* end = cp + aValue.Length(); + while (cp < end) { + PRUnichar ch = *cp++; + if (ch == '"') { + aResult.Append("""); + } + else if (ch == '&') { + aResult.Append("&"); + } + else if ((ch < 32) || (ch >= 127)) { + aResult.Append("&#"); + aResult.Append(PRInt32(ch), 10); + aResult.Append(';'); + } + else { + aResult.Append(ch); + } + } + return NS_OK; +} diff --git a/mozilla/parser/htmlparser/tests/logparse/logparse.cpp b/mozilla/parser/htmlparser/tests/logparse/logparse.cpp new file mode 100644 index 00000000000..7e2d0e5d056 --- /dev/null +++ b/mozilla/parser/htmlparser/tests/logparse/logparse.cpp @@ -0,0 +1,102 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See + * the License for the specific language governing rights and limitations + * under the License. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ +#include "nsRepository.h" +#include "nsParserCIID.h" +#include "nsIParser.h" +#include "nsILoggingSink.h" +#include "CNavDTD.h" +#include <fstream.h> + +#ifdef XP_PC +#define PARSER_DLL "raptorhtmlpars.dll" +#endif +#ifdef XP_MAC +#endif +#ifdef XP_UNIX +#define PARSER_DLL "libraptorhtmlpars.so" +#endif + +// Class IID's +static NS_DEFINE_IID(kParserCID, NS_PARSER_IID); +static NS_DEFINE_IID(kLoggingSinkCID, NS_LOGGING_SINK_IID); + +// Interface IID's +static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID); +static NS_DEFINE_IID(kILoggingSinkIID, NS_ILOGGING_SINK_IID); + +static void SetupRegistry() +{ + NSRepository::RegisterFactory(kParserCID, PARSER_DLL, PR_FALSE, PR_FALSE); + NSRepository::RegisterFactory(kLoggingSinkCID, PARSER_DLL,PR_FALSE,PR_FALSE); +} + +//---------------------------------------------------------------------- + +int main(int argc, char** argv) +{ + if (argc != 3) { + fprintf(stderr, "Usage: logparse in out\n"); + return -1; + } + + fstream *in = new fstream(); + in->open(argv[1], ios::in | ios::nocreate); + + FILE* fp = fopen(argv[2], "wb"); + if (nsnull == fp) { + fprintf(stderr, "can't create '%s'\n", argv[2]); + return -1; + } + + SetupRegistry(); + + // Create a parser + nsIParser* parser; + 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 + nsILoggingSink* sink; + rv = NSRepository::CreateInstance(kLoggingSinkCID, + nsnull, + kILoggingSinkIID, + (void**)&sink); + 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; + NS_NewNavHTMLDTD(&dtd); + parser->RegisterDTD(dtd); + parser->SetContentSink(sink); + PRInt32 status = parser->Parse(*in); + NS_RELEASE(parser); + NS_RELEASE(sink); + + return (NS_OK == status) ? 0 : -1; +} diff --git a/mozilla/parser/htmlparser/tests/logparse/makefile.win b/mozilla/parser/htmlparser/tests/logparse/makefile.win new file mode 100644 index 00000000000..95b779c3311 --- /dev/null +++ b/mozilla/parser/htmlparser/tests/logparse/makefile.win @@ -0,0 +1,56 @@ +#!nmake +# +# The contents of this file are subject to the Netscape Public License +# Version 1.0 (the "NPL"); you may not use this file except in +# compliance with the NPL. You may obtain a copy of the NPL at +# http://www.mozilla.org/NPL/ +# +# Software distributed under the NPL is distributed on an "AS IS" basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL +# for the specific language governing rights and limitations under the +# NPL. +# +# The Initial Developer of this code under the NPL is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All Rights +# Reserved. + +DEPTH=..\..\.. +IGNORE_MANIFEST=1 + +MAKE_OBJ_TYPE = EXE +PROGRAM = .\$(OBJDIR)\logparse.exe + +MISCDEP= \ + $(DIST)\lib\raptorhtmlpars.lib \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) + +OBJS = \ + .\$(OBJDIR)\logparse.obj \ + $(NULL) + +LINCS= \ + -I$(PUBLIC)\raptor \ + -I$(PUBLIC)\xpcom \ + -I$(PUBLIC)\netlib + +MYLIBS= \ + $(DIST)\lib\raptorhtmlpars.lib \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) \ + $(NULL) + +LLIBS= $(MYLIBS) -SUBSYSTEM:CONSOLE + +include <$(DEPTH)\config\rules.mak> + +!ifdef MOZ_NO_DEBUG_RTL +OS_CFLAGS = $(OS_CFLAGS) -DMOZ_NO_DEBUG_RTL +!endif + +install:: $(PROGRAM) + $(MAKE_INSTALL) $(PROGRAM) $(DIST)\bin + +clobber:: + rm -f $(DIST)\bin\logparse.exe