KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > confix > CompositeObjectFactory


1 package org.sapia.util.xml.confix;
2
3
4 // Import of Sun's JDK classes
5
// ---------------------------
6
import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9
10 /**
11  * This instance implements an <code>ObjectFactoryIF</code> that internally
12  * maps child object factories to XML namepspace prefixes OR URIs.
13  *
14  * @author Jean-Cedric Desrochers
15  *
16  * <dl>
17  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
18  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
19  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
20  * </dl>
21  */

22 public class CompositeObjectFactory implements ObjectFactoryIF {
23   /////////////////////////////////////////////////////////////////////////////////////////
24
///////////////////////////////// INSTANCE ATTRIBUTES /////////////////////////////////
25
/////////////////////////////////////////////////////////////////////////////////////////
26

27   /** The map of object factories identified by the namespace thei manage. */
28   private Map JavaDoc _theFactories;
29
30   /**
31    * If true, indicates that factories are mapped to prefixes - else to the namespace URI.
32    * Defaults to false.
33    */

34   private boolean _mapToNameSpacePrefix = false;
35
36   /////////////////////////////////////////////////////////////////////////////////////////
37
//////////////////////////////////// CONSTRUCTORS /////////////////////////////////////
38
/////////////////////////////////////////////////////////////////////////////////////////
39

40   /**
41    * Creates a new CompositeObjectFactory instance.
42    */

43   public CompositeObjectFactory() {
44     _theFactories = new HashMap JavaDoc();
45   }
46
47   /**
48    * Indicates if internal factories are mapped to namespace URIs or
49    * prefixes; if <code>true</code>, object factories are mapped to
50    * prefixes. By default, an instance of this class maps object factories
51    * to the URI.
52    *
53    * @param mapToPrefix if <code>true</code>, object factories are mapped to
54    * prefixes.
55    */

56   public void setMapToPrefix(boolean mapToPrefix) {
57     _mapToNameSpacePrefix = mapToPrefix;
58   }
59
60   /////////////////////////////////////////////////////////////////////////////////////////
61
/////////////////////////////////// MUTATOR METHODS ///////////////////////////////////
62
/////////////////////////////////////////////////////////////////////////////////////////
63

64   /**
65    * Registers the object factory passed in for the namespace URI passed in.
66    *
67    * @param aNamespaceURIorPrefix The namespace URI or prefix to associate with the factory.
68    * @param aFactory The object factory to register.
69    * @exception IllegalArgumentExcption If a factory is already registered for
70    * the passed in namespace URI.
71    */

72   public void registerFactory(String JavaDoc aNamespaceURIorPrefix,
73     ObjectFactoryIF aFactory) {
74     if (_theFactories.containsKey(aNamespaceURIorPrefix)) {
75       throw new IllegalArgumentException JavaDoc(
76         "An object factory is already registered for the namespace URI or prefix " +
77         aNamespaceURIorPrefix);
78     }
79
80     _theFactories.put(aNamespaceURIorPrefix, aFactory);
81   }
82
83   /**
84    * Returns the child object factory internally mapped to the given namespace or
85    * prefix.
86    *
87    * @return an <code>ObjectFactoryIF</code>.
88    * @param aNamespaceURIorPrefix a XML namepsace prefix or URI.
89    * @throws IllegalArgumentException if no factory can be found for the given
90    * namespace of prefix.
91    */

92   public ObjectFactoryIF getFactoryFor(String JavaDoc aNamespaceURIorPrefix)
93     throws IllegalArgumentException JavaDoc {
94     ObjectFactoryIF fac = (ObjectFactoryIF) _theFactories.get(aNamespaceURIorPrefix);
95
96     if (fac == null) {
97       throw new IllegalArgumentException JavaDoc(
98         "No object factory registered for the namespace URI or prefix " +
99         aNamespaceURIorPrefix);
100     }
101
102     return fac;
103   }
104
105   /**
106    * Returns true if an object factory is internally mapped to the given
107    * namespace or prefix.
108    *
109    * @return <code>true</code> if an object factory is internally mapped to the given
110    * namespace or prefix.
111    */

112   public boolean containsObjectFactory(String JavaDoc aNamespaceURIorPrefix) {
113     return _theFactories.get(aNamespaceURIorPrefix) != null;
114   }
115
116   /////////////////////////////////////////////////////////////////////////////////////////
117
////////////////////////////// INTERFACE IMPLEMENTATION ///////////////////////////////
118
/////////////////////////////////////////////////////////////////////////////////////////
119

120   /**
121    * Creates an object for the element passed in. This method in fact delegates
122    * object creation logic to the factory that is mapped to the given namespace OR prefix.
123    * Whether this method uses the namespace or prefix depends on the flag that was set
124    * through this instance's <code>setMapToPrefix(...)</code> method.
125    *
126    * @param aPrefix The namespace prefix of the element.
127    * @param aNamespaceURI The namespace URI of the element.
128    * @param anElementName The element name for wich to create an object.
129    * @param aParent The parent object of the object to create.
130    * @see #setMapToPrefix(boolean)
131    * @exception ObjectCreationException If an error occurs creating the object.
132    */

133   public CreationStatus newObjectFor(String JavaDoc aPrefix, String JavaDoc aNamespaceURI,
134     String JavaDoc anElementName, Object JavaDoc aParent) throws ObjectCreationException {
135     ObjectFactoryIF aFactory;
136
137     if (_mapToNameSpacePrefix) {
138       if ((aPrefix == null) || (aPrefix.length() == 0)) {
139         throw new ObjectCreationException("Prefix no defined");
140       }
141
142       aFactory = (ObjectFactoryIF) _theFactories.get(aPrefix);
143     } else {
144       if ((aNamespaceURI == null) || (aNamespaceURI.length() == 0)) {
145         throw new ObjectCreationException("Namespace no defined");
146       }
147
148       aFactory = (ObjectFactoryIF) _theFactories.get(aNamespaceURI);
149     }
150
151     if (aFactory == null) {
152       throw new ObjectCreationException("No factory found for the namespace " +
153         aNamespaceURI);
154     } else {
155       return aFactory.newObjectFor(aPrefix, aNamespaceURI, anElementName,
156         aParent);
157     }
158   }
159 }
160
Popular Tags