dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #37702
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19247: Use regex for hex color validity check. Removes unnecessary transitive dependency on JDOM
------------------------------------------------------------
revno: 19247
committer: Halvdan Hoem Grelland <halvdanhg@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-06-02 13:11:54 +0200
message:
Use regex for hex color validity check. Removes unnecessary transitive dependency on JDOM
modified:
dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java
dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java
--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk
Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2015-02-13 13:17:36 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ValidationUtils.java 2015-06-02 11:11:54 +0000
@@ -33,7 +33,6 @@
import org.apache.commons.validator.routines.UrlValidator;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.datavalue.DataValue;
-import org.jdom.Verifier;
import java.awt.geom.Point2D;
import java.util.Locale;
@@ -51,6 +50,7 @@
private static Pattern POINT_PATTERN = Pattern.compile( "\\[(.+),\\s?(.+)\\]" );
private static Pattern DIGIT_PATTERN = Pattern.compile( ".*\\d.*" );
private static Pattern UPPERCASE_PATTERN = Pattern.compile( ".*[A-Z].*" );
+ private static Pattern HEX_COLOR_PATTERN = Pattern.compile( "^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$" );
private static int VALUE_MAX_LENGTH = 50000;
private static int LONG_MAX = 180;
@@ -398,7 +398,7 @@
return null;
}
-
+
/**
* Checks to see if given parameter is a valid hex color string (#xxx and #xxxxxx, xxx, xxxxxx).
*
@@ -407,29 +407,6 @@
*/
public static boolean isValidHexColor( String value )
{
- if ( value == null )
- {
- return false;
- }
-
- if ( value.startsWith( "#" ) )
- {
- value = value.substring( 1 );
- }
-
- if ( !(value.length() == 3 || value.length() == 6) )
- {
- return false;
- }
-
- for ( char aChar : value.toCharArray() )
- {
- if ( !Verifier.isHexDigit( aChar ) )
- {
- return false;
- }
- }
-
- return true;
+ return value != null && HEX_COLOR_PATTERN.matcher( value ).matches();
}
}
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2015-01-17 07:41:26 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ValidationUtilsTest.java 2015-06-02 11:11:54 +0000
@@ -33,6 +33,7 @@
import static org.hisp.dhis.system.util.ValidationUtils.emailIsValid;
import static org.hisp.dhis.system.util.ValidationUtils.getLatitude;
import static org.hisp.dhis.system.util.ValidationUtils.getLongitude;
+import static org.hisp.dhis.system.util.ValidationUtils.isValidHexColor;
import static org.hisp.dhis.system.util.ValidationUtils.passwordIsValid;
import static org.hisp.dhis.system.util.ValidationUtils.dataValueIsZeroAndInsignificant;
import static org.junit.Assert.assertEquals;
@@ -175,4 +176,18 @@
assertNotNull( dataValueIsValid( "2012304-01", de ) );
assertNotNull( dataValueIsValid( "Date", de ) );
}
+
+ @Test
+ public void testIsValidHexColor() {
+ assertFalse( isValidHexColor( "abcpqr" ) );
+ assertFalse( isValidHexColor( "#qwerty" ) );
+ assertFalse( isValidHexColor( "FFAB#O") );
+
+ assertTrue( isValidHexColor( "#FF0" ) );
+ assertTrue( isValidHexColor( "#FF0000" ) );
+ assertTrue( isValidHexColor( "FFFFFF" ) );
+ assertTrue( isValidHexColor( "ffAAb4" ) );
+ assertTrue( isValidHexColor( "#4a6" ) );
+ assertTrue( isValidHexColor ( "abc" ) );
+ }
}