Back to index

lightning-sunbird  0.9+nobinonly
nsSchemaValidatorUtils.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 __nsSchemaValidatorUtils_h__
00040 #define __nsSchemaValidatorUtils_h__
00041 
00042 #include "nsCOMPtr.h"
00043 #include "nsString.h"
00044 #include "nsISchema.h"
00045 #include "nsIDOMNode.h"
00046 #include "nsCOMArray.h"
00047 #include "nsIServiceManager.h"
00048 #include "nsIComponentManager.h"
00049 #include "nsISchemaDuration.h"
00050 #include "nsISchemaValidatorRegexp.h"
00051 
00052 struct nsSchemaGDay {
00053   PRUint32 day;            // day represented (1-31)
00054   PRBool tz_negative;      // is timezone negative
00055   PRUint32 tz_hour;        // timezone - hour (0-23) - null if not specified
00056   PRUint32 tz_minute;      // timezone - minute (0-59) - null if not specified
00057 } ;
00058 
00059 struct nsSchemaGMonth {
00060   PRUint32 month;          // month represented (1-12)
00061   PRBool tz_negative;      // is timezone negative
00062   PRUint32 tz_hour;        // timezone - hour (0-23) - null if not specified
00063   PRUint32 tz_minute;      // timezone - minute (0-59) - null if not specified
00064 } ;
00065 
00066 struct nsSchemaGYear {
00067   long year;               // year
00068   PRBool tz_negative;      // is timezone negative
00069   PRUint32 tz_hour;        // timezone - hour (0-23) - null if not specified
00070   PRUint32 tz_minute;      // timezone - minute (0-59) - null if not specified
00071 } ;
00072 
00073 struct nsSchemaGYearMonth {
00074   nsSchemaGYear gYear;
00075   nsSchemaGMonth gMonth;
00076 } ;
00077 
00078 struct nsSchemaGMonthDay {
00079   nsSchemaGMonth gMonth;
00080   nsSchemaGDay gDay;
00081 } ;
00082 
00083 struct nsMonthShortHand {
00084   const char *number;
00085   const char *shortHand;
00086 } ;
00087 
00088 const nsMonthShortHand monthShortHand[] = {
00089   { "01", "Jan" },
00090   { "02", "Feb" },
00091   { "03", "Mar" },
00092   { "04", "Apr" },
00093   { "05", "May" },
00094   { "06", "Jun" },
00095   { "07", "Jul" },
00096   { "08", "Aug" },
00097   { "09", "Sep" },
00098   { "10", "Oct" },
00099   { "11", "Nov" },
00100   { "12", "Dec" }
00101 } ;
00102 
00103 #define kREGEXP_CID "@mozilla.org/xmlextras/schemas/schemavalidatorregexp;1"
00104 
00105 class nsSchemaStringFacet
00106 {
00107 public:
00108   PRBool isDefined;
00109   nsString value;
00110   nsSchemaStringFacet() {
00111     isDefined = PR_FALSE;
00112   }
00113 } ;
00114 
00115 class nsSchemaIntFacet
00116 {
00117 public:
00118   PRBool isDefined;
00119   PRUint32 value;
00120   nsSchemaIntFacet() {
00121     isDefined = PR_FALSE;
00122     value = 0;
00123   }
00124 } ;
00125 
00126 struct nsSchemaDerivedSimpleType {
00127   nsISchemaSimpleType* mBaseType;
00128 
00129   nsSchemaIntFacet length;
00130   nsSchemaIntFacet minLength;
00131   nsSchemaIntFacet maxLength;
00132 
00133   nsSchemaStringFacet pattern;
00134 
00135   PRBool isWhitespaceDefined;
00136   unsigned short whitespace;
00137 
00138   nsSchemaStringFacet maxInclusive;
00139   nsSchemaStringFacet minInclusive;
00140   nsSchemaStringFacet maxExclusive;
00141   nsSchemaStringFacet minExclusive;
00142 
00143   nsSchemaIntFacet totalDigits;
00144   nsSchemaIntFacet fractionDigits;
00145 
00146   nsStringArray enumerationList;
00147 } ;
00148 
00149 struct nsSchemaDate {
00150   PRBool isNegative;
00151   PRUint32 year;
00152   PRUint8 month;
00153   PRUint8 day;
00154 } ;
00155 
00156 struct nsSchemaTime {
00157   PRUint8 hour;
00158   PRUint8 minute;
00159   PRUint8 second;
00160   PRUint32 milisecond;
00161   PRBool tzIsNegative;
00162   PRUint8 tzhour;
00163   PRUint8 tzminute;
00164 } ;
00165 
00166 struct nsSchemaDateTime {
00167   nsSchemaDate date;
00168   nsSchemaTime time;
00169 } ;
00170 
00171 
00172 class nsSchemaValidatorUtils
00173 {
00174 public:
00175   static PRBool IsValidSchemaInteger(const nsAString & aNodeValue, long *aResult,
00176                                      PRBool aOverFlowCheck = PR_FALSE);
00177   static PRBool IsValidSchemaInteger(const char* aString, long *aResult,
00178                                      PRBool aOverFlowCheck);
00179 
00180   static PRBool IsValidSchemaDouble(const nsAString & aNodeValue, double *aResult);
00181   static PRBool IsValidSchemaDouble(const char* aString, double *aResult);
00182 
00183   static PRBool ParseDateTime(const nsAString & aNodeValue,
00184                               nsSchemaDateTime *aResult);
00185   static PRBool ParseSchemaDate(const nsAString & aStrValue,
00186                                 PRBool aAllowTimeZone,
00187                                 nsSchemaDate *aDate);
00188   static PRBool ParseSchemaTime(const nsAString & aStrValue, nsSchemaTime *aTime);
00189 
00190   static PRBool ParseSchemaTimeZone(const nsAString & aStrValue, 
00191                                     char *rv_tzhour, char *rv_tzminute);
00192 
00193   static void GetMonthShorthand(PRUint8 aMonth, nsACString & aReturn);
00194 
00195   static int CompareDateTime(nsSchemaDateTime aDateTime1,
00196                              nsSchemaDateTime aDateTime2);
00197   static int CompareDate(nsSchemaDate aDate1, nsSchemaDate aDate2);
00198   static int CompareTime(nsSchemaTime aTime1, nsSchemaTime aTime2);
00199 
00200   static void AddTimeZoneToDateTime(nsSchemaDateTime aDateTime,
00201                                     nsSchemaDateTime* aDestDateTime2);
00202 
00203   static int CompareGYearMonth(nsSchemaGYearMonth aYearMonth1, nsSchemaGYearMonth aYearMonth2);
00204   static int CompareGMonthDay(nsSchemaGMonthDay aMonthDay1, nsSchemaGMonthDay aMonthDay2);
00205 
00206   static PRBool ParseSchemaDuration(const char * aStrValue,
00207                                     nsISchemaDuration **aDuration);
00208   static PRBool ParseSchemaDuration(const nsAString & aStrValue,
00209                                     nsISchemaDuration **aDuration);
00210 
00211   static int CompareStrings(const nsAString & aString1, const nsAString & aString2);
00212 
00213   static int GetMaximumDayInMonthFor(PRUint32 aYearValue, PRUint8 aMonthValue);
00214   static int CompareDurations(nsISchemaDuration *aDuration1,
00215                               nsISchemaDuration *aDuration2);
00216   static void AddDurationToDatetime(nsSchemaDateTime aDatetime,
00217                                     nsISchemaDuration *aDuration,
00218                                     nsSchemaDateTime* aResultDateTime);
00219 
00220   static PRBool IsValidSchemaNormalizedString(const nsAString & aStrValue);
00221   static PRBool IsValidSchemaToken(const nsAString & aStrValue);
00222   static PRBool IsValidSchemaLanguage(const nsAString & aStrValue);
00223   static PRBool IsValidSchemaName(const nsAString & aStrValue);
00224   static PRBool IsValidSchemaNCName(const nsAString & aStrValue);
00225   static PRBool IsValidSchemaNMToken(const nsAString & aStrValue);
00226   static PRBool IsValidSchemaNMTokens(const nsAString & aStrValue);
00227   static PRBool IsValidSchemaID(const nsAString & aStrValue);
00228   static PRBool IsValidSchemaIDRef(const nsAString & aStrValue);
00229   static PRBool IsValidSchemaIDRefs(const nsAString & aStrValue);
00230   static PRBool IsWhitespace(PRUnichar aChar);
00231 
00232   static PRBool HandleEnumeration(const nsAString &aStrValue,
00233                                   const nsStringArray &aEnumerationList);
00234 
00235   static void RemoveLeadingZeros(nsAString & aString);
00236   static void RemoveTrailingZeros(nsAString & aString);
00237 
00238   static nsresult GetDerivedSimpleType(nsISchemaSimpleType *aSimpleType,
00239                                        nsSchemaDerivedSimpleType *aDerived);
00240   static void CopyDerivedSimpleType(nsSchemaDerivedSimpleType *aDerivedDest,
00241                                     nsSchemaDerivedSimpleType *aDerivedSrc);
00242 
00243   static void SetToNullOrElement(nsIDOMNode *aNode, nsIDOMNode **aResultNode);
00244 
00250   static PRBool IsGMT(const nsAString & aDateTime);
00251 
00252 private:
00253   nsSchemaValidatorUtils();
00254   ~nsSchemaValidatorUtils();
00255 
00256 protected:
00257 };
00258 
00259 #endif // __nsSchemaValidatorUtils_h__