2006/unicorn/src/org/w3c/unicorn/output SimpleOutputFormater.java,1.1,1.2 XMLOutputFormater.java,1.1,1.2 OutputFactory.java,1.1,1.2 OutputModule.java,1.1,1.2 EnumOutputModule.java,1.1,1.2 SimpleOutputModule.java,1.1,1.2 OutputFormater.java,1.1,1.2

Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/output
In directory hutz:/tmp/cvs-serv22368/src/org/w3c/unicorn/output

Added Files:
	SimpleOutputFormater.java XMLOutputFormater.java 
	OutputFactory.java OutputModule.java EnumOutputModule.java 
	SimpleOutputModule.java OutputFormater.java 
Log Message:
Merging dev2 in HEAD

--- NEW FILE: OutputFactory.java ---
// $Id: OutputFactory.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.w3c.unicorn.util.Property;

/**
 * This class allow to manage all output module and output formatter. It provide
 * a way to share an instance of a output module or output formater between
 * different call of the framework UniCORN.
 * 
 * @author Damien LEROY
 */
public class OutputFactory {

	protected static final Log logger = LogFactory.getLog(OutputFactory.class);

	private static final Map<EnumOutputModule, OutputModule> mapOfOutputModule = new LinkedHashMap<EnumOutputModule, OutputModule>();

	private static final Map<String, OutputFormater> mapOfOutputFormater = new LinkedHashMap<String, OutputFormater>();

	/**
	 * Create a new output module and add it to the map.
	 * 
	 * @param aEnumOutputModule
	 *            To identify which type of output module will be created.
	 * @return The new output module.
	 */
	private static OutputModule createOutputModule(
			final EnumOutputModule aEnumOutputModule) {
		OutputFactory.logger.trace("createOutputModule");
		if (OutputFactory.logger.isDebugEnabled()) {
			OutputFactory.logger.debug("Output module : " + aEnumOutputModule
					+ ".");
		}
		final OutputModule aOutputModule;
		switch (aEnumOutputModule) {
		case SIMPLE:
			aOutputModule = new SimpleOutputModule();
			break;
		default:
			return null;
		}
		OutputFactory.mapOfOutputModule.put(aEnumOutputModule, aOutputModule);
		return aOutputModule;
	}

	/**
	 * Create a new output formatter and add it to the map.
	 * 
	 * @param sOutputFormat
	 *            The format who the output formatter must produce.
	 * @return The new output formatter.
	 * @throws ResourceNotFoundException
	 * @throws ParseErrorException
	 * @throws Exception
	 */
	private static OutputFormater createOutputFormater(
			final String sOutputFormat, final String sLang,
			final String sMimeType) throws ResourceNotFoundException,
			ParseErrorException, Exception {
		OutputFactory.logger.trace("createOutputformater");
		if (OutputFactory.logger.isDebugEnabled()) {
			OutputFactory.logger
					.debug("Output format : " + sOutputFormat + ".");
			OutputFactory.logger.debug("Language : " + sLang + ".");
			OutputFactory.logger.debug("Mime type : " + sMimeType + ".");
		}

		final String sFormaterName = Property.getProps("specialFormaters.properties")
											 .getProperty(sMimeType);
		if (null != sFormaterName) {
			final Class<?> aFormaterClass = Class
					.forName("org.w3c.unicorn.output." + sFormaterName);
			final Class<?>[] tClassParamType = { String.class, String.class };
			final Object[] tObjectParamValue = { sOutputFormat, sLang };
			final OutputFormater aOutputFormater;
			aOutputFormater = (OutputFormater) aFormaterClass.getConstructor(
					tClassParamType).newInstance(tObjectParamValue);
			OutputFactory.mapOfOutputFormater.put(sMimeType + "_" + sLang + "_"
					+ sOutputFormat, aOutputFormater);
			return aOutputFormater;
		}

		final OutputFormater aOutputFormater;
		aOutputFormater = new SimpleOutputFormater(sOutputFormat, sLang);
		OutputFactory.mapOfOutputFormater.put(sLang + "_" + sOutputFormat,
				aOutputFormater);
		return aOutputFormater;
	}

