Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsSchemaValidator.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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 XForms Schema Validation.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * IBM Corporation. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Doron Rosenberg <doronr@us.ibm.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 // XPCOM includes
00040 #include "nsIServiceManager.h"
00041 #include "nsString.h"
00042 
00043 #include "nsXFormsUtils.h"
00044 #include "nsISchemaValidator.h"
00045 #include "nsISchemaDuration.h"
00046 #include "nsXFormsSchemaValidator.h"
00047 
00048 #include "nsIDOM3Node.h"
00049 
00050 #define NS_SCHEMAVALIDATOR_CONTRACTID "@mozilla.org/schemavalidator;1"
00051 
00052 nsXFormsSchemaValidator::nsXFormsSchemaValidator()
00053 {
00054   mSchemaValidator = do_CreateInstance(NS_SCHEMAVALIDATOR_CONTRACTID);
00055 }
00056 
00057 nsresult
00058 nsXFormsSchemaValidator::LoadSchema(nsISchema* aSchema)
00059 {
00060   NS_ENSURE_TRUE(mSchemaValidator, NS_ERROR_UNEXPECTED);
00061 
00062   return mSchemaValidator->LoadSchema(aSchema);
00063 }
00064 
00065 PRBool
00066 nsXFormsSchemaValidator::ValidateString(const nsAString & aValue,
00067                                         const nsAString & aType,
00068                                         const nsAString & aNamespace)
00069 {
00070   PRBool isValid = PR_FALSE;
00071 
00072   NS_ENSURE_TRUE(mSchemaValidator, isValid);
00073 
00074   // if it is the XForms namespace, handle it internally, else delegate to
00075   // nsISchemaValidator
00076   if (aNamespace.EqualsLiteral(NS_NAMESPACE_XFORMS)) {
00077     isValid = ValidateXFormsTypeString(aValue, aType);
00078   } else {
00079     mSchemaValidator->ValidateString(aValue, aType, aNamespace, &isValid);
00080   }
00081 
00082   return isValid;
00083 }
00084 
00085 PRBool
00086 nsXFormsSchemaValidator::Validate(nsIDOMNode* aElement)
00087 {
00088   PRBool isValid = PR_FALSE;
00089   NS_ENSURE_TRUE(mSchemaValidator, isValid);
00090 
00091   mSchemaValidator->Validate(aElement, &isValid);
00092 
00093   return isValid;
00094 }
00095 
00096 PRBool
00097 nsXFormsSchemaValidator::GetType(const nsAString & aType,
00098                                  const nsAString & aNamespace,
00099                                  nsISchemaType **aSchemaType)
00100 {
00101   NS_ENSURE_TRUE(mSchemaValidator, PR_FALSE);
00102 
00103   nsresult rv = mSchemaValidator->GetType(aType, aNamespace, aSchemaType);
00104 
00105   return NS_SUCCEEDED(rv);
00106 }
00107 
00108 // xforms schema types
00109 PRBool
00110 nsXFormsSchemaValidator::ValidateXFormsTypeString(const nsAString & aValue,
00111                                                   const nsAString & aType)
00112 {
00113   if (aType.IsEmpty()) {
00114     return PR_FALSE;
00115   }
00116 
00117   PRBool isValid = PR_FALSE;
00118 
00119   if (aType.EqualsLiteral("yearMonthDuration")) {
00120     isValid = IsValidSchemaYearMonthDuration(aValue);
00121   } else if (aType.EqualsLiteral("dayTimeDuration")) {
00122     isValid = IsValidSchemaDayTimeDuration(aValue);
00123   } else if (aType.EqualsLiteral("listItem")) {
00124     isValid = IsValidSchemaListItem(aValue);
00125   } else if (aType.EqualsLiteral("listItems")) {
00126     isValid = IsValidSchemaListItems(aValue);
00127   } else if (aType.EqualsLiteral("card-number")) {
00128     isValid = IsValidSchemaCardNumber(aValue);
00129   }
00130 
00131   return isValid;
00132 }
00133 
00134 PRBool
00135 nsXFormsSchemaValidator::IsValidSchemaYearMonthDuration(const nsAString & aValue)
00136 {
00137   PRBool isValid = PR_FALSE;
00138 
00139   nsCOMPtr<nsISchemaDuration> duration;
00140   nsresult rv =
00141     mSchemaValidator->ValidateBuiltinTypeDuration(aValue,
00142                                                   getter_AddRefs(duration));
00143 
00144   // valid duration
00145   if (NS_SUCCEEDED(rv)) {
00146     // check if no days/hours/minutes/seconds/fractionseconds were set
00147     PRUint32 temp;
00148     double dbltemp;
00149     duration->GetDays(&temp);
00150 
00151     if (temp == 0) {
00152       duration->GetHours(&temp);
00153       if (temp == 0) {
00154         duration->GetMinutes(&temp);
00155         if (temp == 0) {
00156           duration->GetSeconds(&temp);
00157           if (temp == 0) {
00158             duration->GetFractionSeconds(&dbltemp);
00159             if (dbltemp == 0) {
00160               isValid = PR_TRUE;
00161             }
00162           }
00163         }
00164       }
00165     }
00166   }
00167 
00168   return isValid;
00169 }
00170 
00171 PRBool
00172 nsXFormsSchemaValidator::IsValidSchemaDayTimeDuration(const nsAString & aValue)
00173 {
00174   PRBool isValid = PR_FALSE;
00175 
00176   nsCOMPtr<nsISchemaDuration> duration;
00177   nsresult rv =
00178     mSchemaValidator->ValidateBuiltinTypeDuration(aValue,
00179                                                   getter_AddRefs(duration));
00180 
00181   if (NS_SUCCEEDED(rv)) {
00182     PRUint32 years, months;
00183     duration->GetYears(&years);
00184     duration->GetMonths(&months);
00185 
00186     // if years/months exist, invalid
00187     if ((years == 0) && (months == 0))
00188       isValid = PR_TRUE;
00189   }
00190 
00191   return isValid;
00192 }
00193 
00194 PRBool
00195 nsXFormsSchemaValidator::IsValidSchemaListItem(const nsAString & aValue)
00196 {
00197   PRBool isValid = PR_FALSE;
00198 
00199   // like a string, but no whitespace
00200   nsAutoString string(aValue);
00201   if (string.FindCharInSet(" \t\r\n") == kNotFound) {
00202     mSchemaValidator->ValidateString(aValue, NS_LITERAL_STRING("string"),
00203                                      NS_LITERAL_STRING("http://www.w3.org/1999/XMLSchema"),
00204                                      &isValid);
00205   }
00206 
00207   return isValid;
00208 }
00209 
00210 PRBool
00211 nsXFormsSchemaValidator::IsValidSchemaListItems(const nsAString & aValue)
00212 {
00213   PRBool isValid = PR_FALSE;
00214 
00215   // listItem is like a string, but no whitespace.  listItems is a whitespace
00216   // delimited list of listItem, so therefore just need to see if it is a valid
00217   // xsd:string
00218   mSchemaValidator->ValidateString(aValue, NS_LITERAL_STRING("string"),
00219                                    NS_LITERAL_STRING("http://www.w3.org/1999/XMLSchema"),
00220                                    &isValid);
00221 
00222   return isValid;
00223 }
00224 
00225 PRBool
00226 nsXFormsSchemaValidator::IsValidSchemaCardNumber(const nsAString & aValue)
00227 {
00228   nsAutoString value(aValue);
00229 
00230   // A valid card-number is between 12 and 19 digits in length.
00231   PRInt32 len = value.Length();
00232   if ((len < 12) || (len > 19))
00233     return PR_FALSE;
00234 
00235   for (PRInt32 i = 0; i < len; ++i) {
00236     PRUnichar c = value.CharAt(i);
00237     if ((c > '9') || (c < '0'))
00238         return PR_FALSE;
00239   }
00240 
00241   return PR_TRUE;
00242 }