]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/ModuleEditor/src/org/tianocore/common/DataValidation.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / ModuleEditor / src / org / tianocore / common / DataValidation.java
1 /** @file
2
3 The file is used to provides all kinds of Data Validation interface
4
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
10
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.
13
14 **/
15
16 package org.tianocore.common;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 /**
22 The class is used to provides all kinds of data validation interface
23
24 <p>All provided interfaces are in static mode</p>
25
26 @since ModuleEditor 1.0
27
28 **/
29 public class DataValidation {
30
31 /**
32 Reserved for test
33
34 @param args
35
36 **/
37 public static void main(String[] args) {
38 // TODO Auto-generated method stub
39
40 }
41
42 //
43 // The below is used to check common data types
44 //
45
46 /**
47 Check if the imput data is int
48
49 @param strInt The input string which needs validation
50
51 @retval true - The input is Int
52 @retval false - The input is not Int
53
54 **/
55 public static boolean isInt(String strInt) {
56 return isMatch("^-?[0-9]\\d*$", strInt);
57 }
58
59 /**
60 Check if the input data is int and it is in the valid scope
61 The scope is provided by String
62
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
66
67 @retval true - The input is Int and in the scope;
68 @retval false - The input is not Int or not in the scope
69
70 **/
71 public static boolean isInt(String strNumber, int BeginNumber, int EndNumber) {
72 //
73 //Check if the input data is int first
74 //
75 if (!isInt(strNumber)) {
76 return false;
77 }
78 //
79 //And then check if the data is between the scope
80 //
81 Integer intTemp = new Integer(strNumber);
82 if ((intTemp.intValue() < BeginNumber) || (intTemp.intValue() > EndNumber)) {
83 return false;
84 }
85 return true;
86 }
87
88 /**
89 Check if the input data is int and it is in the valid scope
90 The scope is provided by String
91
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
95
96 @retval true - The input is Int and in the scope;
97 @retval false - The input is not Int or not in the scope
98
99 **/
100 public static boolean isInt(String strNumber, String strBeginNumber, String strEndNumber) {
101 //
102 //Check if all input data are int
103 //
104 if (!isInt(strNumber)) {
105 return false;
106 }
107 if (!isInt(strBeginNumber)) {
108 return false;
109 }
110 if (!isInt(strEndNumber)) {
111 return false;
112 }
113 //
114 //And then check if the data is between the scope
115 //
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())) {
120 return false;
121 }
122 return true;
123 }
124
125 /**
126 Use regex to check if the input data is in valid format
127
128 @param strPattern The input regex
129 @param strMatcher The input data need be checked
130
131 @retval true - The data matches the regex
132 @retval false - The data doesn't match the regex
133
134 **/
135 public static boolean isMatch(String strPattern, String strMatcher) {
136 Pattern pattern = Pattern.compile(strPattern);
137 Matcher matcher = pattern.matcher(strMatcher);
138
139 return matcher.find();
140 }
141
142 //
143 // The below is used to check common customized data types
144 //
145
146 /**
147 Check if the input data is BaseNameConvention
148
149 @param strBaseNameConvention The input string need be checked
150
151 @retval true - The input is BaseNameConvention
152 @retval false - The input is not BaseNameConvention
153
154 **/
155 public static boolean isBaseNameConvention(String strBaseNameConvention) {
156 return isMatch("[A-Z]([a-zA-Z0-9])*(_)?([a-zA-Z0-9])*", strBaseNameConvention);
157 }
158
159 /**
160 Check if the input data is GuidArrayType
161
162 @param strGuidArrayType The input string need be checked
163
164 @retval true - The input is GuidArrayType
165 @retval false - The input is not GuidArrayType
166
167 **/
168 public static boolean isGuidArrayType(String strGuidArrayType) {
169 return isMatch(
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}( )*(\\})?",
171 strGuidArrayType);
172 }
173
174 /**
175 Check if the input data is GuidNamingConvention
176
177 @param strGuidNamingConvention The input string need be checked
178
179 @retval true - The input is GuidNamingConvention
180 @retval false - The input is not GuidNamingConvention
181
182 **/
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);
186 }
187
188 /**
189 Check if the input data is GuidType
190
191 @param strGuidType The input string need be checked
192
193 @retval true - The input is GuidType
194 @reture false is not GuidType
195
196 **/
197 public static boolean isGuidType(String strGuidType) {
198 return (isGuidArrayType(strGuidType) || isGuidNamingConvention(strGuidType));
199 }
200
201 /**
202 Check if the input data is Guid
203
204 @param strGuid The input string need be checked
205
206 @retval true - The input is Guid
207 @retval false - The input is not Guid
208
209 **/
210 public static boolean isGuid(String strGuid) {
211 return isGuidType(strGuid);
212 }
213
214 /**
215 Check if the input data is Sentence
216
217 @param strSentence The input string need be checked
218
219 @retval true - The input is Sentence
220 @retval false - The input is not Sentence
221
222 **/
223 public static boolean isSentence(String strSentence) {
224 return isMatch("(\\w+\\W*)+( )+(\\W*\\w*\\W*\\s*)*", strSentence);
225 }
226
227 /**
228 Check if the input data is DateType
229
230 @param strDateType The input string need be checked
231
232 @retval true - The input is DateType
233 @retval false - The input is not DateType
234
235 **/
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);
238 }
239
240 /**
241 Check if the input data is DosPath
242
243 @param strDosPath The input string need be checked
244
245 @retval true - The input is DosPath
246 @retval false - The input is not DosPath
247
248 **/
249 public static boolean isDosPath(String strDosPath) {
250 return isMatch("([a-zA-Z]:\\\\)?(((\\\\?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*", strDosPath);
251 }
252
253 /**
254 Check if the input data is UnixPath
255
256 @param strUnixPath The input string need be checked
257
258 @retval true - The input is UnixPath
259 @retval false - The input is not UnixPath
260
261 **/
262 public static boolean isUnixPath(String strUnixPath) {
263 return isMatch("(\\/)?(((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*", strUnixPath);
264 }
265
266 /**
267 Check if the input data is DirectoryNamingConvention
268
269 @param strDirectoryNamingConvention The input string need be checked
270
271 @retval true - The input is DirectoryNamingConvention
272 @retval false - The input is not DirectoryNamingConvention
273
274 **/
275 public static boolean isDirectoryNamingConvention(String strDirectoryNamingConvention) {
276 return (isDosPath(strDirectoryNamingConvention) || isUnixPath(strDirectoryNamingConvention));
277 }
278
279 /**
280 Check if the input data is HexDoubleWordDataType
281
282 @param strHexDoubleWordDataType The input string need be checked
283
284 @retval true - The input is HexDoubleWordDataType
285 @retval false - The input is not HexDoubleWordDataType
286
287 **/
288 public static boolean isHexDoubleWordDataType(String strHexDoubleWordDataType) {
289 return isMatch("0x[a-fA-F0-9]{1,8}", strHexDoubleWordDataType);
290 }
291
292 /**
293 Check if the input data is V1
294
295 @param strV1 The input string need be checked
296
297 @retval true - The input is V1
298 @retval false - The input is not V1
299
300 **/
301 public static boolean isV1(String strV1) {
302 return isMatch("((%[A-Z](_*[A-Z0-9]*)*%)+((((\\\\)?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*)", strV1);
303 }
304
305 /**
306 Check if the input data is V2
307
308 @param strV2 The input string need be checked
309
310 @retval true - The input is V2
311 @retval false - The input is not V2
312
313 **/
314 public static boolean isV2(String strV2) {
315 return isMatch(
316 "(($[A-Z](_*[A-Z0-9]*)*)+||($\\([A-Z](_*[A-Z0-9]*)*\\))+||($\\{[A-Z](_*[A-Z0-9]*)*\\})+)+(\\/)?(((((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*)*)",
317 strV2);
318 }
319
320 /**
321 Check if the input data is VariableConvention
322
323 @param strVariableConvention The input string need be checked
324
325 @retval true - The input is VariableConvention
326 @retval false - The input is not VariableConvention
327
328 **/
329 public static boolean isVariableConvention(String strVariableConvention) {
330 return (isV1(strVariableConvention) || isV2(strVariableConvention));
331 }
332
333 /**
334 Check if the input data is UCName
335
336 @param strUCName The input string need be checked
337
338 @retval true - The input is UCName
339 @retval false - The input is not UCName
340
341 **/
342 public static boolean isUCName(String strUCName) {
343 return isMatch("[A-Z]+(_*[A-Z0-9]*( )*)*", strUCName);
344 }
345
346 /**
347 Check if the input data is HexByteDataType
348
349 @param strHex64BitDataType The input string need be checked
350
351 @retval true - The input is HexByteDataType
352 @retval false - The input is not HexByteDataType
353
354 **/
355 public static boolean isHexByteDataType(String strHex64BitDataType) {
356 return isMatch("(0x)?[a-fA-F0-9]{1,2}", strHex64BitDataType);
357 }
358
359 /**
360 Check if the input data is Hex64BitDataType
361
362 @param strHex64BitDataType The input string need be checked
363
364 @retval true - The input is Hex64BitDataType
365 @retval false - The input is not Hex64BitDataType
366
367 **/
368 public static boolean isHex64BitDataType(String strHex64BitDataType) {
369 return isMatch("(0x)?[a-fA-F0-9]{1,16}", strHex64BitDataType);
370 }
371
372 /**
373 Check if the input data is HexWordDataType
374
375 @param strHexWordDataType The input string need be checked
376
377 @retval true - The input is HexWordDataType
378 @retval false - The input is not HexWordDataType
379
380 **/
381 public static boolean isHexWordDataType(String strHexWordDataType) {
382 return isMatch("0x[a-fA-F0-9]{1,4}", strHexWordDataType);
383 }
384
385 /**
386 Check if the input data is CName
387
388 @param strCName The input string need be checked
389
390 @retval true - The input is CName
391 @retval false - The input is not CName
392
393 **/
394 public static boolean isCName(String strCName) {
395 return isMatch("((_)*([a-zA-Z])+((_)*[a-zA-Z0-9]*))*", strCName);
396 }
397
398 /**
399 Check if the input data is OverrideID
400
401 @param strOverrideID The input string need be checked
402
403 @retval true - The input is OverrideID
404 @retval false - The input is not OverrideID
405
406 **/
407 public static boolean isOverrideID(String strOverrideID) {
408 return isInt(strOverrideID);
409 }
410
411 //
412 //The below is used to check msaheader data type
413 //
414
415 /**
416 Check if the input data is BaseName
417
418 @param strBaseName The input string need be checked
419
420 @retval true - The input is BaseName
421 @retval false - The input is not BaseName
422
423 **/
424 public static boolean isBaseName(String strBaseName) {
425 return isBaseNameConvention(strBaseName);
426 }
427
428 /**
429 Check if the input data is Abstract
430
431 @param strAbstract The input string need be checked
432
433 @retval true - The input is Abstract
434 @retval false - The input is not Abstract
435
436 **/
437 public static boolean isAbstract(String strAbstract) {
438 return isSentence(strAbstract);
439 }
440
441 /**
442 Check if the input data is Copyright
443
444 @param strCopyright The input string need be checked
445
446 @retval true - The input is Copyright
447 @retval false - The input is not Copyright
448
449 **/
450 public static boolean isCopyright(String strCopyright) {
451 return isSentence(strCopyright);
452 }
453
454 /**
455 Check if the input data is Created
456
457 @param strCreated The input string need be checked
458
459 @retval true - The input is Created
460 @retval false - The input is not Created
461
462 **/
463 public static boolean isCreated(String strCreated) {
464 return isDateType(strCreated);
465 }
466
467 /**
468 Check if the input data is Updated
469
470 @param strUpdated The input string need be checked
471
472 @retval true - The input is Updated
473 @retval false - The input is not Updated
474
475 **/
476 public static boolean isUpdated(String strUpdated) {
477 return isDateType(strUpdated);
478 }
479
480 //
481 // The below is used to check LibraryClass data types
482 //
483
484 /**
485 Check if the input data is LibraryClass
486
487 @param strLibraryClass The input string need be checked
488
489 @retval true - The input is LibraryClass
490 @retval false - The input is not LibraryClass
491
492 **/
493 public static boolean isLibraryClass(String strLibraryClass) {
494 return isBaseNameConvention(strLibraryClass);
495 }
496
497 //
498 // The below is used to check sourcefiles data types
499 //
500
501 /**
502 Check if the input data is Path
503
504 @param strPath The input string need be checked
505
506 @retval true - The input is Path
507 @retval false - The input is not Path
508
509 **/
510 public static boolean isPath(String strPath) {
511 return isDirectoryNamingConvention(strPath);
512 }
513
514 /**
515 Check if the input data is FileName
516
517 @param strFileName The input string need be checked
518
519 @retval true - The input is FileName
520 @retval false - The input is not FileName
521
522 **/
523 public static boolean isFileName(String strFileName) {
524 return isVariableConvention(strFileName);
525 }
526
527 //
528 // The below is used to check includes data types
529 //
530
531 /**
532 Check if the input data is UpdatedDate
533
534 @param strUpdatedDate The input string need be checked
535
536 @retval true - The input is UpdatedDate
537 @retval false - The input is not UpdatedDate
538
539 **/
540 public static boolean isUpdatedDate(String strUpdatedDate) {
541 return isDateType(strUpdatedDate);
542 }
543
544 /**
545 Check if the input data is PackageName
546
547 @param strPackageName The input string need be checked
548
549 @retval true - The input is PackageName
550 @retval false - The input is not PackageName
551
552 **/
553 public static boolean isPackageName(String strPackageName) {
554 return isBaseNameConvention(strPackageName);
555 }
556
557 //
558 // The below is used to check protocols data types
559 //
560
561 /**
562 Check if the input data is ProtocolName
563
564 @param strProtocolName The input string need be checked
565
566 @retval true - The input is ProtocolName
567 @retval false - The input is not ProtocolName
568
569 **/
570 public static boolean isProtocolName(String strProtocolName) {
571 return isCName(strProtocolName);
572 }
573
574 /**
575 Check if the input data is ProtocolNotifyName
576
577 @param strProtocolNotifyName The input string need be checked
578
579 @retval true - The input is ProtocolNotifyName
580 @retval false - The input is not ProtocolNotifyName
581
582 **/
583 public static boolean isProtocolNotifyName(String strProtocolNotifyName) {
584 return isCName(strProtocolNotifyName);
585 }
586
587 //
588 // The below is used to check ppis data types
589 //
590
591 /**
592 Check if the input data is PpiName
593
594 @param strPpiName The input string need be checked
595
596 @retval true - The input is PpiName
597 @retval false - The input is not PpiName
598
599 **/
600 public static boolean isPpiName(String strPpiName) {
601 return isCName(strPpiName);
602 }
603
604 /**
605 Check if the input data is PpiNotifyName
606
607 @param strPpiNotifyName The input string need be checked
608
609 @retval true - The input is PpiNotifyName
610 @retval false - The input is not PpiNotifyName
611
612 **/
613 public static boolean isPpiNotifyName(String strPpiNotifyName) {
614 return isCName(strPpiNotifyName);
615 }
616
617 /**
618 Check if the input data is FeatureFlag
619
620 @param strFeatureFlag The input string need be checked
621
622 @retval true - The input is FeatureFlag
623 @retval false - The input is not FeatureFlag
624
625 **/
626 public static boolean isFeatureFlag(String strFeatureFlag) {
627 return isCName(strFeatureFlag);
628 }
629
630 //
631 // The below is used to check variable data types
632 //
633
634 /**
635 Check if the input data is ByteOffset
636
637 @param strByteOffset The input string need be checked
638
639 @retval true - The input is ByteOffset
640 @retval false - The input is not ByteOffset
641
642 **/
643 public static boolean isByteOffset(String strByteOffset) {
644 return isByteOffset(strByteOffset);
645 }
646
647 /**
648 Check if the input data is BitOffset
649
650 @param strBitOffset The input string need be checked
651
652 @retval true - The input is BitOffset
653 @retval false - The input is not BitOffset
654
655 **/
656 public static boolean isBitOffset(String strBitOffset) {
657 return isInt(strBitOffset, 0, 8);
658 }
659
660 /**
661 Check if the input data is OffsetBitSize
662
663 @param strOffsetBitSize The input string need be checked
664
665 @retval true - The input is OffsetBitSize
666 @retval false - The input is not OffsetBitSize
667
668 **/
669 public static boolean isOffsetBitSize(String strOffsetBitSize) {
670 return isInt(strOffsetBitSize, 0, 7);
671 }
672
673 //
674 // The below is used to check formsets data types
675 //
676
677 /**
678 Check if the input data is Formsets
679
680 @param strFormsets The input string need be checked
681
682 @retval true - The input is Formsets
683 @retval false - The input is not Formsets
684
685 **/
686 public static boolean isFormsets(String strFormsets) {
687 return isCName(strFormsets);
688 }
689
690 //
691 // The below is used to check externs data types
692 //
693
694 /**
695 Check if the input data is Constructor
696
697 @param strConstructor The input string need be checked
698
699 @retval true - The input is Constructor
700 @retval false - The input is not Constructor
701
702 **/
703 public static boolean isConstructor(String strConstructor) {
704 return isCName(strConstructor);
705 }
706
707 /**
708 Check if the input data is Destructor
709
710 @param strDestructor The input string need be checked
711
712 @retval true - The input is Destructor
713 @retval false - The input is not Destructor
714
715 **/
716 public static boolean isDestructor(String strDestructor) {
717 return isCName(strDestructor);
718 }
719
720 /**
721 Check if the input data is DriverBinding
722
723 @param strDriverBinding The input string need be checked
724
725 @retval true - The input is DriverBinding
726 @retval false - The input is not DriverBinding
727
728 **/
729 public static boolean isDriverBinding(String strDriverBinding) {
730 return isCName(strDriverBinding);
731 }
732
733 /**
734 Check if the input data is ComponentName
735
736 @param strComponentName The input string need be checked
737
738 @retval true - The input is ComponentName
739 @retval false - The input is not ComponentName
740
741 **/
742 public static boolean isComponentName(String strComponentName) {
743 return isCName(strComponentName);
744 }
745
746 /**
747 Check if the input data is DriverConfig
748
749 @param strDriverConfig The input string need be checked
750
751 @retval true - The input is DriverConfig
752 @retval false - The input is not DriverConfig
753
754 **/
755 public static boolean isDriverConfig(String strDriverConfig) {
756 return isCName(strDriverConfig);
757 }
758
759 /**
760 Check if the input data is DriverDiag
761
762 @param strDriverDiag The input string need be checked
763
764 @retval true - The input is DriverDiag
765 @retval false - The input is not DriverDiag
766
767 **/
768 public static boolean isDriverDiag(String strDriverDiag) {
769 return isCName(strDriverDiag);
770 }
771
772 /**
773 Check if the input data is SetVirtualAddressMapCallBack
774
775 @param strSetVirtualAddressMapCallBack The input string need be checked
776
777 @retval true - The input is SetVirtualAddressMapCallBack
778 @retval false - The input is not SetVirtualAddressMapCallBack
779
780 **/
781 public static boolean isSetVirtualAddressMapCallBack(String strSetVirtualAddressMapCallBack) {
782 return isCName(strSetVirtualAddressMapCallBack);
783 }
784
785 /**
786 Check if the input data is ExitBootServicesCallBack
787
788 @param strExitBootServicesCallBack The input string need be checked
789
790 @retval true - The input is ExitBootServicesCallBack
791 @retval false - The input is not ExitBootServicesCallBack
792
793 **/
794 public static boolean isExitBootServicesCallBack(String strExitBootServicesCallBack) {
795 return isCName(strExitBootServicesCallBack);
796 }
797
798 /**
799 Check if the input data is UserDefined
800
801 @param strUserDefined The input string need be checked
802
803 @retval true - The input is UserDefined
804 @retval false - The input is not UserDefined
805
806 **/
807 public static boolean isUserDefined(String strUserDefined) {
808 return isCName(strUserDefined);
809 }
810
811 //
812 // The below is used to check PCDs data types
813 //
814
815 /**
816 Check if the input data is Token
817
818 @param strToken The input string need be checked
819
820 @retval true - The input is Token
821 @retval false - The input is not Token
822
823 **/
824 public static boolean isToken(String strToken) {
825 return isHexDoubleWordDataType(strToken);
826 }
827
828 /**
829 Check if the input data is MaxSku
830
831 @param strMaxSku The input string need be checked
832
833 @retval true - The input is MaxSku
834 @retval false - The input is not MaxSku
835
836 **/
837 public static boolean isMaxSku(String strMaxSku) {
838 return isHexByteDataType(strMaxSku);
839 }
840
841 /**
842 Check if the input data is SkuId
843
844 @param strSkuId The input string need be checked
845
846 @retval true - The input is SkuId
847 @retval false - The input is not SkuId
848
849 **/
850 public static boolean isSkuId(String strSkuId) {
851 return isHexByteDataType(strSkuId);
852 }
853
854 /**
855 Check if the input data is DatumSize
856
857 @param strDatumSize The input string need be checked
858
859 @retval true - The input is DatumSize
860 @retval false - The input is not DatumSize
861
862 **/
863 public static boolean isDatumSize(String strDatumSize) {
864 return isInt(strDatumSize, 1, 16777215);
865 }
866
867 /**
868 Check if the input data is VariableGuid
869
870 @param strVariableGuid The input string need be checked
871
872 @retval true - The input is VariableGuid
873 @retval false - The input is not VariableGuid
874
875 **/
876 public static boolean isVariableGuid(String strVariableGuid) {
877 return (isGuid(strVariableGuid) || strVariableGuid.equals("0"));
878 }
879
880 /**
881 Check if the input data is DataOffset
882
883 @param strDataOffset The input string need be checked
884
885 @retval true - The input is DataOffset
886 @retval false - The input is not DataOffset
887
888 **/
889 public static boolean isDataOffset(String strDataOffset) {
890 return isHex64BitDataType(strDataOffset);
891 }
892
893 /**
894 Check if the input data is GuidOffset
895
896 @param strGuidOffset The input string need be checked
897
898 @retval true - The input is GuidOffset
899 @retval false - The input is not GuidOffset
900
901 **/
902 public static boolean isGuidOffset(String strGuidOffset) {
903 return isHex64BitDataType(strGuidOffset);
904 }
905 }