Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by FilipSAdamsen: http://wiki.apache.org/tapestry/Tapestry5HowToAddMessageFormatBindingPrefix The comment on the change is: Fixed a bug and cleaned up the code. ------------------------------------------------------------------------------ key = This is the first value: %s, it's a string. This is the second value: %d, it's a number. }}} + First create the !MessageFormatBinding: + {{{ + public class MessageFormatBinding extends AbstractBinding { + + private final String messageKey; + private final Messages messages; + private final List<Binding> bindings; + private final boolean invariant; + + public MessageFormatBinding(Location location, String messageKey, Messages messages, List<Binding> bindings, boolean invariant) { + super(location); + + this.messageKey = messageKey; + this.messages = messages; + this.bindings = bindings; + this.invariant = invariant; + } + + public Object get() { + List<Object> values = new ArrayList<Object>(bindings.size()); + for (Binding binding : bindings) + values.add(binding.get()); + + return messages.format(messageKey, values.toArray()); + } + + @Override + public boolean isInvariant() { + return this.invariant; + } + + @Override + public Class getBindingType() { + return String.class; + } + } + }}} + - First create the class !MessageFormatBindingFactory: + Then create the class !MessageFormatBindingFactory: {{{ public class MessageFormatBindingFactory implements BindingFactory { @@ -25, +63 @@ public Binding newBinding(String description, ComponentResources container, ComponentResources component, String expression, Location location) { - String key = expression.substring(0, expression.indexOf(',')); + String messageKey = expression.substring(0, expression.indexOf('=')); - String[] parts = expression.substring(expression.indexOf(',') + 1).split(","); + List<String> parts = Arrays.asList(expression.substring(expression.indexOf('=') + 1).split(",")); + ArrayList<Binding> bindings = new ArrayList<Binding>(parts.size()); - Object[] values = new Object[parts.length]; - for (int i = 0; i < parts.length; i++) { - String part = parts[i]; + for (String part : parts) { String prefix = TapestryConstants.PROP_BINDING_PREFIX; if ('\'' == part.charAt(0) && '\'' == part.charAt(part.length() - 1)) { @@ -38, +75 @@ prefix = TapestryConstants.LITERAL_BINDING_PREFIX; } - values[i] = bindingSource.newBinding(description, container, component, prefix, part, location).get(); + bindings.add(bindingSource.newBinding(description, container, component, prefix, part, location)); } - String messageValue = container.getMessages().format(key, values); + boolean invariant = true; + for (Binding binding : bindings) { + if (false == binding.isInvariant()) { + invariant = false; + break; + } + } + return new MessageFormatBinding(location, messageKey, component.getMessages(), bindings, invariant); - return new LiteralBinding(description, messageValue, location) { - - @Override - public boolean isInvariant() { - return false; - } - }; } } }}} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
