2006/unicorn/src/org/w3c/unicorn/output OutputFactory.java,1.3,1.4 OutputFormater.java,1.2,1.3 SimpleOutputFormater.java,1.2,1.3 XMLOutputFormater.java,1.4,1.5

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

Modified Files:
	OutputFactory.java OutputFormater.java 
	SimpleOutputFormater.java XMLOutputFormater.java 
Log Message:
simplified outputs a bit

Index: XMLOutputFormater.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/output/XMLOutputFormater.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- XMLOutputFormater.java	1 Sep 2009 13:39:41 -0000	1.4
+++ XMLOutputFormater.java	1 Sep 2009 16:00:24 -0000	1.5
@@ -4,19 +4,7 @@
 // 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.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;
 
 /**
@@ -24,106 +12,20 @@
  * 
  * @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 VelocityContext aVelocityContext;
-	
-	private String sOutputFormat;
+public class XMLOutputFormater extends SimpleOutputFormater {
 	
-	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;
-		
+	public XMLOutputFormater(final String format, final String lang) {
+		super(format, lang);
 	}
+	
+	public void setLang(String lang) {
+		super.setLang(lang);
 
-	/*
-	 * (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 + ".");
-		
+		// Replace tag objects (A, Img, ...) with their XHTML representation
 		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();
-	}
-}
+}
\ No newline at end of file

Index: OutputFormater.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/output/OutputFormater.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- OutputFormater.java	28 Aug 2009 12:40:06 -0000	1.2
+++ OutputFormater.java	1 Sep 2009 16:00:24 -0000	1.3
@@ -7,6 +7,8 @@
 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;
@@ -19,8 +21,18 @@
  */
 public interface OutputFormater {
 
+	public static final Log logger = LogFactory.getLog(OutputFormater.class);
+	
+	/**
+	 * @param mapOfStringObject
+	 * @param output
+	 * @throws ResourceNotFoundException
+	 * @throws ParseErrorException
+	 * @throws MethodInvocationException
+	 * @throws Exception
+	 */
 	public abstract void produceOutput(
-			final Map<String, Object> mapOfStringObject, final Writer aWriter)
+			final Map<String, Object> mapOfStringObject, final Writer output)
 			throws ResourceNotFoundException, ParseErrorException,
 			MethodInvocationException, Exception;
 
@@ -33,7 +45,7 @@
 	 * @throws ResourceNotFoundException
 	 */
 	public abstract void produceError(final Exception aException,
-			final Writer aWriter) throws ResourceNotFoundException,
+			final Writer output) throws ResourceNotFoundException,
 			ParseErrorException, MethodInvocationException, Exception;
 
 }
\ No newline at end of file

Index: SimpleOutputFormater.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/output/SimpleOutputFormater.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- SimpleOutputFormater.java	28 Aug 2009 12:40:06 -0000	1.2
+++ SimpleOutputFormater.java	1 Sep 2009 16:00:24 -0000	1.3
@@ -7,8 +7,6 @@
 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;
@@ -24,25 +22,47 @@
  */
 public class SimpleOutputFormater implements OutputFormater {
 
-	private static final Log logger = LogFactory.getLog(SimpleOutputFormater.class);
-
-	private static VelocityContext aVelocityContext;
+	protected VelocityContext aVelocityContext;
 	
-	private String sOutputFormat;
+	private String format;
 	
-	private String sLang;
+	private String lang;
+	
+	public SimpleOutputFormater() {
+		setLang(Property.get("DEFAULT_LANGUAGE"));
+		setFormat(Property.get("DEFAULT_FORMAT"));
+	}
 
-	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;
+	public SimpleOutputFormater(final String format, final String lang) {
+		OutputFormater.logger.trace("Constructor");
+		OutputFormater.logger.debug("Output format : " + format + ".");
+		OutputFormater.logger.debug("Output language : " + lang + ".");
 		
+		setFormat(format);
+		setLang(lang);		
+	}
+
+	public String getLang() {
+		return lang;
+	}
+
+	public void setLang(String lang) {
+		this.lang = lang;
+		if (Framework.getLanguageContexts().get(lang) != null) {
+			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(lang));
+		} else {
+			aVelocityContext = new VelocityContext(Framework.getLanguageContexts().get(Property.get("DEFAULT_LANGUAGE")));
+		}
 	}
 
+	public String getFormat() {
+		return format;
+	}
+
+	public void setFormat(String outputFormat) {
+		this.format = outputFormat;
+	}
+	
 	/*
 	 * (non-Javadoc)
 	 * 
@@ -50,27 +70,21 @@
 	 *      java.io.Writer)
 	 */
 	public void produceOutput(final Map<String, Object> mapOfStringObject,
-			final Writer aWriter) throws ResourceNotFoundException,
+			final Writer output) 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 : "
+
+		OutputFormater.logger.trace("produceOutput");
+		OutputFormater.logger.debug("Map of String -> Object : "
 				+ mapOfStringObject + ".");
