package org.mandarax.util.xmlser;

/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.beans.*;

import org.mandarax.kernel.LogicFactory;
 
/**
 * Utility class that can be used to configure an XMLEncoder. The idea is to instruct the 
 * encoder not to use direct instanciation of classes nut to use the logic factory instead. 
 * Moreover, an exception listener is set which forwards exception details to the mandarax logging system.<p>
 * <strong>Warning:</strong> This is not yet working for the persistence delegate for rules, perhaps there is a bug 
 * in the jdk xml encoder. The delegates for facts and queries do not yet support properties!
 * @author <a HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 * @version 3.3.1 <9 August 2004>
 * @since 2.3.1
 */

public class XMLEncoderConfigurator {
	/**
	 * Configure an encoder.
	 * @param encoder the encoder
	 */
	public static void configure(XMLEncoder encoder) {
		LogicFactory lfactory = LogicFactory.getDefaultFactory();				
		encoder.setPersistenceDelegate(lfactory.getClass(),new PersistenceDelegate4LogicFactories());	
		// encoder.setPersistenceDelegate(lfactory.getQueryImplementationClass(),new PersistenceDelegate4Queries());	
		// encoder.setPersistenceDelegate(lfactory.getRuleImplementationClass(),new PersistenceDelegate4Rules());		
		encoder.setPersistenceDelegate(lfactory.getPrerequisiteImplementationClass(),new PersistenceDelegate4Prerequisites());
		// encoder.setPersistenceDelegate(lfactory.getFactImplementationClass(),new PersistenceDelegate4Facts());		
		encoder.setPersistenceDelegate(lfactory.getConstantTermImplementationClass(),new PersistenceDelegate4ConstantTerms());
		encoder.setPersistenceDelegate(lfactory.getComplexTermImplementationClass(),new PersistenceDelegate4ComplexTerms());
		encoder.setPersistenceDelegate(lfactory.getVariableTermImplementationClass(),new PersistenceDelegate4VariableTerms());		
		encoder.setExceptionListener(new LogExceptionListener());
	}
	/**
	 * Configure a decoder.
	 * @param decoder the decoder
	 */
	public static void configure(XMLDecoder decoder) {
		decoder.setExceptionListener(new LogExceptionListener());
	}
}
