W3C home > Mailing lists > Public > www-validator-cvs@w3.org > August 2009

2006/unicorn/src/org/w3c/unicorn/output SimpleOutputModule.java,NONE,1.1.2.1 OutputFormater.java,NONE,1.1.2.1 XMLOutputFormater.java,NONE,1.1.2.1 SimpleOutputFormater.java,NONE,1.1.2.1 OutputModule.java,NONE,1.1.2.1 OutputFactory.java,NONE,1.1.2.1 XHTMLize.java,NONE,1.1.2.1 EscapeXMLEntities.java,NONE,1.1.2.1 EnumOutputModule.java,NONE,1.1.2.1

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Tue, 11 Aug 2009 16:20:45 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1Mau5d-0001sG-Po@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/src/org/w3c/unicorn/output
In directory hutz:/tmp/cvs-serv7188/src/org/w3c/unicorn/output

Added Files:
      Tag: dev2
	SimpleOutputModule.java OutputFormater.java 
	XMLOutputFormater.java SimpleOutputFormater.java 
	OutputModule.java OutputFactory.java XHTMLize.java 
	EscapeXMLEntities.java EnumOutputModule.java 
Log Message:
commit

--- NEW FILE: OutputFactory.java ---
// $Id: OutputFactory.java,v 1.1.2.1 2009/08/11 16:20:43 tgambet 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.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

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 formater. 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("org.w3c.unicorn.output");

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

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

	private static Properties aPropertiesSpecialFormaters;

	public static void init() {
		
		OutputFactory.aPropertiesSpecialFormaters = Property.getProps("specialFormaters.properties");
		
		/*try {
			OutputFactory.aPropertiesSpecialFormaters.load(Property
					.getPropertyFileURL("specialFormaters.properties")
					.openStream());
		} catch (final IOException e) {
			OutputFactory.logger.error("IOException : " + e.getMessage(), e);
			e.printStackTrace();
		}*/
	}

	/**
	 * 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 formater and add it to the map.
	 * 
	 * @param sOutputFormat
	 *            The format who the output formater must produce.
	 * @return The new output formater.
	 * @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 = OutputFactory.aPropertiesSpecialFormaters
				.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);
	}

	/**
	 * Retuern 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 formater asked.
	 * 
	 * @param sOutputFormat
	 *            The output format who be puduce by the output formater.
	 * @return The output formater 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.1.2.1 2009/08/11 16:20:43 tgambet 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.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 = OutputFactory.logger;

	/**
	 * 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.1.2.1 2009/08/11 16:20:42 tgambet 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.1.2.1 2009/08/11 16:20:43 tgambet 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.1.2.1 2009/08/11 16:20:42 tgambet 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: EscapeXMLEntities.java ---
package org.w3c.unicorn.output;

import org.apache.velocity.app.event.ReferenceInsertionEventHandler;

/**
 * Escape all XML Entities in the reference insertion. Specifically, the
 * following conversions are performed:
 * <DL>
 * <DT>&amp;</DT>
 * <DD>&amp;amp;</DD>
 * <DT>&lt;</DT>
 * <DD>&amp;lt;</DD>
 * <DT>&gt;</DT>
 * <DD>&amp;gt;</DD>
 * <DT>&quot;</DT>
 * <DD>&amp;quot;</DD>
 * </DL>
 * 
 * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
 */
public class EscapeXMLEntities implements ReferenceInsertionEventHandler {

	/**
	 * Escape the XML entities for all inserted references.
	 */
	public Object referenceInsert(final String sUnused, final Object oValue) {
		final String sValue = oValue.toString();
		return EscapeXMLEntities.escapeText(sValue);
	}

