2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-19 02:01:16 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-04-04 01:41:19 +04:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
2000-04-04 01:41:19 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* 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.
|
2000-04-04 01:41:19 +04:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-29 00:14:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-04-04 01:41:19 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-09-29 00:14:13 +04:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-19 02:01:16 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-29 00:14:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-19 02:01:16 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-04-04 01:41:19 +04:00
|
|
|
|
|
|
|
#include "nsTXTToHTMLConv.h"
|
|
|
|
#include "nsNetUtil.h"
|
2006-01-02 05:57:15 +03:00
|
|
|
#include "nsStringStream.h"
|
2004-07-02 03:33:47 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2000-04-04 01:41:19 +04:00
|
|
|
|
2002-03-05 02:18:21 +03:00
|
|
|
#define TOKEN_DELIMITERS NS_LITERAL_STRING("\t\r\n ").get()
|
2000-04-04 01:41:19 +04:00
|
|
|
|
|
|
|
// nsISupports methods
|
2007-03-23 02:01:14 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS4(nsTXTToHTMLConv,
|
|
|
|
nsIStreamConverter,
|
|
|
|
nsITXTToHTMLConv,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener)
|
2000-04-04 01:41:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
// nsIStreamConverter methods
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTXTToHTMLConv::Convert(nsIInputStream *aFromStream,
|
2004-06-29 20:45:07 +04:00
|
|
|
const char *aFromType, const char *aToType,
|
2004-07-02 03:33:47 +04:00
|
|
|
nsISupports *aCtxt, nsIInputStream * *_retval)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-06-29 20:45:07 +04:00
|
|
|
nsTXTToHTMLConv::AsyncConvertData(const char *aFromType,
|
|
|
|
const char *aToType,
|
2000-04-04 01:41:19 +04:00
|
|
|
nsIStreamListener *aListener,
|
2004-07-02 03:33:47 +04:00
|
|
|
nsISupports *aCtxt)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
NS_ASSERTION(aListener, "null pointer");
|
|
|
|
mListener = aListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
// nsIRequestObserver methods
|
2000-04-04 01:41:19 +04:00
|
|
|
NS_IMETHODIMP
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::OnStartRequest(nsIRequest* request, nsISupports *aContext)
|
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
mBuffer.AssignLiteral("<html>\n<head><title>");
|
2000-04-05 09:53:35 +04:00
|
|
|
mBuffer.Append(mPageTitle);
|
2004-06-17 04:13:25 +04:00
|
|
|
mBuffer.AppendLiteral("</title></head>\n<body>\n");
|
2000-04-05 09:53:35 +04:00
|
|
|
if (mPreFormatHTML) { // Use <pre> tags
|
2004-07-02 03:33:47 +04:00
|
|
|
mBuffer.AppendLiteral("<pre>\n");
|
2000-04-05 09:53:35 +04:00
|
|
|
}
|
|
|
|
|
2000-04-17 08:40:44 +04:00
|
|
|
// Push mBuffer to the listener now, so the initial HTML will not
|
|
|
|
// be parsed in OnDataAvailable().
|
|
|
|
|
2002-09-24 05:04:54 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
2003-01-18 05:15:14 +03:00
|
|
|
if (channel)
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/html"));
|
|
|
|
// else, assume there is a channel somewhere that knows what it is doing!
|
2002-09-24 05:04:54 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult rv = mListener->OnStartRequest(request, aContext);
|
2000-04-17 08:40:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2006-08-01 21:17:32 +04:00
|
|
|
// The request may have been canceled, and if that happens, we want to
|
|
|
|
// suppress calls to OnDataAvailable.
|
|
|
|
request->GetStatus(&rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-04-17 08:40:44 +04:00
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
2002-01-28 01:02:00 +03:00
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputData), mBuffer);
|
2000-04-17 08:40:44 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mListener->OnDataAvailable(request, aContext,
|
2000-04-17 08:40:44 +04:00
|
|
|
inputData, 0, mBuffer.Length());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2004-01-30 01:04:45 +03:00
|
|
|
mBuffer.Truncate();
|
2000-04-17 08:40:44 +04:00
|
|
|
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-21 23:38:08 +03:00
|
|
|
nsTXTToHTMLConv::OnStopRequest(nsIRequest* request, nsISupports *aContext,
|
2004-07-02 03:33:47 +04:00
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mToken) {
|
|
|
|
// we still have an outstanding token
|
2004-07-02 03:33:47 +04:00
|
|
|
NS_ASSERTION(mToken->prepend,
|
|
|
|
"Non prepending tokens should be handled in "
|
|
|
|
"OnDataAvailable. There should only be a single "
|
|
|
|
"prepending token left to be processed.");
|
2000-04-04 01:41:19 +04:00
|
|
|
(void)CatHTML(0, mBuffer.Length());
|
|
|
|
}
|
2000-04-05 09:53:35 +04:00
|
|
|
if (mPreFormatHTML) {
|
2004-07-02 03:33:47 +04:00
|
|
|
mBuffer.AppendLiteral("</pre>\n");
|
2000-04-05 09:53:35 +04:00
|
|
|
}
|
2004-07-02 03:33:47 +04:00
|
|
|
mBuffer.AppendLiteral("\n</body></html>");
|
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
|
|
|
|
2002-01-28 01:02:00 +03:00
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputData), mBuffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mListener->OnDataAvailable(request, aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
inputData, 0, mBuffer.Length());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
return mListener->OnStopRequest(request, aContext, aStatus);
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
2000-04-05 09:53:35 +04:00
|
|
|
// nsITXTToHTMLConv methods
|
|
|
|
NS_IMETHODIMP
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::SetTitle(const PRUnichar *aTitle)
|
|
|
|
{
|
|
|
|
mPageTitle.Assign(aTitle);
|
|
|
|
return NS_OK;
|
2000-04-05 09:53:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::PreFormatHTML(PRBool value)
|
|
|
|
{
|
|
|
|
mPreFormatHTML = value;
|
|
|
|
return NS_OK;
|
2000-04-05 09:53:35 +04:00
|
|
|
}
|
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
// nsIStreamListener method
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsTXTToHTMLConv::OnDataAvailable(nsIRequest* request, nsISupports *aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
nsIInputStream *aInStream,
|
2004-07-02 03:33:47 +04:00
|
|
|
PRUint32 aOffset, PRUint32 aCount)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsString pushBuffer;
|
|
|
|
PRUint32 amtRead = 0;
|
2004-07-02 03:33:47 +04:00
|
|
|
nsAutoArrayPtr<char> buffer(new char[aCount+1]);
|
2000-04-04 01:41:19 +04:00
|
|
|
if (!buffer) return NS_ERROR_OUT_OF_MEMORY;
|
2004-07-02 03:33:47 +04:00
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
do {
|
|
|
|
PRUint32 read = 0;
|
2004-05-11 18:27:59 +04:00
|
|
|
// XXX readSegments, to avoid the first copy?
|
2000-04-04 01:41:19 +04:00
|
|
|
rv = aInStream->Read(buffer, aCount-amtRead, &read);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
buffer[read] = '\0';
|
2004-05-11 18:27:59 +04:00
|
|
|
// XXX charsets?? non-latin1 characters?? utf-16??
|
2004-07-21 16:25:20 +04:00
|
|
|
AppendASCIItoUTF16(buffer, mBuffer);
|
2000-04-04 01:41:19 +04:00
|
|
|
amtRead += read;
|
|
|
|
|
2004-07-02 03:33:47 +04:00
|
|
|
PRInt32 front = -1, back = -1, tokenLoc = -1, cursor = 0;
|
2000-04-04 01:41:19 +04:00
|
|
|
|
|
|
|
while ( (tokenLoc = FindToken(cursor, &mToken)) > -1) {
|
2004-07-02 03:33:47 +04:00
|
|
|
if (mToken->prepend) {
|
|
|
|
front = mBuffer.RFindCharInSet(TOKEN_DELIMITERS, tokenLoc);
|
|
|
|
front++;
|
|
|
|
back = mBuffer.FindCharInSet(TOKEN_DELIMITERS, tokenLoc);
|
|
|
|
} else {
|
|
|
|
front = tokenLoc;
|
|
|
|
back = front + mToken->token.Length();
|
|
|
|
}
|
2000-04-04 01:41:19 +04:00
|
|
|
if (back == -1) {
|
|
|
|
// didn't find an ending, buffer up.
|
|
|
|
mBuffer.Left(pushBuffer, front);
|
|
|
|
cursor = front;
|
|
|
|
break;
|
|
|
|
}
|
2004-07-02 03:33:47 +04:00
|
|
|
// found the end of the token.
|
|
|
|
cursor = CatHTML(front, back);
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
2002-03-19 09:46:56 +03:00
|
|
|
PRInt32 end = mBuffer.RFind(TOKEN_DELIMITERS, mBuffer.Length());
|
2000-04-04 01:41:19 +04:00
|
|
|
mBuffer.Left(pushBuffer, PR_MAX(cursor, end));
|
|
|
|
mBuffer.Cut(0, PR_MAX(cursor, end));
|
|
|
|
cursor = 0;
|
|
|
|
|
|
|
|
if (!pushBuffer.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
2002-01-28 01:02:00 +03:00
|
|
|
|
|
|
|
rv = NS_NewStringInputStream(getter_AddRefs(inputData), pushBuffer);
|
2004-07-02 03:33:47 +04:00
|
|
|
if (NS_FAILED(rv))
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mListener->OnDataAvailable(request, aContext,
|
2000-04-04 01:41:19 +04:00
|
|
|
inputData, 0, pushBuffer.Length());
|
2004-07-02 03:33:47 +04:00
|
|
|
if (NS_FAILED(rv))
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} while (amtRead < aCount);
|
|
|
|
|
2004-07-02 03:33:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
// nsTXTToHTMLConv methods
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::nsTXTToHTMLConv()
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
mToken = nsnull;
|
2000-04-05 09:53:35 +04:00
|
|
|
mPreFormatHTML = PR_FALSE;
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
2004-07-02 03:33:47 +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;
|
|
|
|
}
|
|
|
|
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::~nsTXTToHTMLConv()
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
mTokens.EnumerateForwards((nsVoidArrayEnumFunc)CleanupTokens, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::Init()
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// build up the list of tokens to handle
|
|
|
|
convToken *token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
2004-07-02 03:33:47 +04:00
|
|
|
token->prepend = PR_FALSE;
|
2004-12-04 13:19:29 +03:00
|
|
|
token->token.Assign(PRUnichar('<'));
|
|
|
|
token->modText.AssignLiteral("<");
|
2004-07-02 03:33:47 +04:00
|
|
|
mTokens.AppendElement(token);
|
|
|
|
|
|
|
|
token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
token->prepend = PR_FALSE;
|
2004-12-04 13:19:29 +03:00
|
|
|
token->token.Assign(PRUnichar('>'));
|
|
|
|
token->modText.AssignLiteral(">");
|
2004-07-02 03:33:47 +04:00
|
|
|
mTokens.AppendElement(token);
|
|
|
|
|
|
|
|
token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
token->prepend = PR_FALSE;
|
2004-12-04 13:19:29 +03:00
|
|
|
token->token.Assign(PRUnichar('&'));
|
|
|
|
token->modText.AssignLiteral("&");
|
2004-07-02 03:33:47 +04:00
|
|
|
mTokens.AppendElement(token);
|
|
|
|
|
|
|
|
token = new convToken;
|
|
|
|
if (!token) return NS_ERROR_OUT_OF_MEMORY;
|
2000-04-04 01:41:19 +04:00
|
|
|
token->prepend = PR_TRUE;
|
2004-06-17 04:13:25 +04:00
|
|
|
token->token.AssignLiteral("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;
|
2001-12-16 14:58:03 +03:00
|
|
|
token->token.Assign(PRUnichar('@'));
|
2004-06-17 04:13:25 +04:00
|
|
|
token->modText.AssignLiteral("mailto:");
|
2000-04-04 01:41:19 +04:00
|
|
|
mTokens.AppendElement(token);
|
2004-07-02 03:33:47 +04:00
|
|
|
|
2000-04-04 01:41:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::FindToken(PRInt32 cursor, convToken* *_retval)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
PRInt32 loc = -1, firstToken = mBuffer.Length();
|
|
|
|
PRInt8 token = -1;
|
|
|
|
for (PRInt8 i=0; i < mTokens.Count(); i++) {
|
2002-03-19 09:46:56 +03:00
|
|
|
loc = mBuffer.Find(((convToken*)mTokens[i])->token, cursor);
|
2000-04-04 01:41:19 +04:00
|
|
|
if (loc != -1)
|
|
|
|
if (loc < firstToken) {
|
|
|
|
firstToken = loc;
|
|
|
|
token = i;
|
|
|
|
}
|
|
|
|
}
|
2004-07-02 03:33:47 +04:00
|
|
|
if (token == -1)
|
2000-04-04 01:41:19 +04:00
|
|
|
return -1;
|
2004-07-02 03:33:47 +04:00
|
|
|
|
|
|
|
*_retval = (convToken*)mTokens[token];
|
|
|
|
return firstToken;
|
2000-04-04 01:41:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32
|
2004-07-02 03:33:47 +04:00
|
|
|
nsTXTToHTMLConv::CatHTML(PRInt32 front, PRInt32 back)
|
|
|
|
{
|
2000-04-04 01:41:19 +04:00
|
|
|
PRInt32 cursor = 0;
|
2004-07-02 03:33:47 +04:00
|
|
|
PRInt32 modLen = mToken->modText.Length();
|
2000-04-04 01:41:19 +04:00
|
|
|
if (!mToken->prepend) {
|
|
|
|
// replace the entire token (from delimiter to delimiter)
|
2004-07-02 03:33:47 +04:00
|
|
|
mBuffer.Cut(front, back - front);
|
2000-04-04 01:41:19 +04:00
|
|
|
mBuffer.Insert(mToken->modText, front);
|
2004-07-02 03:33:47 +04:00
|
|
|
cursor = front+modLen;
|
2000-04-04 01:41:19 +04:00
|
|
|
} else {
|
|
|
|
nsString linkText;
|
|
|
|
// href is implied
|
|
|
|
mBuffer.Mid(linkText, front, back-front);
|
2001-12-16 14:58:03 +03:00
|
|
|
mBuffer.Insert(NS_LITERAL_STRING("<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;
|
2001-12-16 14:58:03 +03:00
|
|
|
mBuffer.Insert(NS_LITERAL_STRING("\">"), cursor);
|
2000-04-04 01:41:19 +04:00
|
|
|
cursor += 2;
|
|
|
|
mBuffer.Insert(linkText, cursor);
|
|
|
|
cursor += linkText.Length();
|
2001-12-16 14:58:03 +03:00
|
|
|
mBuffer.Insert(NS_LITERAL_STRING("</a>"), cursor);
|
2000-04-04 01:41:19 +04:00
|
|
|
cursor += 4;
|
|
|
|
}
|
|
|
|
mToken = nsnull; // indicates completeness
|
|
|
|
return cursor;
|
|
|
|
}
|