	/**
	 * Return the output module asked.
	 * 
	 * @param sOutputModule
	 *            The name of the output module to return.
	 * @return The output module asked.
	 */
	public static OutputModule getOutputModule(final String sOutputModule) {
		OutputFactory.logger.trace("getOutputModule");
		if (OutputFactory.logger.isDebugEnabled()) {
			OutputFactory.logger
					.debug("Output module : " + sOutputModule + ".");
		}
		final EnumOutputModule aEnumOutputModule = EnumOutputModule
				.fromValue(sOutputModule);
		if (null == aEnumOutputModule) {
			OutputFactory.logger.error("Unknow output module.");
			return null;
		}
		return OutputFactory.getOutputModule(aEnumOutputModule);
	}

	/**
	 * Return the output module asked.
	 * 
	 * @param aEnumOutputModule
	 * @return The output module asked.
	 */
	public static OutputModule getOutputModule(
			final EnumOutputModule aEnumOutputModule) {
		OutputFactory.logger.trace("getOutputModule");
		if (OutputFactory.logger.isDebugEnabled()) {
			OutputFactory.logger.debug("Output module : " + aEnumOutputModule
					+ ".");
		}
		final OutputModule aOutputModule = OutputFactory.mapOfOutputModule
				.get(aEnumOutputModule);
		// if output module not already exist
		if (null == aOutputModule) {
			// create it
			return OutputFactory.createOutputModule(aEnumOutputModule);
		}
		return aOutputModule;
	}

	/**
	 * Return the output formatter asked.
	 * 
	 * @param sOutputFormat
	 *            The output format who be produce by the output formatter.
	 * @return The output formatter asked.
	 * @throws ResourceNotFoundException
	 * @throws ParseErrorException
	 * @throws Exception
	 */
	public static OutputFormater getOutputFormater(final String sOutputFormat,
			final String sLang, final String sMimeType)
			throws ResourceNotFoundException, ParseErrorException, Exception {
		OutputFactory.logger.trace("getOutputformater");
		if (OutputFactory.logger.isDebugEnabled()) {
			OutputFactory.logger
					.debug("Output format : " + sOutputFormat + ".");
			OutputFactory.logger.debug("Language : " + sLang + ".");
		}
		OutputFormater aOutputFormater = OutputFactory.mapOfOutputFormater
				.get(sMimeType + "_" + sLang + "_" + sOutputFormat);
		if (null != aOutputFormater) {
			return aOutputFormater;
		}
		aOutputFormater = OutputFactory.mapOfOutputFormater.get(sLang + "_"
				+ sOutputFormat);

		if (null != aOutputFormater) {
			return aOutputFormater;
		}
		// if output formater not already exist create it
		aOutputFormater = OutputFactory.createOutputFormater(sOutputFormat,
				sLang, sMimeType);

		return aOutputFormater;
	}

}

--- NEW FILE: OutputModule.java ---
// $Id: OutputModule.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * Interface for all output module.
 * 
 * @author Damien LEROY
 */
public interface OutputModule {

	public static final Log logger = LogFactory.getLog(OutputModule.class);

	/**
	 * Generate the output of all response.
	 * 
	 * @throws IOException
	 * @throws Exception
	 * @throws MethodInvocationException
	 * @throws ParseErrorException
	 * @throws ResourceNotFoundException
	 */
	public abstract void produceOutput(final OutputFormater aOutputFormater,
			final Map<String, Object> mapOfStringObject,
			final Map<String, String[]> mapOfParameter, final Writer aWriter)
			throws IOException, ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception;

	/**
	 * Generates an error output
	 * 
	 * @throws IOException
	 * @throws Exception
	 * @throws MethodInvocationException
	 * @throws ParseErrorException
	 * @throws ResourceNotFoundException
	 */
	public abstract void produceError(final OutputFormater aOutputFormater,
			final Exception error, final Map<String, String[]> mapOfParameter,
			final Writer aWriter) throws IOException,
			ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception;

}

--- NEW FILE: SimpleOutputModule.java ---
// $Id: SimpleOutputModule.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.io.Writer;
import java.util.Map;

import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * This module allow to generate output in text format.
 * 
 * @author Damien LEROY
 */
public class SimpleOutputModule implements OutputModule {

	public void produceOutput(final OutputFormater aOutputFormater,
			final Map<String, Object> mapOfStringObject,
			final Map<String, String[]> mapOfParameter, final Writer aWriter)
			throws ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception {
		OutputModule.logger.trace("Constructor");
		aOutputFormater.produceOutput(mapOfStringObject, aWriter);
	}

	public void produceError(final OutputFormater aOutputFormater,
			final Exception aException,
			final Map<String, String[]> mapOfParameter, final Writer aWriter)
			throws ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception {
		OutputModule.logger.trace("produceError");
		aOutputFormater.produceError(aException, aWriter);
	}

}

