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 long 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 BeginNumber The left boundary of the scope
91 @param EndNumber 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 isLongInt(String strNumber
, long BeginNumber
, long EndNumber
) throws Exception
{
99 //Check if the input data is int first
101 if (!isInt(strNumber
)) {
105 //And then check if the data is between the scope
108 Long intTemp
= new Long(strNumber
);
109 if ((intTemp
.longValue() < BeginNumber
) || (intTemp
.longValue() > EndNumber
)) {
113 catch (Exception e
) {
120 Check if the input data is int and it is in the valid scope
121 The scope is provided by String
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
127 @retval true - The input is Int and in the scope;
128 @retval false - The input is not Int or not in the scope
131 public static boolean isInt(String strNumber
, String strBeginNumber
, String strEndNumber
) {
133 //Check if all input data are int
135 if (!isInt(strNumber
)) {
138 if (!isInt(strBeginNumber
)) {
141 if (!isInt(strEndNumber
)) {
145 //And then check if the data is between the scope
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())) {
157 Use regex to check if the input data is in valid format
159 @param strPattern The input regex
160 @param strMatcher The input data need be checked
162 @retval true - The data matches the regex
163 @retval false - The data doesn't match the regex
166 public static boolean isMatch(String strPattern
, String strMatcher
) {
167 Pattern pattern
= Pattern
.compile(strPattern
);
168 Matcher matcher
= pattern
.matcher(strMatcher
);
170 return matcher
.find();
174 // The below is used to check common customized data types
178 Check if the input data is UiNameType
180 @param arg0 The input string need be checked
182 @retval true - The input is UiNameType
183 @retval false - The input is not UiNameType
186 public static boolean isUiNameType(String arg0
) {
187 if (arg0
.length() < 1) {
190 return isMatch("[^ ].*", arg0
);
194 Check if the input data is GuidType2
196 @param arg0 The input string need be checked
198 @retval true - The input is GuidType2
199 @retval false - The input is not GuidType2
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
);
207 Check if the input data is Guid
209 @param strGuid The input string need be checked
211 @retval true - The input is Guid
212 @retval false - The input is not Guid
215 public static boolean isGuid(String arg0
) {
216 return isGuidType2(arg0
);
220 Check if the input data is Version
222 @param arg0 The input string need be checked
224 @retval true - The input is Version
225 @retval false - The input is not Version
228 public static boolean isVersionDataType(String arg0
) {
229 return isMatch("\\d+(\\.\\d+)*", arg0
);
233 Check if the input data is Sentence
235 @param strSentence The input string need be checked
237 @retval true - The input is Sentence
238 @retval false - The input is not Sentence
241 public static boolean isSentence(String arg0
) {
242 return isMatch("(\\w+\\W*)+( )+(\\W*\\w*\\W*\\s*)*", arg0
);
246 Check if the input data is FileNameConventio
248 @param strSentence The input string need be checked
250 @retval true - The input is FileNameConventio
251 @retval false - The input is not FileNameConventio
254 public static boolean isFileNameConvention(String arg0
) {
255 return isMatch("[a-zA-Z](\\.?[-_a-zA-Z0-9]+)*", arg0
);
259 Check if the input data is KeywordType
261 @param strSentence The input string need be checked
263 @retval true - The input is KeywordType
264 @retval false - The input is not KeywordType
267 public static boolean isKeywordType(String arg0
) {
268 return isMatch("[a-zA-Z]+(_*[a-zA-Z0-9]*)*", arg0
);
272 Check if the input data is FeatureFlagExpressionType
274 @param strSentence The input string need be checked
276 @retval true - The input is FeatureFlagExpressionType
277 @retval false - The input is not FeatureFlagExpressionType
280 public static boolean isFeatureFlagExpressionType(String arg0
) {
281 return (arg0
.length() >= 1);
285 Check if the input data is FeatureFlag
287 @param strSentence The input string need be checked
289 @retval true - The input is FeatureFlag
290 @retval false - The input is not FeatureFlag
293 public static boolean isFeatureFlag(String arg0
) {
294 return isFeatureFlagExpressionType(arg0
);
298 Check if the input data is PathAndFilename
300 @param strSentence The input string need be checked
302 @retval true - The input is PathAndFilename
303 @retval false - The input is not PathAndFilename
306 public static boolean isPathAndFilename(String arg0
) {
307 return !arg0
.equals("");
311 Check if the input data is ToolsNameConvention
313 @param strSentence The input string need be checked
315 @retval true - The input is ToolsNameConvention
316 @retval false - The input is not ToolsNameConvention
319 public static boolean isToolsNameConvention(String arg0
) {
320 return isMatch("[a-zA-Z][a-zA-Z0-9]*", arg0
);
324 Check if the input data is C_NameType
326 @param strSentence The input string need be checked
328 @retval true - The input is C_NameType
329 @retval false - The input is not C_NameType
332 public static boolean isC_NameType(String arg0
) {
333 return isMatch("(_)*[a-zA-Z]+((_)*[a-zA-Z0-9]*)*", arg0
);
337 Check if the input data is HexWordArrayType
339 @param strSentence The input string need be checked
341 @retval true - The input is HexWordArrayType
342 @retval false - The input is not HexWordArrayType
345 public static boolean isHexWordArrayType(String arg0
) {
346 return arg0
.length() >=6 && isMatch("((\\s)*0x([a-fA-F0-9]){4}(,)?(\\s)*)+", arg0
);
350 Check if the input data is Hex64BitDataType
352 @param strSentence The input string need be checked
354 @retval true - The input is Hex64BitDataType
355 @retval false - The input is not Hex64BitDataType
358 public static boolean isHex64BitDataType(String arg0
) {
359 return isMatch("(0x)?[a-fA-F0-9]{1,16}", arg0
);
363 Check if the input data is UnicodeString
365 @param strSentence The input string need be checked
367 @retval true - The input is UnicodeString
368 @retval false - The input is not UnicodeString
371 public static boolean isUnicodeString(String arg0
) {
372 return arg0
.length() >= 3 && isMatch("(\\s)*L(\\:)?\"[^\"]*\"(\\s)*", arg0
);
376 Check if the input data is HexByteArrayType
378 @param strSentence The input string need be checked
380 @retval true - The input is HexByteArrayType
381 @retval false - The input is not HexByteArrayType
384 public static boolean isHexByteArrayType(String arg0
) {
385 return arg0
.length() >= 4 && isMatch("((\\s)*0x([a-fA-F0-9]){2}(,)?(\\s)*)+", arg0
);
390 Check if the input data is DateType
392 @param strDateType The input string need be checked
394 @retval true - The input is DateType
395 @retval false - The input is not DateType
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
);
403 Check if the input data is DosPath
405 @param strDosPath The input string need be checked
407 @retval true - The input is DosPath
408 @retval false - The input is not DosPath
411 public static boolean isDosPath(String strDosPath
) {
412 return isMatch("([a-zA-Z]:\\\\)?(((\\\\?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*", strDosPath
);
416 Check if the input data is UnixPath
418 @param strUnixPath The input string need be checked
420 @retval true - The input is UnixPath
421 @retval false - The input is not UnixPath
424 public static boolean isUnixPath(String strUnixPath
) {
425 return isMatch("(\\/)?(((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*", strUnixPath
);
429 Check if the input data is DirectoryNamingConvention
431 @param strDirectoryNamingConvention The input string need be checked
433 @retval true - The input is DirectoryNamingConvention
434 @retval false - The input is not DirectoryNamingConvention
437 public static boolean isDirectoryNamingConvention(String strDirectoryNamingConvention
) {
438 return (isDosPath(strDirectoryNamingConvention
) || isUnixPath(strDirectoryNamingConvention
));
442 Check if the input data is HexDoubleWordDataType
444 @param strHexDoubleWordDataType The input string need be checked
446 @retval true - The input is HexDoubleWordDataType
447 @retval false - The input is not HexDoubleWordDataType
450 public static boolean isHexDoubleWordDataType(String strHexDoubleWordDataType
) {
451 return isMatch("0x[a-fA-F0-9]{1,8}", strHexDoubleWordDataType
);
455 Check if the input data is V1
457 @param strV1 The input string need be checked
459 @retval true - The input is V1
460 @retval false - The input is not V1
463 public static boolean isV1(String strV1
) {
464 return isMatch("((%[A-Z](_*[A-Z0-9]*)*%)+((((\\\\)?_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\\\)?)*)", strV1
);
468 Check if the input data is V2
470 @param strV2 The input string need be checked
472 @retval true - The input is V2
473 @retval false - The input is not V2
476 public static boolean isV2(String strV2
) {
478 "(($[A-Z](_*[A-Z0-9]*)*)+||($\\([A-Z](_*[A-Z0-9]*)*\\))+||($\\{[A-Z](_*[A-Z0-9]*)*\\})+)+(\\/)?(((((_*-*.*[a-zA-Z0-9]*)*(_*-*.*[a-zA-Z0-9])*)+(\\/)?)*)*)",
483 Check if the input data is VariableConvention
485 @param strVariableConvention The input string need be checked
487 @retval true - The input is VariableConvention
488 @retval false - The input is not VariableConvention
491 public static boolean isVariableConvention(String strVariableConvention
) {
492 return (isV1(strVariableConvention
) || isV2(strVariableConvention
));
496 Check if the input data is UCName
498 @param strUCName The input string need be checked
500 @retval true - The input is UCName
501 @retval false - The input is not UCName
504 public static boolean isUCName(String strUCName
) {
505 return isMatch("[A-Z]+(_*[A-Z0-9]*( )*)*", strUCName
);
509 Check if the input data is HexByteDataType
511 @param strHex64BitDataType The input string need be checked
513 @retval true - The input is HexByteDataType
514 @retval false - The input is not HexByteDataType
517 public static boolean isHexByteDataType(String strHex64BitDataType
) {
518 return isMatch("(0x)?[a-fA-F0-9]{1,2}", strHex64BitDataType
);
522 Check if the input data is HexWordDataType
524 @param strHexWordDataType The input string need be checked
526 @retval true - The input is HexWordDataType
527 @retval false - The input is not HexWordDataType
530 public static boolean isHexWordDataType(String strHexWordDataType
) {
531 return isMatch("0x[a-fA-F0-9]{1,4}", strHexWordDataType
);
535 Check if the input data is OverrideID
537 @param strOverrideID The input string need be checked
539 @retval true - The input is OverrideID
540 @retval false - The input is not OverrideID
543 public static boolean isOverrideID(String strOverrideID
) {
544 return isInt(strOverrideID
);
548 Check if the input data is Supported Architectures
550 @param strSupportedArchitectures
551 @retval true - The input is Supported Architectures
552 @retval false - The input isn't Supported Architectures
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
);
561 //The below is used to check msaheader data type
565 Check if the input data is BaseName
567 @param strBaseName The input string need be checked
569 @retval true - The input is BaseName
570 @retval false - The input is not BaseName
573 public static boolean isBaseName(String arg0
) {
574 return isUiNameType(arg0
);
578 Check if the input data is Version
580 @param arg0 The input string need be checked
582 @retval true - The input is Version
583 @retval false - The input is not Version
586 public static boolean isVersion(String arg0
) {
587 return isVersionDataType(arg0
);
591 Check if the input data is Abstract
593 @param strAbstract The input string need be checked
595 @retval true - The input is Abstract
596 @retval false - The input is not Abstract
599 public static boolean isAbstract(String arg0
) {
600 return isSentence(arg0
);
604 Check if the input data is Copyright
606 @param strCopyright The input string need be checked
608 @retval true - The input is Copyright
609 @retval false - The input is not Copyright
612 public static boolean isCopyright(String arg0
) {
613 return !arg0
.equals("");
617 Check if the input data is Specification
619 @param strCopyright The input string need be checked
621 @retval true - The input is Specification
622 @retval false - The input is not Specification
625 public static boolean isSpecification(String arg0
) {
626 return isSentence(arg0
);
630 // The below is used to check ModuleDefinitions data types
633 Check if the input data is OutputFileBasename
635 @param strCopyright The input string need be checked
637 @retval true - The input is OutputFileBasename
638 @retval false - The input is not OutputFileBasename
641 public static boolean isOutputFileBasename(String arg0
) {
642 return isFileNameConvention(arg0
);
646 // The below is used to check LibraryClass data types
649 Check if the input data is LibraryClass
651 @param strCopyright The input string need be checked
653 @retval true - The input is LibraryClass
654 @retval false - The input is not LibraryClass
657 public static boolean isLibraryClass(String arg0
) {
658 return isKeywordType(arg0
);
662 Check if the input data is RecommendedInstanceVersion
664 @param strCopyright The input string need be checked
666 @retval true - The input is RecommendedInstanceVersion
667 @retval false - The input is not RecommendedInstanceVersion
670 public static boolean isRecommendedInstanceVersion(String arg0
) {
671 return isVersionDataType(arg0
);
675 // The below is used to check sourcefiles data types
679 Check if the input data is Filename
681 @param strPath The input string need be checked
683 @retval true - The input is Filename
684 @retval false - The input is not Filename
687 public static boolean isFilename(String arg0
) {
688 return isPathAndFilename(arg0
);
692 Check if the input data is TagName
694 @param strPath The input string need be checked
696 @retval true - The input is TagName
697 @retval false - The input is not TagName
700 public static boolean isTagName(String arg0
) {
701 return isToolsNameConvention(arg0
);
705 Check if the input data is ToolCode
707 @param strPath The input string need be checked
709 @retval true - The input is ToolCode
710 @retval false - The input is not ToolCode
713 public static boolean isToolCode(String arg0
) {
714 return isToolsNameConvention(arg0
);
718 Check if the input data is ToolChainFamily
720 @param strPath The input string need be checked
722 @retval true - The input is ToolChainFamily
723 @retval false - The input is not ToolChainFamily
726 public static boolean isToolChainFamily(String arg0
) {
727 return isToolsNameConvention(arg0
);
731 // The below is used to check pcdcoded data types
734 Check if the input data is DefaultValueType
736 @param strPath The input string need be checked
738 @retval true - The input is DefaultValueType
739 @retval false - The input is not DefaultValueType
742 public static boolean isDefaultValueType(String arg0
) {
743 return isHex64BitDataType(arg0
) || isUnicodeString(arg0
) || isHexByteArrayType(arg0
);