001 package org.trails.component;
002
003 import java.text.Format;
004 import java.util.List;
005 import java.util.Locale;
006
007 import org.apache.tapestry.annotations.ComponentClass;
008 import org.apache.tapestry.annotations.InjectObject;
009 import org.apache.tapestry.annotations.Parameter;
010 import org.apache.tapestry.components.Insert;
011 import org.apache.tapestry.components.InsertMode;
012 import org.trails.i18n.TrailsMessageSource;
013
014 /**
015 * Used to insert some text (from a parameter) into the HTML.
016 * Emits a value into the response page.
017 */
018 @ComponentClass(allowBody = false)
019 public abstract class InsertI18N extends Insert
020 {
021 /**
022 * Return the Spring TrailsMessageSource. This is used to implement
023 * i18n in all Trails components, accessing a i18n properties file in the
024 * application instead of accessing the property file located in org.trais.component package.
025 * By doing this, someone who would need i18n wouldn't need to change the property
026 * located in the org.trails.component package and rebuild the trails.jar
027 *
028 * @return
029 */
030 @InjectObject("service:trails.core.MessageSource")
031 public abstract TrailsMessageSource getResourceBundleMessageSource();
032
033 @Parameter(required = true)
034 public abstract String getBundleKey();
035
036 @Parameter
037 public abstract String getDefaultMessage();
038
039 @Parameter
040 public abstract Object getParams();
041
042 @Parameter(defaultValue = "page.locale")
043 public abstract Locale getLocale();
044
045 /**
046 * A Format object used to convert the value to a string.
047 *
048 * @return
049 */
050 @Parameter
051 public abstract Format getFormat();
052
053 /**
054 * If false (the default), then HTML characters in the value are escaped. If
055 * true, then value is emitted exactly as is.
056 *
057 * @return
058 */
059 @Parameter
060 public abstract boolean getRaw();
061
062 /**
063 * If true, causes the tag used to define the insert to be used, as well as any informal
064 * parameters bound.
065 *
066 * @return
067 */
068 @Parameter(defaultValue = "false")
069 public abstract boolean getRenderTag();
070
071 /**
072 * Determines which mode to use: breaks after each line, or wrap each line
073 * as a paragraph.
074 *
075 * @return
076 */
077 @Parameter
078 public abstract InsertMode getMode();
079
080 public String getValue()
081 {
082 if (getDefaultMessage() != null)
083 {
084 return getResourceBundleMessageSource().getMessageWithDefaultValue(getBundleKey(), getParams() != null ? constructServiceParameters(getParams()) : TrailsMessageSource.EMPTY_ARGUMENTS, getLocale(), getDefaultMessage());
085 } else
086 {
087 return getResourceBundleMessageSource().getMessage(getBundleKey(), getParams() != null ? constructServiceParameters(getParams()) : TrailsMessageSource.EMPTY_ARGUMENTS, getLocale());
088 }
089 }
090
091 /**
092 * Converts a parameter value to an array of objects.
093 *
094 * @param parameterValue the input value which may be
095 * <ul>
096 * <li>null (returns null)
097 * <li>An array of Object (returns the array)
098 * <li>A {@link java.util.List}(returns an array of the values in the List})
099 * <li>A single object (returns the object as a single-element array)
100 * </ul>
101 * @return An array representation of the input object.
102 */
103
104 public static Object[] constructServiceParameters(Object parameterValue)
105 {
106 if (parameterValue == null)
107 return null;
108
109 if (parameterValue instanceof Object[])
110 return (Object[]) parameterValue;
111
112 if (parameterValue instanceof List)
113 {
114 List list = (List) parameterValue;
115
116 return list.toArray();
117 }
118
119 return new Object[]{parameterValue};
120 }
121 }