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.ListBinding;
029    
030    /**
031     * Binding factory where the expression is a collection type.
032     *
033     * @version $Id: ListBindingFactory.java 594 2009-12-05 15:17:26Z mlusetti $
034     */
035    public class ListBindingFactory implements BindingFactory
036    {
037        private final BindingSource _bindingSource;
038    
039        public ListBindingFactory(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            List<String> parts = Arrays.asList(expression.split(","));
048            ArrayList<Binding> bindings = new ArrayList<Binding>(parts.size());
049    
050            for (String part : parts)
051            {
052                String prefix = BindingConstants.PROP;
053                part = part.trim();
054    
055                if ('\'' == part.charAt(0) && '\'' == part.charAt(part.length() - 1))
056                {
057                    part = part.substring(1, part.length() - 1);
058                    prefix = BindingConstants.LITERAL;
059                }
060    
061                bindings.add(_bindingSource.newBinding(description, container, component, prefix, part, location));
062            }
063    
064            boolean invariant = true;
065            for (Binding binding : bindings)
066            {
067                if (!binding.isInvariant())
068                {
069                    invariant = false;
070                    break;
071                }
072            }
073    
074            return new ListBinding(location, bindings, invariant);
075        }
076    }