From a58e476f038345be859276db45a50a3f3bdcb57e Mon Sep 17 00:00:00 2001 From: maopengzhang Date: Wed, 30 Aug 2017 18:29:10 +0800 Subject: Resolve the Nullpoint issue add protect for null point issue Change-Id: Ic27231ca7d82158cd103af91f229c85e5edfcdc2 Issue-ID: SDC-261 Signed-off-by: maopengzhang --- .../sdc/toscaparser/api/utils/ValidateUtils.java | 86 +++++++++++++--------- 1 file changed, 51 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/main/java/org/openecomp/sdc/toscaparser/api/utils/ValidateUtils.java b/src/main/java/org/openecomp/sdc/toscaparser/api/utils/ValidateUtils.java index 291316f..9909685 100644 --- a/src/main/java/org/openecomp/sdc/toscaparser/api/utils/ValidateUtils.java +++ b/src/main/java/org/openecomp/sdc/toscaparser/api/utils/ValidateUtils.java @@ -33,45 +33,55 @@ public class ValidateUtils { } public static Object validateNumeric(Object value) { - if(!(value instanceof Number)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not a numeric",value.toString())); + if(value != null) { + if (!(value instanceof Number)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not a numeric", value.toString())); + } } return value; } public static Object validateInteger(Object value) { - if(!(value instanceof Integer)) { - // allow "true" and "false" - if(value instanceof Boolean) { - return (Boolean)value ? 1 : 0; + if(value != null) { + if (!(value instanceof Integer)) { + // allow "true" and "false" + if (value instanceof Boolean) { + return (Boolean) value ? 1 : 0; + } + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not an integer", value.toString())); } - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not an integer",value.toString())); } return value; } public static Object validateFloat(Object value) { - if(!(value instanceof Float || value instanceof Double)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not a float",value.toString())); + if(value != null) { + if (!(value instanceof Float || value instanceof Double)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not a float", value.toString())); + } } return value; } public static Object validateString(Object value) { - if(!(value instanceof String)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \'%s\' is not a string",value.toString())); + if(value != null) { + if (!(value instanceof String)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \'%s\' is not a string", value.toString())); + } } return value; } public static Object validateList(Object value) { - if(!(value instanceof ArrayList)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not a list",value.toString())); + if(value != null) { + if (!(value instanceof ArrayList)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not a list", value.toString())); + } } return value; } @@ -209,25 +219,29 @@ public class ValidateUtils { } public static Object validateMap(Object ob) { - if(!(ob instanceof LinkedHashMap)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError\"%s\" is not a map.",ob.toString())); + if(ob != null) { + if (!(ob instanceof LinkedHashMap)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError\"%s\" is not a map.", ob.toString())); + } } return ob; } public static Object validateBoolean(Object value) { - if(value instanceof Boolean) { - return value; - } - if(value instanceof String) { - String normalized = ((String)value).toLowerCase(); - if(normalized.equals("true") || normalized.equals("false")) { - return normalized.equals("true"); + if(value != null) { + if (value instanceof Boolean) { + return value; + } + if (value instanceof String) { + String normalized = ((String) value).toLowerCase(); + if (normalized.equals("true") || normalized.equals("false")) { + return normalized.equals("true"); + } } + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not a boolean", value.toString())); } - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not a boolean",value.toString())); return value; } @@ -248,11 +262,13 @@ public class ValidateUtils { */ // timestamps are loaded as Date objects by the YAML parser - if(!(value instanceof Date)) { - ThreadLocalsHolder.getCollector().appendException(String.format( - "ValueError: \"%s\" is not a valid timestamp", - value.toString())); - + if(value != null) { + if (!(value instanceof Date)) { + ThreadLocalsHolder.getCollector().appendException(String.format( + "ValueError: \"%s\" is not a valid timestamp", + value.toString())); + + } } return value; } -- cgit 1.2.3-korg