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

2006/unicorn/org/w3c/unicorn/output SimpleOutputFormater.java,1.5,1.6 XMLOutputFormater.java,1.6,1.7

From: Thomas Gambet via cvs-syncmail <cvsmail@w3.org>
Date: Wed, 29 Jul 2009 09:18:26 +0000
To: www-validator-cvs@w3.org
Message-Id: <E1MW5Io-0000VE-Im@lionel-hutz.w3.org>
Update of /sources/public/2006/unicorn/org/w3c/unicorn/output
In directory hutz:/tmp/cvs-serv1802/org/w3c/unicorn/output

Modified Files:
	SimpleOutputFormater.java XMLOutputFormater.java 
Log Message:
Internationalized IO errors
Refactoring of template related functions in TemplateHelper class

Index: XMLOutputFormater.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/output/XMLOutputFormater.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- XMLOutputFormater.java	28 Jul 2009 10:36:31 -0000	1.6
+++ XMLOutputFormater.java	29 Jul 2009 09:18:24 -0000	1.7
@@ -4,24 +4,17 @@
 // Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.output;
 
-import java.io.File;
 import java.io.Writer;
-import java.net.URL;
 import java.util.Map;
-import java.util.Properties;
 
 import org.apache.commons.logging.Log;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
-import org.apache.velocity.app.VelocityEngine;
 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.ListFiles;
-import org.w3c.unicorn.util.MergeProperties;
-import org.w3c.unicorn.util.Property;
+import org.w3c.unicorn.util.TemplateHelper;
 
 /**
  * Class for XML output formater.
@@ -46,15 +39,8 @@
 	private Template aTemplateError = null;
 
 	/**
-	 * Apache velocity engine for the output
-	 */
-	private static VelocityEngine aVelocityEngineOutput = new VelocityEngine();
-
-	/**
-	 * Apache velocity engine for the error
+	 * Apache velocity context
 	 */
