package org.geotools.xml.impl.jxpath;

import org.apache.commons.jxpath.ri.QName;
import org.apache.commons.jxpath.ri.model.NodePointer;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureType;

/**
 * Pointer to a single attribute of a feature type.
 *
 * @author Justin Deoliveira, The Open Planning Project
 *
 */
public class FeatureTypeAttributePointer extends NodePointer {

	/**
	 * the feature type
	 */
	FeatureType featureType;
	/**
	 * The parent pointer
	 */
	FeatureTypePointer parent;
	/**
	 * the indedx of hte property being pointed at
	 */
	int index;
	
	/**
	 * Creates the pointer.
	 * 
	 * @param parent The parent pointer, pointer at the feature type.
	 * @param index The index of hte property to point to
	 */
	public FeatureTypeAttributePointer( FeatureTypePointer parent, int index ) {
		super( parent );
		this.index = index;
		this.featureType = (FeatureType) parent.getImmediateNode();
	}
	
	/**
	 * Return <code>true</code>.
	 */
	public boolean isLeaf() {
		return true;
	}

	/**
	 * Return <code>false</code>. 
	 */
	public boolean isCollection() {
		return false;
	}

	/**
	 * Return <code>1</code>
	 */
	public int getLength() {
		return 1;
	}

	/**
	 * Returns the qname with prefix as <code>null</code>, and local part the name of the 
	 * feature attribute.
	 */
	public QName getName() {
		return new QName( null, featureType.getAttributeType( index ).getName() );
	}

	public Object getBaseValue() {
		return featureType;
	}

	public Object getImmediateNode() {
		return featureType.getAttributeType( index );
	}

	public void setValue(Object value) {
		throw new UnsupportedOperationException( "Feautre types are immutable" );
	}

	/**
	 * Always return <code>0</code>, can never have child pointers.
	 */
	public int compareChildNodePointers(NodePointer pointer1,
			NodePointer pointer2) {
		return 0;
	}

}
