KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > idefix > CompositeNamespaceFactory


1 package org.sapia.util.xml.idefix;
2
3 import org.sapia.util.xml.Namespace;
4
5 import java.lang.reflect.Method JavaDoc;
6
7 import java.util.Iterator JavaDoc;
8 import java.util.LinkedList JavaDoc;
9
10
11 /**
12  * This implementation of the <code>NamespaceFactoryIF</code> interface contains
13  * a collection of namespace factories to which it delegates the method calls. The
14  * order in which the delegate factories are added will define the internal hierarchy
15  * of factories. That will directly affect the order in which the factories are called
16  * when a <code>getNamespaceFor()</code> method is called on this compostie factory.<p>
17  *
18  * When such a call is made, this factory will forward the call to the first factory
19  * of the hierarchy. If this root factory returns a <code>Namespace</code> it returns
20  * the object. If <code>null</code> is return then the call will be forwarded to the
21  * next factry untill a <code>Namespace<code> object is returned or untill it reaches
22  * the last factoy of the chain.<p>
23  *
24  * Note that this factory is <b>not synchronzed</b> so if you add factories to it while
25  * another thread is calling one of the <code>getNamespaceFor()</code> method, the result
26  * can be... kind of weird!
27  *
28  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
29  * <dl>
30  * <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>
31  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
32  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">license page</a> at the Sapia OSS web site</dd></dt>
33  * </dl>
34  */

35 public class CompositeNamespaceFactory implements NamespaceFactoryIF {
36   /** The list of delegate factories. */
37   private LinkedList JavaDoc _theDelegates;
38
39   /**
40    * Creates a new CompositeNamespaceFactory instance.
41    */

42   public CompositeNamespaceFactory() {
43     _theDelegates = new LinkedList JavaDoc();
44   }
45
46   /**
47    * Register the namespace factory passed in as a delegate.
48    *
49    * @param aFactory The delegate factory to add.
50    */

51   public void registerNamespaceFactory(NamespaceFactoryIF aFactory) {
52     _theDelegates.addLast(aFactory);
53   }
54
55   /**
56    * Returns the namespace that should be associated with the XML representation
57    * of the passed in class. If no association is found <code>null</code> is
58    * return.
59    *
60    * @param aClass The class for which to retrieve a namespace.
61    * @return The associated namespace or <code>null</code> is none is found.
62    */

63   public Namespace getNamespaceFor(Class JavaDoc aClass) {
64     Namespace aNamespace = null;
65
66     for (Iterator JavaDoc it = _theDelegates.iterator();
67           it.hasNext() && (aNamespace == null);) {
68       NamespaceFactoryIF aFactory = (NamespaceFactoryIF) it.next();
69       aNamespace = aFactory.getNamespaceFor(aClass);
70     }
71
72     return aNamespace;
73   }
74
75   /**
76    * Returns the namespace that should be associated with the XML representation
77    * of the passed in method. If no association is found <code>null</code> is
78    * return.
79    *
80    * @param aMethod The method for which to retrieve a namespace.
81    * @return The associated namespace or <code>null</code> is none is found.
82    */

83   public Namespace getNamespaceFor(Method JavaDoc aMethod) {
84     Namespace aNamespace = null;
85
86     for (Iterator JavaDoc it = _theDelegates.iterator();
87           it.hasNext() && (aNamespace == null);) {
88       NamespaceFactoryIF aFactory = (NamespaceFactoryIF) it.next();
89       aNamespace = aFactory.getNamespaceFor(aMethod);
90     }
91
92     return aNamespace;
93   }
94 }
95
Popular Tags