2000-04-04 01:41:19 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (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.org 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.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsTXTToHTMLConv.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIStringStream.h"
|
|
|
|
|
|
|
|
#define TOKEN_DELIMITERS "\t\r\n "
|
|
|
|
|
|
|
|
// nsISupports methods
|
2000-04-05 09:53:35 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS4(nsTXTToHTMLConv,
|
2000-04-04 01:41:19 +04:00
|
|
|
nsIStreamConverter,
|
2000-04-05 09:53:35 +04:00
|
|
|
nsITXTToHTMLConv,
|
2000-04-04 01:41:19 +04:00
|
|
|
nsIStreamObserver,
|
|
|
|
nsIStreamListener);
|
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamConverter methods
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTXTToHTMLConv::Convert(nsIInputStream *aFromStream,
|
|
|
|
const PRUnichar *aFromType, const PRUnichar *aToType,
|
|
|
|
nsISupports *aCtxt, nsIInputStream * *_retval) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTXTToHTMLConv::AsyncConvertData(const PRUnichar *aFromType,
|
|
|
|
const PRUnichar *aToType,
|
|
|
|
nsIStreamListener *aListener,
|
|
|
|
nsISupports *aCtxt) {
|
|
|
|
NS_ASSERTION(aListener, "null pointer");
|
|
|
|
mListener = aListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamObserver methods
|
|
|
|
NS_IMETHODIMP
|
2001-02-12 06:14:23 +03:00
|
|
|
nsTXTToHTMLConv::OnStartRequest(nsIChannel *aChannel, nsISupports *aContext) {
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.AssignWithConversion("<html>\n<head><title>");
|
2000-04-05 09:53:35 +04:00
|
|
|
mBuffer.Append(mPageTitle);
|
|
|
|
mBuffer.AppendWithConversion("</title></head>\n<body>\n");
|
|
|
|
if (mPreFormatHTML) { // Use <pre> tags
|
|
|
|
mBuffer.AppendWithConversion("<pre>\n");
|
|
|
|
}
|
|
|
|
|
2000-04-17 08:40:44 +04:00
|
|
|
// Push mBuffer to the listener now, so the initial HTML will not
|
|
|
|
// be parsed in OnDataAvailable().
|
|
|
|
|
2001-02-12 06:14:23 +03:00
|
|
|
nsresult rv = mListener->OnStartRequest(aChannel, aContext);
|
2000-04-17 08:40:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
|
|
|
nsCOMPtr<nsISupports> inputDataSup;
|
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputDataSup), mBuffer);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
inputData = do_QueryInterface(inputDataSup);
|
2001-02-12 06:14:23 +03:00
|
|
|
rv = mListener->OnDataAvailable(aChannel, aContext,
|
2000-04-17 08:40:44 +04:00
|
|
|
inputData, 0, mBuffer.Length());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
mBuffer.AssignWithConversion("");
|
|
|
|
return rv;
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
2000-04-17 08:40:44 +04:00
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
NS_IMETHODIMP
|
2001-02-12 06:14:23 +03:00
|
|
|
nsTXTToHTMLConv::OnStopRequest(nsIChannel *aChannel, nsISupports *aContext,
|
2000-07-25 09:45:56 +04:00
|
|
|
nsresult aStatus, const PRUnichar* aStatusArg) {
|
2000-04-04 01:41:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mToken) {
|
|
|
|
// we still have an outstanding token
|
|
|
|
PRInt32 back = mBuffer.FindCharInSet(TOKEN_DELIMITERS);
|
|
|
|
if (back == -1) back = mBuffer.Length();
|
|
|
|
(void)CatHTML(0, mBuffer.Length());
|
|
|
|
}
|
2000-04-05 09:53:35 +04:00
|
|
|
if (mPreFormatHTML) {
|
|
|
|
mBuffer.AppendWithConversion("</pre>\n");
|
|
|
|
}
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.AppendWithConversion("\n</body></html>");
|
2000-04-04 01:41:19 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
|
|
|
nsCOMPtr<nsISupports> inputDataSup;
|
|
|
|
|
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputDataSup), mBuffer);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
inputData = do_QueryInterface(inputDataSup);
|
|
|
|
|
2001-02-12 06:14:23 +03:00
|
|
|
rv = mListener->OnDataAvailable(aChannel, aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
inputData, 0, mBuffer.Length());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-12 06:14:23 +03:00
|
|
|
return mListener->OnStopRequest(aChannel, aContext, aStatus, aStatusArg);
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
2000-04-05 09:53:35 +04:00
|
|
|
// nsITXTToHTMLConv methods
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTXTToHTMLConv::SetTitle(const PRUnichar *aTitle) {
|
|
|
|
mPageTitle.Assign(aTitle);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTXTToHTMLConv::PreFormatHTML(PRBool value) {
|
|
|
|
mPreFormatHTML = value;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
// nsIStreamListener method
|
|
|
|
NS_IMETHODIMP
|
2001-02-12 06:14:23 +03:00
|
|
|
nsTXTToHTMLConv::OnDataAvailable(nsIChannel *aChannel, nsISupports *aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
nsIInputStream *aInStream,
|
|
|
|
PRUint32 aOffset, PRUint32 aCount) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsString pushBuffer;
|
|
|
|
PRUint32 amtRead = 0;
|
2000-06-03 13:46:12 +04:00
|
|
|
char *buffer = (char*)nsMemory::Alloc(aCount+1);
|
2000-04-04 01:41:19 +04:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
do {
|
|
|
|
PRUint32 read = 0;
|
|
|
|
rv = aInStream->Read(buffer, aCount-amtRead, &read);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
buffer[read] = '\0';
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.AppendWithConversion(buffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
amtRead += read;
|
|
|
|
|
|
|
|
PRInt32 front = -1, back = -1, tokenLoc = -1, cursor = 0;
|
|
|
|
|
|
|
|
while ( (tokenLoc = FindToken(cursor, &mToken)) > -1) {
|
|
|
|
front = mBuffer.RFindCharInSet(TOKEN_DELIMITERS, tokenLoc);
|
|
|
|
front++;
|
|
|
|
|
|
|
|
back = mBuffer.FindCharInSet(TOKEN_DELIMITERS, tokenLoc);
|
|
|
|
if (back == -1) {
|
|
|
|
// didn't find an ending, buffer up.
|
|
|
|
mBuffer.Left(pushBuffer, front);
|
|
|
|
cursor = front;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// found the end of the token.
|
|
|
|
cursor = CatHTML(front, back);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 end = mBuffer.RFind(TOKEN_DELIMITERS,
|
|
|
|
PR_FALSE, mBuffer.Length());
|
|
|
|
mBuffer.Left(pushBuffer, PR_MAX(cursor, end));
|
|
|
|
mBuffer.Cut(0, PR_MAX(cursor, end));
|
|
|
|
cursor = 0;
|
|
|
|
|
|
|
|
if (!pushBuffer.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
|
|
|
nsCOMPtr<nsISupports> inputDataSup;
|
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputDataSup), pushBuffer);
|
|
|
|
if (NS_FAILED(rv)) {
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(buffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputData = do_QueryInterface(inputDataSup);
|
|
|
|
|
2001-02-12 06:14:23 +03:00
|
|
|
rv = mListener->OnDataAvailable(aChannel, aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
inputData, 0, pushBuffer.Length());
|
|
|
|
if (NS_FAILED(rv)) {
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(buffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (amtRead < aCount);
|
|
|
|
|
2000-06-03 13:46:12 +04:00
|
|
|
nsMemory::Free(buffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
// nsTXTToHTMLConv methods
|
|
|
|
nsTXTToHTMLConv::nsTXTToHTMLConv() {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
mToken = nsnull;
|
2000-04-05 09:53:35 +04:00
|
|
|
mPreFormatHTML = PR_FALSE;
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
2000-05-06 13:15:56 +04:00
|
|
|
static PRBool CleanupTokens(void *aElement, void *aData) {
|
2000-04-04 01:41:19 +04:00
|
|
|
if (aElement) delete (convToken*)aElement;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTXTToHTMLConv::~nsTXTToHTMLConv() {
|
|
|
|
mTokens.EnumerateForwards((nsVoidArrayEnumFunc)CleanupTokens, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTXTToHTMLConv::Init() {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// build up the list of tokens to handle
|
|
|
|
convToken *token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
token->prepend = PR_TRUE;
|
2000-04-15 13:16:58 +04:00
|
|
|
token->token.AssignWithConversion("http://"); // XXX need to iterate through all protos
|
2000-04-04 01:41:19 +04:00
|
|
|
mTokens.AppendElement(token);
|
|
|
|
|
|
|
|
token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
token->prepend = PR_TRUE;
|
2000-04-15 13:16:58 +04:00
|
|
|
token->token.AssignWithConversion('@');
|
|
|
|
token->modText.AssignWithConversion("mailto:");
|
2000-04-04 01:41:19 +04:00
|
|
|
mTokens.AppendElement(token);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsTXTToHTMLConv::FindToken(PRInt32 cursor, convToken* *_retval) {
|
|
|
|
PRInt32 loc = -1, firstToken = mBuffer.Length();
|
|
|
|
PRInt8 token = -1;
|
|
|
|
for (PRInt8 i=0; i < mTokens.Count(); i++) {
|
|
|
|
loc = mBuffer.Find(((convToken*)mTokens[i])->token, PR_FALSE, cursor);
|
|
|
|
if (loc != -1)
|
|
|
|
if (loc < firstToken) {
|
|
|
|
firstToken = loc;
|
|
|
|
token = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (token != -1) {
|
|
|
|
*_retval = (convToken*)mTokens[token];
|
|
|
|
return firstToken;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
|
|
|
nsTXTToHTMLConv::CatHTML(PRInt32 front, PRInt32 back) {
|
|
|
|
PRInt32 cursor = 0;
|
|
|
|
if (!mToken->prepend) {
|
|
|
|
// replace the entire token (from delimiter to delimiter)
|
|
|
|
mBuffer.Cut(front, back);
|
|
|
|
mBuffer.Insert(mToken->modText, front);
|
|
|
|
} else {
|
|
|
|
nsString linkText;
|
|
|
|
// href is implied
|
|
|
|
PRInt32 modLen = mToken->modText.Length();
|
|
|
|
mBuffer.Mid(linkText, front, back-front);
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.InsertWithConversion("<a href=\"", front);
|
2000-04-04 01:41:19 +04:00
|
|
|
cursor += front+9;
|
|
|
|
if (modLen)
|
|
|
|
mBuffer.Insert(mToken->modText, cursor);
|
|
|
|
cursor += modLen-front+back;
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.InsertWithConversion("\">", cursor);
|
2000-04-04 01:41:19 +04:00
|
|
|
cursor += 2;
|
|
|
|
mBuffer.Insert(linkText, cursor);
|
|
|
|
cursor += linkText.Length();
|
2000-04-15 13:16:58 +04:00
|
|
|
mBuffer.InsertWithConversion("</a>", cursor);
|
2000-04-04 01:41:19 +04:00
|
|
|
cursor += 4;
|
|
|
|
}
|
|
|
|
mToken = nsnull; // indicates completeness
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2001-02-12 06:14:23 +03:00
|
|
|
nsresult
|
|
|
|
NS_NewNSTXTToHTMLConv(nsTXTToHTMLConv** aTXTToHTMLConv)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aTXTToHTMLConv != nsnull, "null ptr");
|
|
|
|
if (!aTXTToHTMLConv)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aTXTToHTMLConv = new nsTXTToHTMLConv();
|
|
|
|
if (!*aTXTToHTMLConv)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(*aTXTToHTMLConv);
|
|
|
|
return (*aTXTToHTMLConv)->Init();
|
|
|
|
}
|