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