Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Member Functions | Private Attributes
nsSpellCheckController Class Reference

implementation of a text services object. More...

#include <nsSpellCheckController.h>

Collaboration diagram for nsSpellCheckController:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsSpellCheckController ()
 The default constructor.
virtual ~nsSpellCheckController ()
 The default destructor.

Private Member Functions

nsresult LoadTextBlockIntoBuffer ()
nsresult ReplaceAll (const nsString *aOldWord, const nsString *aNewWord)
nsresult ReplaceAllOccurrences (const CharBuffer *aOldWord, const nsString *aNewWord)
nsresult FindNextMisspelledWord (const PRUnichar *aText, const PRUint32 &aTextLen, PRUint32 &aWLen, PRUint32 &aBeginWord, PRUint32 &aEndWord, PRUint32 &aOffset, PRUnichar *&aWord, PRBool aReturnWord, PRBool &aIsMisspelled)
nsresult SetDocument (nsITextServicesDocument *aDoc, nsIDOMRange *aInitialRange)
nsresult FindBeginningOfWord (PRUint32 &aPos)

Private Attributes

nsCOMPtr< nsITextServicesDocumentmDocument
CharBuffer mBlockBuffer
CharBuffer mWordBuffer
nsCOMPtr< nsIWordBreakermWordBreaker
nsCOMPtr< nsISpellCheckermSpellChecker
nsString mText
PRUint32 mOffset
PRUint32 mEndPoint
nsCOMPtr< nsIDOMNodemStartNode
nsCOMPtr< nsIDOMNodemEndNode

Detailed Description

implementation of a text services object.

Definition at line 53 of file nsSpellCheckController.h.


Constructor & Destructor Documentation

The default constructor.

Definition at line 47 of file nsSpellCheckController.cpp.

                                               :
  mOffset(0),
  mEndPoint(0)
{
}

The default destructor.

Definition at line 53 of file nsSpellCheckController.cpp.

{
}

Member Function Documentation

Definition at line 175 of file nsSpellCheckController.cpp.

