package com.jxml.quick.tf;
import com.jxml.quick.*;
import com.jxml.quick.access.*;
import java.beans.*;
import java.lang.reflect.*;

public abstract class QGenericTargetFactory implements QTargetFactory
{
    protected abstract void insert(Object wrapper, Object object);

    public abstract Object create(QContext context)
	throws QPE;

    public abstract boolean isInstance(Object object, QContext context);

    public String getName(Object object)
    {
	return "";
    }

    public String getText(Object object, 
			  QContext context)
	throws QPE
    {
	return object.getClass().getName();
    }

    public void add(Object object, 
		    String name, 
		    String text,
		    QAccess access,
		    Object parent,
		    QTargetFactory parentFactory,
		    QContext context)
	throws QPE
    {
	Class c=context.forName(text);
	Object rv=null;
	try
	    {
		rv=c.newInstance();
	    }
	catch (InstantiationException ie)
	    {
		System.err.println("class="+text);
		context.throwPE(ie);
	    }
	catch (IllegalAccessException iae)
	    {
		System.err.println("class="+text);
		context.throwPE(iae);
	    }
	if (!isInstance(rv,context))
	    context.throwPE("Class mismatch: "+text);
	insert(object,rv);
	if (parent==null)
	    context.doc.setRoot(rv);
	else
	    access.add(parent,parentFactory,rv,context);
    }

    public PropertyDescriptor[] getPropertyDescriptors(QContext context)
	throws QPE
    {
	return null;
    }

    public Field[] getFields(QContext context)
	throws QPE
    {
	return null;
    }
}
