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 org.apache.hivemind.Location;
018 import org.apache.tapestry.IBinding;
019 import org.apache.tapestry.IComponent;
020 import org.apache.tapestry.binding.BindingFactory;
021 import org.apache.tapestry.binding.BindingSource;
022 import org.apache.tapestry.spec.IParameterSpecification;
023
024 import java.util.HashMap;
025 import java.util.Iterator;
026 import java.util.List;
027 import java.util.Map;
028
029 /**
030 * Implementation of the {@link BindingSource} service.
031 *
032 * @author Howard Lewis Ship
033 * @since 4.0
034 */
035 public class BindingSourceImpl implements BindingSource
036 {
037 private List _contributions;
038
039 private Map _propertyMap;
040
041 /**
042 * Keyed on prefix, value is {@link BindingFactory}.
043 */
044 private Map _factoryMap = new HashMap();
045
046 public void initializeService()
047 {
048 Iterator i = _contributions.iterator();
049
050 while (i.hasNext())
051 {
052 BindingPrefixContribution c = (BindingPrefixContribution) i.next();
053
054 _factoryMap.put(c.getPrefix(), c.getFactory());
055 }
056 }
057
058 public IBinding createBinding(IComponent component, String description,
059 String reference, String defaultBindingType, Location location)
060 {
061 return createBinding(component, null, description, reference, defaultBindingType, location);
062 }
063
064 public IBinding createBinding(IComponent component, IParameterSpecification parameter, String description,
065 String reference, String defaultBindingType, Location location)
066 {
067 String prefix = null;
068 String path = reference;
069 BindingFactory factory = null;
070
071 int colonx = reference.indexOf(':');
072
073 if (colonx > -1)
074 {
075 String pathPrefix = reference.substring(0, colonx);
076
077 if (_factoryMap.containsKey(pathPrefix))
078 {
079 prefix = pathPrefix;
080
081 path = reference.substring(colonx + 1);
082 }
083 } else if (parameter != null && _propertyMap.containsKey(parameter.getParameterName()))
084 {
085
086 factory = (BindingFactory) _propertyMap.get(parameter.getParameterName());
087 }
088
089 if (prefix == null)
090 {
091 prefix = defaultBindingType;
092 }
093
094 if (factory == null)
095 {
096 factory = (BindingFactory) _factoryMap.get(prefix);
097 }
098
099 return factory.createBinding(component, description, path, location);
100 }
101
102 public void setContributions(List contributions)
103 {
104 _contributions = contributions;
105 }
106
107 public void setPropertyContributions(Map properties)
108 {
109 _propertyMap = properties;
110 }
111 }