3 The file is used to provides all kinds of Data Validation interface
5 Copyright (c) 2006, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 package org
.tianocore
.common
;
18 import java
.util
.regex
.Matcher
;
19 import java
.util
.regex
.Pattern
;
22 The class is used to provides all kinds of data validation interface
24 <p>All provided interfaces are in static mode</p>
26 @since ModuleEditor 1.0
29 public class DataValidation
{
37 public static void main(String
[] args
) {
38 // TODO Auto-generated method stub
43 // The below is used to check common data types
47 Check if the imput data is int
49 @param strInt The input string which needs validation
51 @retval true - The input is Int
52 @retval false - The input is not Int
55 public static boolean isInt(String strInt
) {
56 return isMatch("^-?[0-9]\\d*$", strInt
);
60 Check if the input data is int and it is in the valid scope
61 The scope is provided by String
63 @param strNumber The input string which needs validation
64 @param BeginNumber The left boundary of the scope
65 @param EndNumber The right boundary of the scope
67 @retval true - The input is Int and in the scope;
68 @retval false - The input is not Int or not in the scope
71 public static boolean isInt(String strNumber
, int BeginNumber
, int EndNumber
) {
73 //Check if the input data is int first
75 if (!isInt(strNumber
)) {
79 //And then check if the data is between the scope
81 Integer intTemp
= new Integer(strNumber
);
82 if ((intTemp
.intValue() < BeginNumber
) || (intTemp
.intValue() > EndNumber
)) {
89 Check if the input data is int and it is in the valid scope
90 The scope is provided by String
92 @param strNumber The input string which needs validation
93 @param strBeginNumber The left boundary of the scope
94 @param strEndNumber The right boundary of the scope
96 @retval true - The input is Int and in the scope;
97 @retval false - The input is not Int or not in the scope
100 public static boolean isInt(String strNumber
, String strBeginNumber
, String strEndNumber
) {
102 //Check if all input data are int
104 if (!isInt(strNumber
)) {
107 if (!isInt(strBeginNumber
)) {
110 if (!isInt(strEndNumber
)) {
114 //And then check if the data is between the scope
116 Integer intI
= new Integer(strNumber
);
117 Integer intJ
= new Integer(strBeginNumber
);
118 Integer intK
= new Integer(strEndNumber
);
119 if ((intI
.intValue() < intJ
.intValue()) || (intI
.intValue() > intK
.intValue())) {
126 Use regex to check if the input data is in valid format
128 @param strPattern The input regex
129 @param strMatcher The input data need be checked
131 @retval true - The data matches the regex
132 @retval false - The data doesn't match the regex
135 public static boolean isMatch(String strPattern
, String strMatcher
) {
136 Pattern pattern
= Pattern
.compile(strPattern
);
137 Matcher matcher
= pattern
.matcher(strMatcher
);
139 return matcher
.find();
143 // The below is used to check common customized data types
147 Check if the input data is BaseNameConvention
149 @param strBaseNameConvention The input string need be checked
151 @retval true - The input is BaseNameConvention
152 @retval false - The input is not BaseNameConvention
155 public static boolean isBaseNameConvention(String strBaseNameConvention
) {
156 return isMatch("[A-Z]([a-zA-Z0-9])*(_)?([a-zA-Z0-9])*", strBaseNameConvention
);
160 Check if the input data is GuidArrayType
162 @param strGuidArrayType The input string need be checked
164 @retval true - The input is GuidArrayType
165 @retval false - The input is not GuidArrayType
168 public static boolean isGuidArrayType(String strGuidArrayType
) {
170 "0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},( )*0x[a-fA-F0-9]{1,4}(,( )*\\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\\})?",
175 Check if the input data is GuidNamingConvention
177 @param strGuidNamingConvention The input string need be checked
179 @retval true - The input is GuidNamingConvention
180 @retval false - The input is not GuidNamingConvention
183 public static boolean isGuidNamingConvention(String strGuidNamingConvention
) {
184 return isMatch("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}",
185 strGuidNamingConvention
);
189 Check if the input data is GuidType
191 @param strGuidType The input string need be checked
193 @retval true - The input is GuidType
194 @reture false is not GuidType
197 public static boolean isGuidType(String strGuidType
) {
198 return (isGuidArrayType(strGuidType
) || isGuidNamingConvention(strGuidType
));
202 Check if the input data is Guid
204 @param strGuid The input string need be checked
206 @retval true - The input is Guid
207 @retval false - The input is not Guid
210 public static boolean isGuid(String strGuid
) {
211 return isGuidType(strGuid
);
215 Check if the input data is Sentence
217 @param strSentence The input string need be checked
219 @retval true - The input is Sentence
220 @retval false - The input is not Sentence
223 public static boolean isSentence(String strSentence
) {
224 return isMatch("(\\w+\\W*)+( )+(\\W*\\w*\\W*\\s*)*", strSentence
);
228 Check if the input data is DateType
230 @param strDateType The input string need be checked
232 @retval true - The input is DateType
233 @retval false - The input is not DateType
236 public static boolean isDateType(String strDateType
) {
237 return isMatch("[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]", strDateType
);
241 Check if the input data is DosPath
243 @param strDosPath The input string need be checked
245 @retval true - The input is DosPath
246 @retval false - The input is not DosPath
249 public static boolean isDosPath(String strDosPath
) {
250 return isMatch("([a-zA-Z]:\\\\)?(((\\\\?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*", strDosPath
);
254 Check if the input data is UnixPath
256 @param strUnixPath The input string need be checked
258 @retval true - The input is UnixPath
259 @retval false - The input is not UnixPath
262 public static boolean isUnixPath(String strUnixPath
) {
263 return isMatch("(\\/)?(((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*", strUnixPath
);
267 Check if the input data is DirectoryNamingConvention
269 @param strDirectoryNamingConvention The input string need be checked
271 @retval true - The input is DirectoryNamingConvention
272 @retval false - The input is not DirectoryNamingConvention
275 public static boolean isDirectoryNamingConvention(String strDirectoryNamingConvention
) {
276 return (isDosPath(strDirectoryNamingConvention
) || isUnixPath(strDirectoryNamingConvention
));
280 Check if the input data is HexDoubleWordDataType
282 @param strHexDoubleWordDataType The input string need be checked
284 @retval true - The input is HexDoubleWordDataType
285 @retval false - The input is not HexDoubleWordDataType
288 public static boolean isHexDoubleWordDataType(String strHexDoubleWordDataType
) {
289 return isMatch("0x[a-fA-F0-9]{1,8}", strHexDoubleWordDataType
);
293 Check if the input data is V1
295 @param strV1 The input string need be checked
297 @retval true - The input is V1
298 @retval false - The input is not V1
301 public static boolean isV1(String strV1
) {
302 return isMatch("((%[A-Z](_*[A-Z0-9]*)*%)+((((\\\\)?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*)", strV1
);
306 Check if the input data is V2
308 @param strV2 The input string need be checked
310 @retval true - The input is V2
311 @retval false - The input is not V2
314 public static boolean isV2(String strV2
) {
316 "(($[A-Z](_*[A-Z0-9]*)*)+||($\\([A-Z](_*[A-Z0-9]*)*\\))+||($\\{[A-Z](_*[A-Z0-9]*)*\\})+)+(\\/)?(((((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*)*)",
321 Check if the input data is VariableConvention
323 @param strVariableConvention The input string need be checked
325 @retval true - The input is VariableConvention
326 @retval false - The input is not VariableConvention
329 public static boolean isVariableConvention(String strVariableConvention
) {
330 return (isV1(strVariableConvention
) || isV2(strVariableConvention
));
334 Check if the input data is UCName
336 @param strUCName The input string need be checked
338 @retval true - The input is UCName
339 @retval false - The input is not UCName
342 public static boolean isUCName(String strUCName
) {
343 return isMatch("[A-Z]+(_*[A-Z0-9]*( )*)*", strUCName
);
347 Check if the input data is HexByteDataType
349 @param strHex64BitDataType The input string need be checked
351 @retval true - The input is HexByteDataType
352 @retval false - The input is not HexByteDataType
355 public static boolean isHexByteDataType(String strHex64BitDataType
) {
356 return isMatch("(0x)?[a-fA-F0-9]{1,2}", strHex64BitDataType
);
360 Check if the input data is Hex64BitDataType
362 @param strHex64BitDataType The input string need be checked
364 @retval true - The input is Hex64BitDataType
365 @retval false - The input is not Hex64BitDataType
368 public static boolean isHex64BitDataType(String strHex64BitDataType
) {
369 return isMatch("(0x)?[a-fA-F0-9]{1,16}", strHex64BitDataType
);
373 Check if the input data is HexWordDataType
375 @param strHexWordDataType The input string need be checked
377 @retval true - The input is HexWordDataType
378 @retval false - The input is not HexWordDataType
381 public static boolean isHexWordDataType(String strHexWordDataType
) {
382 return isMatch("0x[a-fA-F0-9]{1,4}", strHexWordDataType
);
386 Check if the input data is CName
388 @param strCName The input string need be checked
390 @retval true - The input is CName
391 @retval false - The input is not CName
394 public static boolean isCName(String strCName
) {
395 return isMatch("((_)*([a-zA-Z])+((_)*[a-zA-Z0-9]*))*", strCName
);
399 Check if the input data is OverrideID
401 @param strOverrideID The input string need be checked
403 @retval true - The input is OverrideID
404 @retval false - The input is not OverrideID
407 public static boolean isOverrideID(String strOverrideID
) {
408 return isInt(strOverrideID
);
412 //The below is used to check msaheader data type
416 Check if the input data is BaseName
418 @param strBaseName The input string need be checked
420 @retval true - The input is BaseName
421 @retval false - The input is not BaseName
424 public static boolean isBaseName(String strBaseName
) {
425 return isBaseNameConvention(strBaseName
);
429 Check if the input data is Abstract
431 @param strAbstract The input string need be checked
433 @retval true - The input is Abstract
434 @retval false - The input is not Abstract
437 public static boolean isAbstract(String strAbstract
) {
438 return isSentence(strAbstract
);
442 Check if the input data is Copyright
444 @param strCopyright The input string need be checked
446 @retval true - The input is Copyright
447 @retval false - The input is not Copyright
450 public static boolean isCopyright(String strCopyright
) {
451 return isSentence(strCopyright
);
455 Check if the input data is Created
457 @param strCreated The input string need be checked
459 @retval true - The input is Created
460 @retval false - The input is not Created
463 public static boolean isCreated(String strCreated
) {
464 return isDateType(strCreated
);
468 Check if the input data is Updated
470 @param strUpdated The input string need be checked
472 @retval true - The input is Updated
473 @retval false - The input is not Updated
476 public static boolean isUpdated(String strUpdated
) {
477 return isDateType(strUpdated
);
481 // The below is used to check LibraryClass data types
485 Check if the input data is LibraryClass
487 @param strLibraryClass The input string need be checked
489 @retval true - The input is LibraryClass
490 @retval false - The input is not LibraryClass
493 public static boolean isLibraryClass(String strLibraryClass
) {
494 return isBaseNameConvention(strLibraryClass
);
498 // The below is used to check sourcefiles data types
502 Check if the input data is Path
504 @param strPath The input string need be checked
506 @retval true - The input is Path
507 @retval false - The input is not Path
510 public static boolean isPath(String strPath
) {
511 return isDirectoryNamingConvention(strPath
);
515 Check if the input data is FileName
517 @param strFileName The input string need be checked
519 @retval true - The input is FileName
520 @retval false - The input is not FileName
523 public static boolean isFileName(String strFileName
) {
524 return isVariableConvention(strFileName
);
528 // The below is used to check includes data types
532 Check if the input data is UpdatedDate
534 @param strUpdatedDate The input string need be checked
536 @retval true - The input is UpdatedDate
537 @retval false - The input is not UpdatedDate
540 public static boolean isUpdatedDate(String strUpdatedDate
) {
541 return isDateType(strUpdatedDate
);
545 Check if the input data is PackageName
547 @param strPackageName The input string need be checked
549 @retval true - The input is PackageName
550 @retval false - The input is not PackageName
553 public static boolean isPackageName(String strPackageName
) {
554 return isBaseNameConvention(strPackageName
);
558 // The below is used to check protocols data types
562 Check if the input data is ProtocolName
564 @param strProtocolName The input string need be checked
566 @retval true - The input is ProtocolName
567 @retval false - The input is not ProtocolName
570 public static boolean isProtocolName(String strProtocolName
) {
571 return isCName(strProtocolName
);
575 Check if the input data is ProtocolNotifyName
577 @param strProtocolNotifyName The input string need be checked
579 @retval true - The input is ProtocolNotifyName
580 @retval false - The input is not ProtocolNotifyName
583 public static boolean isProtocolNotifyName(String strProtocolNotifyName
) {
584 return isCName(strProtocolNotifyName
);
588 // The below is used to check ppis data types
592 Check if the input data is PpiName
594 @param strPpiName The input string need be checked
596 @retval true - The input is PpiName
597 @retval false - The input is not PpiName
600 public static boolean isPpiName(String strPpiName
) {
601 return isCName(strPpiName
);
605 Check if the input data is PpiNotifyName
607 @param strPpiNotifyName The input string need be checked
609 @retval true - The input is PpiNotifyName
610 @retval false - The input is not PpiNotifyName
613 public static boolean isPpiNotifyName(String strPpiNotifyName
) {
614 return isCName(strPpiNotifyName
);
618 Check if the input data is FeatureFlag
620 @param strFeatureFlag The input string need be checked
622 @retval true - The input is FeatureFlag
623 @retval false - The input is not FeatureFlag
626 public static boolean isFeatureFlag(String strFeatureFlag
) {
627 return isCName(strFeatureFlag
);
631 // The below is used to check variable data types
635 Check if the input data is ByteOffset
637 @param strByteOffset The input string need be checked
639 @retval true - The input is ByteOffset
640 @retval false - The input is not ByteOffset
643 public static boolean isByteOffset(String strByteOffset
) {
644 return isByteOffset(strByteOffset
);
648 Check if the input data is BitOffset
650 @param strBitOffset The input string need be checked
652 @retval true - The input is BitOffset
653 @retval false - The input is not BitOffset
656 public static boolean isBitOffset(String strBitOffset
) {
657 return isInt(strBitOffset
, 0, 8);
661 Check if the input data is OffsetBitSize
663 @param strOffsetBitSize The input string need be checked
665 @retval true - The input is OffsetBitSize
666 @retval false - The input is not OffsetBitSize
669 public static boolean isOffsetBitSize(String strOffsetBitSize
) {
670 return isInt(strOffsetBitSize
, 0, 7);
674 // The below is used to check formsets data types
678 Check if the input data is Formsets
680 @param strFormsets The input string need be checked
682 @retval true - The input is Formsets
683 @retval false - The input is not Formsets
686 public static boolean isFormsets(String strFormsets
) {
687 return isCName(strFormsets
);
691 // The below is used to check externs data types
695 Check if the input data is Constructor
697 @param strConstructor The input string need be checked
699 @retval true - The input is Constructor
700 @retval false - The input is not Constructor
703 public static boolean isConstructor(String strConstructor
) {
704 return isCName(strConstructor
);
708 Check if the input data is Destructor
710 @param strDestructor The input string need be checked
712 @retval true - The input is Destructor
713 @retval false - The input is not Destructor
716 public static boolean isDestructor(String strDestructor
) {
717 return isCName(strDestructor
);
721 Check if the input data is DriverBinding
723 @param strDriverBinding The input string need be checked
725 @retval true - The input is DriverBinding
726 @retval false - The input is not DriverBinding
729 public static boolean isDriverBinding(String strDriverBinding
) {
730 return isCName(strDriverBinding
);
734 Check if the input data is ComponentName
736 @param strComponentName The input string need be checked
738 @retval true - The input is ComponentName
739 @retval false - The input is not ComponentName
742 public static boolean isComponentName(String strComponentName
) {
743 return isCName(strComponentName
);
747 Check if the input data is DriverConfig
749 @param strDriverConfig The input string need be checked
751 @retval true - The input is DriverConfig
752 @retval false - The input is not DriverConfig
755 public static boolean isDriverConfig(String strDriverConfig
) {
756 return isCName(strDriverConfig
);
760 Check if the input data is DriverDiag
762 @param strDriverDiag The input string need be checked
764 @retval true - The input is DriverDiag
765 @retval false - The input is not DriverDiag
768 public static boolean isDriverDiag(String strDriverDiag
) {
769 return isCName(strDriverDiag
);
773 Check if the input data is SetVirtualAddressMapCallBack
775 @param strSetVirtualAddressMapCallBack The input string need be checked
777 @retval true - The input is SetVirtualAddressMapCallBack
778 @retval false - The input is not SetVirtualAddressMapCallBack
781 public static boolean isSetVirtualAddressMapCallBack(String strSetVirtualAddressMapCallBack
) {
782 return isCName(strSetVirtualAddressMapCallBack
);
786 Check if the input data is ExitBootServicesCallBack
788 @param strExitBootServicesCallBack The input string need be checked
790 @retval true - The input is ExitBootServicesCallBack
791 @retval false - The input is not ExitBootServicesCallBack
794 public static boolean isExitBootServicesCallBack(String strExitBootServicesCallBack
) {
795 return isCName(strExitBootServicesCallBack
);
799 Check if the input data is UserDefined
801 @param strUserDefined The input string need be checked
803 @retval true - The input is UserDefined
804 @retval false - The input is not UserDefined
807 public static boolean isUserDefined(String strUserDefined
) {
808 return isCName(strUserDefined
);
812 // The below is used to check PCDs data types
816 Check if the input data is Token
818 @param strToken The input string need be checked
820 @retval true - The input is Token
821 @retval false - The input is not Token
824 public static boolean isToken(String strToken
) {
825 return isHexDoubleWordDataType(strToken
);
829 Check if the input data is MaxSku
831 @param strMaxSku The input string need be checked
833 @retval true - The input is MaxSku
834 @retval false - The input is not MaxSku
837 public static boolean isMaxSku(String strMaxSku
) {
838 return isHexByteDataType(strMaxSku
);
842 Check if the input data is SkuId
844 @param strSkuId The input string need be checked
846 @retval true - The input is SkuId
847 @retval false - The input is not SkuId
850 public static boolean isSkuId(String strSkuId
) {
851 return isHexByteDataType(strSkuId
);
855 Check if the input data is DatumSize
857 @param strDatumSize The input string need be checked
859 @retval true - The input is DatumSize
860 @retval false - The input is not DatumSize
863 public static boolean isDatumSize(String strDatumSize
) {
864 return isInt(strDatumSize
, 1, 16777215);
868 Check if the input data is VariableGuid
870 @param strVariableGuid The input string need be checked
872 @retval true - The input is VariableGuid
873 @retval false - The input is not VariableGuid
876 public static boolean isVariableGuid(String strVariableGuid
) {
877 return (isGuid(strVariableGuid
) || strVariableGuid
.equals("0"));
881 Check if the input data is DataOffset
883 @param strDataOffset The input string need be checked
885 @retval true - The input is DataOffset
886 @retval false - The input is not DataOffset
889 public static boolean isDataOffset(String strDataOffset
) {
890 return isHex64BitDataType(strDataOffset
);
894 Check if the input data is GuidOffset
896 @param strGuidOffset The input string need be checked
898 @retval true - The input is GuidOffset
899 @retval false - The input is not GuidOffset
902 public static boolean isGuidOffset(String strGuidOffset
) {
903 return isHex64BitDataType(strGuidOffset
);