/*
 Copyright (c) 2008 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
{
    import mx.rpc.xml.SimpleXMLEncoder;
    import mx.collections.ArrayCollection;
    import flash.xml.XMLDocument;
    
    /**
     * 
     * All static API which provides a utility for serializing an n-level 
     * nested <code>Object</code> to an <code>XML</code> encoded document 
     * structure.
     * 
     * @see mx.rpc.xml.SimpleXMLEncoder;
     * 
     */    
    public final class XMLSerializationUtil
    {
        /**
         *
         * Serializes an n-level nested <code>Object</code> to an <code>XML</code> 
         * encoded document structure.
         * 
         * @example The following example demonstrates how to encode an object to 
         * an <code>XML</code> structure using <code>XMLSerializationUtil</code>
         * 
         * <listing version="3.0">
         * 
         * var user:User = new User("foo", "passwordfoo");
         * XMLSerializationUtil.serialize( user, "user" );
         * 
         * // outputs:
         * // &lt;user&gt;
         * //   &ltpassword&gt;passwordfoo&lt/password&gt;
         * //   &ltusername&gt;foo&lt/username&gt;
         * // &lt/user&gt;
         * 
         * </listing>
         * 
         * @param  object in which to serialize to <code>XML</code>
         * @param  root node name to be used for the document root name
         * @return <code>XML</code> object representation of object instance
         * 
         */        
        public static function serialize(value:Object, rootNodeName:String) : XML
        {
            var qname:QName = new QName( rootNodeName );
            var document:XMLDocument = new XMLDocument();
            var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder( document );

            if ( value is ArrayCollection )
            {
                value = ArrayCollection( value ).source;
            }
            simpleXMLEncoder.encodeValue( value, qname, document);
            var xml:XML =  new XML( document.toString() );
            
            return xml;
        }
    }
}