flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcl...@apache.org
Subject svn commit: r1300466 - in /incubator/flex/branches/patches: ./ projects/apache/ projects/apache/asdocs/ projects/apache/bundles/ projects/apache/bundles/en_AU/ projects/apache/bundles/en_US/ projects/apache/src/ projects/apache/src/core/ projects/apach...
Date Wed, 14 Mar 2012 06:24:20 GMT
Author: jmclean
Date: Wed Mar 14 06:24:19 2012
New Revision: 1300466

URL: http://svn.apache.org/viewvc?rev=1300466&view=rev
Log:
added apache target to build script

Added:
    incubator/flex/branches/patches/projects/apache/
    incubator/flex/branches/patches/projects/apache/asdocs/
    incubator/flex/branches/patches/projects/apache/bundles/
    incubator/flex/branches/patches/projects/apache/bundles/en_AU/
    incubator/flex/branches/patches/projects/apache/bundles/en_AU/validators.properties
    incubator/flex/branches/patches/projects/apache/bundles/en_US/
    incubator/flex/branches/patches/projects/apache/bundles/en_US/validators.properties
    incubator/flex/branches/patches/projects/apache/manifest.xml
    incubator/flex/branches/patches/projects/apache/src/
    incubator/flex/branches/patches/projects/apache/src/ApacheClasses.as
    incubator/flex/branches/patches/projects/apache/src/core/
    incubator/flex/branches/patches/projects/apache/src/core/Version.as
    incubator/flex/branches/patches/projects/apache/src/org/
    incubator/flex/branches/patches/projects/apache/src/org/apache/
    incubator/flex/branches/patches/projects/apache/src/org/apache/flex/
    incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/
    incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/PostCodeFormatter.as
    incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/
    incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/PostCodeValidator.as
Modified:
    incubator/flex/branches/patches/flex-config.template.xml

Modified: incubator/flex/branches/patches/flex-config.template.xml
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/flex-config.template.xml?rev=1300466&r1=1300465&r2=1300466&view=diff
==============================================================================
--- incubator/flex/branches/patches/flex-config.template.xml (original)
+++ incubator/flex/branches/patches/flex-config.template.xml Wed Mar 14 06:24:19 2012
@@ -101,6 +101,10 @@
             <uri>http://www.adobe.com/2006/mxml</uri>
             <manifest>mxml-manifest.xml</manifest>
          </namespace>
+         <namespace>
+            <uri>http://www.apache.org/flex</uri>
+            <manifest>apache-manifest.xml</manifest>
+         </namespace>
       </namespaces>
 
       <!-- Enable post-link SWF optimization. -->

Added: incubator/flex/branches/patches/projects/apache/bundles/en_AU/validators.properties
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/bundles/en_AU/validators.properties?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/bundles/en_AU/validators.properties (added)
+++ incubator/flex/branches/patches/projects/apache/bundles/en_AU/validators.properties Wed Mar 14 06:24:19 2012
@@ -0,0 +1,24 @@
+################################################################################
+##
+##  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.
+##
+################################################################################
+
+#PostCodeValidator
+invalidCharPostcodeError=The postcode contains invalid characters.
+wrongLengthPostcodeError=The postcode is the wrong length.
+wrongFormatPostcodeError=The postcode is the wrong format.
+incorrectFormatPostcodeError=The postcode format string is incorrect.
\ No newline at end of file

Added: incubator/flex/branches/patches/projects/apache/bundles/en_US/validators.properties
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/bundles/en_US/validators.properties?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/bundles/en_US/validators.properties (added)
+++ incubator/flex/branches/patches/projects/apache/bundles/en_US/validators.properties Wed Mar 14 06:24:19 2012
@@ -0,0 +1,24 @@
+################################################################################
+##
+##  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.
+##
+################################################################################
+
+#PostCodeValidator
+invalidCharPostcodeError=The ZIP code contains invalid characters.
+wrongLengthPostcodeError=The ZIP code is the wrong length.
+wrongFormatPostcodeError=The ZIP code is the wrong format.
+incorrectFormatPostcodeError=The ZIP code format string is incorrect.
\ No newline at end of file

