001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 1996-2008 by Sven Homburg
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     */
014    
015    package org.chenillekit.tapestry.core.factories;
016    
017    import java.util.ArrayList;
018    import java.util.Arrays;
019    import java.util.List;
020    
021    import org.apache.tapestry5.Binding;
022    import org.apache.tapestry5.BindingConstants;
023    import org.apache.tapestry5.ComponentResources;
024    import org.apache.tapestry5.ioc.Location;
025    import org.apache.tapestry5.services.BindingFactory;
026    import org.apache.tapestry5.services.BindingSource;
027    
028    import org.chenillekit.tapestry.core.bindings.MessageFormatBinding;
029    
030    /**
031     * Binding factory where the expression is a collection type.
032     *
033     * @version $Id: MessageFormatBindingFactory.java 594 2009-12-05 15:17:26Z mlusetti $
034     */
035    public class MessageFormatBindingFactory implements BindingFactory
036    {
037        private final BindingSource _bindingSource;
038    
039        public MessageFormatBindingFactory(BindingSource bindingSource)
040        {
041            _bindingSource = bindingSource;
042        }
043    
044        public Binding newBinding(String description, ComponentResources container, ComponentResources component,
045                                  String expression, Location location)
046        {
047            String messageKey = expression.substring(0, expression.indexOf('='));
048            List<String> parts = Arrays.asList(expression.substring(expression.indexOf('=') + 1).split(","));
049    
050            ArrayList<Binding> bindings = new ArrayList<Binding>(parts.size());
051            for (String part : parts)
052            {
053                String prefix = BindingConstants.PROP;
054                part = part.trim();
055    
056                if ('\'' == part.charAt(0) && '\'' == part.charAt(part.length() - 1))
057                {
058                    part = part.substring(1, part.length() - 1);
059                    prefix = BindingConstants.LITERAL;
060                }
061    
062                bindings.add(_bindingSource.newBinding(description, container, component, prefix, part, location));
063            }
064    
065            boolean invariant = true;
066            for (Binding binding : bindings)
067            {
068                if (!binding.isInvariant())
069                {
070                    invariant = false;
071                    break;
072                }
073            }
074    
075            return new MessageFormatBinding(location, messageKey, component.getMessages(), bindings, invariant);
076        }
077    }