Fixes for oparent upgrade
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / PolicyUtils.java
index fa23f4a..67210c8 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * PolicyEngineUtils
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import java.util.StringTokenizer;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import javax.xml.XMLConstants;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
@@ -53,12 +54,12 @@ import com.google.gson.JsonSyntaxException;
 
 public class PolicyUtils {
     private static final Logger LOGGER = FlexLogger.getLogger(PolicyUtils.class);
-    
+    public static final String CATCH_EXCEPTION = "PE500: An exception was caught.";  
     public static final String EMAIL_PATTERN =
             "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
             + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
     private static final String PACKAGE_ERROR = "mismatched input '{' expecting one of the following tokens: '[package";
-    private static final String SUCCESS = "success";
+    public static final String SUCCESS = "success";
     
     private PolicyUtils(){
         // Private Constructor
@@ -156,6 +157,21 @@ public class PolicyUtils {
         return SUCCESS;   
     } 
     
+    /**
+     * Validate a field (accepts Dash) if it contains unacceptable policy input and return "success" if good. 
+     * 
+     * @param field
+     * @return
+     */
+    public static String  policySpecialCharWithDashValidator(String field){
+        String error;
+        if ("".equals(field) || !field.matches("^[a-zA-Z0-9_-]*$")) {
+            error = "The Value in Required Field will allow only '{0-9}, {a-z}, {A-Z}, _, -' following set of Combinations";
+            return error;
+        }
+        return SUCCESS;   
+    } 
+    
     /**
      * Validate the XACML description tag and return "success" if good. 
      * 
@@ -180,7 +196,7 @@ public class PolicyUtils {
      * @return
      */
     public static boolean containsNonAsciiEmptyChars(String value) {
-        return (value == null|| value.contains(" ") || "".equals(value.trim())|| !CharMatcher.ASCII.matchesAllOf((CharSequence) value))? true:false;
+        return (value == null || value.contains(" ") || "".equals(value.trim()) || !CharMatcher.ascii().matchesAllOf((CharSequence) value)) ? true : false;
     }
     
     /**
@@ -194,7 +210,9 @@ public class PolicyUtils {
             return false;
         }
         for (char c : number.toCharArray()){
-            if (!Character.isDigit(c)) return false;
+            if (!Character.isDigit(c)) {
+               return false;
+            }
         }
         return true;
     }
@@ -276,10 +294,15 @@ public class PolicyUtils {
      * @return
      */
     public static boolean isXMLValid(String data) {
+       if(data == null || data.isEmpty()){
+               return false;
+        }
         SAXParserFactory factory = SAXParserFactory.newInstance();
         factory.setValidating(false);
         factory.setNamespaceAware(true);
-        try {
+        
+        try {    
+                       factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);               
             SAXParser parser = factory.newSAXParser();
             XMLReader reader = parser.getXMLReader();
             reader.setErrorHandler(new XMLErrorHandler());