package org.geotools.wms.v1_1_1.bindings;


import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import javax.xml.namespace.QName;

import org.geotools.data.ows.CRSEnvelope;
import org.geotools.data.ows.Layer;
import org.geotools.xml.AbstractComplexBinding;
import org.geotools.xml.ElementInstance;
import org.geotools.xml.Node;

/**
 * Binding object for the type :_Layer.
 *
 * <p>
 *	<pre>
 *	 <code>
 *  &lt;xs:complexType name="_Layer"&gt;
 *      &lt;xs:sequence&gt;
 *          &lt;xs:element minOccurs="0" ref="Name"/&gt;
 *          &lt;xs:element ref="Title"/&gt;
 *          &lt;xs:element minOccurs="0" ref="Abstract"/&gt;
 *          &lt;xs:element minOccurs="0" ref="KeywordList"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="SRS"/&gt;
 *          &lt;xs:element minOccurs="0" ref="LatLonBoundingBox"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="BoundingBox"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="Dimension"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="Extent"/&gt;
 *          &lt;xs:element minOccurs="0" ref="Attribution"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="AuthorityURL"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="Identifier"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="MetadataURL"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="DataURL"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="FeatureListURL"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="Style"/&gt;
 *          &lt;xs:element minOccurs="0" ref="ScaleHint"/&gt;
 *          &lt;xs:element maxOccurs="unbounded" minOccurs="0" ref="Layer"/&gt;
 *      &lt;/xs:sequence&gt;
 *      &lt;xs:attribute default="0" name="queryable"&gt;
 *          &lt;xs:simpleType&gt;
 *              &lt;xs:restriction base="xs:token"&gt;
 *                  &lt;xs:enumeration value="0"/&gt;
 *                  &lt;xs:enumeration value="1"/&gt;
 *              &lt;/xs:restriction&gt;
 *          &lt;/xs:simpleType&gt;
 *      &lt;/xs:attribute&gt;
 *      &lt;xs:attribute name="cascaded"/&gt;
 *      &lt;xs:attribute default="0" name="opaque"&gt;
 *          &lt;xs:simpleType&gt;
 *              &lt;xs:restriction base="xs:token"&gt;
 *                  &lt;xs:enumeration value="0"/&gt;
 *                  &lt;xs:enumeration value="1"/&gt;
 *              &lt;/xs:restriction&gt;
 *          &lt;/xs:simpleType&gt;
 *      &lt;/xs:attribute&gt;
 *      &lt;xs:attribute default="0" name="noSubsets"&gt;
 *          &lt;xs:simpleType&gt;
 *              &lt;xs:restriction base="xs:token"&gt;
 *                  &lt;xs:enumeration value="0"/&gt;
 *                  &lt;xs:enumeration value="1"/&gt;
 *              &lt;/xs:restriction&gt;
 *          &lt;/xs:simpleType&gt;
 *      &lt;/xs:attribute&gt;
 *      &lt;xs:attribute name="fixedWidth"/&gt;
 *      &lt;xs:attribute name="fixedHeight"/&gt;
 *  &lt;/xs:complexType&gt; 
 *		
 *	  </code>
 *	 </pre>
 * </p>
 *
 * @generated
 */
public class _LayerBinding extends AbstractComplexBinding {

	/**
	 * @generated
	 */
	public QName getTarget() {
		return WMSV1_1_1._Layer;
	}
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 *	
	 * @generated modifiable
	 */	
	public Class getType() {
		return Layer.class;
	}
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 *	
	 * @generated modifiable
	 */	
	public Object parse(ElementInstance instance, Node node, Object value) 
		throws Exception {
		Layer ret = new Layer();
		ret.set_abstract((String)node.getChildValue("Abstract"));
		
		HashMap bboxes = new HashMap();
		Iterator iBboxList = node.getChildValues("BoundingBox").iterator();
		while (iBboxList.hasNext()) {
			CRSEnvelope bbox = (CRSEnvelope)iBboxList.next();
			bboxes.put(bbox.getEPSGCode(), bbox);
		}
		ret.setBoundingBoxes(bboxes);
		
		ret.setLatLonBoundingBox((CRSEnvelope)node.getChildValue("LatLonBoundingBox"));
		
		//get all layer children and set their parent to point back to us.
		List childLayers = node.getChildValues("Layer");
		Iterator iChildLayers = childLayers.iterator();
		while (iChildLayers.hasNext())
			((Layer)iChildLayers.next()).setParent(ret);
		ret.setChildren((Layer[])childLayers.toArray(new Layer[childLayers.size()]));
		
		ret.setKeywords((String[])node.getChildValue("KeywordList"));
		ret.setName((String)node.getChildValue("Name"));
		
		//This code from WMSComplexTypes.java
		String queryable = (String)node.getAttributeValue("queryable");
		if (queryable != null) {
			if ("1".equals(queryable)) {
				ret.setQueryable(true);
			} else if ("0".equals(queryable)) {
				ret.setQueryable(new Boolean(queryable).booleanValue());
			}
		}
		
		Map shints = (Map)node.getChildValue("ScaleHint");
		if (shints != null && shints.get("max") != null)
			ret.setScaleHintMax(Double.parseDouble((String)shints.get("max")));
		if (shints != null && shints.get("min") != null)
			ret.setScaleHintMin(Double.parseDouble((String)shints.get("min")));
		
		TreeSet srs = new TreeSet();
		srs.addAll(node.getChildValues("SRS"));
		ret.setSrs(srs);
		
		ret.setStyles((List)node.getChildValues("Style"));
		
		ret.setTitle((String)node.getChildValue("Title"));
		
		return ret;
		
	}

}