/**
 * @requires OpenLayers/Format/XML.js
 *
 * Class: OpenLayers.Format.WFSDescribeFeatureType
 * Read WFS DescribeFeatureType response
 * DescribeFeatureType is meant to describe a feature type
 * and give back its attributes and attribute types
 * The output is an XML Schema
 * WFS 1.0.0 uses XML Schema version 0.1, so this version
 * comes back in the response
 * 
 * Inherits from:
 *  - <OpenLayers.Format.XML>
 */
OpenLayers.Format.WFSDescribeFeatureType = OpenLayers.Class(OpenLayers.Format.XML, {

    /**
     * APIProperty: defaultVersion
     * {String} Version number to assume if none found.  Default is "1.0.0".
     */
    defaultVersion: "1.0",
   
    /**
     * APIProperty: version
     * {String} Specify a version string if one is known.
     */
    version: null,

    /**
     * Constructor: OpenLayers.Format.WFSDescribeFeatureType
     * Create a new parser for WFS DescribeFeatureType responses.
     *
     * Parameters:
     * options - {Object} An optional object whose properties will be set on
     *     this instance.
     */
    initialize: function(options) {
        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
        this.options = options;
    },

    /**
     * APIMethod: read
     * Read DescribeFeatureType data from a string, and return the response. 
     * 
     * Parameters: 
     * data - {String} or {DOMElement} data to read/parse.
     * options - {Object} additional options that influence the result
     *     returned from the reader.
     *     
     * Currently supported reader options:
     * verbose - {Boolean} set to true to get more information than just the
     *     attributes. The reader will return a hash, containing the
     *     attributes in the attributes property, and the feature namespace
     *     in the featureNS property.
     *
     * Returns:
     * {Array} Array of objects which have:
     * - {String} name: name of the attribute
     * - {String} type: type of the attribute, xsd:string etc.
     */
    read: function(data, options) {      
       if(typeof data == "string") {
            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
        }
        var root = data.documentElement;
        var version = this.version;
        if(!version) {
            version = root.getAttribute("version");
            
            if(!version) {
                version = this.defaultVersion;
            }
        }
        if(!this.parser || this.parser.VERSION != version) {
            var format = OpenLayers.Format.WFSDescribeFeatureType[
                "v" + version.replace(/\./g, "_")
            ];
            if(!format) {
                throw "Can't find a WFSDescribeFeatureType parser for version " +
                      version;
            }
            this.parser = new format(this.options);
        }
        var describeFeatureType = this.parser.read(data, options);
        return describeFeatureType;
       
    },
    
    CLASS_NAME: "OpenLayers.Format.WFSDescribeFeatureType" 

});

