Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
nsTXTToHTMLConv Class Reference

Convert plain text to HTML. More...

#include <nsTXTToHTMLConv.h>

Inheritance diagram for nsTXTToHTMLConv:
Inheritance graph
[legend]
Collaboration diagram for nsTXTToHTMLConv:
Collaboration graph
[legend]

List of all members.

Public Member Functions

NS_DECL_ISUPPORTS
NS_DECL_NSISTREAMCONVERTER
NS_DECL_NSITXTTOHTMLCONV
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSISTREAMLISTENER 
nsTXTToHTMLConv ()
virtual ~nsTXTToHTMLConv ()
nsresult Init ()
void setTitle (in wstring text)
void preFormatHTML (in boolean value)
nsIInputStream convert (in nsIInputStream aFromStream, in string aFromType, in string aToType, in nsISupports aCtxt)
 SYNCRONOUS VERSION Converts a stream of one type, to a stream of another type.
void asyncConvertData (in string aFromType, in string aToType, in nsIStreamListener aListener, in nsISupports aCtxt)
 ASYNCRONOUS VERSION Converts data arriving via the converter's nsIStreamListener::OnDataAvailable() method from one type to another, pushing the converted data out to the caller via aListener::OnDataAvailable().
void onDataAvailable (in nsIRequest aRequest, in nsISupports aContext, in nsIInputStream aInputStream, in unsigned long aOffset, in unsigned long aCount)
 Called when the next chunk of data (corresponding to the request) may be read without blocking the calling thread.
void onStartRequest (in nsIRequest aRequest, in nsISupports aContext)
 Called to signify the beginning of an asynchronous request.
void onStopRequest (in nsIRequest aRequest, in nsISupports aContext, in nsresult aStatusCode)
 Called to signify the end of an asynchronous request.

Static Public Member Functions

static NS_METHOD Create (nsISupports *aOuter, REFNSIID aIID, void **aResult)

Protected Member Functions

PRInt32 FindToken (PRInt32 cursor, convToken **_retval)
PRInt32 CatHTML (PRInt32 front, PRInt32 back)

Protected Attributes

nsCOMPtr< nsIStreamListenermListener
nsString mBuffer
nsVoidArray mTokens
convTokenmToken
nsString mPageTitle
PRBool mPreFormatHTML

Detailed Description

Convert plain text to HTML.

OVERVIEW OF HOW THIS CLASS WORKS:

This class stores an array of tokens that should be replaced by something, or something that should be prepended. The "token" member of convToken is the text to search for. This is a substring of the desired token. Tokens are delimited by TOKEN_DELIMITERS. That entire token will be replaced by modText (if prepend is false); or it will be linkified and modText will be prepended to the token if prepend is true.

Note that all of the text will be in a preformatted block, so there is no need to emit line-end tags, or set the font face to monospace.

This works as a stream converter, so data will arrive by OnStartRequest/OnDataAvailable/OnStopRequest calls.

OStopR will possibly process a remaining token.

If the data of one pass contains a part of a token, that part will be stored in mBuffer. The rest of the data will be sent to the next listener.

XXX this seems suboptimal. this means that this design will only work for links. and it is impossible to append anything to the token. this means that, for example, making foo bold is not possible.

Definition at line 90 of file nsTXTToHTMLConv.h.


Constructor & Destructor Documentation

Definition at line 224 of file nsTXTToHTMLConv.cpp.

Here is the caller graph for this function:

Definition at line 236 of file nsTXTToHTMLConv.cpp.

{
    mTokens.EnumerateForwards((nsVoidArrayEnumFunc)CleanupTokens, nsnull);
}

Here is the call graph for this function:


Member Function Documentation

void nsIStreamConverter::asyncConvertData ( in string  aFromType,
in string  aToType,
in nsIStreamListener  aListener,
in nsISupports  aCtxt 
) [inherited]