Added: incubator/flex/branches/patches/projects/apache/manifest.xml
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/manifest.xml?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/manifest.xml (added)
+++ incubator/flex/branches/patches/projects/apache/manifest.xml Wed Mar 14 06:24:19 2012
@@ -0,0 +1,31 @@
+<?xml version="1.0"?>
+<!--
+
+  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.
+
+-->
+
+<!--
+
+    Apache Components
+
+-->
+<componentPackage>
+
+    <component id="PostCodeFormatter" class="org.apache.flex.formatters.PostCodeFormatter"/>
+    <component id="PostCodeValidator" class="org.apache.flex.validators.PostCodeValidator"/>
+
+</componentPackage>

Added: incubator/flex/branches/patches/projects/apache/src/ApacheClasses.as
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/src/ApacheClasses.as?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/src/ApacheClasses.as (added)
+++ incubator/flex/branches/patches/projects/apache/src/ApacheClasses.as Wed Mar 14 06:24:19 2012
@@ -0,0 +1,35 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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
+{
+	
+	internal class ApacheClasses
+	{
+		
+		/**
+		 *  @private
+		 *  This class is used to link additional classes into apache.swc
+		 *  beyond those that are found by dependecy analysis starting
+		 *  from the classes specified in manifest.xml.
+		 */
+
+	}
+	
+}

Added: incubator/flex/branches/patches/projects/apache/src/core/Version.as
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/src/core/Version.as?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/src/core/Version.as (added)
+++ incubator/flex/branches/patches/projects/apache/src/core/Version.as Wed Mar 14 06:24:19 2012
@@ -0,0 +1,26 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+import mx.core.mx_internal;
+
+/**
+ *  @private
+ *  Version string for this class.
+ */
+mx_internal static const VERSION:String = "4.8.0.0";

Added: incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/PostCodeFormatter.as
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/PostCodeFormatter.as?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/PostCodeFormatter.as (added)
+++ incubator/flex/branches/patches/projects/apache/src/org/apache/flex/formatters/PostCodeFormatter.as Wed Mar 14 06:24:19 2012
@@ -0,0 +1,299 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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 org.apache.flex.formatters
+{
+import org.apache.flex.validators.PostCodeValidator;
+	
+import mx.events.ValidationResultEvent;
+import mx.formatters.Formatter;
+import mx.managers.ISystemManager;
+import mx.managers.SystemManager;
+import mx.validators.ValidationResult;
+
+[ResourceBundle("validators")]
+
+/**
+ *  The PostCodeFormatter class formats a valid postcode
+ *  based on a user set <code>formatString</code> or
+ *  <code>formats</code> property.
+ *
+ *  <p>Postcode formats consists of the letters C, N, A and spaces or hyphens
+ *  <ul>
+ *  <li>CC or C is the country code (required for some postcodes).</li>
+ *	<li>N is a number 0-9.</li>
+ *  <li>A is a letter A-Z or a-z,</li>
+ *  </ul></p>
+ *
+ *  <p>Country codes one be one or two digits.</p>
+ *
+ *  <p>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 then
+ *  followed by four digits.</p>
+ *
+ *  <p>More than one format can be specified by setting the <code>formats</code>
+ *  property to an array of format strings.</p>
+ *
+ *  <p>Spaces and hypens will be added if missing to format the postcode correctly.</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:
+ *  <ul>
+ *    <li><code>"invalidFormat"</code> means the format constants an invalid
+ *    character.</li>
+ *    <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></p>
+ *
+ *  <p>Fullwidth numbers and letters are supported in postcodes by ignoring character
+ *  width via the <code>flash.globalization.Collator</code> <code>equals</code> method.</p>
+ *
+ *  @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>
+ *
+ *  @see mx.validators.PostCodeValidator
+ * 
+ *  @includeExample PostCodeValidationExample.mxml
+ *
+ *  @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 string to format the postcode in.
+     *
+     *  <p>The format string consists of the letters C, N, A and spaces
+     *  or hyphens:
+     *  <ul>
+     *  <li>CC or C is country code (required for some postcodes).</li>
+     *	<li>N is a number 0-9.</li>
+     *  <li>A is a letter A-Z or a-z.</li>
+     *  </ul></p>
+     *
+     *  @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 != null)
+            _formats = [ value ];
+        else
+            _formats = [];
+    }
+
+    /**
+     *  An array of format strings to format the postcode in.
+     *
+     *  <p>Use for locales where more than one format is required.
+     *  eg en_UK</p>
+     *
+     *  <p>See <code>formatString</code> for format of the format
+     *  strings.</p>
+     *
+     *  @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
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+       *  Formats the value by using the format set in <code>formatString</code>
+       *  or <code>formats</code>.
+       *
+       *  <p>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.</p>
+       *
+       *  @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 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 condensedPostcode:String = condensedFormat(postCode);
+            var condensedFormat:String = condensedFormat(format);
+            var char:String;
+            var length:int = format.length;
+            var condensedErrors:Array;
+
+            validator.format = condensedFormat;
+
+            condensedErrors = PostCodeValidator.validatePostCode(validator, condensedPostcode, null);
+
+            if (condensedErrors.length == 0)
+            {
+                var pos:int = 0;
+
+                for (var i:int = 0; i < length; i++)
+                {
+                    char = format.charAt(i);
+
+                    if (PostCodeValidator.FORMAT_SPACERS.indexOf(char) >= 0)
+                        formatted += char;
+                    else
+                        formatted += condensedPostcode.charAt(pos++);
+                }
+
+                //TODO may want to return the longest match?
+                errors = [];
+                break;
+            }
+        }
+
+        if (errors.length > 0)
+            error = (errors[0] as ValidationResult).errorCode;
+
+        return formatted;
+    }
+
+    /**
+     *  @private
+     *
+     *  Take a format or paostCode and strip all spacing characters
+     *  out of it.
+     *
+     */
+    protected function condensedFormat(postCode:String):String
+    {
+        var condensed:String = postCode;
+        var length:int;
+
+        if (postCode)
+            length = postCode.length;
+
+        for (var i:int = 0; i < length; i++)
+        {
+            var char:String = PostCodeValidator.FORMAT_SPACERS.charAt(i);
+
+            condensed = condensed.split(char).join("");
+        }
+
+        return condensed;
+    }
+
+}
+}