--- NEW FILE: EnumOutputModule.java ---
// $Id: EnumOutputModule.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

/**
 * @author Damien LEROY
 * 
 */
public enum EnumOutputModule {

	SIMPLE("simple"), MAIL("mail");

	private final String sValue;

	private EnumOutputModule(final String sValue) {
		this.sValue = sValue;
	}

	public final String value() {
		return this.sValue;
	}

	/**
	 * Returns if the string is an output method possible value
	 * 
	 * @param sValue
	 *            string to compare
	 * @return the output method if matches and null otherwise
	 */
	public static EnumOutputModule fromValue(final String sValue) {
		for (final EnumOutputModule aEnumOutputMethod : EnumOutputModule
				.values()) {
			if (aEnumOutputMethod.sValue.equals(sValue)) {
				return aEnumOutputMethod;
			}
		}
		return null;
	}

}

--- NEW FILE: OutputFormater.java ---
// $Id: OutputFormater.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Jean-Guilhem Rouel
// (c) COPYRIGHT MIT, ERCIM and Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.io.Writer;
import java.util.Map;

import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * SimpleOutputFormater<br />
 * Created: Jul 19, 2006 5:54:33 PM<br />
 * 
 * @author Jean-Guilhem Rouel
 */
public interface OutputFormater {

	public abstract void produceOutput(
			final Map<String, Object> mapOfStringObject, final Writer aWriter)
			throws ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception;

	/**
	 * @param aException
	 * @param aWriter
	 * @throws Exception
	 * @throws MethodInvocationException
	 * @throws ParseErrorException
	 * @throws ResourceNotFoundException
	 */
	public abstract void produceError(final Exception aException,
			final Writer aWriter) throws ResourceNotFoundException,
			ParseErrorException, MethodInvocationException, Exception;

}
--- NEW FILE: SimpleOutputFormater.java ---
// $Id: SimpleOutputFormater.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.w3c.unicorn.Framework;
import org.w3c.unicorn.util.Property;
import org.w3c.unicorn.util.Templates;

/**
 * Class for simple output formater.
 * 
 * @author Jean-Guilhem ROUEL
 */
public class SimpleOutputFormater implements OutputFormater {

	private static final Log logger = LogFactory.getLog(SimpleOutputFormater.class);

	private static VelocityContext aVelocityContext;
	
	private String sOutputFormat;
	
	private String sLang;

	public SimpleOutputFormater(final String sOutputFormat, final String sLang)
		throws ResourceNotFoundException, ParseErrorException, Exception {
		SimpleOutputFormater.logger.trace("Constructor");
		SimpleOutputFormater.logger.debug("Output format : " + sOutputFormat + ".");
		SimpleOutputFormater.logger.debug("Output language : " + sLang + ".");
		
		this.sOutputFormat = sOutputFormat;
		this.sLang = sLang;
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.output.OutputFormater#produceOutput(java.util.Map,
	 *      java.io.Writer)
	 */
	public void produceOutput(final Map<String, Object> mapOfStringObject,
			final Writer aWriter) throws ResourceNotFoundException,
			ParseErrorException, MethodInvocationException, Exception {
		
		if (Framework.getLanguageContexts().get(sLang) != null) {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(sLang));
		} else {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(Property.get("DEFAULT_LANGUAGE")));
		}
		
		SimpleOutputFormater.logger.trace("produceOutput");
		SimpleOutputFormater.logger.debug("Map of String -> Object : "
				+ mapOfStringObject + ".");
		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
		
		for (final String sObjectName : mapOfStringObject.keySet()) {
			aVelocityContext.put(sObjectName, mapOfStringObject
					.get(sObjectName));
		}
		
		Templates.write(sOutputFormat + ".vm", aVelocityContext, aWriter);
		aWriter.close();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.output.OutputFormater#produceError(java.lang.Exception,
	 *      java.io.Writer)
	 */
	public void produceError(final Exception aException, final Writer aWriter)
			throws ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception {
		
		if (Framework.getLanguageContexts().get(sLang) != null) {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(sLang));
		} else {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(Property.get("DEFAULT_LANGUAGE")));
		}
		
		SimpleOutputFormater.logger.trace("produceError");
		SimpleOutputFormater.logger.debug("Error : " + aException.getMessage()
				+ ".");
		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
		if (aException != null)
			aVelocityContext.put("error", aException);
		
		Templates.write(sOutputFormat + ".error.vm", aVelocityContext, aWriter);
		aWriter.close();
	}
}

