incubator-flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1297317 - in /incubator/flex/whiteboard/jmclean/validators/src: mx/formatters/ mx/formatters/PostCodeFormatter.as tests/PostCodeFormatterTests.as
Date Tue, 06 Mar 2012 01:30:49 GMT
Author: jmclean
Date: Tue Mar  6 01:30:48 2012
New Revision: 1297317

URL: http://svn.apache.org/viewvc?rev=1297317&view=rev
Log:
draft version of a mx style post code formatter

Added:
    incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/
    incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as
    incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as

Added: incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as?rev=1297317&view=auto
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as (added)
+++ incubator/flex/whiteboard/jmclean/validators/src/mx/formatters/PostCodeFormatter.as Tue
Mar  6 01:30:48 2012
@@ -0,0 +1,290 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package mx.formatters
+{
+
+import mx.events.ValidationResultEvent;
+import mx.managers.ISystemManager;
+import mx.managers.SystemManager;
+import mx.validators.PostCodeValidator;
+import mx.validators.ValidationResult;
+
+[ResourceBundle("formatters")]
+
+/**
+ *  The PostCodeFormatter class formats a valid number
+ *  based on a user-supplied <code>formatString</code> or
+ *  <code>formats</code> property.
+ *  
+ *  Postcode format consists of CC,N,A and spaces or hyphens
+ *  CC is country code (required for some postcodes)
+ *	N is a number 0-9
+ *  A is a letter A-Z or a-z
+ * 
+ *  For example "NNNN" is a four digit numeric postcode, "CCNNNN" is country code
+ *  followed by four digits and "AA NNNN" is two letters, followed by a space
+ *  followed by four digits.
+ * 
+ *  More than one format can be specified by setting the <code>formats</code>
+ *  property to an array of formats.
+ *  
+ *  <p>Spaces and hypens will be added if missing to format the postcode.</p>
+ *
+ *  <p>If an error occurs, an empty String is returned and a String that  
+ *  describes the error is saved to the <code>error</code> property.  
+ *  The <code>error</code> property can have one of the following values:</p>
+ *
+ *  <ul>
+ *    <li><code>"wrongFormat"</code> means the postcode has an invalid
format.</li>
+ *    <li><code>"wrongLength"</code> means the postcode is not a valid
length.</li>
+ *    <li><code>"invalidChar"</code> means the postcode contains an invalid
+ *    character.</li>
+ *  </ul>
+ *  
+ *  @mxml
+ *  
+ *  <p>The <code>&lt;mx:PostCodeFormatter&gt;</code> tag
+ *  inherits all of the tag attributes of its superclass,
+ *  and adds the following tag attributes:</p>
+ *  
+ *  <pre>
+ *  &lt;mx:PostCodeFormatter
+ *    formatString="NNNNN"
+ *    formats="['NNNNN', 'NNNNN-NNNN']"
+ *  />
+ *  </pre>
+ *  
+ *  
+ *  @langversion 3.0
+ *  @playerversion Flash 10.2
+ *  @productversion ApacheFlex 4.8
+ */
+public class PostCodeFormatter extends Formatter
+{
+    include "../core/Version.as";
+	
+	//--------------------------------------------------------------------------
+	//
+	//  Constructor
+	//
+	//--------------------------------------------------------------------------
+
+	/**
+	 *  Constructor.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function PostCodeFormatter()
+	{
+		super();
+	}
+
+	//--------------------------------------------------------------------------
+	//
+	//  Properties
+	//
+	//--------------------------------------------------------------------------
+
+	//----------------------------------
+	//  formats
+	//----------------------------------
+
+	/**
+	 *  @private
+	 *  An array of the postcode formats to check against.
+	 */
+	private var _formats:Array = [];
+	
+	
+	[Inspectable(category="General", defaultValue="null")]
+
+	/** 
+	 *  Format of postcode
+	 *  Format consists of CC,N,A and spaces or hyphens
+	 *  CC is country code (required for some postcodes)
+	 *	N is a number 0-9
+	 *  A is a letter A-Z or a-z
+	 *
+	 *  @default null
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get formatString():String
+	{
+		if (_formats && _formats.length == 1)
+		{
+			return _formats[0];
+		}
+		
+		return null;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set formatString(value:String):void
+	{
+		if (value)
+		{
+			_formats = [value];
+		}
+		else
+		{
+			_formats = []
+		}
+	}
+	
+	/** 
+	 *  Formats of postcode
+	 *  Sets an array of valid formats, use for locales
+	 *  where more than one format is required. eg en_UK
+	 *  See format for format.
+	 * 
+	 *
+	 *  @default []
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	public function get formats():Array
+	{
+		return _formats;
+	}
+	
+	/**
+	 *  @private
+	 */
+	public function set formats(value:Array):void
+	{
+		_formats = value;
+	}
+
+	//--------------------------------------------------------------------------
+	//
+	//  Overidden methods
+	//
+	//--------------------------------------------------------------------------
+
+    /**
+	 *  @private    
+     */
+	override protected function resourcesChanged():void
+	{
+		super.resourcesChanged();
+		
+		//TODO may not be required
+	}
+
+ 	/**
+	 *  Formats the value by using the specified format(s).
+	 *  If the value cannot be formatted this method returns an empty String 
+	 *  and write a description of the error to the <code>error</code> property.
+	 *
+	 *  @param value Value to format.
+	 *
+	 *  @return Formatted String. Empty if an error occurs. A description 
+	 *  of the error condition is written to the <code>error</code> property.
+	 *  
+	 *  @langversion 3.0
+	 *  @playerversion Flash 10.2
+	 *  @productversion ApacheFlex 4.8
+	 */
+	override public function format(value:Object):String
+	{
+		var postCode:String = value as String;
+		var formatted:String = "";
+		var spacers:String = " -";
+		var validator:PostCodeValidator = new PostCodeValidator();
+		var errors:Array;
+		
+		error = "";
+		
+		validator.formats = formats;
+		errors = PostCodeValidator.validatePostCode(validator, postCode, null);
+		
+		// Valid postcode no need for formatting
+		if (errors.length == 0)
+		{
+			return postCode?postCode:"";
+		}
+		
+		// Check and add missing (or convert) padding characters
+		for each (var format:String in formats)
+		{
+			var condensedFormat:String = format;
+			var condensedPostcode:String = postCode;
+			var char:String;
+			var length:int = spacers.length;
+			var condensedErrors:Array;
+			
+			for (var i:int = 0; i < length; i++)
+			{
+				char = spacers.charAt(i);
+				if (condensedFormat)
+				{
+					condensedFormat = condensedFormat.split(char).join("");
+				}
+				if (condensedPostcode)
+				{
+					condensedPostcode = condensedPostcode.split(char).join("");
+				}
+			}
+			
+			validator.format = condensedFormat;
+			
+			condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
+			
+			if (condensedErrors.length == 0)
+			{
+				var pos:int = 0;
+				
+				length = format.length;
+				
+				for (i = 0; i < length; i++)
+				{
+					char = format.charAt(i);
+					
+					if (spacers.indexOf(char) >= 0)
+					{
+						formatted += char;
+					}
+					else {
+						formatted += condensedPostcode.charAt(pos++);
+					}
+				}
+				break;
+			}
+		}
+		
+		if (!formatted && errors.length > 0)
+		{
+			error = (errors[0] as ValidationResult).errorCode;
+		}
+
+		return formatted;
+	}
+}
+
+}

