[extra] Import Firefox 3.0 beta 5 tarball
[mozilla-extra.git] / extensions / schema-validation / src / nsSchemaValidatorUtils.h
blob0cafeb722732c2b6b0d109216831fd77b34969f1
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is Mozilla Schema Validation.
17 * The Initial Developer of the Original Code is
18 * IBM Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * IBM Corporation. All Rights Reserved.
22 * Contributor(s):
23 * Doron Rosenberg <doronr@us.ibm.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef __nsSchemaValidatorUtils_h__
40 #define __nsSchemaValidatorUtils_h__
42 #include "nsCOMPtr.h"
43 #include "nsStringAPI.h"
44 #include "nsISVSchema.h"
45 #include "nsIDOMNode.h"
46 #include "nsCOMArray.h"
47 #include "nsIServiceManager.h"
48 #include "nsIComponentManager.h"
49 #include "nsISchemaDuration.h"
50 #include "nsISchemaValidatorRegexp.h"
52 struct nsSchemaGDay {
53 PRUint32 day; // day represented (1-31)
54 PRBool tz_negative; // is timezone negative
55 PRUint32 tz_hour; // timezone - hour (0-23) - null if not specified
56 PRUint32 tz_minute; // timezone - minute (0-59) - null if not specified
57 } ;
59 struct nsSchemaGMonth {
60 PRUint32 month; // month represented (1-12)
61 PRBool tz_negative; // is timezone negative
62 PRUint32 tz_hour; // timezone - hour (0-23) - null if not specified
63 PRUint32 tz_minute; // timezone - minute (0-59) - null if not specified
64 } ;
66 struct nsSchemaGYear {
67 long year; // year
68 PRBool tz_negative; // is timezone negative
69 PRUint32 tz_hour; // timezone - hour (0-23) - null if not specified
70 PRUint32 tz_minute; // timezone - minute (0-59) - null if not specified
71 } ;
73 struct nsSchemaGYearMonth {
74 nsSchemaGYear gYear;
75 nsSchemaGMonth gMonth;
76 } ;
78 struct nsSchemaGMonthDay {
79 nsSchemaGMonth gMonth;
80 nsSchemaGDay gDay;
81 } ;
83 struct nsMonthShortHand {
84 const char *number;
85 const char *shortHand;
86 } ;
88 const nsMonthShortHand monthShortHand[] = {
89 { "01", "Jan" },
90 { "02", "Feb" },
91 { "03", "Mar" },
92 { "04", "Apr" },
93 { "05", "May" },
94 { "06", "Jun" },
95 { "07", "Jul" },
96 { "08", "Aug" },
97 { "09", "Sep" },
98 { "10", "Oct" },
99 { "11", "Nov" },
100 { "12", "Dec" }
103 #define kREGEXP_CID "@mozilla.org/xmlextras/schemas/schemavalidatorregexp;1"
105 class nsSchemaStringFacet
107 public:
108 PRBool isDefined;
109 nsString value;
110 nsSchemaStringFacet() {
111 isDefined = PR_FALSE;
115 class nsSchemaIntFacet
117 public:
118 PRBool isDefined;
119 PRUint32 value;
120 nsSchemaIntFacet() {
121 isDefined = PR_FALSE;
122 value = 0;
126 struct nsSchemaDerivedSimpleType {
127 nsISVSchemaSimpleType* mBaseType;
129 nsSchemaIntFacet length;
130 nsSchemaIntFacet minLength;
131 nsSchemaIntFacet maxLength;
133 nsSchemaStringFacet pattern;
135 PRBool isWhitespaceDefined;
136 unsigned short whitespace;
138 nsSchemaStringFacet maxInclusive;
139 nsSchemaStringFacet minInclusive;
140 nsSchemaStringFacet maxExclusive;
141 nsSchemaStringFacet minExclusive;
143 nsSchemaIntFacet totalDigits;
144 nsSchemaIntFacet fractionDigits;
146 nsStringArray enumerationList;
149 struct nsSchemaDate {
150 PRBool isNegative;
151 PRUint32 year;
152 PRUint8 month;
153 PRUint8 day;
156 struct nsSchemaTime {
157 PRUint8 hour;
158 PRUint8 minute;
159 PRUint8 second;
160 PRUint32 millisecond;
161 PRBool tzIsNegative;
162 PRUint8 tzhour;
163 PRUint8 tzminute;
166 struct nsSchemaDateTime {
167 nsSchemaDate date;
168 nsSchemaTime time;
172 class nsSchemaValidatorUtils
174 public:
175 static PRBool IsValidSchemaInteger(const nsAString & aNodeValue, long *aResult,
176 PRBool aOverFlowCheck = PR_FALSE);
177 static PRBool IsValidSchemaInteger(const char* aString, long *aResult,
178 PRBool aOverFlowCheck);
180 static PRBool IsValidSchemaDouble(const nsAString & aNodeValue, double *aResult);
181 static PRBool IsValidSchemaDouble(const char* aString, double *aResult);
183 static PRBool ParseDateTime(const nsAString & aNodeValue,
184 nsSchemaDateTime *aResult);
185 static PRBool ParseSchemaDate(const nsAString & aStrValue,
186 PRBool aAllowTimeZone,
187 nsSchemaDate *aDate);
188 static PRBool ParseSchemaTime(const nsAString & aStrValue, nsSchemaTime *aTime);
190 static PRBool ParseSchemaTimeZone(const nsAString & aStrValue,
191 char *rv_tzhour, char *rv_tzminute);
193 static void GetMonthShorthand(PRUint8 aMonth, nsACString & aReturn);
195 static int CompareDateTime(nsSchemaDateTime aDateTime1,
196 nsSchemaDateTime aDateTime2);
197 static int CompareDate(nsSchemaDate aDate1, nsSchemaDate aDate2);
198 static int CompareTime(nsSchemaTime aTime1, nsSchemaTime aTime2);
200 static void AddTimeZoneToDateTime(nsSchemaDateTime aDateTime,
201 nsSchemaDateTime* aDestDateTime2);
203 static int CompareGYearMonth(nsSchemaGYearMonth aYearMonth1, nsSchemaGYearMonth aYearMonth2);
204 static int CompareGMonthDay(nsSchemaGMonthDay aMonthDay1, nsSchemaGMonthDay aMonthDay2);
206 static PRBool ParseSchemaDuration(const char * aStrValue,
207 nsISchemaDuration **aDuration);
208 static PRBool ParseSchemaDuration(const nsAString & aStrValue,
209 nsISchemaDuration **aDuration);
211 static int CompareStrings(const nsAString & aString1, const nsAString & aString2);
213 static int GetMaximumDayInMonthFor(PRUint32 aYearValue, PRUint8 aMonthValue);
214 static int CompareDurations(nsISchemaDuration *aDuration1,
215 nsISchemaDuration *aDuration2);
216 static void AddDurationToDatetime(nsSchemaDateTime aDatetime,
217 nsISchemaDuration *aDuration,
218 nsSchemaDateTime* aResultDateTime);
220 static PRBool IsValidSchemaNormalizedString(const nsAString & aStrValue);
221 static PRBool IsValidSchemaToken(const nsAString & aStrValue);
222 static PRBool IsValidSchemaLanguage(const nsAString & aStrValue);
223 static PRBool IsValidSchemaName(const nsAString & aStrValue);
224 static PRBool IsValidSchemaNCName(const nsAString & aStrValue);
225 static PRBool IsValidSchemaNMToken(const nsAString & aStrValue);
226 static PRBool IsValidSchemaNMTokens(const nsAString & aStrValue);
227 static PRBool IsValidSchemaID(const nsAString & aStrValue);
228 static PRBool IsValidSchemaIDRef(const nsAString & aStrValue);
229 static PRBool IsValidSchemaIDRefs(const nsAString & aStrValue);
230 static PRBool IsWhitespace(PRUnichar aChar);
232 static PRBool HandleEnumeration(const nsAString &aStrValue,
233 const nsStringArray &aEnumerationList);
235 static void RemoveLeadingZeros(nsAString & aString);
236 static void RemoveTrailingZeros(nsAString & aString);
238 static nsresult GetDerivedSimpleType(nsISVSchemaSimpleType *aSimpleType,
239 nsSchemaDerivedSimpleType *aDerived);
240 static void CopyDerivedSimpleType(nsSchemaDerivedSimpleType *aDerivedDest,
241 nsSchemaDerivedSimpleType *aDerivedSrc);
243 static void SetToNullOrElement(nsIDOMNode *aNode, nsIDOMNode **aResultNode);
244 static PRInt32 FindCharInSet(const nsAString & aString, const char *aSet,
245 PRInt32 aOffset = 0);
246 private:
247 nsSchemaValidatorUtils();
248 ~nsSchemaValidatorUtils();
250 protected:
253 #endif // __nsSchemaValidatorUtils_h__