2006/unicorn/org/w3c/unicorn/util TemplateHelper.java,1.5,1.6

Update of /sources/public/2006/unicorn/org/w3c/unicorn/util
In directory hutz:/tmp/cvs-serv8979/org/w3c/unicorn/util

Modified Files:
	TemplateHelper.java 
Log Message:
new templates

Index: TemplateHelper.java
===================================================================
RCS file: /sources/public/2006/unicorn/org/w3c/unicorn/util/TemplateHelper.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- TemplateHelper.java	29 Jul 2009 13:23:34 -0000	1.5
+++ TemplateHelper.java	31 Jul 2009 11:16:26 -0000	1.6
@@ -1,6 +1,6 @@
 // $Id$
 // Author: Thomas GAMBET.
-// (c) COPYRIGHT MIT, ERCIM ant Keio, 2006.
+// (c) COPYRIGHT MIT, ERCIM ant Keio, 2009.
 // Please first read the full copyright statement in file COPYRIGHT.html
 package org.w3c.unicorn.util;
 
@@ -57,19 +57,68 @@
 			e.printStackTrace();
 		}
 	}
+	
+	@SuppressWarnings("finally")
+	public static Properties getPropsFromFile(File propFile) {
+		Properties props = new Properties();
+		try { 
+			props.load(propFile.toURL().openStream());
+		} catch (IOException e) {
+			logger.error("Unable to load properties file : " + e.getMessage(), e);
+			e.printStackTrace();
+		} finally {
+			return props;
+		}
+	}
+	
+	public static Properties getMergeProps(Properties defaultProps, Properties sourceProps) {
+		Properties propMerge = new Properties();
+		
+		Set<Object> keys = defaultProps.keySet();
+		Iterator<Object> itr = keys.iterator();
+		String key;
+		
+		while (itr.hasNext()) {
+			key = itr.next().toString();
+			if (sourceProps.containsKey(key))
+				propMerge.put(key, sourceProps.get(key));
+			else
+				propMerge.put(key, defaultProps.get(key));
+		}
+		
+		keys = sourceProps.keySet();
+		itr = keys.iterator();
+		while (itr.hasNext()) {
+			key = itr.next().toString();
+			if (!defaultProps.containsKey(key))
+				propMerge.put(key, sourceProps.get(key));
+		}
+		
+		return propMerge;
+	}
   
-	public static Properties getMergeProperties(File defaultPropFile, File sourcePropFile) { 
+	public static Properties getMergePropsFromFiles(File defaultPropFile, File sourcePropFile) { 
 		Properties defaultProps = new Properties();
 		Properties sourceProps = new Properties();
 		try { 
 			defaultProps.load(defaultPropFile.toURL().openStream());
+		} catch (IOException e) {
+			logger.error("Unable to load default language properties : " + e.getMessage(), e);
+			e.printStackTrace();
+			return null;
+		}
+		
+		try { 
 			sourceProps.load(sourcePropFile.toURL().openStream());
 		} catch (IOException e) {
-			logger.error("Merge Properties Error : " + e.getMessage(), e);
+			logger.error("Unable to find desired language properties : " + e.getMessage(), e);
 			e.printStackTrace();
+			return defaultProps;
 		}
 		
-		Properties propMerge = new Properties();
+		return getMergeProps(defaultProps, sourceProps);
+		
+		/*Properties propMerge = new Properties();
 		
 		Set<Object> keys = defaultProps.keySet();
 		Iterator<Object> itr = keys.iterator();
@@ -83,7 +132,7 @@
 				propMerge.put(key, defaultProps.get(key));
 		}
 		
-		return propMerge;
+		return propMerge;*/
 	}
 	
 	public static void loadInVelocityContext(Properties props, VelocityContext context) {
@@ -99,6 +148,8 @@
 	public static Template getInternationalizedTemplate(String templateName, String langCode, VelocityContext context) {
 		
 		if (langCode != null) {
+			context.put("lang", langCode);
+			
 			// Error templates have the same language properties file that their coresponding non-error template
 			String langFileName = templateName;
 			if(templateName.length() > 6 && templateName.substring(templateName.length()-6, templateName.length()).equals(".error"))
@@ -114,15 +165,27 @@
 			
 			// Merge the properties or use default language
 			Properties mergedProps = new Properties();
-			if (langFile.exists())
-				mergedProps = TemplateHelper.getMergeProperties(defaultLangFile, langFile);
-			else
+			
+			if (langFile.exists()) {
+				mergedProps = getMergePropsFromFiles(defaultLangFile, langFile);
+			}
+			else {
 				try {
 					mergedProps.load(defaultLangFile.toURL().openStream());
 				} catch (IOException e1) {
 					logger.error("IOException : " + e1.getMessage(), e1);
 					e1.printStackTrace();
 				}
+			}
+			
+			File generalLangFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
+					"general." + langCode + ".properties");
+			
+			if(!generalLangFile.exists())
+				generalLangFile = new File(Property.get("PATH_TO_LANGUAGE_FILES") +
+						"general." + Property.get("DEFAULT_LANGUAGE") + ".properties");
+			
+			mergedProps = getMergeProps(mergedProps, getPropsFromFile(generalLangFile));
 			
 			// Load in velocity context
 			TemplateHelper.loadInVelocityContext(mergedProps, context);

Received on Friday, 31 July 2009 11:16:45 UTC