/*
 Copyright (c) 2006 Eric J. Feminella  <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.xml
{
    /**
     *
     * All static utility class which provides an API for working
     * with and modifyinmg XML attributes
     *
     * @see XMLList
     *
     */
    public final class XMLAttributeUtil
    {
        /**
         *
         * Retrieves all attribute values for a specified XML object
         * and returns the values as an Array
         *
         * @param  the XML source to query
         * @return an array of attribute values
         *
         */
        public static function getAttributeValues(source:XML) : Array
        {
            var attributesList:XMLList = source..@*;
            var attributeValues:Array = [];
            var n:int = attributesList.length();

            for (var i:int = 0; i < n; i++)
            {
                attributeValues.push( attributesList[i] );
            }
            return attributeValues;
        }

        /**
         *
         * Retrieves all attribute names for a specified XML object
         * and returns the names as a String Array
         *
         * @param  the XML source to query
         * @return String array of attribute names
         *
         */
        public static function getAttributeNames(source:XML) : Array
        {
            var attributesList:XMLList = source..@*;
            var attributeNames:Array = [];
            var n:int = attributesList.length();

            for (var i:int = 0; i < n; i++)
            {
                attributeNames.push( attributesList[i].name() );
            }
            return attributeNames;
        }

        /**
         *
         * Retrieves an attribute value for a specified XML object
         * via the attribute name
         *
         * @param  the XML source to query
         * @param  the name of the attribute to query on
         * @return an XMLList of attribute values
         *
         */
        public static function getAttributeValueByName(source:XML, attributeName:String) : String
        {
            return source.attribute( attributeName ).toString();
        }

        /**
         *
         * Retrieves all attribute values for a specified XML object
         * via a specific attribute name
         *
         * @param  the XML source to query
         * @param  the name of the attribute to query on
         * @return an Array of attribute values for the specified attribute
         *
         */
        public static function getAttributeValuesByName(source:XML, attributeName:String) : Array
        {
            var attributesList:XMLList = source..@*;
            var attributeValues:Array = [];
            var n:int = attributesList.length();

            for (var i:int = 0; i < n; i++)
            {
                if ( attributesList[i].name() == attributeName )
                {
                    attributeValues.push( attributesList[i] );
                }
            }
            return attributeValues;
        }

        /**
         *
         * Retrieves an attribute name for a specified XML
         * object via the attributes value
         *
         * @param  the XML source to query
         * @param  the value of the attribute to query on
         * @return the attribute name for the specified value
         *
         */
        public static function getAttributeNameByValue(source:XML, attributeValue:*) : String
        {
            var attributesList:XMLList = source..@*;
            var n:int = attributesList.length();
            var result:String = null;

            for (var i:int = 0; i < n; i++)
            {
                if ( attributesList[i] == attributeValue )
                {
                    result = attributesList[i].name();
                    break;
                }
            }
            return result;
        }
    }
}