	/**
	 * Escape the provided text.
	 * 
	 * @param sValue
	 * @return
	 */
	public static String escapeText(final String sValue) {
		final StringBuffer aStringBuffer = new StringBuffer(sValue.length());
		final int iLength = sValue.length();
		int iPosition = 0;
		int iNextPosition = EscapeXMLEntities.nextPosition(sValue, iPosition);

		while (iNextPosition != -1) {
			aStringBuffer.append(sValue.substring(iPosition, iNextPosition));
			aStringBuffer.append(EscapeXMLEntities.escapeChar(sValue
					.charAt(iNextPosition)));
			iPosition = iNextPosition + 1;
			if (iPosition < iLength) {
				iNextPosition = EscapeXMLEntities.nextPosition(sValue,
						iPosition);
			} else {
				iNextPosition = -1;
			}
		}

		if (iPosition < iLength) {
			aStringBuffer.append(sValue.substring(iPosition));
		}

		return aStringBuffer.toString();
	}

	/**
	 * Transform the escape char into xml encoding
	 * 
	 * @param c
	 *            char to transform
	 * @return same character in xml form
	 */
	private static String escapeChar(final char c) {
		switch (c) {
		case '<':
			return "&lt;";
		case '>':
			return "&gt;";
		case '&':
			return "&amp;";
		case '"':
			return "&quot;";
		}
		return null;
	}

	/**
	 * Returns the position of the next xml tag
	 * 
	 * @param s
	 *            string to analyze
	 * @param iCurrentPosition
	 *            current position in the string
	 * @return position of the next tag
	 */
	private static int nextPosition(final String s, final int iCurrentPosition) {
		final int iLT = s.indexOf('<', iCurrentPosition);
		final int iGT = s.indexOf('>', iCurrentPosition);
		final int iAMP = s.indexOf('&', iCurrentPosition);
		final int iQUOT = s.indexOf('"', iCurrentPosition);

		if ((iLT != -1) && ((iGT == -1) || (iLT < iGT))
				&& ((iAMP == -1) || (iLT < iAMP))
				&& ((iQUOT == -1) || (iLT < iQUOT))) {
			return iLT;
		}
		if ((iGT != -1) && ((iAMP == -1) || (iGT < iAMP))
				&& ((iQUOT == -1) || (iGT < iQUOT))) {
			return iGT;
		}
		if ((iAMP != -1) && ((iQUOT == -1) || (iAMP < iQUOT))) {
			return iAMP;
		}
		return iQUOT;
	}

}

--- NEW FILE: XHTMLize.java ---
package org.w3c.unicorn.output;

import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
import org.w3c.unicorn.response.A;
import org.w3c.unicorn.response.Code;
import org.w3c.unicorn.response.Img;

/**
 * Escape all XML Entities in the reference insertion. Specifically, the
 * following conversions are performed:
 * <DL>
 * <DT>&amp;</DT>
 * <DD>&amp;amp;</DD>
 * <DT>&lt;</DT>
 * <DD>&amp;lt;</DD>
 * <DT>&gt;</DT>
 * <DD>&amp;gt;</DD>
 * <DT>&quot;</DT>
 * <DD>&amp;quot;</DD>
 * </DL>
 * 
 * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
 */
public class XHTMLize implements ReferenceInsertionEventHandler {

	/**
	 * Escape the XML entities for all inserted references.
	 */
	public Object referenceInsert(final String sUnused, final Object oValue) {
		if (oValue == null) {
			return null;
		}
		if (oValue instanceof A) {
			A link = (A) oValue;
			return insertA(link);
		}
		if (oValue instanceof Img) {
			Img image = (Img) oValue;
			return insertImg(image);
		}
		if (oValue instanceof Code) {
			Code code = (Code) oValue;
			return insertCode(code);
		}
		return EscapeXMLEntities.escapeText(oValue.toString());
	}

	/**
	 * Insert a link
	 * 
	 * @param aLink
	 *            link to insert
	 * @return return the object containing the link
	 */
	private Object insertA(final A aLink) {
		String sResultat = "<a href=\""
				+ EscapeXMLEntities.escapeText(aLink.getHref()) + "\">";
		for (final Object oElement : aLink.getContent()) {
			if (oElement instanceof Img) {
				sResultat += insertImg((Img) oElement);
			} else {
				sResultat += EscapeXMLEntities.escapeText(oElement.toString());
			}
		}
		sResultat += "</a>";
		return sResultat;
	}