ASYNCRONOUS VERSION Converts data arriving via the converter's nsIStreamListener::OnDataAvailable() method from one type to another, pushing the converted data out to the caller via aListener::OnDataAvailable().

Use this method when you want to proxy (and convert) nsIStreamListener callbacks asynchronously.

Parameters:
aFromTypeThe MIME type of the original/raw data.
aToTypeThe MIME type of the converted data.
aListenerThe listener who receives the converted data.
aCtxtEither an opaque context, or a converter specific context (implementation specific).
PRInt32 nsTXTToHTMLConv::CatHTML ( PRInt32  front,
PRInt32  back 
) [protected]

Definition at line 305 of file nsTXTToHTMLConv.cpp.

{
    PRInt32 cursor = 0;
    PRInt32 modLen = mToken->modText.Length();
    if (!mToken->prepend) {
        // replace the entire token (from delimiter to delimiter)
        mBuffer.Cut(front, back - front);
        mBuffer.Insert(mToken->modText, front);
        cursor = front+modLen;
    } else {
        nsString linkText;
        // href is implied
        mBuffer.Mid(linkText, front, back-front);
        mBuffer.Insert(NS_LITERAL_STRING("<a href=\""), front);
        cursor += front+9;
        if (modLen)
            mBuffer.Insert(mToken->modText, cursor);
        cursor += modLen-front+back;
        mBuffer.Insert(NS_LITERAL_STRING("\">"), cursor);
        cursor += 2;
        mBuffer.Insert(linkText, cursor);
        cursor += linkText.Length();
        mBuffer.Insert(NS_LITERAL_STRING("</a>"), cursor);
        cursor += 4;
    }
    mToken = nsnull; // indicates completeness
    return cursor;
}
nsIInputStream nsIStreamConverter::convert ( in nsIInputStream  aFromStream,
in string  aFromType,
in string  aToType,
in nsISupports  aCtxt 
) [inherited]

SYNCRONOUS VERSION Converts a stream of one type, to a stream of another type.

Use this method when you have a stream you want to convert.

Parameters:
aFromStreamThe stream representing the original/raw data.
aFromTypeThe MIME type of aFromStream.
aToTypeThe MIME type of the returned stream.
aCtxtEither an opaque context, or a converter specific context (implementation specific).
Returns:
The converted stream. NOTE: The returned stream may not already be converted. An efficient stream converter implementation will converter data on demand rather than buffering the converted data until it is used.
static NS_METHOD nsTXTToHTMLConv::Create ( nsISupports *  aOuter,
REFNSIID  aIID,
void **  aResult 
) [inline, static]

Definition at line 104 of file nsTXTToHTMLConv.h.

                                                               {
        nsresult rv;
        if (aOuter)
            return NS_ERROR_NO_AGGREGATION;

        nsTXTToHTMLConv* _s = new nsTXTToHTMLConv();
        if (_s == nsnull)
            return NS_ERROR_OUT_OF_MEMORY;
        NS_ADDREF(_s);
        rv = _s->Init();
        if (NS_FAILED(rv)) {
            delete _s;
            return rv;
        }
        rv = _s->QueryInterface(aIID, aResult);
        NS_RELEASE(_s);
        return rv;
    }

Here is the call graph for this function:

PRInt32 nsTXTToHTMLConv::FindToken ( PRInt32  cursor,
convToken **  _retval 
) [protected]

Definition at line 285 of file nsTXTToHTMLConv.cpp.

{
    PRInt32 loc = -1, firstToken = mBuffer.Length();
    PRInt8 token = -1;
    for (PRInt8 i=0; i < mTokens.Count(); i++) {
        loc = mBuffer.Find(((convToken*)mTokens[i])->token, cursor);
        if (loc != -1)
            if (loc < firstToken) {
                firstToken = loc;
                token = i;
            }
    }
    if (token == -1)
        return -1;

    *_retval = (convToken*)mTokens[token];
    return firstToken;
}

Definition at line 242 of file nsTXTToHTMLConv.cpp.

