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
.frameworkwizard
.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>
27 public class DataValidation
{
35 public static void main(String
[] args
) {
40 // The below is used to check common data types
44 Check if the imput data is int
46 @param strInt The input string which needs validation
48 @retval true - The input is Int
49 @retval false - The input is not Int
52 public static boolean isInt(String strInt
) {
53 return isMatch("^-?[0-9]\\d*$", strInt
);
57 Check if the input data is int and it is in the valid scope
58 The scope is provided by String
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
64 @retval true - The input is Int and in the scope;
65 @retval false - The input is not Int or not in the scope
68 public static boolean isInt(String strNumber
, int BeginNumber
, int EndNumber
) {
70 //Check if the input data is int first
72 if (!isInt(strNumber
)) {
76 //And then check if the data is between the scope
78 Integer intTemp
= new Integer(strNumber
);
79 if ((intTemp
.intValue() < BeginNumber
) || (intTemp
.intValue() > EndNumber
)) {
86 Check if the input data is int and it is in the valid scope
87 The scope is provided by String
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
93 @retval true - The input is Int and in the scope;
94 @retval false - The input is not Int or not in the scope
97 public static boolean isInt(String strNumber
, String strBeginNumber
, String strEndNumber
) {
99 //Check if all input data are int
101 if (!isInt(strNumber
)) {
104 if (!isInt(strBeginNumber
)) {
107 if (!isInt(strEndNumber
)) {
111 //And then check if the data is between the scope
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())) {
123 Use regex to check if the input data is in valid format
125 @param strPattern The input regex
126 @param strMatcher The input data need be checked
128 @retval true - The data matches the regex
129 @retval false - The data doesn't match the regex
132 public static boolean isMatch(String strPattern
, String strMatcher
) {
133 Pattern pattern
= Pattern
.compile(strPattern
);
134 Matcher matcher
= pattern
.matcher(strMatcher
);
136 return matcher
.find();
140 // The below is used to check common customized data types
144 Check if the input data is UiNameType
146 @param arg0 The input string need be checked
148 @retval true - The input is UiNameType
149 @retval false - The input is not UiNameType
152 public static boolean isUiNameType(String arg0
) {
153 if (arg0
.length() < 1) {
156 return isMatch("[^ ].*", arg0
);
160 Check if the input data is GuidType2
162 @param arg0 The input string need be checked
164 @retval true - The input is GuidType2
165 @retval false - The input is not GuidType2
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
);
173 Check if the input data is Guid
175 @param strGuid The input string need be checked
177 @retval true - The input is Guid
178 @retval false - The input is not Guid
181 public static boolean isGuid(String arg0
) {
182 return isGuidType2(arg0
);
186 Check if the input data is Version
188 @param arg0 The input string need be checked
190 @retval true - The input is Version
191 @retval false - The input is not Version
194 public static boolean isVersionDataType(String arg0
) {
195 return isMatch("\\d+(\\.\\d+)*", arg0
);
199 Check if the input data is Sentence
201 @param strSentence The input string need be checked
203 @retval true - The input is Sentence
204 @retval false - The input is not Sentence
207 public static boolean isSentence(String arg0
) {
208 return isMatch("(\\w+\\W*)+( )+(\\W*\\w*\\W*\\s*)*", arg0
);
212 Check if the input data is FileNameConventio
214 @param strSentence The input string need be checked
216 @retval true - The input is FileNameConventio
217 @retval false - The input is not FileNameConventio
220 public static boolean isFileNameConvention(String arg0
) {
221 return isMatch("[a-zA-Z][a-zA-Z0-9]*((_)*(-)*(.)*[a-zA-Z0-9]*)*", arg0
);
225 Check if the input data is KeywordType
227 @param strSentence The input string need be checked
229 @retval true - The input is KeywordType
230 @retval false - The input is not KeywordType
233 public static boolean isKeywordType(String arg0
) {
234 return isMatch("[a-zA-Z]+(_*[a-zA-Z0-9]*)*", arg0
);
238 Check if the input data is FeatureFlagExpressionType
240 @param strSentence The input string need be checked
242 @retval true - The input is FeatureFlagExpressionType
243 @retval false - The input is not FeatureFlagExpressionType
246 public static boolean isFeatureFlagExpressionType(String arg0
) {
247 return (arg0
.length() >= 1);
251 Check if the input data is FeatureFlag
253 @param strSentence The input string need be checked
255 @retval true - The input is FeatureFlag
256 @retval false - The input is not FeatureFlag
259 public static boolean isFeatureFlag(String arg0
) {
260 return isFeatureFlagExpressionType(arg0
);
264 Check if the input data is PathAndFilename
266 @param strSentence The input string need be checked
268 @retval true - The input is PathAndFilename
269 @retval false - The input is not PathAndFilename
272 public static boolean isPathAndFilename(String arg0
) {
273 return !arg0
.equals("");
277 Check if the input data is ToolsNameConvention
279 @param strSentence The input string need be checked
281 @retval true - The input is ToolsNameConvention
282 @retval false - The input is not ToolsNameConvention
285 public static boolean isToolsNameConvention(String arg0
) {
286 return isMatch("[a-zA-Z][a-zA-Z0-9]*", arg0
);
290 Check if the input data is C_NameType
292 @param strSentence The input string need be checked
294 @retval true - The input is C_NameType
295 @retval false - The input is not C_NameType
298 public static boolean isC_NameType(String arg0
) {
299 return isMatch("(_)*[a-zA-Z]+((_)*[a-zA-Z0-9]*)*", arg0
);
303 Check if the input data is HexWordArrayType
305 @param strSentence The input string need be checked
307 @retval true - The input is HexWordArrayType
308 @retval false - The input is not HexWordArrayType
311 public static boolean isHexWordArrayType(String arg0
) {
312 return arg0
.length() <=6 && isMatch("((\\s)*0x([a-fA-F0-9]){4}(,)?(\\s)*)+", arg0
);
316 Check if the input data is Hex64BitDataType
318 @param strSentence The input string need be checked
320 @retval true - The input is Hex64BitDataType
321 @retval false - The input is not Hex64BitDataType
324 public static boolean isHex64BitDataType(String arg0
) {
325 return isMatch("(0x)?[a-fA-F0-9]{1,16}", arg0
);
329 Check if the input data is UnicodeString
331 @param strSentence The input string need be checked
333 @retval true - The input is UnicodeString
334 @retval false - The input is not UnicodeString
337 public static boolean isUnicodeString(String arg0
) {
338 return arg0
.length() >= 3 && isMatch("(\\s)*L(\\:)?\"[^\"]*\"(\\s)*", arg0
);
342 Check if the input data is HexByteArrayType
344 @param strSentence The input string need be checked
346 @retval true - The input is HexByteArrayType
347 @retval false - The input is not HexByteArrayType
350 public static boolean isHexByteArrayType(String arg0
) {
351 return arg0
.length() >= 4 && isMatch("((\\s)*0x([a-fA-F0-9]){2}(,)?(\\s)*)+", arg0
);
356 Check if the input data is DateType
358 @param strDateType The input string need be checked
360 @retval true - The input is DateType
361 @retval false - The input is not DateType
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
);
369 Check if the input data is DosPath
371 @param strDosPath The input string need be checked
373 @retval true - The input is DosPath
374 @retval false - The input is not DosPath
377 public static boolean isDosPath(String strDosPath
) {
378 return isMatch("([a-zA-Z]:\\\\)?(((\\\\?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*", strDosPath
);
382 Check if the input data is UnixPath
384 @param strUnixPath The input string need be checked
386 @retval true - The input is UnixPath
387 @retval false - The input is not UnixPath
390 public static boolean isUnixPath(String strUnixPath
) {
391 return isMatch("(\\/)?(((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*", strUnixPath
);
395 Check if the input data is DirectoryNamingConvention
397 @param strDirectoryNamingConvention The input string need be checked
399 @retval true - The input is DirectoryNamingConvention
400 @retval false - The input is not DirectoryNamingConvention
403 public static boolean isDirectoryNamingConvention(String strDirectoryNamingConvention
) {
404 return (isDosPath(strDirectoryNamingConvention
) || isUnixPath(strDirectoryNamingConvention
));
408 Check if the input data is HexDoubleWordDataType
410 @param strHexDoubleWordDataType The input string need be checked
412 @retval true - The input is HexDoubleWordDataType
413 @retval false - The input is not HexDoubleWordDataType
416 public static boolean isHexDoubleWordDataType(String strHexDoubleWordDataType
) {
417 return isMatch("0x[a-fA-F0-9]{1,8}", strHexDoubleWordDataType
);
421 Check if the input data is V1
423 @param strV1 The input string need be checked
425 @retval true - The input is V1
426 @retval false - The input is not V1
429 public static boolean isV1(String strV1
) {
430 return isMatch("((%[A-Z](_*[A-Z0-9]*)*%)+((((\\\\)?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*)", strV1
);
434 Check if the input data is V2
436 @param strV2 The input string need be checked
438 @retval true - The input is V2
439 @retval false - The input is not V2
442 public static boolean isV2(String strV2
) {
444 "(($[A-Z](_*[A-Z0-9]*)*)+||($\\([A-Z](_*[A-Z0-9]*)*\\))+||($\\{[A-Z](_*[A-Z0-9]*)*\\})+)+(\\/)?(((((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*)*)",
449 Check if the input data is VariableConvention
451 @param strVariableConvention The input string need be checked
453 @retval true - The input is VariableConvention
454 @retval false - The input is not VariableConvention
457 public static boolean isVariableConvention(String strVariableConvention
) {
458 return (isV1(strVariableConvention
) || isV2(strVariableConvention
));
462 Check if the input data is UCName
464 @param strUCName The input string need be checked
466 @retval true - The input is UCName
467 @retval false - The input is not UCName
470 public static boolean isUCName(String strUCName
) {
471 return isMatch("[A-Z]+(_*[A-Z0-9]*( )*)*", strUCName
);
475 Check if the input data is HexByteDataType
477 @param strHex64BitDataType The input string need be checked
479 @retval true - The input is HexByteDataType
480 @retval false - The input is not HexByteDataType
483 public static boolean isHexByteDataType(String strHex64BitDataType
) {
484 return isMatch("(0x)?[a-fA-F0-9]{1,2}", strHex64BitDataType
);
488 Check if the input data is HexWordDataType
490 @param strHexWordDataType The input string need be checked
492 @retval true - The input is HexWordDataType
493 @retval false - The input is not HexWordDataType
496 public static boolean isHexWordDataType(String strHexWordDataType
) {
497 return isMatch("0x[a-fA-F0-9]{1,4}", strHexWordDataType
);
501 Check if the input data is OverrideID
503 @param strOverrideID The input string need be checked
505 @retval true - The input is OverrideID
506 @retval false - The input is not OverrideID
509 public static boolean isOverrideID(String strOverrideID
) {
510 return isInt(strOverrideID
);
514 Check if the input data is Supported Architectures
516 @param strSupportedArchitectures
517 @retval true - The input is Supported Architectures
518 @retval false - The input isn't Supported Architectures
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
);
527 //The below is used to check msaheader data type
531 Check if the input data is BaseName
533 @param strBaseName The input string need be checked
535 @retval true - The input is BaseName
536 @retval false - The input is not BaseName
539 public static boolean isBaseName(String arg0
) {
540 return isUiNameType(arg0
);
544 Check if the input data is Version
546 @param arg0 The input string need be checked
548 @retval true - The input is Version
549 @retval false - The input is not Version
552 public static boolean isVersion(String arg0
) {
553 return isVersionDataType(arg0
);
557 Check if the input data is Abstract
559 @param strAbstract The input string need be checked
561 @retval true - The input is Abstract
562 @retval false - The input is not Abstract
565 public static boolean isAbstract(String arg0
) {
566 return isSentence(arg0
);
570 Check if the input data is Copyright
572 @param strCopyright The input string need be checked
574 @retval true - The input is Copyright
575 @retval false - The input is not Copyright
578 public static boolean isCopyright(String arg0
) {
579 return !arg0
.equals("");
583 Check if the input data is Specification
585 @param strCopyright The input string need be checked
587 @retval true - The input is Specification
588 @retval false - The input is not Specification
591 public static boolean isSpecification(String arg0
) {
592 return isSentence(arg0
);
596 // The below is used to check ModuleDefinitions data types
599 Check if the input data is OutputFileBasename
601 @param strCopyright The input string need be checked
603 @retval true - The input is OutputFileBasename
604 @retval false - The input is not OutputFileBasename
607 public static boolean isOutputFileBasename(String arg0
) {
608 return isFileNameConvention(arg0
);
612 // The below is used to check LibraryClass data types
615 Check if the input data is LibraryClass
617 @param strCopyright The input string need be checked
619 @retval true - The input is LibraryClass
620 @retval false - The input is not LibraryClass
623 public static boolean isLibraryClass(String arg0
) {
624 return isKeywordType(arg0
);
628 Check if the input data is RecommendedInstanceVersion
630 @param strCopyright The input string need be checked
632 @retval true - The input is RecommendedInstanceVersion
633 @retval false - The input is not RecommendedInstanceVersion
636 public static boolean isRecommendedInstanceVersion(String arg0
) {
637 return isVersionDataType(arg0
);
641 // The below is used to check sourcefiles data types
645 Check if the input data is Filename
647 @param strPath The input string need be checked
649 @retval true - The input is Filename
650 @retval false - The input is not Filename
653 public static boolean isFilename(String arg0
) {
654 return isPathAndFilename(arg0
);
658 Check if the input data is TagName
660 @param strPath The input string need be checked
662 @retval true - The input is TagName
663 @retval false - The input is not TagName
666 public static boolean isTagName(String arg0
) {
667 return isToolsNameConvention(arg0
);
671 Check if the input data is ToolCode
673 @param strPath The input string need be checked
675 @retval true - The input is ToolCode
676 @retval false - The input is not ToolCode
679 public static boolean isToolCode(String arg0
) {
680 return isToolsNameConvention(arg0
);
684 Check if the input data is ToolChainFamily
686 @param strPath The input string need be checked
688 @retval true - The input is ToolChainFamily
689 @retval false - The input is not ToolChainFamily
692 public static boolean isToolChainFamily(String arg0
) {
693 return isToolsNameConvention(arg0
);
697 // The below is used to check pcdcoded data types
700 Check if the input data is DefaultValueType
702 @param strPath The input string need be checked
704 @retval true - The input is DefaultValueType
705 @retval false - The input is not DefaultValueType
708 public static boolean isDefaultValueType(String arg0
) {
709 return isHex64BitDataType(arg0
) || isUnicodeString(arg0
) || isHexByteArrayType(arg0
);