Back to index

lightning-sunbird  0.9+nobinonly
nsSchemaValidator.h
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 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 #ifndef __nsSchemaValidator_h__
00040 #define __nsSchemaValidator_h__
00041 
00042 #include "nsSchemaValidatorUtils.h"
00043 #include "nsISchemaValidator.h"
00044 #include "nsISchema.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsCOMArray.h"
00047 #include "nsIAtom.h"
00048 
00049 /* eced2af3-fde9-4575-b5a4-e1c830b24611 */
00050 #define NS_SCHEMAVALIDATOR_CID \
00051 { 0xeced2af3, 0xfde9, 0x4575, \
00052   {0xb5, 0xa4, 0xe1, 0xc8, 0x30, 0xb2, 0x46, 0x11}}
00053 
00054 #define NS_SCHEMAVALIDATOR_CONTRACTID "@mozilla.org/schemavalidator;1"
00055 
00056 #define NS_ERROR_SCHEMAVALIDATOR_NO_SCHEMA_LOADED      NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_SCHEMA, 1)
00057 #define NS_ERROR_SCHEMAVALIDATOR_NO_DOM_NODE_SPECIFIED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_SCHEMA, 2)
00058 #define NS_ERROR_SCHEMAVALIDATOR_NO_TYPE_FOUND         NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_SCHEMA, 3)
00059 #define NS_ERROR_SCHEMAVALIDATOR_TYPE_NOT_FOUND        NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_SCHEMA, 4)
00060 
00061 class nsSchemaValidator : public nsISchemaValidator
00062 {
00063 public:
00064   NS_DECL_ISUPPORTS
00065   NS_DECL_NSISCHEMAVALIDATOR
00066 
00067   nsSchemaValidator();
00068 
00069 private:
00070   ~nsSchemaValidator();
00071 
00072   // methods dealing with simpletypes
00073   nsresult ValidateSimpletype(const nsAString & aNodeValue,
00074                               nsISchemaSimpleType *aSchemaSimpleType,
00075                               PRBool *aResult);
00076   nsresult ValidateDerivedSimpletype(const nsAString & aNodeValue,
00077                                      nsSchemaDerivedSimpleType *aDerived,
00078                                      PRBool *aResult);
00079 
00080   nsresult ValidateRestrictionSimpletype(const nsAString & aNodeValue,
00081                                          nsISchemaSimpleType *aSchemaSimpleType,
00082                                          PRBool *aResult);
00083   nsresult ValidateDerivedBuiltinType(const nsAString & aNodeValue,
00084                                       nsSchemaDerivedSimpleType *aDerived,
00085                                       PRBool *aResult);
00086 
00087   nsresult ValidateBuiltinType(const nsAString & aNodeValue,
00088                                nsISchemaSimpleType *aSchemaSimpleType,
00089                                PRBool *aResult);
00090 
00091   nsresult ValidateListSimpletype(const nsAString & aNodeValue,
00092                                   nsISchemaSimpleType *aSchemaSimpleType,
00093                                   nsSchemaDerivedSimpleType *aDerived,
00094                                   PRBool *aResult);
00095 
00096   nsresult ValidateUnionSimpletype(const nsAString & aNodeValue,
00097                                    nsISchemaSimpleType *aSchemaSimpleType,
00098                                    PRBool *aResult);
00099   nsresult ValidateDerivedUnionSimpletype(const nsAString & aNodeValue,
00100                                           nsSchemaDerivedSimpleType *aDerived,
00101                                           PRBool *aResult);
00102 
00103   // methods dealing with validation of built-in types
00104   nsresult ValidateBuiltinTypeString(const nsAString & aNodeValue,
00105                                      PRUint32 aLength, PRBool aLengthDefined,
00106                                      PRUint32 aMinLength, PRBool aMinLengthDefined,
00107                                      PRUint32 aMaxLength, PRBool aMaxLengthDefined,
00108                                      nsStringArray *aEnumerationList,
00109                                      PRBool *aResult);
00110 
00111   nsresult ValidateBuiltinTypeBoolean(const nsAString & aNodeValue,
00112                                       PRBool *aResult);
00113 
00114   nsresult ValidateBuiltinTypeGDay(const nsAString & aNodeValue,
00115                                    const nsAString & aMaxExclusive,
00116                                    const nsAString & aMinExclusive,
00117                                    const nsAString & aMaxInclusive,
00118                                    const nsAString & aMinInclusive,
00119                                    PRBool *aResult);
00120   PRBool IsValidSchemaGDay(const nsAString & aNodeValue, nsSchemaGDay *aResult);
00121 
00122   PRBool IsValidSchemaGType(const nsAString & aNodeValue, long aMinValue,
00123                             long aMaxValue, int *aResult);
00124 
00125   nsresult ValidateBuiltinTypeGMonth(const nsAString & aNodeValue,
00126                                      const nsAString & aMaxExclusive,
00127                                      const nsAString & aMinExclusive,
00128                                      const nsAString & aMaxInclusive,
00129                                      const nsAString & aMinInclusive,
00130                                      PRBool *aResult);
00131   PRBool IsValidSchemaGMonth(const nsAString & aNodeValue,
00132                              nsSchemaGMonth *aResult);
00133 
00134   nsresult ValidateBuiltinTypeGYear(const nsAString & aNodeValue,
00135                                     const nsAString & aMaxExclusive,
00136                                     const nsAString & aMinExclusive,
00137                                     const nsAString & aMaxInclusive,
00138                                     const nsAString & aMinInclusive,
00139                                     PRBool *aResult);
00140   PRBool IsValidSchemaGYear(const nsAString & aNodeValue,
00141                             nsSchemaGYear *aResult);
00142 
00143   nsresult ValidateBuiltinTypeGYearMonth(const nsAString & aNodeValue,
00144                                          const nsAString & aMaxExclusive,
00145                                          const nsAString & aMinExclusive,
00146                                          const nsAString & aMaxInclusive,
00147                                          const nsAString & aMinInclusive,
00148                                          PRBool *aResult);
00149   PRBool IsValidSchemaGYearMonth(const nsAString & aNodeValue,
00150                                  nsSchemaGYearMonth *aYearMonth);
00151 
00152   nsresult ValidateBuiltinTypeGMonthDay(const nsAString & aNodeValue,
00153                                         const nsAString & aMaxExclusive,
00154                                         const nsAString & aMinExclusive,
00155                                         const nsAString & aMaxInclusive,
00156                                         const nsAString & aMinInclusive,
00157                                         PRBool *aResult);
00158   PRBool IsValidSchemaGMonthDay(const nsAString & aNodeValue,
00159                                 nsSchemaGMonthDay *aYearMonth);
00160 
00161   nsresult ValidateBuiltinTypeDateTime(const nsAString & aNodeValue,
00162                                        const nsAString & aMaxExclusive,
00163                                        const nsAString & aMinExclusive,
00164                                        const nsAString & aMaxInclusive,
00165                                        const nsAString & aMinInclusive,
00166                                        PRBool *aResult);
00167   int CompareSchemaDateTime(nsSchemaDateTime datetime1,
00168                             nsSchemaDateTime datetime2);
00169   PRBool IsValidSchemaDateTime(const nsAString & aNodeValue,
00170                                nsSchemaDateTime *aResult);
00171 
00172   nsresult ValidateBuiltinTypeDate(const nsAString & aNodeValue,
00173                                    const nsAString & aMaxExclusive,
00174                                    const nsAString & aMinExclusive,
00175                                    const nsAString & aMaxInclusive,
00176                                    const nsAString & aMinInclusive,
00177                                    PRBool *aResult);
00178   PRBool IsValidSchemaDate(const nsAString & aNodeValue, nsSchemaDate *aResult);
00179 
00180   nsresult ValidateBuiltinTypeTime(const nsAString & aNodeValue,
00181                                    const nsAString & aMaxExclusive,
00182                                    const nsAString & aMinExclusive,
00183                                    const nsAString & aMaxInclusive,
00184                                    const nsAString & aMinInclusive,
00185                                    PRBool *aResult);
00186   PRBool IsValidSchemaTime(const nsAString & aNodeValue, nsSchemaTime *aResult);
00187 
00188   nsresult ValidateBuiltinTypeDuration(const nsAString & aNodeValue,
00189                                        const nsAString & aMaxExclusive,
00190                                        const nsAString & aMinExclusive,
00191                                        const nsAString & aMaxInclusive,
00192                                        const nsAString & aMinInclusive,
00193                                        PRBool *aResult);
00194   PRBool IsValidSchemaDuration(const nsAString & aNodeValue,
00195                                nsISchemaDuration **aResult);
00196 
00197   nsresult ValidateBuiltinTypeInteger(const nsAString & aNodeValue,
00198                                       PRUint32 aTotalDigits,
00199                                       const nsAString & aMaxExclusive,
00200                                       const nsAString & aMinExclusive,
00201                                       const nsAString & aMaxInclusive,
00202                                       const nsAString & aMinInclusive,
00203                                       nsStringArray *aEnumerationList,
00204                                       PRBool *aResult);
00205 
00206   nsresult ValidateBuiltinTypeFloat(const nsAString & aNodeValue,
00207                                     PRUint32 aTotalDigits,
00208                                     const nsAString & aMaxExclusive,
00209                                     const nsAString & aMinExclusive,
00210                                     const nsAString & aMaxInclusive,
00211                                     const nsAString & aMinInclusive,
00212                                     nsStringArray *aEnumerationList,
00213                                     PRBool *aResult);
00214   PRBool IsValidSchemaFloat(const nsAString & aNodeValue, float *aResult);
00215 
00216   nsresult ValidateBuiltinTypeDouble(const nsAString & aNodeValue,
00217                                     PRUint32 aTotalDigits,
00218                                     const nsAString & aMaxExclusive,
00219                                     const nsAString & aMinExclusive,
00220                                     const nsAString & aMaxInclusive,
00221                                     const nsAString & aMinInclusive,
00222                                     nsStringArray *aEnumerationList,
00223                                     PRBool *aResult);
00224   PRBool IsValidSchemaDouble(const nsAString & aNodeValue, double *aResult);
00225 
00226   nsresult ValidateBuiltinTypeByte(const nsAString & aNodeValue,
00227                                    PRUint32 aTotalDigits,
00228                                    const nsAString & aMaxExclusive,
00229                                    const nsAString & aMaxInclusive,
00230                                    const nsAString & aMinInclusive,
00231                                    const nsAString & aMinExclusive,
00232                                    nsStringArray *aEnumerationList,
00233                                    PRBool *aResult);
00234   PRBool IsValidSchemaByte(const nsAString & aNodeValue, long *aResult);
00235 
00236   nsresult ValidateBuiltinTypeDecimal(const nsAString & aNodeValue,
00237                                       PRUint32 aTotalDigits,
00238                                       PRUint32 aTotalFractionDigits,
00239                                       PRBool aFractionDigitsSet,
00240                                       const nsAString & aMaxExclusive,
00241                                       const nsAString & aMinExclusive,
00242                                       const nsAString & aMaxInclusive,
00243                                       const nsAString & aMinInclusive,
00244                                       nsStringArray *aEnumerationList,
00245                                       PRBool *aResult);
00246   PRBool IsValidSchemaDecimal(const nsAString & aNodeValue, nsAString & aWholePart,
00247                               nsAString & aFractionPart);
00248   int CompareFractionStrings(const nsAString & aString1,
00249                              const nsAString & aString2);
00250 
00251   nsresult ValidateBuiltinTypeAnyURI(const nsAString & aNodeValue,
00252                                      PRUint32 aLength, PRUint32 aMinLength,
00253                                      PRUint32 aMaxLength,
00254                                      nsStringArray *aEnumerationList,
00255                                      PRBool *aResult);
00256   PRBool IsValidSchemaAnyURI(const nsAString & aString);
00257 
00258   nsresult ValidateBuiltinTypeBase64Binary(const nsAString & aNodeValue,
00259                                            PRUint32 aLength,
00260                                            PRBool aLengthDefined,
00261                                            PRUint32 aMinLength,
00262                                            PRBool aMinLengthDefined,
00263                                            PRUint32 aMaxLength,
00264                                            PRBool aMaxLengthDefined,
00265                                            nsStringArray *aEnumerationList,
00266                                            PRBool *aResult);
00267   PRBool IsValidSchemaBase64Binary(const nsAString & aString,
00268                                    char** aDecodedString);
00269 
00270 
00271   nsresult ValidateBuiltinTypeHexBinary(const nsAString & aNodeValue,
00272                                         PRUint32 aLength,
00273                                         PRBool aLengthDefined,
00274                                         PRUint32 aMinLength,
00275                                         PRBool aMinLengthDefined,
00276                                         PRUint32 aMaxLength,
00277                                         PRBool aMaxLengthDefined,
00278                                         nsStringArray *aEnumerationList,
00279                                         PRBool *aResult);
00280   PRBool IsValidSchemaHexBinary(const nsAString & aString);
00281 
00282 
00283   nsresult ValidateBuiltinTypeQName(const nsAString & aNodeValue,
00284                                     PRUint32 aLength,
00285                                     PRBool aLengthDefined,
00286                                     PRUint32 aMinLength,
00287                                     PRBool aMinLengthDefined,
00288                                     PRUint32 aMaxLength,
00289                                     PRBool aMaxLengthDefined,
00290                                     nsStringArray *aEnumerationList,
00291                                     PRBool *aResult);
00292   PRBool IsValidSchemaQName(const nsAString & aString);
00293 
00294   // helper methods
00295   void DumpBaseType(nsISchemaBuiltinType *aBuiltInType);
00296 
00297   // methods dealing with complextypes
00298   nsresult ValidateComplextype(nsIDOMNode *aNode,
00299                                nsISchemaComplexType *aSchemaComplexType,
00300                                PRBool *aResult);
00301 
00302   nsresult ValidateComplexModelElement(nsIDOMNode *aNode,
00303                                        nsISchemaComplexType *aSchemaComplexType,
00304                                        PRBool *aResult);
00305 
00306   nsresult ValidateComplexModelEmpty(nsIDOMNode    *aNode,
00307                               nsISchemaComplexType *aSchemaComplexType,
00308                               PRBool               *aResult);
00309 
00310   nsresult ValidateComplexModelSimple(nsIDOMNode *aNode,
00311                                       nsISchemaComplexType *aSchemaComplexType,
00312                                       PRBool *aResult);
00313 
00314   nsresult ValidateComplexModelGroup(nsIDOMNode* aNode,
00315                                      nsISchemaModelGroup *aSchemaModelGroup,
00316                                      nsIDOMNode **aLeftOvers, PRBool *aResult);
00317 
00318   nsresult ValidateComplexSequence(nsIDOMNode *aStartNode,
00319                                    nsISchemaModelGroup *aSchemaModelGroup,
00320                                    nsIDOMNode **aLeftOvers, PRBool *aNotFound,
00321                                    PRBool *aResult, PRUint32 *aValidatedNodes);
00322 
00323   nsresult ValidateComplexParticle(nsIDOMNode* aNode,
00324                                    nsISchemaParticle *aSchemaParticle,
00325                                    nsIDOMNode **aLeftOvers, PRBool *aNotFound,
00326                                    PRBool *aResult);
00327 
00328   nsresult ValidateComplexElement(nsIDOMNode* aNode,
00329                                   nsISchemaParticle *aSchemaParticle,
00330                                   PRBool *aResult);
00331 
00332   nsresult ValidateComplexChoice(nsIDOMNode* aStartNode,
00333                                  nsISchemaModelGroup *aSchemaModelGroup,
00334                                  nsIDOMNode **aLeftOvers, PRBool *aNotFound,
00335                                  PRBool *aResult);
00336 
00337   nsresult ValidateComplexAll(nsIDOMNode* aStartNode,
00338                               nsISchemaModelGroup *aSchemaModelGroup,
00339                               nsIDOMNode **aLeftOvers, PRBool *aNotFound,
00340                               PRBool *aResult);
00341 
00342   nsresult ValidateAttributeComponent(nsIDOMNode* aNode,
00343                                       nsISchemaAttributeComponent *aAttrComp,
00344                                       PRUint32 *aFoundAttrCount, PRBool *aResult);
00345   nsresult ValidateSchemaAttribute(nsIDOMNode* aNode, nsISchemaAttribute *aAttr,
00346                                    const nsAString & aAttrName,
00347                                    PRUint32 *aFoundAttrCount, PRBool *aResult);
00348   nsresult ValidateSchemaAttributeGroup(nsIDOMNode* aNode,
00349                                         nsISchemaAttributeGroup *aAttr,
00350                                         const nsAString & aAttrName,
00351                                         PRUint32 *aFoundAttrCount,
00352                                         PRBool *aResult);
00353   nsresult GetElementXsiType(nsIDOMNode* aNode, nsISchemaType** aType);
00354 
00355 static void
00356 ReleaseObject(void    *aObject,
00357               nsIAtom *aPropertyName,
00358               void    *aPropertyValue,
00359               void    *aData)
00360 {
00361   NS_STATIC_CAST(nsISupports *, aPropertyValue)->Release();
00362 }
00363 
00364 protected:
00365   nsCOMPtr<nsISchemaCollection> mSchema;
00366   PRBool mForceInvalid;
00367 };
00368 
00369 #endif // __nsSchemaValidator_h__