{
    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_FALSE;
    token->token.Assign(PRUnichar('<'));
    token->modText.AssignLiteral("&lt;");
    mTokens.AppendElement(token);

    token = new convToken;
    if (!token) return NS_ERROR_OUT_OF_MEMORY;
    token->prepend = PR_FALSE;
    token->token.Assign(PRUnichar('>'));
    token->modText.AssignLiteral("&gt;");
    mTokens.AppendElement(token);

    token = new convToken;
    if (!token) return NS_ERROR_OUT_OF_MEMORY;
    token->prepend = PR_FALSE;
    token->token.Assign(PRUnichar('&'));
    token->modText.AssignLiteral("&amp;");
    mTokens.AppendElement(token);

    token = new convToken;
    if (!token) return NS_ERROR_OUT_OF_MEMORY;
    token->prepend = PR_TRUE;
    token->token.AssignLiteral("http://"); // XXX need to iterate through all protos
    mTokens.AppendElement(token);

    token = new convToken;
    if (!token) return NS_ERROR_OUT_OF_MEMORY;
    token->prepend = PR_TRUE;
    token->token.Assign(PRUnichar('@'));
    token->modText.AssignLiteral("mailto:");
    mTokens.AppendElement(token);

    return rv;
}

Here is the caller graph for this function:

void nsIStreamListener::onDataAvailable ( in nsIRequest  aRequest,
in nsISupports  aContext,
in nsIInputStream  aInputStream,
in unsigned long  aOffset,
in unsigned long  aCount 
) [inherited]

Called when the next chunk of data (corresponding to the request) may be read without blocking the calling thread.

The onDataAvailable impl must read exactly |aCount| bytes of data before returning.

Parameters:
aRequestrequest corresponding to the source of the data
aContextuser defined context
aInputStreaminput stream containing the data chunk
aOffsetNumber of bytes that were sent in previous onDataAvailable calls for this request. In other words, the sum of all previous count parameters. If that number is greater than or equal to 2^32, this parameter will be PR_UINT32_MAX (2^32 - 1).
aCountnumber of bytes available in the stream

NOTE: The aInputStream parameter must implement readSegments.

An exception thrown from onDataAvailable has the side-effect of causing the request to be canceled.

void nsIRequestObserver::onStartRequest ( in nsIRequest  aRequest,
in nsISupports  aContext 
) [inherited]

Called to signify the beginning of an asynchronous request.

Parameters:
aRequestrequest being observed
aContextuser defined context

An exception thrown from onStartRequest has the side-effect of causing the request to be canceled.

Here is the caller graph for this function:

void nsIRequestObserver::onStopRequest ( in nsIRequest  aRequest,
in nsISupports  aContext,
in nsresult  aStatusCode 
) [inherited]

Called to signify the end of an asynchronous request.

This call is always preceded by a call to onStartRequest.

Parameters:
aRequestrequest being observed
aContextuser defined context
aStatusCodereason for stopping (NS_OK if completed successfully)

An exception thrown from onStopRequest is generally ignored.

Here is the caller graph for this function:

Parameters:
value,:true to use an HTML header and footer on the document, false to omit it.
Returns:
The document will use a header and footer if value is true.
void nsITXTToHTMLConv::setTitle ( in wstring  text) [inherited]
Parameters:
text,:Title to set for the HTML document. Only applicable if preFormatHTML(true) is called.
Returns:
The given title will be used to form an HTML document from the plain text document.

Member Data Documentation

Definition at line 133 of file nsTXTToHTMLConv.h.

Definition at line 132 of file nsTXTToHTMLConv.h.

Definition at line 136 of file nsTXTToHTMLConv.h.

Definition at line 137 of file nsTXTToHTMLConv.h.

Definition at line 135 of file nsTXTToHTMLConv.h.

Definition at line 134 of file nsTXTToHTMLConv.h.


The documentation for this class was generated from the following files: