/**
 * 
 */
package org.geotools.xml.handlers;

import java.net.URI;

import org.geotools.xml.schema.ComplexType;
import org.geotools.xml.schema.Element;
import org.geotools.xml.schema.ElementGrouping;
import org.geotools.xml.schema.Type;

/**
 * Utility class with methods to help implement Element and Type Handlers 
 * @author Jesse
 */
public class XMLTypeHelper {

	public static Element findChildElement(Type type, String localName, URI namespaceURI) {
		if( type instanceof ComplexType ){
			ComplexType complexType=(ComplexType) type;
			ElementGrouping child = complexType.getChild();
			if( child!=null){
				Element found=child.findChildElement(localName, namespaceURI);
				if( found!=null )
					return found;
			}
			Element[] children = complexType.getChildElements();
			if( children==null || children.length==0)
				return null;
			for (int i = 0; i < children.length; i++) {
				Element element = children[i];
				if( localName.equals(element.getName()) && namespaceURI.equals(element.getNamespace()) )
					return element;
			}
			if( complexType.getParent()!=null ){
				Type parent = complexType.getParent();
				return findChildElement((ComplexType) parent, localName, namespaceURI);
			}
		}else{
			return type.findChildElement(localName);
		}
		return null;
	}

}
