labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r880952 - in /labs/magma/trunk: foundation-i18n/src/main/java/org/apache/magma/i18n/ foundation-website/src/main/java/org/apache/magma/website/producers/ foundation-website/src/main/java/org/apache/magma/website/templating/ foundation-websi...
Date Mon, 16 Nov 2009 20:44:47 GMT
Author: simoneg
Date: Mon Nov 16 20:44:46 2009
New Revision: 880952

URL: http://svn.apache.org/viewvc?rev=880952&view=rev
Log:
LABS-221 : LinkHelper to encapsulate link creation everywhere, will later work with cglib
for link type safety. Also reorganized i18nRedenringHelper to a more generic helper including
link support

Added:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
Removed:
    labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/I18nRenderingHelper.java
Modified:
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizableHtmlProducerBase.java
    labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
    labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizableHtmlProducerBase.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizableHtmlProducerBase.java?rev=880952&r1=880951&r2=880952&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizableHtmlProducerBase.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ParametrizableHtmlProducerBase.java
Mon Nov 16 20:44:46 2009
@@ -16,11 +16,7 @@
  */
 package org.apache.magma.website.producers;
 
-import org.apache.magma.i18n.I18nRenderingHelper;
 import org.apache.magma.website.HtmlProducer;
-import org.apache.magma.website.WebHandler;
-
-import java.io.OutputStream;
 
 public abstract class ParametrizableHtmlProducerBase extends HtmlProducer implements
 		ParametrizableProducer {
@@ -28,7 +24,10 @@
 	protected ProducerParameters parameters = new ProducerParameters();
 	
 	{
-		parameters.addParameter("i18n", new I18nRenderingHelper());
+		ProducerHelper ph = new ProducerHelper();
+		// Kept here for legacy reasons, may be removed in the future
+		parameters.addParameter("i18n", ph);
+		parameters.addParameter("mag", ph);
 	}
 	
 	public ParametrizableHtmlProducerBase addParameter(Object param) {

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java?rev=880952&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/producers/ProducerHelper.java
Mon Nov 16 20:44:46 2009
@@ -0,0 +1,131 @@
+package org.apache.magma.website.producers;
+
+import java.util.Date;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.i18n.Formatter;
+import org.apache.magma.i18n.Formatters;
+import org.apache.magma.i18n.formatters.DateFormatter;
+import org.apache.magma.website.utils.LinkHelper;
+
+
+/**
+ * This helper contains functions useful inside a producer, 
+ * expecially inside a {@link TemplatingProducer}, and is always
+ * avaliable inside a {@link ParametrizableProducer} in a parameter
+ * named "mag".
+ * 
+ * There functions range from i18n manipulation, link generation, and
+ * other utility function.
+ * 
+ * Packages can contribute to this class to add specific functions.
+ *
+ * Note: this supercedes the old I18nRenderingHelper accessed via $i18n.
+ *
+ * @author Simone Gianni <simoneg@apache.org>
+ */
+public class ProducerHelper {
+
+	/**
+	 * Translates the given message.
+	 * @param message The message to translate
+	 * @return the translated message
+	 * @deprecated use {@link #translate(String)} 
+	 */
+	@Deprecated
+	public String tr(String message) {
+		return new LocalizableString(message).toString();
+	}
+
+	/**
+	 * Translates the given message, replacing placeholders with values.
+	 * @param message The message to translate
+	 * @param values The values to use in placeholders
+	 * @return the translated message
+	 * @deprecated use {@link #translate(String, Object...)}
+	 */
+	@Deprecated
+	public String tr(String message, Object... values) {
+		return new LocalizableString(message, values).toString();
+	}
+	
+	
+	/**
+	 * Formats the given date correctly for the current locale
+	 * @param date The date to format
+	 * @return the formatted date
+	 * @deprecated use {@link #format(Object)}
+	 */
+	@Deprecated
+	public String tr(Date date) {
+		return tr(date, "default");
+	}
+	
+	/**
+	 * Formats the given date correctly for the current locale, using the given format configuration
+	 * @param date The date to format
+	 * @param format The format to use, see {@link DateFormatter} docs
+	 * @return the formatted date
+	 * @deprecated use {@link #format(Object, String)}
+	 */
+	@Deprecated
+	public String tr(Date date, String format) {
+		Formatter<Date> df = Formatters.getFormatterFor(Date.class);
+		df.setFormat(format);
+		return df.to(date);
+	}
+	
+	
+	/**
+	 * Translates the given message.
+	 * @param message The message to translate
+	 * @return the translated message
+	 */
+	public String translate(String message) {
+		return new LocalizableString(message).toString();
+	}
+
+	/**
+	 * Translates the given message, replacing placeholders with values.
+	 * @param message The message to translate
+	 * @param values The values to use in placeholders
+	 * @return the translated message
+	 */
+	public String translate(String message, Object... values) {
+		return new LocalizableString(message, values).toString();
+	}
+	
+	
+	/**
+	 * Formats the given value correctly for the current locale
+	 * @param date The value to format
+	 * @return the formatted value
+	 */
+	public String format(Object value) {
+		return format(value, "default");
+	}
+	
+	/**
+	 * Formats the given value correctly for the current locale, using the given format configuration
+	 * @param value The value to format
+	 * @param format The format to use, see javadocs of {@link Formatter} and its subclasses
+	 * @return the formatted value
+	 */
+	public String format(Object value, String format) {
+		if (value == null) return "";
+		Formatter df = Formatters.getFormatterFor(value.getClass());
+		df.setFormat(format);
+		return df.to(date);
+	}
+	
+	/**
+	 * Creates a link to the given method, formatting properly given parameters.
+	 * @param method The method name 
+	 * @param params Parameters to pass
+	 * @return The link string
+	 */
+	public String link(String method, Object... params) {
+		return LinkHelper.makeLink(method, params);
+	}
+	
+}

Modified: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java?rev=880952&r1=880951&r2=880952&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
(original)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/templating/TemplateData.java
Mon Nov 16 20:44:46 2009
@@ -43,6 +43,7 @@
 	public TemplateData() {
 	}
 	
+	// TODO report errors from here, with HTML line and all the rest.
 	public void init(Reader reader) {
 		StringBuffer alltpl = new StringBuffer();
 		BufferedReader br = new BufferedReader(reader);

Added: labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java?rev=880952&view=auto
==============================================================================
--- labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
(added)
+++ labs/magma/trunk/foundation-website/src/main/java/org/apache/magma/website/utils/LinkHelper.java
Mon Nov 16 20:44:46 2009
@@ -0,0 +1,130 @@
+package org.apache.magma.website.utils;
+
+import java.lang.reflect.Method;
+import java.net.URLEncoder;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.basics.utils.GenericClass;
+import org.apache.magma.basics.utils.GenericClass.MethodDef;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+import org.apache.magma.website.WebHandler;
+
+/**
+ * Helper class for creating links.
+ * 
+ * This class takes a method, and knowing its parameter types,
+ * concatenates parameter values after having properly converted and
+ * escaped them.
+ * 
+ * There are static methods that can be used when a single link has to
+ * be created, otherwise a new instance of this class can be created
+ * to achieve better performances when multiple links need to be generated.
+ *
+ * Anyway this class is thread safe.
+ *
+ * @author Simone Gianni <simoneg@apache.org>
+ */
+public class LinkHelper {
+
+	// TODO LABS-211 (cglib based links) apply mostly here
+	
+	private String baseMethod;
+	private Converter[] converters;
+	
+	public LinkHelper(Method method) {
+		this(method.getName(), method.getParameterTypes());
+	}
+	
+	public LinkHelper(MethodDef method) {
+		this(method.getName(), GenericClass.toRawClasses(method.getParameterTypes()));
+	}
+	
+	public LinkHelper(String methodName, Class<?>[] parameters) {
+		if (methodName.startsWith("do")) {
+			methodName = methodName.substring(2);
+		} else if (method.startsWith("handle")) {
+			methodName = methodName.substring(6);
+		} else if (method.startsWith("hidden")) {
+			methodName = methodName.substring(6);
+		}
+		
+		this.baseMethod = methodName;
+		if (parameters == null) {
+			converters = new Converter[0];
+		} else {
+			converters = new Converter[parameters.length];
+			for (int i = 0; i < parameters.length; i++) {
+				converters[i] = Converters.getConverterFor(parameters[i]);
+				if (converters[i] == null) throw new MagmaException("Cannot find a converter for {0},
needed to create link for {1}", parameters[i], methodName);
+			}
+		}
+	}
+	
+	public String makeLink(Object... params) {
+		StringBuilder ret = new StringBuilder();
+		ret.append(this.baseMethod);
+		if (params != null) {
+			if (params.length > converters.length) throw new MagmaException("Incorrect number of
parameters for link to {0}, expecting {1} got {2}", this.baseMethod, this.converters.length,
params.length);
+			for (int i = 0; i < params.length; i++) {
+				Object param = params[i];
+				if (param == null) {
+					// TODO is this the global null value? See LABS-175
+					ret.append("_null");
+				} else {
+					String converted = converters[i].to(param);
+					converted = URLEncoder.encode(converted, "UTF-8");
+					converted = converted.replaceAll("\\!", "%21");
+					ret.append(converted);
+				}
+				
+			}
+		}
+		return ret.toString();
+	}
+	
+	
+	/**
+	 * Static method for creating a relative link.
+	 * @param method The method name
+	 * @param params Parameters of the method
+	 * @return A relative link, with parameters correctly converted and escaped
+	 */
+	public static String makeLink(String method, Object... params) {
+		StringBuilder ret = new StringBuilder();
+		
+		if (method.startsWith("do")) {
+			method = method.substring(2);
+		} else if (method.startsWith("handle")) {
+			method = method.substring(6);
+		} else if (method.startsWith("hidden")) {
+			method = method.substring(6);
+		}
+		
+		ret.append(method);
+		for (Object param : params) {
+			ret.append(WebHandler.paramSeparator);
+			if (param == null) {
+				// TODO is this the global null value? See LABS-175
+				ret.append("_null");
+			} else {
+				Converter conv = Converters.getConverterFor(param.getClass());
+				if (conv == null) throw new MagmaException("Cannot find a converter for {0}, needed to
create link for {1}", param.getClass(), method);
+				String converted = conv.to(param);
+				converted = URLEncoder.encode(converted, "UTF-8");
+				converted = converted.replaceAll("\\!", "%21");
+				ret.append(converted);
+			}
+		}
+		return ret.toString();
+	}
+	
+	public static String makeLink(Method method, Object... params) {
+		return makeLink(method.getName(), params);
+	}
+	
+	public static String makeLink(MethodDef method, Object... params) {
+		return makeLink(method.getName(), params);
+	}
+	
+}

Modified: labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java?rev=880952&r1=880951&r2=880952&view=diff
==============================================================================
--- labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
(original)
+++ labs/magma/trunk/foundation-website/src/test/java/org/apache/magma/website/teststuff/FakeHtmlProducer.java
Mon Nov 16 20:44:46 2009
@@ -23,6 +23,7 @@
 import org.apache.magma.website.WebHandler;
 import org.apache.magma.website.producers.ParametrizableHtmlProducerBase;
 import org.apache.magma.website.producers.ParametrizableProducer;
+import org.apache.magma.website.producers.ProducerHelper;
 import org.apache.magma.website.producers.ProducerParameters;
 
 import java.io.IOException;
@@ -54,8 +55,8 @@
 		str += ".";
 		str += getCreatingMethod().getName();
 		str += "]";
-		I18nRenderingHelper i18nh = (I18nRenderingHelper) getParameters().getParameters().get("i18n");
-		str += i18nh.tr("test message");
+		ProducerHelper i18nh = (ProducerHelper) getParameters().getParameters().get("mag");
+		str += i18nh.translate("test message");
 		if (params != null && params.length > 0) {
 			str += "(" + params[0] + ")";
 		}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message