01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
18: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
19:
20: import java.util.Map;
21:
22: import org.apache.tapestry.Binding;
23: import org.apache.tapestry.ComponentResources;
24: import org.apache.tapestry.ioc.Location;
25: import org.apache.tapestry.ioc.internal.util.TapestryException;
26: import org.apache.tapestry.services.BindingFactory;
27: import org.apache.tapestry.services.BindingSource;
28:
29: public class BindingSourceImpl implements BindingSource {
30: private final Map<String, BindingFactory> _factories;
31:
32: public BindingSourceImpl(Map<String, BindingFactory> factories) {
33: _factories = factories;
34: }
35:
36: public Binding newBinding(String description,
37: ComponentResources container, String defaultPrefix,
38: String expression) {
39: return newBinding(description, container, container,
40: defaultPrefix, expression, null);
41: }
42:
43: public Binding newBinding(String description,
44: ComponentResources container, ComponentResources component,
45: String defaultPrefix, String expression, Location location) {
46: notBlank(description, "description");
47: notNull(container, "container");
48: notNull(component, "component");
49: notBlank(defaultPrefix, "defaultPrefix");
50: notBlank(expression, "expression");
51: // Location might be null
52:
53: String subexpression = expression;
54: int colonx = expression.indexOf(':');
55:
56: BindingFactory factory = null;
57:
58: if (colonx > 0) {
59: String prefix = expression.substring(0, colonx);
60:
61: factory = _factories.get(prefix);
62: if (factory != null)
63: subexpression = expression.substring(colonx + 1);
64: }
65:
66: if (factory == null)
67: factory = _factories.get(defaultPrefix);
68:
69: // And if that's null, what then? We assume that the default prefix is a valid prefix,
70: // or we'll get an NPE below and report it like any other error.
71:
72: try {
73: return factory.newBinding(description, container,
74: component, subexpression, location);
75: } catch (Exception ex) {
76: throw new TapestryException(ServicesMessages
77: .bindingSourceFailure(expression, ex), location, ex);
78: }
79: }
80: }
|