01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.xml.impl;
17:
18: import javax.xml.namespace.QName;
19:
20: import org.geotools.xml.Binding;
21: import org.picocontainer.ComponentAdapter;
22: import org.picocontainer.MutablePicoContainer;
23: import org.picocontainer.defaults.DefaultPicoContainer;
24: import org.picocontainer.defaults.DuplicateComponentKeyRegistrationException;
25:
26: public class BindingLoader {
27: MutablePicoContainer container;
28:
29: public BindingLoader() {
30: container = new DefaultPicoContainer();
31: }
32:
33: /**
34: * Loads a binding with a specifc QName into a context.
35: *
36: * @param qName The qualified name of the type of the binding object.
37: * @param context The context which is to contain the binding.
38: *
39: * @return The binding object of the associated type, otherwise null if
40: * no such binding could be created.
41: *
42: */
43: public Binding loadBinding(QName qName, MutablePicoContainer context) {
44: ComponentAdapter adapter = container.getComponentAdapter(qName);
45: if (adapter == null) {
46: return null;
47: }
48:
49: return (Binding) adapter.getComponentInstance(context);
50: }
51:
52: /**
53: * Loads a binding with a specifc class into a context.
54: *
55: * @param bindingClass The class of the binding.
56: * @param context The context which is to contain the binding.
57: *
58: * @return The binding object of the associated type, otherwise null if
59: * no such binding could be created.
60: *
61: */
62: public Binding loadBinding(Class bindingClass,
63: MutablePicoContainer context) {
64: ComponentAdapter adapter = container
65: .getComponentAdapterOfType(bindingClass);
66: if (adapter == null) {
67: return null;
68: }
69:
70: return (Binding) adapter.getComponentInstance(context);
71: }
72:
73: /**
74: * Returns the component adapter for a binding with the specified name.
75: *
76: * @param type The qualified name of the type of the binding.
77: *
78: * @return The binding class, or null if no such class exists.
79: */
80: protected ComponentAdapter getBinding(QName type) {
81: return container.getComponentAdapter(type);
82: }
83:
84: /**
85: * @return The container which houses the bindings.
86: */
87: public MutablePicoContainer getContainer() {
88: return container;
89: }
90:
91: /**
92: * Sets the container which houses bindings.
93: *
94: */
95: public void setContainer(MutablePicoContainer container) {
96: this.container = container;
97: }
98: }
|