-	private static VelocityEngine aVelocityEngineError = new VelocityEngine();
-	
 	private static VelocityContext aVelocityContext = new VelocityContext();
 
 	/**
@@ -68,58 +54,12 @@
 	public XMLOutputFormater(final String sOutputFormat, final String sLang)
 			throws ResourceNotFoundException, ParseErrorException, Exception {
 		XMLOutputFormater.logger.trace("Constructor");
-		if (XMLOutputFormater.logger.isDebugEnabled()) {
-			XMLOutputFormater.logger.debug("Output format : " + sOutputFormat
-					+ ".");
-			XMLOutputFormater.logger.debug("Output language : " + sLang + ".");
-		}
-		
-		// Template file for this output
-		String sFileName = sOutputFormat + Property.get("TEMPLATE_FILE_EXTENSION");
-		
-		// Language file for this output
-		File langFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
-				sOutputFormat + "." + sLang + ".properties");
-		
-		// Default language file
-		File defaultLangFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
-				sOutputFormat + "." + Property.get("DEFAULT_LANGUAGE") + ".properties");
-		
-		// Merge the properties
-		Properties props = MergeProperties.getMergeProperties(defaultLangFile, langFile);
-		
-		// Load in velocity context
-		MergeProperties.loadInVelocityContext(props, aVelocityContext);
-		
-		// check if sFileName exist
-		try {
-			this.aTemplateOutput = XMLOutputFormater.aVelocityEngineOutput
-					.getTemplate(sFileName,"UTF-8");
-		} catch (final ResourceNotFoundException aRNFE) {
-			XMLOutputFormater.logger.warn("Resource " + sFileName
-					+ " not found.", aRNFE);
-			sFileName = Property.get("DEFAULT_LANGUAGE") + "_" + sOutputFormat
-					+ Property.get("TEMPLATE_FILE_EXTENSION");
-			XMLOutputFormater.logger.warn("Redirect to resource : " + sFileName
-					+ ".");
-			this.aTemplateOutput = XMLOutputFormater.aVelocityEngineOutput
-					.getTemplate(sFileName);
-		}
-		sFileName = sOutputFormat + ".error" + Property.get("TEMPLATE_FILE_EXTENSION");
+		XMLOutputFormater.logger.debug("Output format : " + sOutputFormat + ".");
+		XMLOutputFormater.logger.debug("Output language : " + sLang + ".");
 		
-		try {
-			this.aTemplateError = XMLOutputFormater.aVelocityEngineError
-					.getTemplate(sFileName);
-		} catch (final ResourceNotFoundException aRNFE) {
-			XMLOutputFormater.logger.warn("Resource " + sFileName
-					+ " not found.", aRNFE);
-			sFileName = Property.get("DEFAULT_LANGUAGE") + "_" + sOutputFormat
-					+ Property.get("TEMPLATE_FILE_EXTENSION");
-			XMLOutputFormater.logger.warn("Redirect to resource : " + sFileName
-					+ ".");
-			this.aTemplateError = XMLOutputFormater.aVelocityEngineError
-					.getTemplate(sFileName);
-		}
+		aTemplateOutput = TemplateHelper.getInternationalizedTemplate(sOutputFormat, sLang, aVelocityContext);
+		aTemplateError = TemplateHelper.getInternationalizedTemplate(sOutputFormat + ".error", sLang, aVelocityContext);
+
 	}
 
 	/*
@@ -132,21 +72,14 @@
 			final Writer aWriter) throws ResourceNotFoundException,
 			ParseErrorException, MethodInvocationException, Exception {
 		XMLOutputFormater.logger.trace("produceOutput");
-		if (XMLOutputFormater.logger.isDebugEnabled()) {
-			XMLOutputFormater.logger.debug("Map of String -> Object : "
-					+ mapOfStringObject + ".");
-			XMLOutputFormater.logger.debug("Writer : " + aWriter + ".");
-		}
-
+		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));
-		}
+		for (final String sObjectName : mapOfStringObject.keySet())
+			aVelocityContext.put(sObjectName, mapOfStringObject.get(sObjectName));
 		this.aTemplateOutput.merge(aVelocityContext, aWriter);
 	}
 
@@ -160,38 +93,12 @@
 			throws ResourceNotFoundException, ParseErrorException,
 			MethodInvocationException, Exception {
 		XMLOutputFormater.logger.trace("produceError");
-		if (XMLOutputFormater.logger.isDebugEnabled()) {
-			XMLOutputFormater.logger.debug("Error : " + aException + ".");
-			XMLOutputFormater.logger.debug("Writer : " + aWriter + ".");
-		}
+		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);
 	}
-
-	static {
-		try {
-			final Properties aProperties = new Properties();
-			
-			aProperties.load(new URL(Property.class.getResource("/"),
-					Property.get("REL_PATH_TO_CONF_FILES") + "velocity.properties").openStream());
-
-			aProperties.put(Velocity.FILE_RESOURCE_LOADER_PATH, Property
-					.get("PATH_TO_TEMPLATES"));
-			
-			XMLOutputFormater.aVelocityEngineOutput.init(aProperties);
-			XMLOutputFormater.logger.debug("OutputEngine "
-					+ Velocity.FILE_RESOURCE_LOADER_PATH);
-			aProperties.put(Velocity.FILE_RESOURCE_LOADER_PATH, Property
-					.get("PATH_TO_TEMPLATES"));
-			XMLOutputFormater.aVelocityEngineError.init(aProperties);
-		} catch (final Exception e) {
-			XMLOutputFormater.logger.error("Exception : " + e.getMessage(), e);
-			e.printStackTrace();
-		}
-	}
-
 }

Index: SimpleOutputFormater.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/output/SimpleOutputFormater.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- SimpleOutputFormater.java	28 Jul 2009 10:36:31 -0000	1.5
+++ SimpleOutputFormater.java	29 Jul 2009 09:18:24 -0000	1.6
@@ -4,22 +4,16 @@
 // Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.output;
 
-import java.io.File;
 import java.io.Writer;
-import java.net.URL;
 import java.util.Map;
-import java.util.Properties;
 
 import org.apache.commons.logging.Log;
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
-import org.apache.velocity.app.VelocityEngine;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
 import org.apache.velocity.exception.ResourceNotFoundException;
-import org.w3c.unicorn.util.MergeProperties;
-import org.w3c.unicorn.util.Property;
+import org.w3c.unicorn.util.TemplateHelper;
 
 /**
  * Class for simple output formater.
@@ -33,43 +27,16 @@
 	private Template aTemplateOutput = null;
 
 	private Template aTemplateError = null;
-
-	private static VelocityEngine aVelocityEngineOutput = new VelocityEngine();
-
-	private static VelocityEngine aVelocityEngineError = new VelocityEngine();
 	
 	private static VelocityContext aVelocityContext = new VelocityContext();
 
 	public SimpleOutputFormater(final String sOutputFormat, final String sLang)
 			throws ResourceNotFoundException, ParseErrorException, Exception {
 		SimpleOutputFormater.logger.trace("Constructor");
-		if (SimpleOutputFormater.logger.isDebugEnabled()) {
-			SimpleOutputFormater.logger.debug("Output format : "
-					+ sOutputFormat + ".");
-			SimpleOutputFormater.logger.debug("Output language : " + sLang
-					+ ".");
-		}
-		
-		String sFileName = sOutputFormat + Property.get("TEMPLATE_FILE_EXTENSION");
-		
-		// Language file for this output
-		File langFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
-				sOutputFormat + "." + sLang + ".properties");
-		
-		// Default language file
-		File defaultLangFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
-				sOutputFormat + "." + Property.get("DEFAULT_LANGUAGE") + ".properties");
-		
-		// Merge the properties
-		Properties props = MergeProperties.getMergeProperties(defaultLangFile, langFile);
-		
-		// Load in velocity context
-		MergeProperties.loadInVelocityContext(props, aVelocityContext);
-		
-		this.aTemplateOutput = SimpleOutputFormater.aVelocityEngineOutput
-				.getTemplate(sFileName,"UTF-8");
-		this.aTemplateError = SimpleOutputFormater.aVelocityEngineError
-				.getTemplate(sFileName,"UTF-8");
+		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);
 	}
 
 	/*
@@ -82,15 +49,10 @@
 			final Writer aWriter) throws ResourceNotFoundException,
 			ParseErrorException, MethodInvocationException, Exception {
 		SimpleOutputFormater.logger.trace("produceOutput");
-		if (SimpleOutputFormater.logger.isDebugEnabled()) {
-			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));
-		}
+		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);
 	}
 
@@ -104,35 +66,9 @@
 			throws ResourceNotFoundException, ParseErrorException,
 			MethodInvocationException, Exception {
 		SimpleOutputFormater.logger.trace("produceError");
-		if (SimpleOutputFormater.logger.isDebugEnabled()) {
-			SimpleOutputFormater.logger.debug("Error : "
-					+ aException.getMessage() + ".");
-			SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
-		}
+		SimpleOutputFormater.logger.debug("Error : " + aException.getMessage() + ".");
+		SimpleOutputFormater.logger.debug("Writer : " + aWriter + ".");
 		aVelocityContext.put("error", aException);
 		this.aTemplateError.merge(aVelocityContext, aWriter);
 	}
-
-	static {
-		try {
-			final Properties aProperties = new Properties();
-			
-			aProperties.load(new URL(Property.class.getResource("/"),
-					Property.get("REL_PATH_TO_CONF_FILES") + "velocity.properties").openStream());
-
-			aProperties.put(Velocity.FILE_RESOURCE_LOADER_PATH, Property
-					.get("PATH_TO_TEMPLATES"));
-			SimpleOutputFormater.aVelocityEngineOutput.init(aProperties);
-			SimpleOutputFormater.logger.debug("OutputEngine "
-					+ Velocity.FILE_RESOURCE_LOADER_PATH);
-			aProperties.put(Velocity.FILE_RESOURCE_LOADER_PATH, Property
-					.get("PATH_TO_TEMPLATES"));
-			SimpleOutputFormater.aVelocityEngineError.init(aProperties);
-		} catch (final Exception e) {
-			SimpleOutputFormater.logger.error("Exception : " + e.getMessage(),
-					e);
-			e.printStackTrace();
-		}
-	}
-
 }
Received on Wednesday, 29 July 2009 09:18:36 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Thursday, 26 April 2012 12:55:09 GMT