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