--- NEW FILE: XMLOutputFormater.java ---
// $Id: XMLOutputFormater.java,v 1.2 2009/08/28 12:40:06 jean-gui Exp $
// Author: Damien LEROY.
// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
// Please first read the full copyright statement in file COPYRIGHT.html
package org.w3c.unicorn.output;

import java.io.Writer;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.event.EventCartridge;
import org.apache.velocity.app.event.implement.EscapeXmlReference;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.w3c.unicorn.Framework;
import org.w3c.unicorn.util.Property;
import org.w3c.unicorn.util.Templates;
import org.w3c.unicorn.util.XHTMLize;

/**
 * Class for XML output formater.
 * 
 * @author Jean-Guilhem ROUEL
 */
public class XMLOutputFormater implements OutputFormater {

	/**
	 * Object used for complex logging purpose
	 */
	private static final Log logger = LogFactory.getLog(XMLOutputFormater.class);

	/**
	 * Apache velocity context
	 */
	private static VelocityContext aVelocityContext;
	
	private String sOutputFormat;
	
	private String sLang;

	/**
	 * Write the result of the XML in a file
	 * 
	 * @param sOutputFormat
	 *            format of the output
	 * @param sLang
	 *            Language of the output
	 * @throws ResourceNotFoundException
	 *             exception when resources not found using the path
	 * @throws ParseErrorException
	 *             error in the parser
	 * @throws Exception
	 *             odd error occur
	 */
	public XMLOutputFormater(final String sOutputFormat, final String sLang)
			throws ResourceNotFoundException, ParseErrorException, Exception {
		XMLOutputFormater.logger.trace("Constructor");
		XMLOutputFormater.logger.debug("Output format : " + sOutputFormat + ".");
		XMLOutputFormater.logger.debug("Output language : " + sLang + ".");

		this.sOutputFormat = sOutputFormat;
		this.sLang = sLang;
		
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.output.OutputFormater#produceOutput(java.util.Map,
	 *      java.io.Writer)
	 */
	public void produceOutput(final Map<String, Object> mapOfStringObject,
			final Writer aWriter) throws ResourceNotFoundException,
			ParseErrorException, MethodInvocationException, Exception {
		
		if (Framework.getLanguageContexts().get(sLang) != null) {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(sLang));
		} else {
			logger.debug("Context for " + sLang + " doesn't exist.");
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(Property.get("DEFAULT_LANGUAGE")));
		}
		
		XMLOutputFormater.logger.trace("produceOutput");
		XMLOutputFormater.logger.debug("Map of String -> Object : "
				+ mapOfStringObject + ".");
		XMLOutputFormater.logger.debug("Writer : " + aWriter + ".");
		
		final EventCartridge aEventCartridge = new EventCartridge();
		aEventCartridge.addEventHandler(new XHTMLize());
		aEventCartridge.attachToContext(aVelocityContext);
		for (final String sObjectName : mapOfStringObject.keySet()) {
			aVelocityContext.put(sObjectName, mapOfStringObject
					.get(sObjectName));
		}
		
		Templates.write(sOutputFormat + ".vm", aVelocityContext, aWriter);
		aWriter.close();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.w3c.unicorn.output.OutputFormater#produceError(java.lang.Exception,
	 *      java.io.Writer)
	 */
	public void produceError(final Exception aException, final Writer aWriter)
			throws ResourceNotFoundException, ParseErrorException,
			MethodInvocationException, Exception {
		
		if (Framework.getLanguageContexts().get(sLang) != null) {
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(sLang));
		} else {
			logger.debug("Context for " + sLang + " doesn't exist.");
			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(Property.get("DEFAULT_LANGUAGE")));
		}
		
		XMLOutputFormater.logger.trace("produceError");
		XMLOutputFormater.logger.debug("Error : " + aException + ".");
		XMLOutputFormater.logger.debug("Writer : " + aWriter + ".");
		final EventCartridge aEventCartridge = new EventCartridge();
		aEventCartridge.addEventHandler(new XHTMLize());
		aEventCartridge.attachToContext(aVelocityContext);
		aVelocityContext.put("error", aException);
		Templates.write(sOutputFormat + ".error.vm", aVelocityContext, aWriter);
		aWriter.close();
	}
}

Received on Friday, 28 August 2009 12:40:19 UTC