Back to index

lightning-sunbird  0.9+nobinonly
nsAbBoolExprToLDAPFilter.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Created by: Paul Sandoz   <paul.sandoz@sun.com>
00024  *   Dan Mosedale <dan.mosedale@oracle.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsIAbLDAPAttributeMap.h"
00041 #include "nsAbBoolExprToLDAPFilter.h"
00042 #include "nsXPIDLString.h"
00043 
00044 const int nsAbBoolExprToLDAPFilter::TRANSLATE_CARD_PROPERTY = 1 << 0 ;
00045 const int nsAbBoolExprToLDAPFilter::ALLOW_NON_CONVERTABLE_CARD_PROPERTY = 1 << 1 ;
00046 
00047 nsresult nsAbBoolExprToLDAPFilter::Convert (
00048     nsIAbLDAPAttributeMap* map,
00049     nsIAbBooleanExpression* expression,
00050     nsCString& filter,
00051     int flags)
00052 {
00053     nsCString f;
00054     nsresult rv = FilterExpression (map, expression, f, flags);
00055     NS_ENSURE_SUCCESS(rv, rv);
00056 
00057     filter = f;
00058     return rv;
00059 }
00060 
00061 nsresult nsAbBoolExprToLDAPFilter::FilterExpression (
00062     nsIAbLDAPAttributeMap* map,
00063     nsIAbBooleanExpression* expression,
00064     nsCString& filter,
00065     int flags)
00066 {
00067     nsCOMPtr<nsISupportsArray> childExpressions;
00068     nsresult rv = expression->GetExpressions(getter_AddRefs(childExpressions));
00069     NS_ENSURE_SUCCESS(rv, rv);
00070     
00071     PRUint32 count;
00072     rv = childExpressions->Count(&count);
00073     NS_ENSURE_SUCCESS(rv, rv);
00074 
00075     if (count == 0)
00076         return NS_OK;
00077 
00078     nsAbBooleanOperationType operation;
00079     rv = expression->GetOperation(&operation);
00080     NS_ENSURE_SUCCESS(rv, rv);
00081 
00082     /*
00083      * 3rd party query integration with Mozilla is achieved 
00084      * by calling nsAbLDAPDirectoryQuery::DoQuery(). Thus
00085      * we can arrive here with a query asking for all the
00086      * ldap attributes using the card:nsIAbCard interface.
00087      *
00088      * So we need to check that we are not creating a condition 
00089      * filter against this expression otherwise we will end up with an invalid 
00090      * filter equal to "(|)".
00091     */
00092     
00093     if (count == 1 )
00094     {
00095         nsCOMPtr<nsISupports> item;
00096         rv = childExpressions->GetElementAt (0, getter_AddRefs (item));
00097         NS_ENSURE_SUCCESS(rv, rv);
00098 
00099         nsCOMPtr<nsIAbBooleanConditionString> childCondition(do_QueryInterface(item, &rv));
00100         if (NS_SUCCEEDED(rv))
00101         {
00102             nsXPIDLCString name;
00103             rv = childCondition->GetName (getter_Copies (name));
00104             NS_ENSURE_SUCCESS(rv, rv);
00105 
00106             if(name.Equals("card:nsIAbCard"))
00107                 return NS_OK;
00108         }
00109     }
00110 
00111     filter.AppendLiteral("(");
00112     switch (operation)
00113     {
00114         case nsIAbBooleanOperationTypes::AND:
00115             filter.AppendLiteral("&");
00116             rv = FilterExpressions (map, childExpressions, filter, flags);
00117             break;
00118         case nsIAbBooleanOperationTypes::OR:
00119             filter.AppendLiteral("|");
00120             rv = FilterExpressions (map, childExpressions, filter, flags);
00121             break;
00122         case nsIAbBooleanOperationTypes::NOT:
00123             if (count > 1)
00124                 return NS_ERROR_FAILURE;
00125             filter.AppendLiteral("!");
00126             rv = FilterExpressions (map, childExpressions, filter, flags);
00127             break;
00128         default:
00129             break;
00130     }
00131     filter.AppendLiteral(")");
00132 
00133     return rv;
00134 }
00135 
00136 nsresult nsAbBoolExprToLDAPFilter::FilterExpressions (
00137     nsIAbLDAPAttributeMap *map,
00138     nsISupportsArray* expressions,
00139     nsCString& filter,
00140     int flags)
00141 {
00142     PRUint32 count;
00143     nsresult rv = expressions->Count(&count);
00144     NS_ENSURE_SUCCESS(rv, rv);
00145 
00146     for (PRUint32 i = 0; i < count; i++)
00147     {
00148         nsCOMPtr<nsISupports> item;
00149         rv = expressions->GetElementAt (i, getter_AddRefs (item));
00150         NS_ENSURE_SUCCESS(rv, rv);
00151 
00152         nsCOMPtr<nsIAbBooleanConditionString> childCondition(do_QueryInterface(item, &rv));
00153         if (NS_SUCCEEDED(rv))
00154         {
00155             rv = FilterCondition (map, childCondition, filter, flags);
00156             NS_ENSURE_SUCCESS(rv, rv);
00157             continue;
00158         }
00159 
00160         nsCOMPtr<nsIAbBooleanExpression> childExpression(do_QueryInterface(item, &rv));
00161         if (NS_SUCCEEDED(rv))
00162         {
00163             rv = FilterExpression (map, childExpression, filter, flags);
00164             NS_ENSURE_SUCCESS(rv, rv);
00165             continue;
00166         }
00167     }
00168 
00169     return rv;
00170 }
00171 
00172 nsresult nsAbBoolExprToLDAPFilter::FilterCondition (
00173     nsIAbLDAPAttributeMap* map,
00174     nsIAbBooleanConditionString* condition,
00175     nsCString& filter,
00176     int flags)
00177 {
00178     nsXPIDLCString name;
00179     nsresult rv = condition->GetName(getter_Copies (name));
00180     NS_ENSURE_SUCCESS(rv, rv);
00181 
00182     nsCAutoString ldapAttr(name);
00183     if (flags & TRANSLATE_CARD_PROPERTY)
00184     {
00185         rv = map->GetFirstAttribute (name, ldapAttr);
00186         if (!(flags & ALLOW_NON_CONVERTABLE_CARD_PROPERTY) && 
00187             !ATTRMAP_FOUND_ATTR(rv, ldapAttr))
00188             return NS_OK;
00189     }
00190 
00191     nsAbBooleanConditionType conditionType;
00192     rv = condition->GetCondition(&conditionType);
00193     NS_ENSURE_SUCCESS(rv, rv);
00194 
00195     nsXPIDLString value;
00196     rv = condition->GetValue (getter_Copies (value));
00197     NS_ENSURE_SUCCESS(rv, rv);
00198     NS_ConvertUCS2toUTF8 vUTF8 (value);
00199 
00200     switch (conditionType)
00201     {
00202         case nsIAbBooleanConditionTypes::DoesNotExist:
00203             filter += NS_LITERAL_CSTRING("(!(") +
00204                       ldapAttr +
00205                       NS_LITERAL_CSTRING("=*))");
00206             break;
00207         case nsIAbBooleanConditionTypes::Exists:
00208             filter += NS_LITERAL_CSTRING("(") +
00209                       ldapAttr +
00210                       NS_LITERAL_CSTRING("=*)");
00211             break;
00212         case nsIAbBooleanConditionTypes::Contains:
00213             filter += NS_LITERAL_CSTRING("(") +
00214                       ldapAttr +
00215                       NS_LITERAL_CSTRING("=*") +
00216                       vUTF8 +
00217                       NS_LITERAL_CSTRING("*)");
00218             break;
00219         case nsIAbBooleanConditionTypes::DoesNotContain:
00220             filter += NS_LITERAL_CSTRING("(!(") +
00221                       ldapAttr +
00222                       NS_LITERAL_CSTRING("=*") +
00223                       vUTF8 +
00224                       NS_LITERAL_CSTRING("*))");
00225             break;
00226         case nsIAbBooleanConditionTypes::Is:
00227             filter += NS_LITERAL_CSTRING("(") +
00228                       ldapAttr +
00229                       NS_LITERAL_CSTRING("=") +
00230                       vUTF8 +
00231                       NS_LITERAL_CSTRING(")");
00232             break;
00233         case nsIAbBooleanConditionTypes::IsNot:
00234             filter += NS_LITERAL_CSTRING("(!(") +
00235                       ldapAttr +
00236                       NS_LITERAL_CSTRING("=") +
00237                       vUTF8 +
00238                       NS_LITERAL_CSTRING("))");
00239             break;
00240         case nsIAbBooleanConditionTypes::BeginsWith:
00241             filter += NS_LITERAL_CSTRING("(") +
00242                       ldapAttr +
00243                       NS_LITERAL_CSTRING("=") +
00244                       vUTF8 +
00245                       NS_LITERAL_CSTRING("*)");
00246             break;
00247         case nsIAbBooleanConditionTypes::EndsWith:
00248             filter += NS_LITERAL_CSTRING("(") +
00249                       ldapAttr +
00250                       NS_LITERAL_CSTRING("=*") +
00251                       vUTF8 +
00252                       NS_LITERAL_CSTRING(")");
00253             break;
00254         case nsIAbBooleanConditionTypes::LessThan:
00255             filter += NS_LITERAL_CSTRING("(") +
00256                       ldapAttr +
00257                       NS_LITERAL_CSTRING("<=") +
00258                       vUTF8 +
00259                       NS_LITERAL_CSTRING(")");
00260             break;
00261         case nsIAbBooleanConditionTypes::GreaterThan:
00262             filter += NS_LITERAL_CSTRING("(") +
00263                       ldapAttr +
00264                       NS_LITERAL_CSTRING(">=") +
00265                       vUTF8 +
00266                       NS_LITERAL_CSTRING(")");
00267             break;
00268         case nsIAbBooleanConditionTypes::SoundsLike:
00269             filter += NS_LITERAL_CSTRING("(") +
00270                       ldapAttr +
00271                       NS_LITERAL_CSTRING("~=") +
00272                       vUTF8 +
00273                       NS_LITERAL_CSTRING(")");
00274             break;
00275         case nsIAbBooleanConditionTypes::RegExp:
00276             break;
00277         default:
00278             break;
00279     }
00280 
00281     return rv;
00282 }
00283