{
  const PRUnichar* text      = mText.get();
  PRUint32         textLen   = mText.Length();
  PRUint32         wlen      = 0;
  PRUint32         beginWord = 0;
  PRUint32         endWord   = 0;

  PRUint32 prvWordEnd;
  PRUint32 offset = 0;
  while (offset < textLen) {
    prvWordEnd = endWord;
    nsresult rv = mWordBreaker->FindWord(text, textLen, offset, &beginWord, &endWord);
    if (NS_FAILED(rv)) break;

    // The wordBreaker hands back the spaces inbetween the words 
    // so we need to skip any words that are all spaces
    const PRUnichar* start  = (text+offset);
    const PRUnichar* endPtr = (text+endWord);
    while (*start == NBSP_SPACE_CODE && start < endPtr) 
      start++;

    if (start == endPtr) {
      offset = endWord;
      continue;
    }

    offset = endWord+1;

    wlen = endWord - beginWord;
    if (endWord < aPos) {
      continue;
    }

    if (aPos >= beginWord) {
      aPos = beginWord;
      break;
    }
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsSpellCheckController::FindNextMisspelledWord ( const PRUnichar aText,
const PRUint32 aTextLen,
PRUint32 aWLen,
PRUint32 aBeginWord,
PRUint32 aEndWord,
PRUint32 aOffset,
PRUnichar *&  aWord,
PRBool  aReturnWord,
PRBool aIsMisspelled 
) [private]

Definition at line 579 of file nsSpellCheckController.cpp.

{
  aWord = nsnull;
  aIsMisspelled = PR_FALSE;

#ifdef DEBUG_rods
  DUMPWORDS(mWordBreaker, aText, aTextLen);
#endif

  while (aOffset < aTextLen) {
    nsresult result = mWordBreaker->FindWord(aText, aTextLen, aOffset, &aBeginWord, &aEndWord);
    NS_ENSURE_SUCCESS(result, result);
    aWLen = aEndWord - aBeginWord;

    // The wordBreaker hands back the spaces inbetween the words 
    // so we need to skip any words that are all spaces
    const PRUnichar* start  = (aText+aOffset);
    const PRUnichar* endPtr = (aText+aEndWord);
    while (*start == NBSP_SPACE_CODE && start < endPtr) 
      start++;

    if (start == endPtr) {
      aOffset = aEndWord;
      continue;
    }

    PRUnichar* word = nsCRT::strndup(start, aWLen);
    aOffset = aEndWord;

    result = mSpellChecker->Check(word, &aIsMisspelled);
#if DEBUG_rods
    printf("Word [%s] MSP: %s\n", NS_LossyConvertUCS2toASCII(word).get(), aIsMisspelled ? "YES":"NO");
#endif
    if (NS_FAILED(result)) {
      nsMemory::Free(word);
      return result;
    }

    if (aIsMisspelled) {
      if (aReturnWord) {
        aWord = word;

      } else {
        nsMemory::Free(word);
      }
      return NS_OK;
    }
    nsMemory::Free(word);
  }

  return NS_OK;
}

Here is the caller graph for this function:

Here is the caller graph for this function:

nsresult nsSpellCheckController::ReplaceAll ( const nsString aOldWord,
const nsString aNewWord 
) [private]

Definition at line 358 of file nsSpellCheckController.cpp.

{
  NS_ENSURE_ARG_POINTER(aOldWord);
  NS_ENSURE_ARG_POINTER(aNewWord);
  NS_ENSURE_TRUE(mSpellChecker, NS_ERROR_NULL_POINTER);

  nsresult result;
  CharBuffer oldWord;

  nsCOMPtr<nsIUnicodeEncoder> unicodeEncoder;

  nsXPIDLString charSet;
  result = mSpellChecker->GetCharset(getter_Copies(charSet));
  if (NS_SUCCEEDED(result)) {
    nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
    result = nsSpellCheckUtils::CreateUnicodeConverters(charSet, 
                                                        getter_AddRefs(unicodeEncoder), 
                                                        getter_AddRefs(unicodeDecoder));
    NS_ENSURE_TRUE(unicodeEncoder, NS_ERROR_NULL_POINTER);
  }

  result = nsSpellCheckUtils::ReadStringIntoBuffer(unicodeEncoder, aOldWord, &oldWord);
  NS_ENSURE_SUCCESS(result, result);

  // Skip the current occurrence of old word. It will be the last
  // occurrence we replace so that we can figure out where we were
  // before replace all started!

  result = ReplaceAllOccurrences(&oldWord, aNewWord);
  NS_ENSURE_SUCCESS(result, result);

  PRInt32 i, numBlocksBefore = 0;
  PRBool isDone = PR_FALSE;

  // Count the number of text blocks that came before the current
  // one, so that we know when to stop replacing after we hit the
  // end of the document and swing around to the beginning of the
  // document.

  while (!isDone) {
    ++numBlocksBefore;

    result = mDocument->PrevBlock();
    NS_ENSURE_SUCCESS(result, result);

    result = mDocument->IsDone(&isDone);
    NS_ENSURE_SUCCESS(result, result);
  }

  // Now reset the document so that we start replacing all occurrences
  // between the current block and the end of the document!

  result = mDocument->FirstBlock();
  NS_ENSURE_SUCCESS(result, result);

  i = numBlocksBefore;
  while (i-- > 0) {
    result = mDocument->NextBlock();
    NS_ENSURE_SUCCESS(result, result);
  }

  // Now replace all occurrences till we hit the end of the document!

  result = mDocument->IsDone(&isDone);
  NS_ENSURE_SUCCESS(result, result);

  while (!isDone) {
    result = nsSpellCheckUtils::LoadTextBlockIntoBuffer(mDocument, mSpellChecker, mBlockBuffer, mText, mOffset);
    NS_ENSURE_SUCCESS(result, result);

    result = ReplaceAllOccurrences(&oldWord, aNewWord);
    NS_ENSURE_SUCCESS(result, result);

    result = mDocument->NextBlock();
    NS_ENSURE_SUCCESS(result, result);

    result = mDocument->IsDone(&isDone);
    NS_ENSURE_SUCCESS(result, result);
  }

  // Now swing around to the beginning of the document, and
  // replace all occurrences till we hit the old current block!

  result = mDocument->FirstBlock();
  NS_ENSURE_SUCCESS(result, result);

  isDone = PR_FALSE;

  while (numBlocksBefore-- > 0 && !isDone) {
    result = nsSpellCheckUtils::LoadTextBlockIntoBuffer(mDocument, mSpellChecker, mBlockBuffer, mText, mOffset);
    NS_ENSURE_SUCCESS(result, result);

    result = ReplaceAllOccurrences(&oldWord, aNewWord);
    NS_ENSURE_SUCCESS(result, result);

    if (numBlocksBefore > 0) {
      result = mDocument->NextBlock();
      NS_ENSURE_SUCCESS(result, result);

      // Track isDone because I'm paranoid!

      result = mDocument->IsDone(&isDone);
      NS_ENSURE_SUCCESS(result, result);
    }
  }

  // Now reload the text into the buffer so that the
  // spelling checker is reset to look for the next
  // misspelled word.

  result = nsSpellCheckUtils::LoadTextBlockIntoBuffer(mDocument, mSpellChecker, mBlockBuffer, mText, mOffset);
  NS_ENSURE_SUCCESS(result, result);

  return NS_OK;
}

Here is the call graph for this function:

Definition at line 475 of file nsSpellCheckController.cpp.

{
  NS_ENSURE_ARG_POINTER(aOldWord);
  NS_ENSURE_ARG_POINTER(aNewWord);
  NS_ENSURE_TRUE(mSpellChecker && mWordBreaker, NS_ERROR_NULL_POINTER);

  // Now get the next misspelled word in the document.
  const PRUnichar* text         = mText.get();
  PRUint32         textLen      = mText.Length();
  PRUint32         wlen         = 0;
  PRUint32         beginWord    = 0;
  PRUint32         endWord      = 0;
  PRUint32         offset       = 0;

  PRBool isMisspelled = PR_TRUE;
  while (isMisspelled) {
    PRUnichar* word;
    nsresult result  = FindNextMisspelledWord(text, textLen, offset, wlen, 
                                              beginWord, endWord, word, PR_FALSE, isMisspelled);
    NS_ENSURE_SUCCESS(result, result);

    if (isMisspelled) {

      mWordBuffer.AssureCapacity(wlen + 1);

      PRUint32 i;
      for (i = 0; i < wlen; i++)
        mWordBuffer.mData[i] = mBlockBuffer.mData[i + beginWord];

      mWordBuffer.mData[i]    = '\0';
      mWordBuffer.mDataLength = wlen;

      // XXX: Need to do a case insensitive comparison, and replace
      //      with a word that matches the misspelled word's caps.

      if (aOldWord->mDataLength == mWordBuffer.mDataLength &&
          !memcmp(aOldWord->mData, mWordBuffer.mData, aOldWord->mDataLength)) {
        // We found an occurrence of old word, so replace it!

        result = mDocument->SetSelection(beginWord, wlen);
        NS_ENSURE_SUCCESS(result, result);

        result = mDocument->ScrollSelectionIntoView();
        NS_ENSURE_SUCCESS(result, result);

        if (aNewWord->Length() > 0)
          result = mDocument->InsertText(aNewWord);
        else
          result = mDocument->DeleteSelection();

        NS_ENSURE_SUCCESS(result, result);
        offset = endWord;
      }
    }
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 120 of file nsSpellCheckController.cpp.

{
  NS_ENSURE_ARG_POINTER(aDoc);

  // XXX: Modify this method so that it can be called
  //      more than once with a different aDoc. This will
  //      allow us to reuse a spellchecker.

  mDocument = do_QueryInterface(aDoc);
  NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);

  nsresult rv;
  if (!aInitialRange) {
    rv = mDocument->FirstBlock();

  } else  {
    rv = aInitialRange->GetStartContainer(getter_AddRefs(mStartNode));
    NS_ENSURE_SUCCESS(rv, rv);
    PRInt32 startOffset;
    rv = aInitialRange->GetStartOffset(&startOffset);
    NS_ENSURE_SUCCESS(rv, rv);
    mOffset = PRUint32(startOffset);

    rv = aInitialRange->GetEndContainer(getter_AddRefs(mEndNode));
    NS_ENSURE_SUCCESS(rv, rv);
    PRInt32 endOffset;
    rv = aInitialRange->GetEndOffset(&endOffset);
    NS_ENSURE_SUCCESS(rv, rv);
    mEndPoint = PRUint32(endOffset);
    
    nsITextServicesDocument::TSDBlockSelectionStatus selStatus;
    PRInt32 selOffset, selLength;

    rv = mDocument->FirstSelectedBlock(&selStatus, &selOffset, &selLength);
  }
  NS_ENSURE_SUCCESS(rv, rv);

  rv = nsSpellCheckUtils::LoadTextBlockIntoBuffer(mDocument, mSpellChecker, mBlockBuffer, mText, mOffset);
  NS_ENSURE_SUCCESS(rv, rv);

  if (!aInitialRange) {
    mOffset = 0;
    mEndPoint = mText.Length() - 1;
  } else {
    rv = FindBeginningOfWord(mOffset);
    NS_ENSURE_SUCCESS(rv, rv);
    if (mStartNode == mEndNode) {
      rv = FindBeginningOfWord(mEndPoint);
    }
  }

  return rv;
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 89 of file nsSpellCheckController.h.

Definition at line 88 of file nsSpellCheckController.h.

Definition at line 99 of file nsSpellCheckController.h.

Definition at line 96 of file nsSpellCheckController.h.

Definition at line 95 of file nsSpellCheckController.h.

Definition at line 93 of file nsSpellCheckController.h.

Definition at line 98 of file nsSpellCheckController.h.

Definition at line 94 of file nsSpellCheckController.h.

Definition at line 92 of file nsSpellCheckController.h.

Definition at line 90 of file nsSpellCheckController.h.


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