	/**
	 * Insert code tag into the tags
	 * 
	 * @param aCode
	 *            code to insert
	 * @return object with code inserted
	 */
	private Object insertCode(final Code aCode) {
		String sResultat = "<code>";
		for (final Object oElement : aCode.getContent()) {
			if (oElement instanceof A) {
				sResultat += insertA((A) oElement);
			} else if (oElement instanceof Img) {
				sResultat += insertImg((Img) oElement);
			} else {
				sResultat += EscapeXMLEntities.escapeText(oElement.toString());
			}
		}
		sResultat += "</code>";
		return sResultat;
	}

	/**
	 * Insert an img tag
	 * 
	 * @param img
	 *            image path to insert
	 * @return the string containing the image tag
	 */
	private String insertImg(final Img aImage) {
		return "<img src=\"" + EscapeXMLEntities.escapeText(aImage.getSrc())
				+ "\" alt=\"" + EscapeXMLEntities.escapeText(aImage.getAlt())
				+ "\"/>";
	}

}

--- NEW FILE: SimpleOutputFormater.java ---
// $Id: SimpleOutputFormater.java,v 1.1.2.1 2009/08/11 16:20:43 tgambet 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.velocity.Template;
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.util.TemplateHelper;

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

	private static final Log logger = OutputFactory.logger;

	private Template aTemplateOutput = null;

	private Template aTemplateError = null;

	private static VelocityContext aVelocityContext = new VelocityContext();

	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.aTemplateOutput = TemplateHelper.getInternationalizedTemplate(
				sOutputFormat, sLang, aVelocityContext);
		this.aTemplateError = TemplateHelper.getInternationalizedTemplate(
				sOutputFormat + ".error", sLang, aVelocityContext);
	}

	/*
	 * (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 {
		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));
		}
		this.aTemplateOutput.merge(aVelocityContext, aWriter);
	}

	/*
	 * (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 {
		SimpleOutputFormater.logger.trace("produceError");
		SimpleOutputFormater.logger.debug("Error : " + aException.getMessage()
				+ ".");
		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
		aVelocityContext.put("error", aException);
		this.aTemplateError.merge(aVelocityContext, aWriter);
	}
}

--- NEW FILE: XMLOutputFormater.java ---
// $Id: XMLOutputFormater.java,v 1.1.2.1 2009/08/11 16:20:43 tgambet 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.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.event.EventCartridge;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.w3c.unicorn.util.TemplateHelper;

/**
 * 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 = OutputFactory.logger;

	/**
	 * Template for the standard output
	 */
	private Template aTemplateOutput = null;

	/**
	 * Template used for the standard error
	 */
	private Template aTemplateError = null;

	/**
	 * Apache velocity context
	 */
	private static VelocityContext aVelocityContext = new VelocityContext();

	/**
	 * 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 + ".");

		aTemplateOutput = TemplateHelper.getInternationalizedTemplate(
				sOutputFormat, sLang, aVelocityContext);
		aTemplateError = TemplateHelper.getInternationalizedTemplate(
				sOutputFormat + ".error", sLang, aVelocityContext);
	}

	/*
	 * (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 {
		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.addEventHandler(new EscapeXMLEntities());
		aEventCartridge.attachToContext(aVelocityContext);
		for (final String sObjectName : mapOfStringObject.keySet()) {
			aVelocityContext.put(sObjectName, mapOfStringObject
					.get(sObjectName));
		}
		this.aTemplateOutput.merge(aVelocityContext, aWriter);
	}

	/*
	 * (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 {
		XMLOutputFormater.logger.trace("produceError");
		XMLOutputFormater.logger.debug("Error : " + aException + ".");
		XMLOutputFormater.logger.debug("Writer : " + aWriter + ".");
		final EventCartridge aEventCartridge = new EventCartridge();
		aEventCartridge.addEventHandler(new EscapeXMLEntities());
		aEventCartridge.attachToContext(aVelocityContext);
		aVelocityContext.put("error", aException);
		this.aTemplateError.merge(aVelocityContext, aWriter);
	}
}
Received on Tuesday, 11 August 2009 16:20:54 UTC

This archive was generated by hypermail 2.3.1 : Wednesday, 7 January 2015 15:17:38 UTC