-		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
+		OutputFormater.logger.debug("Writer : " + output + ".");
 		
 		for (final String sObjectName : mapOfStringObject.keySet()) {
 			aVelocityContext.put(sObjectName, mapOfStringObject
 					.get(sObjectName));
 		}
 		
-		Templates.write(sOutputFormat + ".vm", aVelocityContext, aWriter);
-		aWriter.close();
+		Templates.write(format + ".vm", aVelocityContext, output);
+		output.close();
 	}
 
 	/*
@@ -79,24 +93,19 @@
 	 * @see org.w3c.unicorn.output.OutputFormater#produceError(java.lang.Exception,
 	 *      java.io.Writer)
 	 */
-	public void produceError(final Exception aException, final Writer aWriter)
+	public void produceError(final Exception aException, final Writer output)
 			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()
+		OutputFormater.logger.trace("produceError");
+		OutputFormater.logger.debug("Error : " + aException.getMessage()
 				+ ".");
-		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
+		OutputFormater.logger.debug("Writer : " + output + ".");
 		if (aException != null)
 			aVelocityContext.put("error", aException);
 		
-		Templates.write(sOutputFormat + ".error.vm", aVelocityContext, aWriter);
-		aWriter.close();
+		Templates.write(format + ".error.vm", aVelocityContext, output);
+		output.close();
 	}
+
 }

Index: OutputFactory.java
===================================================================
RCS file: /sources/public/2006/unicorn/src/org/w3c/unicorn/output/OutputFactory.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- OutputFactory.java	28 Aug 2009 16:11:41 -0000	1.3
+++ OutputFactory.java	1 Sep 2009 16:00:24 -0000	1.4
@@ -4,9 +4,6 @@
 // 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;
@@ -24,38 +21,39 @@
 
 	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.
+	 * Create a new output module
 	 * 
 	 * @param aEnumOutputModule
 	 *            To identify which type of output module will be created.
 	 * @return The new output module.
 	 */
-	private static OutputModule createOutputModule(
-			final EnumOutputModule aEnumOutputModule) {
+	public static OutputModule createOutputModule(String module) {
 		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.logger.debug("Output module : " + module);
 		}
-		OutputFactory.mapOfOutputModule.put(aEnumOutputModule, aOutputModule);
-		return aOutputModule;
+		
+		/* Commented out for now as this is unnecessary and that doesn't seem quite safe */		
+//		if(null == module || "".equals(module)) {
+//			module = "simple";
+//		}
+//		
+//		module = module.substring(0, 1).toUpperCase() + module.substring(1);
+//		
+//		Class<?> moduleClass;
+//		try {
+//			moduleClass = Class.forName("org.w3c.unicorn.output." + module + "OutputModule");
+//			return (OutputModule) moduleClass.getConstructor().newInstance();
+//		} catch (Exception e) {
+//			OutputFactory.logger.error("Couldn't create output module " + module + ". Will use SimpleOutputModule", e);
+//		}
+
+		return new SimpleOutputModule();
 	}
 
 	/**
-	 * Create a new output formatter and add it to the map.
+	 * Create a new output formatter.
 	 * 
 	 * @param sOutputFormat
 	 *            The format who the output formatter must produce.
@@ -64,7 +62,7 @@
 	 * @throws ParseErrorException
 	 * @throws Exception
 	 */
-	private static OutputFormater createOutputFormater(
+	public static OutputFormater createOutputFormater(
 			final String sOutputFormat, final String sLang,
 			final String sMimeType) throws ResourceNotFoundException,
 			ParseErrorException, Exception {
@@ -76,6 +74,8 @@
 			OutputFactory.logger.debug("Mime type : " + sMimeType + ".");
 		}
 
+		final OutputFormater aOutputFormater;
+		
 		final String sFormaterName = Property.getProps("specialFormaters.properties")
 											 .getProperty(sMimeType);
 		if (null != sFormaterName) {
@@ -83,101 +83,15 @@
 					.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;
+		else {
+			aOutputFormater = new SimpleOutputFormater(sOutputFormat, sLang);
 		}
-		// if output formater not already exist create it
-		aOutputFormater = OutputFactory.createOutputFormater(sOutputFormat,
-				sLang, sMimeType);
 
 		return aOutputFormater;
 	}
-
+	
 }

Received on Tuesday, 1 September 2009 16:00:36 UTC