package com.jxml.quick.util.setid;
import com.jxml.quick.*;
import org.xml.sax.*;
import java.util.*;
import com.jxml.quick.ocm.*;
import java.io.*;

/**
 * Represents an element to be assigned an ID.
 */
public class Element extends OCMrootPeerSupport
{
	/**
	 * User-provided code to perform the conversion.
	 */
	public void cvt(Object param)
		throws SAXException, IOException
	{
		int i,s;

		Counter.SubCounter subCounter=(Counter.SubCounter)context;
		OCMelement element=(OCMelement)dataModel;

		addStringAttribute("ID",subCounter.genID());

		s=element.attributes.size();
		for (i=0;i<s;++i)
		{
			OCMattribute a=(OCMattribute)element.attributes.get(i);
			if (!"ID".equals(a.name))
				addStringAttribute(a.name,a.text);
		}

		startElement(element.name);

		s=element.elements.size();
		if (s==0)
			addStringContent(element.text);
		else
			doCvt(element.elements,this);

		endElement(element.name);
	}

	// Implement method on OCMrootPeer
	public QDoc convert()
		throws SAXException, IOException
	{
		QDoc outputSchema=context.getSchemaDoc();
		QDoc inputSchema=context.getInputSchemaDoc();
		QDoc xdoc=Quick.createDoc(this,inputSchema);
		QDoc rv=Quick.convert(xdoc,outputSchema);
		Object root=rv.getRoot();
		if (root instanceof OCMelement)
		{
			OCMelement elem=(OCMelement)root;
			elem.setIDs(rv);
		}
		return rv;
	}

	// Implement method on OCMrootPeer
	public QDoc convert(String url)
		throws SAXException, IOException
	{
		QDoc outputSchema=context.getSchemaDoc();
		QDoc inputSchema=context.getInputSchemaDoc();
		QDoc xdoc=Quick.createDoc(this,inputSchema);
		QDoc rv=Quick.convert(xdoc,outputSchema,url);
		Object root=rv.getRoot();
		if (root instanceof OCMelement)
		{
			OCMelement elem=(OCMelement)root;
			elem.setIDs(rv);
		}
		return rv;
	}
}
