You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2014/10/29 06:10:27 UTC

[6/8] git commit: o Replaced tabs with spaces o Pre-compile the pattern, which is a constant anyway, to save the time it takes to do it everytime the method safeText() is called.

o Replaced tabs with spaces
o Pre-compile the pattern, which is a constant anyway, to save the time
it takes to do it everytime the method safeText() is called.

Project: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/commit/11a7d59c
Tree: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/tree/11a7d59c
Diff: http://git-wip-us.apache.org/repos/asf/directory-fortress-core/diff/11a7d59c

Branch: refs/heads/master
Commit: 11a7d59c5a51010fb7d1e06e09b169475a065cca
Parents: c68593c
Author: Emmanuel Lécharny <el...@symas.com>
Authored: Wed Oct 29 06:08:01 2014 +0100
Committer: Emmanuel Lécharny <el...@symas.com>
Committed: Wed Oct 29 06:08:01 2014 +0100

----------------------------------------------------------------------
 .../fortress/core/util/attr/RegExUtil.java      | 64 ++++++++++++--------
 1 file changed, 38 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-fortress-core/blob/11a7d59c/src/main/java/org/apache/directory/fortress/core/util/attr/RegExUtil.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/directory/fortress/core/util/attr/RegExUtil.java b/src/main/java/org/apache/directory/fortress/core/util/attr/RegExUtil.java
index 08d1f80..9b92a60 100755
--- a/src/main/java/org/apache/directory/fortress/core/util/attr/RegExUtil.java
+++ b/src/main/java/org/apache/directory/fortress/core/util/attr/RegExUtil.java
@@ -19,16 +19,19 @@
  */
 package org.apache.directory.fortress.core.util.attr;
 
-import org.apache.directory.fortress.core.GlobalErrIds;
 
+import org.apache.directory.api.util.Strings;
+import org.apache.directory.fortress.core.GlobalErrIds;
 import org.apache.directory.fortress.core.GlobalIds;
 import org.apache.directory.fortress.core.ValidationException;
 import org.apache.directory.fortress.core.cfg.Config;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+
 /**
  *  Regular expression utilities to perform data validations on Fortress attributes.  These utils use the standard
  * java regular expression library.
@@ -39,30 +42,39 @@ class RegExUtil
 {
     private static final String CLS_NM = RegExUtil.class.getName();
     private static final Logger LOG = LoggerFactory.getLogger( CLS_NM );
-	private static final String safeTextPatternStr = Config.getProperty( GlobalIds.REG_EX_SAFE_TEXT );
+    private static final String SAFE_TEXT_PATTERN_STRING = Config.getProperty( GlobalIds.REG_EX_SAFE_TEXT );
+    private static Pattern SAFE_TEXT_PATTERN;
+    
+    static 
+    {
+        if ( ( SAFE_TEXT_PATTERN_STRING != null ) && ( SAFE_TEXT_PATTERN_STRING.length() != 0 ) )
+        {
+            SAFE_TEXT_PATTERN = Pattern.compile( SAFE_TEXT_PATTERN_STRING );
+        }
+    }
+
 
-	/**
-	 *  Perform safe text validation on character string.
-	 *
-	 * @param  value Contains the string to check.
-	 * @exception org.apache.directory.fortress.core.ValidationException  In the event the data validation fails.
-	 */
-	public static void safeText(String value)
-		throws ValidationException
-	{
-		if (safeTextPatternStr == null || safeTextPatternStr.compareTo("") == 0)
-		{
-			LOG.debug("safeText can't find safeText regular expression pattern.  Check your Fortress cfg");
-		}
-		else
-		{
-			Pattern safeTextPattern = Pattern.compile(safeTextPatternStr);
-			Matcher safeTextMatcher = safeTextPattern.matcher(value);
-			if (!safeTextMatcher.find())
-			{
-				String error = "safeText has detected invalid value [" + value + "]";
-				throw new ValidationException(GlobalErrIds.CONST_INVLD_TEXT, error);
-			}
-		}
-	}
+    /**
+     *  Perform safe text validation on character string.
+     *
+     * @param  value Contains the string to check.
+     * @exception org.apache.directory.fortress.core.ValidationException  In the event the data validation fails.
+     */
+    public static void safeText( String value ) throws ValidationException
+    {
+        if ( Strings.isEmpty( SAFE_TEXT_PATTERN_STRING ) )
+        {
+            LOG.debug( "safeText can't find safeText regular expression pattern.  Check your Fortress cfg" );
+        }
+        else
+        {
+            Matcher safeTextMatcher = SAFE_TEXT_PATTERN.matcher( value );
+            
+            if ( !safeTextMatcher.find() )
+            {
+                String error = "safeText has detected invalid value [" + value + "]";
+                throw new ValidationException( GlobalErrIds.CONST_INVLD_TEXT, error );
+            }
+        }
+    }
 }
\ No newline at end of file