Added: incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as
URL: http://svn.apache.org/viewvc/incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as?rev=1297317&view=auto
==============================================================================
--- incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as (added)
+++ incubator/flex/whiteboard/jmclean/validators/src/tests/PostCodeFormatterTests.as Tue Mar
 6 01:30:48 2012
@@ -0,0 +1,179 @@
+package tests
+{
+	import mx.formatters.PostCodeFormatter;
+	
+	import org.flexunit.asserts.assertTrue;
+
+	public class PostCodeFormatterTests
+	{		
+		public var formatter:PostCodeFormatter;
+		
+		[Before]
+		public function setUp():void
+		{
+			formatter = new PostCodeFormatter();
+		}
+		
+		[After]
+		public function tearDown():void
+		{
+			formatter = null;
+		}
+		
+		
+		[Test]
+		public function initial():void
+		{
+			assertTrue("FormatString is null", formatter.formatString == null);
+			assertTrue("Formats is empty array", formatter.formats.length == 0);
+		}
+		
+		[Test]
+		public function noRTEs():void
+		{
+			assertTrue("No RTE if format not set", formatter.format("2000") == "2000");	
+			assertTrue("No RTE if format passed emptry string", formatter.format("") == "");
+			assertTrue("No RTE if format passed null", formatter.format(null) == "");
+			
+			formatter.formatString = "NNNN";
+			assertTrue("No RTE if format not set", formatter.format("2000") == "2000");	
+			assertTrue("No RTE if format passed emptry string", formatter.format("") == "");
+			assertTrue("No RTE if format passed null", formatter.format(null) == "");
+		}
+		
+		private function invalidFormatError(error:String):void {
+			assertTrue("Has format error", error == "wrongFormat");
+		}
+		
+		private function wrongLengthError(error:String):void {
+			assertTrue("Has wrong length error", error == "wrongLength");
+		}
+		
+		private function invalidCharError(error:String):void {
+			assertTrue("Has invalid character error", error == "invalidChar");
+		}
+
+		
+		[Test]
+		public function numberFormats():void
+		{
+			formatter.formatString ="NNNN";
+			
+			assertTrue("Format is correct", formatter.format("1234") == "1234");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("12 34") == "1234");
+			assertTrue("No error", formatter.error == "");
+			
+			assertTrue("Format is incorrect", formatter.format("123456") == "");
+			wrongLengthError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("AA12") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("12%4") == "");
+			invalidCharError(formatter.error);
+		}
+		
+		[Test]
+		public function multipleFormats():void
+		{
+			formatter.formats = ["NNNN", "NNNNNN" ];
+			
+			assertTrue("Format is correct", formatter.format("1234") == "1234");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("123456") == "123456");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("12 34-") == "1234");
+			assertTrue("No error", formatter.error == "");
+			
+			assertTrue("Format is incorrect", formatter.format("12345") == "");
+			wrongLengthError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("1234 5") == "");
+			invalidFormatError(formatter.error); //TODO perhaps wrong length?
+			assertTrue("Format is incorrect", formatter.format("AA12") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("12%4") == "");
+			invalidCharError(formatter.error);
+		}
+		
+		[Test]
+		public function alphaNumericFormats():void
+		{
+			formatter.formatString ="AAAA";
+			
+			assertTrue("Format is correct", formatter.format("ABCD") == "ABCD");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("AB CD") == "ABCD");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("A-B-CD") == "ABCD");
+			assertTrue("No error", formatter.error == "");
+			
+			assertTrue("Format is incorrect", formatter.format("AB C") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("ABCDEF") == "");
+			wrongLengthError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("AB12") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("AB%D") == "");
+			invalidCharError(formatter.error);
+		}
+		
+		[Test]
+		public function paddingFormats():void
+		{
+			formatter.formatString ="AA-NN NN";
+			
+			assertTrue("Format is correct", formatter.format("AA1234") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("AA-1234") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("AA12 34") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("AA-12 34") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("AA 12-34") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is incorrect", formatter.format("AA-12-34") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is incorrect", formatter.format("A A--1 2-3-4") == "AA-12 34");
+			assertTrue("No error", formatter.error == "");
+			
+			assertTrue("Format is incorrect", formatter.format("AA-12345") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("AA12-3") == "");
+			wrongLengthError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("12-AA 34") == "");
+			invalidFormatError(formatter.error);
+			assertTrue("Format is incorrect", formatter.format("AA$12 34") == "");
+			invalidCharError(formatter.error);
+		}
+		
+		[Test]
+		public function unitedKindomPostcodes():void {
+			//TODO Double check formats correct
+			formatter.formats = ["AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA"];
+			
+			assertTrue("Format is correct", formatter.format("M11AA") == "M1 1AA");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("CR26XH") == "CR2 6XH");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("W1A 1HQ") == "W1A 1HQ");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("EC1A1BB") == "EC1A 1BB");
+			assertTrue("No error", formatter.error == "");
+		}
+		
+		[Test]
+		public function canadianPostcodes():void {
+			var results:Array;
+			var valid:Array = ["K0K 2T0", "V6Z 1T0", "B4V 2K4", "H0H 0H0"];
+			
+			formatter.formatString = "ANA NAN";
+			
+			assertTrue("Format is correct", formatter.format("K0K2T0") == "K0K 2T0");
+			assertTrue("No error", formatter.error == "");
+			assertTrue("Format is correct", formatter.format("V6Z-1T0") == "V6Z 1T0");
+			assertTrue("No error", formatter.error == "");
+		}
+		
+		
+	}
+}
\ No newline at end of file



Mime
View raw message