]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/DataValidation.java
remove unnecessary check for NULL pointer.
[mirror_edk2.git] / Tools / Java / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / 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.frameworkwizard.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 **/
27 public class DataValidation {
28
29 /**
30 Reserved for test
31
32 @param args
33
34 **/
35 public static void main(String[] args) {
36
37 }
38
39 //
40 // The below is used to check common data types
41 //
42
43 /**
44 Check if the imput data is int
45
46 @param strInt The input string which needs validation
47
48 @retval true - The input is Int
49 @retval false - The input is not Int
50
51 **/
52 public static boolean isInt(String strInt) {
53 return isMatch("^-?[0-9]\\d*$", strInt);
54 }
55
56 /**
57 Check if the input data is int and it is in the valid scope
58 The scope is provided by String
59
60 @param strNumber The input string which needs validation
61 @param BeginNumber The left boundary of the scope
62 @param EndNumber The right boundary of the scope
63
64 @retval true - The input is Int and in the scope;
65 @retval false - The input is not Int or not in the scope
66
67 **/
68 public static boolean isInt(String strNumber, int BeginNumber, int EndNumber) {
69 //
70 //Check if the input data is int first
71 //
72 if (!isInt(strNumber)) {
73 return false;
74 }
75 //
76 //And then check if the data is between the scope
77 //
78 Integer intTemp = new Integer(strNumber);
79 if ((intTemp.intValue() < BeginNumber) || (intTemp.intValue() > EndNumber)) {
80 return false;
81 }
82 return true;
83 }
84
85 /**
86 Check if the input data is long int and it is in the valid scope
87 The scope is provided by String
88
89 @param strNumber The input string which needs validation
90 @param BeginNumber The left boundary of the scope
91 @param EndNumber The right boundary of the scope
92
93 @retval true - The input is Int and in the scope;
94 @retval false - The input is not Int or not in the scope
95
96 **/
97 public static boolean isLongInt(String strNumber, long BeginNumber, long EndNumber) throws Exception{
98 //
99 //Check if the input data is int first
100 //
101 if (!isInt(strNumber)) {
102 return false;
103 }
104 //
105 //And then check if the data is between the scope
106 //
107 try {
108 Long intTemp = new Long(strNumber);
109 if ((intTemp.longValue() < BeginNumber) || (intTemp.longValue() > EndNumber)) {
110 return false;
111 }
112 }
113 catch (Exception e) {
114 throw e;
115 }
116
117 return true;
118 }
119 /**
120 Check if the input data is int and it is in the valid scope
121 The scope is provided by String
122
123 @param strNumber The input string which needs validation
124 @param strBeginNumber The left boundary of the scope
125 @param strEndNumber The right boundary of the scope
126
127 @retval true - The input is Int and in the scope;
128 @retval false - The input is not Int or not in the scope
129
130 **/
131 public static boolean isInt(String strNumber, String strBeginNumber, String strEndNumber) {
132 //
133 //Check if all input data are int
134 //
135 if (!isInt(strNumber)) {
136 return false;
137 }
138 if (!isInt(strBeginNumber)) {
139 return false;
140 }
141 if (!isInt(strEndNumber)) {
142 return false;
143 }
144 //
145 //And then check if the data is between the scope
146 //
147 Integer intI = new Integer(strNumber);
148 Integer intJ = new Integer(strBeginNumber);
149 Integer intK = new Integer(strEndNumber);
150 if ((intI.intValue() < intJ.intValue()) || (intI.intValue() > intK.intValue())) {
151 return false;
152 }
153 return true;
154 }
155
156 /**
157 Use regex to check if the input data is in valid format
158
159 @param strPattern The input regex
160 @param strMatcher The input data need be checked
161
162 @retval true - The data matches the regex
163 @retval false - The data doesn't match the regex
164
165 **/
166 public static boolean isMatch(String strPattern, String strMatcher) {
167 Pattern pattern = Pattern.compile(strPattern);
168 Matcher matcher = pattern.matcher(strMatcher);
169
170 return matcher.find();
171 }
172
173 //
174 // The below is used to check common customized data types
175 //
176
177 /**
178 Check if the input data is UiNameType
179
180 @param arg0 The input string need be checked
181
182 @retval true - The input is UiNameType
183 @retval false - The input is not UiNameType
184
185 **/
186 public static boolean isUiNameType(String arg0) {
187 if (arg0.length() < 1) {
188 return false;
189 }
190 return isMatch("[^ ].*", arg0);
191 }
192
193 /**
194 Check if the input data is GuidType2
195
196 @param arg0 The input string need be checked
197
198 @retval true - The input is GuidType2
199 @retval false - The input is not GuidType2
200
201 **/
202 public static boolean isGuidType2(String arg0) {
203 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}", arg0);
204 }
205
206 /**
207 Check if the input data is Guid
208
209 @param strGuid The input string need be checked
210
211 @retval true - The input is Guid
212 @retval false - The input is not Guid
213
214 **/
215 public static boolean isGuid(String arg0) {
216 return isGuidType2(arg0);
217 }
218
219 /**
220 Check if the input data is Version
221
222 @param arg0 The input string need be checked
223
224 @retval true - The input is Version
225 @retval false - The input is not Version
226
227 **/
228 public static boolean isVersionDataType(String arg0) {
229 return isMatch("\\d+(\\.\\d+)*", arg0);
230 }
231
232 /**
233 Check if the input data is Sentence
234
235 @param strSentence The input string need be checked
236
237 @retval true - The input is Sentence
238 @retval false - The input is not Sentence
239
240 **/
241 public static boolean isSentence(String arg0) {
242 return isMatch("(\\w+\\W*)+( )+(\\W*\\w*\\W*\\s*)*", arg0);
243 }
244
245 /**
246 Check if the input data is FileNameConventio
247
248 @param strSentence The input string need be checked
249
250 @retval true - The input is FileNameConventio
251 @retval false - The input is not FileNameConventio
252
253 **/
254 public static boolean isFileNameConvention(String arg0) {
255 return isMatch("[a-zA-Z](\\.?[-_a-zA-Z0-9]+)*", arg0);
256 }
257
258 /**
259 Check if the input data is KeywordType
260
261 @param strSentence The input string need be checked
262
263 @retval true - The input is KeywordType
264 @retval false - The input is not KeywordType
265
266 **/
267 public static boolean isKeywordType(String arg0) {
268 return isMatch("[a-zA-Z]+(_*[a-zA-Z0-9]*)*", arg0);
269 }
270
271 /**
272 Check if the input data is FeatureFlagExpressionType
273
274 @param strSentence The input string need be checked
275
276 @retval true - The input is FeatureFlagExpressionType
277 @retval false - The input is not FeatureFlagExpressionType
278
279 **/
280 public static boolean isFeatureFlagExpressionType(String arg0) {
281 return (arg0.length() >= 1);
282 }
283
284 /**
285 Check if the input data is FeatureFlag
286
287 @param strSentence The input string need be checked
288
289 @retval true - The input is FeatureFlag
290 @retval false - The input is not FeatureFlag
291
292 **/
293 public static boolean isFeatureFlag(String arg0) {
294 return isFeatureFlagExpressionType(arg0);
295 }
296
297 /**
298 Check if the input data is PathAndFilename
299
300 @param strSentence The input string need be checked
301
302 @retval true - The input is PathAndFilename
303 @retval false - The input is not PathAndFilename
304
305 **/
306 public static boolean isPathAndFilename(String arg0) {
307 return !arg0.equals("");
308 }
309
310 /**
311 Check if the input data is ToolsNameConvention
312
313 @param strSentence The input string need be checked
314
315 @retval true - The input is ToolsNameConvention
316 @retval false - The input is not ToolsNameConvention
317
318 **/
319 public static boolean isToolsNameConvention(String arg0) {
320 return isMatch("[a-zA-Z][a-zA-Z0-9]*", arg0);
321 }
322
323 /**
324 Check if the input data is C_NameType
325
326 @param strSentence The input string need be checked
327
328 @retval true - The input is C_NameType
329 @retval false - The input is not C_NameType
330
331 **/
332 public static boolean isC_NameType(String arg0) {
333 return isMatch("(_)*[a-zA-Z]+((_)*[a-zA-Z0-9]*)*", arg0);
334 }
335
336 /**
337 Check if the input data is HexWordArrayType
338
339 @param strSentence The input string need be checked
340
341 @retval true - The input is HexWordArrayType
342 @retval false - The input is not HexWordArrayType
343
344 **/
345 public static boolean isHexWordArrayType(String arg0) {
346 return arg0.length() >=6 && isMatch("((\\s)*0x([a-fA-F0-9]){4}(,)?(\\s)*)+", arg0);
347 }
348
349 /**
350 Check if the input data is Hex64BitDataType
351
352 @param strSentence The input string need be checked
353
354 @retval true - The input is Hex64BitDataType
355 @retval false - The input is not Hex64BitDataType
356
357 **/
358 public static boolean isHex64BitDataType(String arg0) {
359 return isMatch("(0x)?[a-fA-F0-9]{1,16}", arg0);
360 }
361
362 /**
363 Check if the input data is UnicodeString
364
365 @param strSentence The input string need be checked
366
367 @retval true - The input is UnicodeString
368 @retval false - The input is not UnicodeString
369
370 **/
371 public static boolean isUnicodeString(String arg0) {
372 return arg0.length() >= 3 && isMatch("(\\s)*L(\\:)?\"[^\"]*\"(\\s)*", arg0);
373 }
374
375 /**
376 Check if the input data is HexByteArrayType
377
378 @param strSentence The input string need be checked
379
380 @retval true - The input is HexByteArrayType
381 @retval false - The input is not HexByteArrayType
382
383 **/
384 public static boolean isHexByteArrayType(String arg0) {
385 return arg0.length() >= 4 && isMatch("((\\s)*0x([a-fA-F0-9]){2}(,)?(\\s)*)+", arg0);
386 }
387
388
389 /**
390 Check if the input data is DateType
391
392 @param strDateType The input string need be checked
393
394 @retval true - The input is DateType
395 @retval false - The input is not DateType
396
397 **/
398 public static boolean isDateType(String strDateType) {
399 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);
400 }
401
402 /**
403 Check if the input data is DosPath
404
405 @param strDosPath The input string need be checked
406
407 @retval true - The input is DosPath
408 @retval false - The input is not DosPath
409
410 **/
411 public static boolean isDosPath(String strDosPath) {
412 return isMatch("([a-zA-Z]:\\\\)?(((\\\\?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*", strDosPath);
413 }
414
415 /**
416 Check if the input data is UnixPath
417
418 @param strUnixPath The input string need be checked
419
420 @retval true - The input is UnixPath
421 @retval false - The input is not UnixPath
422
423 **/
424 public static boolean isUnixPath(String strUnixPath) {
425 return isMatch("(\\/)?(((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*", strUnixPath);
426 }
427
428 /**
429 Check if the input data is DirectoryNamingConvention
430
431 @param strDirectoryNamingConvention The input string need be checked
432
433 @retval true - The input is DirectoryNamingConvention
434 @retval false - The input is not DirectoryNamingConvention
435
436 **/
437 public static boolean isDirectoryNamingConvention(String strDirectoryNamingConvention) {
438 return (isDosPath(strDirectoryNamingConvention) || isUnixPath(strDirectoryNamingConvention));
439 }
440
441 /**
442 Check if the input data is HexDoubleWordDataType
443
444 @param strHexDoubleWordDataType The input string need be checked
445
446 @retval true - The input is HexDoubleWordDataType
447 @retval false - The input is not HexDoubleWordDataType
448
449 **/
450 public static boolean isHexDoubleWordDataType(String strHexDoubleWordDataType) {
451 return isMatch("0x[a-fA-F0-9]{1,8}", strHexDoubleWordDataType);
452 }
453
454 /**
455 Check if the input data is V1
456
457 @param strV1 The input string need be checked
458
459 @retval true - The input is V1
460 @retval false - The input is not V1
461
462 **/
463 public static boolean isV1(String strV1) {
464 return isMatch("((%[A-Z](_*[A-Z0-9]*)*%)+((((\\\\)?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*)", strV1);
465 }
466
467 /**
468 Check if the input data is V2
469
470 @param strV2 The input string need be checked
471
472 @retval true - The input is V2
473 @retval false - The input is not V2
474
475 **/
476 public static boolean isV2(String strV2) {
477 return isMatch(
478 "(($[A-Z](_*[A-Z0-9]*)*)+||($\\([A-Z](_*[A-Z0-9]*)*\\))+||($\\{[A-Z](_*[A-Z0-9]*)*\\})+)+(\\/)?(((((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*)*)",
479 strV2);
480 }
481
482 /**
483 Check if the input data is VariableConvention
484
485 @param strVariableConvention The input string need be checked
486
487 @retval true - The input is VariableConvention
488 @retval false - The input is not VariableConvention
489
490 **/
491 public static boolean isVariableConvention(String strVariableConvention) {
492 return (isV1(strVariableConvention) || isV2(strVariableConvention));
493 }
494
495 /**
496 Check if the input data is UCName
497
498 @param strUCName The input string need be checked
499
500 @retval true - The input is UCName
501 @retval false - The input is not UCName
502
503 **/
504 public static boolean isUCName(String strUCName) {
505 return isMatch("[A-Z]+(_*[A-Z0-9]*( )*)*", strUCName);
506 }
507
508 /**
509 Check if the input data is HexByteDataType
510
511 @param strHex64BitDataType The input string need be checked
512
513 @retval true - The input is HexByteDataType
514 @retval false - The input is not HexByteDataType
515
516 **/
517 public static boolean isHexByteDataType(String strHex64BitDataType) {
518 return isMatch("(0x)?[a-fA-F0-9]{1,2}", strHex64BitDataType);
519 }
520
521 /**
522 Check if the input data is HexWordDataType
523
524 @param strHexWordDataType The input string need be checked
525
526 @retval true - The input is HexWordDataType
527 @retval false - The input is not HexWordDataType
528
529 **/
530 public static boolean isHexWordDataType(String strHexWordDataType) {
531 return isMatch("0x[a-fA-F0-9]{1,4}", strHexWordDataType);
532 }
533
534 /**
535 Check if the input data is OverrideID
536
537 @param strOverrideID The input string need be checked
538
539 @retval true - The input is OverrideID
540 @retval false - The input is not OverrideID
541
542 **/
543 public static boolean isOverrideID(String strOverrideID) {
544 return isInt(strOverrideID);
545 }
546
547 /**
548 Check if the input data is Supported Architectures
549
550 @param strSupportedArchitectures
551 @retval true - The input is Supported Architectures
552 @retval false - The input isn't Supported Architectures
553
554 */
555 public static boolean isSupportedArchitectures(String strSupportedArchitectures) {
556 return isMatch("(ALL){1}|(((IA32)|((X64)|(IPF)|(EBC)){1}((,((IA32)|(X64)|(IPF)|(EBC)){1} ){0,2}))){1}",
557 strSupportedArchitectures);
558 }
559
560 //
561 //The below is used to check msaheader data type
562 //
563
564 /**
565 Check if the input data is BaseName
566
567 @param strBaseName The input string need be checked
568
569 @retval true - The input is BaseName
570 @retval false - The input is not BaseName
571
572 **/
573 public static boolean isBaseName(String arg0) {
574 return isUiNameType(arg0);
575 }
576
577 /**
578 Check if the input data is Version
579
580 @param arg0 The input string need be checked
581
582 @retval true - The input is Version
583 @retval false - The input is not Version
584
585 **/
586 public static boolean isVersion(String arg0) {
587 return isVersionDataType(arg0);
588 }
589
590 /**
591 Check if the input data is Abstract
592
593 @param strAbstract The input string need be checked
594
595 @retval true - The input is Abstract
596 @retval false - The input is not Abstract
597
598 **/
599 public static boolean isAbstract(String arg0) {
600 return isSentence(arg0);
601 }
602
603 /**
604 Check if the input data is Copyright
605
606 @param strCopyright The input string need be checked
607
608 @retval true - The input is Copyright
609 @retval false - The input is not Copyright
610
611 **/
612 public static boolean isCopyright(String arg0) {
613 return !arg0.equals("");
614 }
615
616 /**
617 Check if the input data is Specification
618
619 @param strCopyright The input string need be checked
620
621 @retval true - The input is Specification
622 @retval false - The input is not Specification
623
624 **/
625 public static boolean isSpecification(String arg0) {
626 return isSentence(arg0);
627 }
628
629 //
630 // The below is used to check ModuleDefinitions data types
631 //
632 /**
633 Check if the input data is OutputFileBasename
634
635 @param strCopyright The input string need be checked
636
637 @retval true - The input is OutputFileBasename
638 @retval false - The input is not OutputFileBasename
639
640 **/
641 public static boolean isOutputFileBasename(String arg0) {
642 return isFileNameConvention(arg0);
643 }
644
645 //
646 // The below is used to check LibraryClass data types
647 //
648 /**
649 Check if the input data is LibraryClass
650
651 @param strCopyright The input string need be checked
652
653 @retval true - The input is LibraryClass
654 @retval false - The input is not LibraryClass
655
656 **/
657 public static boolean isLibraryClass(String arg0) {
658 return isKeywordType(arg0);
659 }
660
661 /**
662 Check if the input data is RecommendedInstanceVersion
663
664 @param strCopyright The input string need be checked
665
666 @retval true - The input is RecommendedInstanceVersion
667 @retval false - The input is not RecommendedInstanceVersion
668
669 **/
670 public static boolean isRecommendedInstanceVersion(String arg0) {
671 return isVersionDataType(arg0);
672 }
673
674 //
675 // The below is used to check sourcefiles data types
676 //
677
678 /**
679 Check if the input data is Filename
680
681 @param strPath The input string need be checked
682
683 @retval true - The input is Filename
684 @retval false - The input is not Filename
685
686 **/
687 public static boolean isFilename(String arg0) {
688 return isPathAndFilename(arg0);
689 }
690
691 /**
692 Check if the input data is TagName
693
694 @param strPath The input string need be checked
695
696 @retval true - The input is TagName
697 @retval false - The input is not TagName
698
699 **/
700 public static boolean isTagName(String arg0) {
701 return isToolsNameConvention(arg0);
702 }
703
704 /**
705 Check if the input data is ToolCode
706
707 @param strPath The input string need be checked
708
709 @retval true - The input is ToolCode
710 @retval false - The input is not ToolCode
711
712 **/
713 public static boolean isToolCode(String arg0) {
714 return isToolsNameConvention(arg0);
715 }
716
717 /**
718 Check if the input data is ToolChainFamily
719
720 @param strPath The input string need be checked
721
722 @retval true - The input is ToolChainFamily
723 @retval false - The input is not ToolChainFamily
724
725 **/
726 public static boolean isToolChainFamily(String arg0) {
727 return isToolsNameConvention(arg0);
728 }
729
730 //
731 // The below is used to check pcdcoded data types
732 //
733 /**
734 Check if the input data is DefaultValueType
735
736 @param strPath The input string need be checked
737
738 @retval true - The input is DefaultValueType
739 @retval false - The input is not DefaultValueType
740
741 **/
742 public static boolean isDefaultValueType(String arg0) {
743 return isHex64BitDataType(arg0) || isUnicodeString(arg0) || isHexByteArrayType(arg0);
744 }
745
746 }