Back to index

lightning-sunbird  0.9+nobinonly
nsMsgSearchBoolExpression.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Howard Chu <hyc@symas.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMsgSearchCore.h"
00040 
00041 #ifndef __nsMsgSearchBoolExpression_h
00042 #define __nsMsgSearchBoolExpression_h
00043 
00044 //-----------------------------------------------------------------------------
00045 // nsMsgSearchBoolExpression is a class added to provide AND/OR terms in search queries. 
00046 //     A nsMsgSearchBoolExpression contains either a search term or two nsMsgSearchBoolExpressions and
00047 //    a boolean operator.
00048 // I (mscott) am placing it here for now....
00049 //-----------------------------------------------------------------------------
00050 
00051 /* CBoolExpression --> encapsulates one or more search terms by internally
00052    representing the search terms and their boolean operators as a binary
00053    expression tree. Each node in the tree consists of either
00054      (1) a boolean operator and two nsMsgSearchBoolExpressions or 
00055      (2) if the node is a leaf node then it contains a search term.
00056    With each search term that is part of the expression we may also keep
00057    a character string. The character 
00058    string is used to store the IMAP/NNTP encoding of the search term. This
00059    makes generating a search encoding (for online) easier.
00060 
00061    For IMAP/NNTP: nsMsgSearchBoolExpression has/assumes knowledge about how
00062    AND and OR search terms are combined according to IMAP4 and NNTP protocol.
00063    That is the only piece of IMAP/NNTP knowledge it is aware of. 
00064 
00065    Order of Evaluation: Okay, the way in which the boolean expression tree
00066    is put together directly effects the order of evaluation. We currently
00067    support left to right evaluation. 
00068    Supporting other order of evaluations involves adding new internal add
00069    term methods. 
00070  */
00071 
00072 class nsMsgSearchBoolExpression 
00073 {
00074 public:
00075 
00076        // create a leaf node expression
00077        nsMsgSearchBoolExpression(nsIMsgSearchTerm * aNewTerm,
00078                               char * aEncodingString = NULL);         
00079 
00080        // create a non-leaf node expression containing 2 expressions
00081     // and a boolean operator
00082        nsMsgSearchBoolExpression(nsMsgSearchBoolExpression *,
00083                               nsMsgSearchBoolExpression *,
00084                               nsMsgSearchBooleanOperator boolOp); 
00085        
00086        nsMsgSearchBoolExpression();
00087        ~nsMsgSearchBoolExpression();  // recursively destroys all sub
00088                                    // expressions as well
00089 
00090        // accessors
00091     
00092     // Offline
00093        static nsMsgSearchBoolExpression * AddSearchTerm (nsMsgSearchBoolExpression * aOrigExpr, nsIMsgSearchTerm * aNewTerm, char * aEncodingStr); // IMAP/NNTP
00094     static nsMsgSearchBoolExpression * AddExpressionTree(nsMsgSearchBoolExpression * aOrigExpr, nsMsgSearchBoolExpression * aExpression, PRBool aBoolOp);
00095 
00096     // parses the expression tree and all
00097     // expressions underneath this node to
00098     // determine if the end result is PR_TRUE or PR_FALSE.
00099        PRBool OfflineEvaluate(nsIMsgDBHdr *msgToMatch,
00100           const char *defaultCharset, nsIMsgSearchScopeTerm *scope,
00101           nsIMsgDatabase *db, const char *headers, PRUint32 headerSize,
00102           PRBool Filtering);
00103     
00104     // assuming the expression is for online
00105     // searches, determine the length of the
00106     // resulting IMAP/NNTP encoding string
00107        PRInt32 CalcEncodeStrSize();
00108     
00109     // fills pre-allocated
00110     // memory in buffer with 
00111     // the IMAP/NNTP encoding for the expression
00112        void GenerateEncodeStr(nsCString * buffer); 
00113 
00114        // if we are not a leaf node, then we have two other expressions
00115     // and a boolean operator
00116        nsMsgSearchBoolExpression * m_leftChild;
00117        nsMsgSearchBoolExpression * m_rightChild;
00118        nsMsgSearchBooleanOperator m_boolOp;
00119 
00120 protected:
00121        // if we are a leaf node, all we have is a search term
00122     
00123     nsIMsgSearchTerm * m_term;
00124     
00125     // store IMAP/NNTP encoding for the search term if applicable
00126        nsCString m_encodingStr;     
00127 
00128        // internal methods
00129 
00130        // the idea is to separate the public interface for adding terms to
00131     // the expression tree from the order of evaluation which influences
00132     // how we internally construct the tree. Right now, we are supporting
00133     // left to right evaluation so the tree is constructed to represent
00134     // that by calling leftToRightAddTerm. If future forms of evaluation
00135     // need to be supported, add new methods here for proper tree construction.
00136        nsMsgSearchBoolExpression * leftToRightAddTerm(nsIMsgSearchTerm * newTerm,
00137                                                    char * encodingStr); 
00138 };
00139 
00140 #endif