001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.services.impl;
016
017 import java.util.HashMap;
018 import java.util.Iterator;
019 import java.util.List;
020 import java.util.Map;
021
022 import org.apache.hivemind.Location;
023 import org.apache.tapestry.IBinding;
024 import org.apache.tapestry.IComponent;
025 import org.apache.tapestry.binding.BindingFactory;
026 import org.apache.tapestry.binding.BindingSource;
027
028 /**
029 * Implementation of the <code>tapestry.bindings.BindingSource</code> service.
030 *
031 * @author Howard Lewis Ship
032 * @since 4.0
033 */
034 public class BindingSourceImpl implements BindingSource
035 {
036 private List _contributions;
037
038 /**
039 * Keyed on prefix, value is {@link BindingFactory}.
040 */
041 private Map _factoryMap = new HashMap();
042
043 public void initializeService()
044 {
045 Iterator i = _contributions.iterator();
046
047 while (i.hasNext())
048 {
049 BindingPrefixContribution c = (BindingPrefixContribution) i.next();
050
051 _factoryMap.put(c.getPrefix(), c.getFactory());
052 }
053 }
054
055 public IBinding createBinding(IComponent component, String bindingDescription,
056 String reference, String defaultPrefix, Location location)
057 {
058 String prefix = defaultPrefix;
059 String path = reference;
060
061 int colonx = reference.indexOf(':');
062
063 if (colonx > 1)
064 {
065 String pathPrefix = reference.substring(0, colonx);
066
067 if (_factoryMap.containsKey(pathPrefix))
068 {
069 prefix = pathPrefix;
070
071 path = reference.substring(colonx + 1);
072 }
073 }
074
075 BindingFactory factory = (BindingFactory) _factoryMap.get(prefix);
076
077 return factory.createBinding(component, bindingDescription, path, location);
078 }
079
080 public void setContributions(List contributions)
081 {
082 _contributions = contributions;
083 }
084 }