Added: incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/PostCodeValidator.as
URL: http://svn.apache.org/viewvc/incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/PostCodeValidator.as?rev=1300466&view=auto
==============================================================================
--- incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/PostCodeValidator.as (added)
+++ incubator/flex/branches/patches/projects/apache/src/org/apache/flex/validators/PostCodeValidator.as Wed Mar 14 06:24:19 2012
@@ -0,0 +1,949 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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 org.apache.flex.validators
+{
+
+import flash.globalization.Collator;
+import flash.globalization.LocaleID;
+import flash.globalization.StringTools;
+
+import mx.resources.IResourceManager;
+import mx.resources.ResourceManager;
+import mx.validators.Validator;
+import mx.validators.ValidationResult;
+
+[ResourceBundle("validators")]
+
+/**
+ *  The PostCodeValidator class validates that a String
+ *  has the correct length and format for a post code.
+ *
+ *  <p>Postcode formats consists of the letters C, N, A and spaces or hyphens
+ *  <ul>
+ *  <li>CC or C is the country code (required for some postcodes).</li>
+ *	<li>N is a number 0-9.</li>
+ *  <li>A is a letter A-Z or a-z.</li>
+ *  </ul></p>
+ *
+ *  <p>Country codes one be one or two digits.</p>
+ *
+ *  <p>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 then
+ *  followed by four digits.</p>
+ *
+ *  <p>More than one format can be specified by setting the <code>formats</code>
+ *  property to an array of format Strings.</p>
+ *
+ *  <p>The validator can suggest postcode formats for small set of know locales by calling the
+ *  <code>suggestFormat</code> method.</p>
+ *
+ *  <p>Postcodes can be further validated by setting the <code>extraValidation</code>
+ *  property to a user defined method that performs further checking on the postcode
+ *  digits.</p>
+ *
+ *  <p>Fullwidth numbers and letters are supported in postcodes by ignoring character
+ *  width via the <code>flash.globalization.Collator</code> <code>equals</code> method.</p>
+ *
+ *  @mxml
+ *
+ *  <p>The <code>&lt;mx:PostCodeValidator&gt;</code> tag
+ *  inherits all of the tag attributes of its superclass,
+ *  and adds the following tag attributes:</p>
+ *
+ *  <pre>
+ *  &lt;mx:PostCodeValidator
+ *    countryCode="CC"
+ *    format="NNNNN"
+ *    formats="['NNNNN', 'NNNNN-NNNN']"
+ *    wrongFromatError="The postcode code must be correctly formatted."
+ *    invalidFormatError="The postcode format string is incorrect."
+ *    invalidCharError="The postcode contains invalid characters."
+ *    wrongLengthError="The postcode is the wrong length."
+ *  /&gt;
+ *  </pre>
+ *
+ *  @see mx.formatters.PostCodeFormatter
+ *
+ *  @langversion 3.0
+ *  @playerversion Flash 10.2
+ *  @productversion ApacheFlex 4.8
+ */
+public class PostCodeValidator extends Validator
+{
+    include "../../../../core/Version.as";
+
+    /**
+     * Value <code>errorCode</code> of a ValidationResult is set to when
+     * the postcode contains an invalid charater.
+     */
+    public static const ERROR_INVALID_CHAR:String = "invalidChar";
+
+    /**
+     * Value <code>errorCode</code> of a ValidationResult is set to when
+     * the postcode is of the wrong length.
+     */
+    public static const ERROR_WRONG_LENGTH:String = "wrongLength";
+
+    /**
+     * Value <code>errorCode</code> of a ValidationResult is set to when
+     * the postcode is of the wrong format.
+     */
+    public static const ERROR_WRONG_FORMAT:String = "wrongFormat";
+
+    /**
+     * Value <code>errorCode</code> of a ValidationResult is set to when
+     * the format contains unknown format characters.
+     */
+    public static const ERROR_INCORRECT_FORMAT:String = "incorrectFormat";
+
+    /**
+     * Symbol used in postcode formats representing a single digit.
+     */
+    public static const FORMAT_NUMBER:String = "N";
+
+    /**
+     * Symbol used in postcode formats representing a single character.
+     */
+    public static const FORMAT_LETTER:String = "A";
+
+    /**
+     * Symbol used in postcode formats representing a letter of a country
+     * code.
+     */
+    public static const FORMAT_COUNTRY_CODE:String = "C";
+
+    /**
+     * Valid spacer character in postcode formats.
+     */
+    public static const FORMAT_SPACERS:String = " -";
+
+    //--------------------------------------------------------------------------
+    //
+    //  Class methods
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  @private
+     *  Simulate String.indexOf but ignore wide characters.
+     *  TODO move to StringValidator or Collator?
+     *
+     *  @return Index of char in string or -1 if char not in string.
+     *
+     */
+    protected function indexOf(string:String, char:String):int
+    {
+        var length:int = string.length;
+        var collate:Collator = new Collator(LocaleID.DEFAULT);
+
+        collate.ignoreCharacterWidth = true;
+
+        for (var i:int = 0; i < length; i++)
+		{
+            if (collate.equals(string.charAt(i), char))
+                return i;
+		}
+
+        return -1;
+    }
+
+    /**
+     *  @private
+     *  Compares if two characters are equal ignoring wide characters.
+     *  TODO move to StringValidator or Collator?
+     *
+     *  @return True is charA is the same as charB, false if they are not.
+     *
+     */
+    protected function equals(charA:String, charB:String):Boolean
+    {
+        var collate:Collator = new Collator(LocaleID.DEFAULT);
+
+        collate.ignoreCharacterWidth = true;
+
+        return collate.equals(charA, charB);
+    }
+
+    /**
+     *  @private
+     *
+     *  @param char to check
+     *  @return True if the char is not a valid format character.
+     *
+     */
+    protected function notFormatChar(char:String):Boolean
+    {
+        return indexOf(FORMAT_SPACERS, char) == -1 && char != FORMAT_NUMBER &&
+            char != FORMAT_LETTER && char != FORMAT_COUNTRY_CODE;
+    }
+
+    /**
+     *  @private
+     *
+     *  @param char to check
+     *  @return True if the char is not a valid digit.
+     *
+     */
+    protected function noDecimalDigits(char:String):Boolean
+    {
+        return indexOf(DECIMAL_DIGITS, char) == -1;
+    }
+
+    /**
+     *  @private
+     *
+     *  @param char to check
+     *  @return True if the char is not a valid letter.
+     *
+     */
+    protected function noRomanLetters(char:String):Boolean
+    {
+        return indexOf(ROMAN_LETTERS, char) == -1;
+    }
+
+    /**
+     *  @private
+     *
+     *  @param char to check
+     *  @return True if the char is not a valid spacer.
+     *
+     */
+    protected function noSpacers(char:String):Boolean
+    {
+        return indexOf(FORMAT_SPACERS, char) == -1;
+    }
+
+    /**
+     *  @private
+     *
+     *  A wrong format ValidationResult is added to the results array
+     *  if the extraValidation user supplied function returns an error.
+     *  An error is added when there is a user defined issue with the
+     *  supplied postCode.
+     *
+     */
+    protected function userValidationResults(validator:PostCodeValidator, baseField:String,
+                                             postCode:String, results:Array):void
+    {
+        if (validator && validator.extraValidation != null)
+        {
+            var extraError:String = validator.extraValidation(postCode);
+
+            if (extraError)
+                results.push(new ValidationResult(true, baseField, ERROR_WRONG_FORMAT, extraError));
+        }
+    }
+
+    /**
+     *  @private
+     *
+     *  Based on flags in the error object new ValidationResults are
+     *  added the the results array.
+     *
+     *  Note that the only first ValidationResult is typically shown
+     *  to the user in validation errors.
+     *
+     */
+    protected function errorValidationResults(validator:PostCodeValidator, baseField:String,
+                                              error:Object, results:Array):void
+    {
+        if (error)
+        {
+            if (error.incorrectFormat)
+                results.push(new ValidationResult(true, baseField, ERROR_INCORRECT_FORMAT,
+                                                  validator.incorrectFormatError));
+            if (error.invalidChar)
+                results.push(new ValidationResult(true, baseField, ERROR_INVALID_CHAR,
+                                                  validator.invalidCharError));
+
+            if (error.wrongLength)
+                results.push(new ValidationResult(true, baseField, ERROR_WRONG_LENGTH,
+                                                  validator.wrongLengthError));
+
+            if (error.invalidFormat)
+                results.push(new ValidationResult(true, baseField, ERROR_WRONG_FORMAT,
+                                                  validator.wrongFormatError));
+        }
+    }
+
+    /**
+     *  @private
+     *
+     *  Check thats a postCode is valid and matches a certain format.
+     *
+     *  @return An error object containing flags for each type of error
+     *  and an indication of invalidness (used later to sort errors).
+     *
+     */
+    protected function checkPostCodeFormat(postCode:String, format:String,
+                                           countryCode:String):Object
+    {
+        var invalidChar:Boolean;
+        var invalidFormat:Boolean;
+        var wrongLength:Boolean;
+        var incorrectFormat:Boolean;
+        var formatLength:int;
+        var postCodeLength:int;
+        var noChars:int;
+        var countryIndex:int;
+
+        if (format)
+            formatLength = format.length;
+
+        if (formatLength == 0)
+            incorrectFormat = true;
+
+        if (postCode)
+            postCodeLength = postCode.length;
+
+        noChars = Math.min(formatLength, postCodeLength);
+
+        for (var postcodeIndex:int = 0; postcodeIndex < noChars; postcodeIndex++)
+        {
+            var char:String = postCode.charAt(postcodeIndex);
+            var formatChar:String = format.charAt(postcodeIndex);
+
+            if (postcodeIndex < postCodeLength)
+                char = postCode.charAt(postcodeIndex);
+
+            if (notFormatChar(formatChar))
+                incorrectFormat = true;
+
+            if (noDecimalDigits(char) && noRomanLetters(char) && noSpacers(char))
+            {
+                if (!countryCode || indexOf(countryCode, char) == -1)
+                    invalidChar = true;
+            }
+            else if (formatChar == FORMAT_NUMBER && noDecimalDigits(char))
+            {
+                invalidFormat = true;
+            }
+            else if (formatChar == FORMAT_LETTER && noRomanLetters(char))
+            {
+                invalidFormat = true;
+            }
+            else if (formatChar == FORMAT_COUNTRY_CODE)
+            {
+                if (countryIndex >= 2 || !countryCode ||
+                    !equals(char, countryCode.charAt(countryIndex)))
+                    invalidFormat = true;
+                countryIndex++;
+            }
+            else if (indexOf(FORMAT_SPACERS, formatChar) >= 0 && !equals(formatChar, char))
+            {
+                invalidFormat = true;
+            }
+        }
+
+        wrongLength = (postCodeLength != formatLength);
+
+        // We want invalid char and invalid format errors to show in preference
+        // so give wrong length errors a higher value
+        if (incorrectFormat || invalidFormat || invalidChar || wrongLength)
+            return { invalidFormat: invalidFormat, incorrectFormat: incorrectFormat,
+                     invalidChar: invalidChar, wrongLength: wrongLength,
+                     invalidness: Number(invalidFormat) + Number(invalidChar) +
+                        Number(incorrectFormat) + Number(wrongLength) * 1.5 };
+        else
+            return null;
+    }
+
+    /**
+     *  Convenience method for calling a validator.
+     *  Each of the standard Flex validators has a similar convenience method.
+     *
+     *  @param validator The PostCodeValidator instance.
+     *
+     *  @param value A field to validate.
+     *
+     *  @param baseField Text representation of the subfield
+     *  specified in the <code>value</code> parameter.
+     *  For example, if the <code>value</code> parameter specifies value.postCode,
+     *  the <code>baseField</code> value is <code>"postCode"</code>.
+     *
+     *  @return An Array of ValidationResult objects, with one ValidationResult
+     *  object for each field examined by the validator.
+     *
+     *  @see mx.validators.ValidationResult
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public static function validatePostCode(validator:PostCodeValidator, postCode:String,
+                                            baseField:String):Array
+    {
+        var numberFormats:int;
+        var errors:Array = [];
+        var results:Array = [];
+
+        if (!validator)
+            return [];
+
+        numberFormats = validator.formats.length;
+
+        for (var formatIndex:int = 0; formatIndex < numberFormats; formatIndex++)
+        {
+            var error:Object =
+                validator.checkPostCodeFormat(postCode, validator.formats[formatIndex],
+                                              validator.countryCode);
+
+            if (error)
+            {
+                errors.push(error);
+            }
+            else
+            {
+                errors = [];
+                break;
+            }
+        }
+
+        // return result with least number of errors
+        errors.sortOn("invalidness", Array.NUMERIC);
+
+        validator.userValidationResults(validator, baseField, postCode, results);
+
+        // TODO return/remember closest format or place in results?
+        validator.errorValidationResults(validator, baseField, errors[0], results);
+
+        return results;
+    }
+
+    //--------------------------------------------------------------------------
+    //
+    //  Constructor
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  Constructor.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function PostCodeValidator()
+    {
+        super();
+    }
+
+    //--------------------------------------------------------------------------
+    //
+    //  Properties
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  @private
+     *  The two letter country code used in some postcode formats
+     */
+    private var _countryCode:String;
+
+    /**
+     *  @private
+     *  An array of the postcode formats to check against.
+     */
+    private var _formats:Array = [];
+
+    /**
+     *  Valid postcode format that postcodes will be compaired against.
+     *
+     *  <p>The format string consists of the letters C, N, A and spaces
+     *  or hyphens:
+     *  <ul>
+     *  <li>CC or C is country code (required for some postcodes).</li>
+     *	<li>N is a number 0-9.</li>
+     *  <li>A is a letter A-Z or a-z.</li>
+     *  </ul></p>
+     *
+     *  @default null
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get format():String
+    {
+        if (_formats && _formats.length == 1)
+            return _formats[0];
+
+        return null;
+    }
+
+    /**
+     *  @private
+     */
+    public function set format(value:String):void
+    {
+        if (value != null)
+            _formats = [ value ];
+        else
+            _formats = [];
+    }
+
+    /**
+     *  Optional 1 or 2 letter country code in postcode format
+     *
+     *  @default null
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get countryCode():String
+    {
+        return _countryCode;
+    }
+
+    /**
+     *  @private
+     */
+    public function set countryCode(value:String):void
+    {
+        // Length is usually 2 character but can use 〒 in Japan
+        if (value == null || value && value.length <= 2)
+            _countryCode = value;
+    }
+
+    /**
+     *  An array of valid format strings to compare postcodes against.
+     *
+     *  <p>Use for locales where more than one format is required.
+     *  eg en_UK</p>
+     *
+     *  <p>See <code>format</code> for format of the format
+     *  strings.</p>
+     *
+     *  @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;
+    }
+
+    /**
+     *  A user supplied method that performs further validation on a postcode.
+     *
+     *  <p>The user supplied method should have the following signature:
+     *  <code>function validatePostcode(postcode:String):String</code></p>
+     *
+     *  <p>The method is passed the postcode to be validated and should
+     *  return either:
+     *  <ol>
+     *  <li>A null string indicating the postcode is valid.</li>
+     *  <li>A non empty string describing why the postcode is invalid.</li>
+     *  </ol></p>
+     *
+     *  <p>The error string will be converted into a ValidationResult when
+     *  doValidation is called by Flex as part of the normal validation
+     *  process.</p>
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public var extraValidation:Function;
+
+    //--------------------------------------------------------------------------
+    //
+    //  Properties: Errors
+    //
+    //--------------------------------------------------------------------------
+
+    //----------------------------------
+    //  invalidCharError
+    //----------------------------------
+
+    /**
+     *  @private
+     *  Storage for the invalidCharError property.
+     */
+    private var _invalidCharError:String;
+
+    /**
+     *  @private
+     */
+    private var invalidCharErrorOverride:String;
+
+    [Inspectable(category = "Errors", defaultValue = "null")]
+
+    /**
+     *  Error message when the post code contains invalid characters.
+     *
+     *  @default "The postcode code contains invalid characters."
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get invalidCharError():String
+    {
+        if (invalidCharErrorOverride)
+            return invalidCharErrorOverride;
+
+        return _invalidCharError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set invalidCharError(value:String):void
+    {
+        invalidCharErrorOverride = value;
+
+        if (!value)
+            _invalidCharError = resourceManager.getString("validators", "invalidCharPostcodeError");
+    }
+
+
+    //----------------------------------
+    //  wrongLengthError
+    //----------------------------------
+
+    /**
+     *  @private
+     *  Storage for the wrongLengthError property.
+     */
+    private var _wrongLengthError:String;
+
+    /**
+     *  @private
+     */
+    private var wrongLengthErrorOverride:String;
+
+    [Inspectable(category = "Errors", defaultValue = "null")]
+
+    /**
+     *  Error message for an invalid postcode.
+     *
+     *  @default "The postcode is invalid."
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get wrongLengthError():String
+    {
+        if (wrongLengthErrorOverride)
+            return wrongLengthErrorOverride;
+
+        return _wrongLengthError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set wrongLengthError(value:String):void
+    {
+        wrongLengthErrorOverride = value;
+
+        if (!value)
+            _wrongLengthError = resourceManager.getString("validators", "wrongLengthPostcodeError");
+    }
+
+    //----------------------------------
+    //  wrongFormatError
+    //----------------------------------
+
+    /**
+     *  @private
+     *  Storage for the wrongFormatError property.
+     */
+    private var _wrongFormatError:String;
+
+    /**
+     *  @private
+     */
+    private var wrongFormatErrorOverride:String;
+
+    [Inspectable(category = "Errors", defaultValue = "null")]
+
+    /**
+     *  Error message for an incorrectly formatted postcode.
+     *
+     *  @default "The postcode code must be correctly formatted."
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get wrongFormatError():String
+    {
+        if (wrongFormatErrorOverride)
+            return wrongFormatErrorOverride;
+
+        return _wrongFormatError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set wrongFormatError(value:String):void
+    {
+        wrongFormatErrorOverride = value;
+
+        if (!value)
+            _wrongFormatError = resourceManager.getString("validators", "wrongFormatPostcodeError");
+    }
+
+    //----------------------------------
+    //  incorrectFormatError
+    //----------------------------------
+
+    /**
+     *  @private
+     *  Storage for the incorrectFormatError property.
+     */
+    private var _incorrectFormatError:String;
+
+    /**
+     *  @private
+     */
+    private var incorrectFormatErrorOverride:String;
+
+    [Inspectable(category = "Errors", defaultValue = "null")]
+
+    /**
+     *  Error message for an incorrect format string.
+     *
+     *  @default "The postcode format string is incorrect"
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function get incorrectFormatError():String
+    {
+        if (incorrectFormatErrorOverride)
+            return incorrectFormatErrorOverride;
+
+        return _incorrectFormatError;
+    }
+
+    /**
+     *  @private
+     */
+    public function set incorrectFormatError(value:String):void
+    {
+        incorrectFormatErrorOverride = value;
+
+        if (!value)
+            _incorrectFormatError =
+                resourceManager.getString("validators", "incorrectFormatPostcodeError");
+    }
+
+    //--------------------------------------------------------------------------
+    //
+    //  Overridden methods
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  @private
+     *  Changes error strings when the locale changes.
+     */
+    override protected function resourcesChanged():void
+    {
+        super.resourcesChanged();
+
+        invalidCharError = invalidCharErrorOverride;
+        wrongLengthError = wrongLengthErrorOverride;
+        wrongFormatError = wrongFormatErrorOverride;
+        incorrectFormatError = incorrectFormatErrorOverride;
+    }
+
+    /**
+     *  Override of the base class <code>doValidation()</code> method
+     *  to validate a postcode.
+     *
+     *  <p>You do not call this method directly;
+     *  Flex calls it as part of performing a validation.
+     *  If you create a custom Validator class, you must implement this method.</p>
+     *
+     *  @param value Object to validate.
+     *
+     *  @return An Array of ValidationResult objects, with one ValidationResult
+     *  object for each field examined by the validator.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    override protected function doValidation(value:Object):Array
+    {
+        var results:Array = super.doValidation(value);
+
+        // Return if there are errors
+        // or if the required property is set to false and length is 0.
+        var val:String = value ? String(value) : "";
+        if (results.length > 0 || ((val.length == 0) && !required))
+            return results;
+        else
+            return PostCodeValidator.validatePostCode(this, val, null);
+    }
+
+
+    //--------------------------------------------------------------------------
+    //
+    //  Methods
+    //
+    //--------------------------------------------------------------------------
+
+    /**
+     *  @private
+     *  Returns the region (usually country) from a locale string.
+     *  If no loacle is provided the default locale is used.
+     *
+     *  @param locale locale to obtain region from.
+     *  @return Region string.
+     *
+     */
+    protected function getRegion(locale:String):String
+    {
+        var localeID:LocaleID;
+
+        if (locale == null)
+        {
+            var tool:StringTools = new StringTools(LocaleID.DEFAULT);
+            localeID = new LocaleID(tool.actualLocaleIDName);
+        }
+        else
+        {
+            localeID = new LocaleID(locale);
+        }
+
+        return localeID.getRegion();
+    }
+
+    /**
+     *  Sets the suggested postcode formats for a given <code>locale</code>.
+     *
+     *  <p>If no locale is suplied the default locale is used.</p>
+     *
+     *  <p>Currenly only a limited set of locales are supported.</p>
+     *
+     *  @param locale Locale to obtain formats for.
+     *
+     *  @return The suggested format (an array of strings) or an empty
+     *  array if the locale is not supported.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function suggestFormat(locale:String = null):Array
+    {
+        var region:String = getRegion(locale);
+
+        switch (region)
+        {
+            case "AU":
+            case "DK":
+            case "NO":
+                formats = [ "NNNN" ];
+                break;
+            case "BR":
+                formats = [ "NNNNN-NNN" ];
+                break;
+            case "CN":
+            case "DE":
+                formats = [ "NNNNNN" ];
+                break;
+            case "CA":
+                formats = [ "ANA NAN" ];
+                break;
+            case "ES":
+            case "FI":
+            case "FR":
+            case "IT":
+            case "TW":
+                formats = [ "NNNNN" ];
+                break;
+            case "GB":
+                formats = [ "AN NAA", "ANN NAA", "AAN NAA", "ANA NAA", "AANN NAA", "AANA NAA" ];
+                break;
+            case "JP":
+                formats = [ "NNNNNNN", "NNN-NNNN", "C NNNNNNN", "C NNN-NNNN" ];
+                break;
+            case "KR":
+                formats = [ "NNNNNN", "NNN-NNN" ];
+                break;
+            case "NL":
+                formats = [ "NNNN AA" ];
+                break;
+            case "RU":
+                formats = [ "NNNNNN" ];
+                break;
+            case "SE":
+                formats = [ "NNNNN", "NNN NN" ];
+                break;
+            case "US":
+                formats = [ "NNNNN", "NNNNN-NNNN" ];
+                break;
+            default:
+                formats = [];
+                break;
+        }
+
+        return formats;
+    }
+
+    /**
+     *  Sets the suggested country code for for a given <code>locale</code>.
+     *
+     *  <p>If no locale is suplied the default locale is used.</p>
+     *
+     *  <p>Currenly only a limited set of locales are supported.</p>
+     *
+     *  @param Locale Locale to obtain country code for.
+     *
+     *  @return The suggested code or an null string if the
+     *  locale is not supported or has no country code.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @productversion ApacheFlex 4.8
+     */
+    public function suggestCountryCode(locale:String = null):String
+    {
+        var region:String = getRegion(locale);
+
+        if (region == "JP")
+            countryCode = "〒";
+
+        return countryCode;
+    }
+}
+
+}
+



Mime
View raw message