KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.util.xml.idefix;
2
3
4 //import org.apache.log4j.Logger;
5
import org.sapia.util.xml.Namespace;
6
7 import java.lang.reflect.Method JavaDoc;
8
9 import java.util.HashMap JavaDoc;
10 import java.util.Map JavaDoc;
11
12
13 /**
14  * This implementation of the <code>NamespaceFactoryIF</code> interface contains
15  * a map of class names and a map of method names to which are associated the
16  * namespace definition. Like the name of the class implies you can configure the
17  * factory using patterns. The only pattern supported for now is the '*' (sorry
18  * if you were expecting regular expressions... maybe in the next release).<p>
19  *
20  * To find namespace using a <code>java.lang.Class</code> object, this factory
21  * uses the following resolution path:
22  * <ol>
23  * <li>search for the full qualified name of the class using the method
24  * <code>getName()</code> on the class.
25  * </li>
26  * <li>search for the package of the class using the method
27  * <code>getPackage().getName()</code> on the class to which the string "<code>.*</code>"
28  * is added (i.e. for a class <code>org.foo.Bar</code>, the search string is
29  * <code>org.foo.*</code>).
30  * </li>
31  * </ol><p>
32  *
33  * To find namespace using a <code>java.lang.reflect.Method</code> object, this factory
34  * uses the following resolution path:
35  * <ol>
36  * <li>search for the full qualified name of the method using the method
37  * <code>aMethod.getDeclaringClass().getName()</code> on the class. Note that this
38  * method will return the class where the method is <b>declared</b>. To that name
39  * the string "<code>#</code>" is added following by the name of the method (i.e.
40  * for a method <code>getName</code> on a class <code>org.foo.Bar</code>, the search
41  * string would be <code>org.foo.Bar#getName</code>).
42  * </li>
43  * <li>search for the full qualified name of the <b>declaring class</b> of the method using the
44  * method <code>getDeclaringClass().getName()</code>.
45  * </li>
46  * <li>search for the package of the <b>declaring class</b> of the method using the method
47  * <code>getDeclaringClass().getPackage().getName()</code> to which the string "<code>.*</code>"
48  * is added.
49  * </li>
50  * </ol><p>
51  *
52  * @author <a HREF="mailto:jc@sapia-oss.org">Jean-Cedric Desrochers</a>
53  * <dl>
54  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">
55  * Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
56  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
57  * <a HREF="http://www.sapia-oss.org/license.html" target="sapia-license">
58  * license page</a> at the Sapia OSS web site</dd></dt>
59  * </dl>
60  */

61 public class PatternNamespaceFactory implements NamespaceFactoryIF {
62   /** Defines the logger instance for this class. */
63
64   /*private static final Logger _theLogger =
65           Logger.getLogger(PatternNamespaceFactory.class);*/

66
67   /** The map of namespace objects by their associated method, class or package name. */
68   private Map JavaDoc _theNamespaces;
69
70   /**
71    * Creates a new PatternNamespaceFactory instance.
72    */

73   public PatternNamespaceFactory() {
74     _theNamespaces = new HashMap JavaDoc();
75   }
76
77   /**
78    * Adds the namespace passed for the class.
79    *
80    * @param aClass The class to which associate the namespace.
81    * @param aNamespace The namespace to associate.
82    */

83   public void addNamespace(Class JavaDoc aClass, Namespace aNamespace) {
84     _theNamespaces.put(aClass.getName(), aNamespace);
85   }
86
87   /**
88    * Adds the namespace passed for the package.
89    *
90    * @param aPackage The package to which associate the namespace
91    * @param aNamespace The namespace to associate.
92    */

93   public void addNamespace(Package JavaDoc aPackage, Namespace aNamespace) {
94     _theNamespaces.put(aPackage.getName() + ".*", aNamespace);
95   }
96
97   /**
98    * Adds the method passed for the package.
99    *
100    * @param aMethod The method to which associate the namespace
101    * @param aNamespace The namespace to associate.
102    */

103   public void addNamespace(Method JavaDoc aMethod, Namespace aNamespace) {
104     String JavaDoc aName = aMethod.getDeclaringClass().getName() + "#" +
105       aMethod.getName();
106     _theNamespaces.put(aName, aNamespace);
107   }
108
109   /**
110    * Associates the namespace to the passed in name. To be found by the get methods
111    * the name passed in must follow the resolution path define by this class.
112    *
113    * @param aName The name to which associate the namespace
114    * @param aNamespace The namespace to associate.
115    */

116   public void addNamespace(String JavaDoc aName, Namespace aNamespace) {
117     _theNamespaces.put(aName, aNamespace);
118   }
119
120   /**
121    * Returns the namespace that should be associated with the XML representation
122    * of the passed in class. If no association is found <code>null</code> is
123    * return.
124    *
125    * @param aClass The class for which to retrieve a namespace.
126    * @return The associated namespace or <code>null</code> is none is found.
127    * @see #DEFAULT_NAMESPACE
128    */

129   public Namespace getNamespaceFor(Class JavaDoc aClass) {
130     Namespace aNamespace = null;
131
132     // First look for the full qualified class name
133
/*if (_theLogger.isDebugEnabled()) {
134       _theLogger.debug("Getting a namespace for the method: " + aClass);
135       _theLogger.debug(" >>> Looking for the class name " + aClass.getName());
136     }*/

137     aNamespace = (Namespace) _theNamespaces.get(aClass.getName());
138
139     if (aNamespace == null) {
140       // If not found search using the package name
141
String JavaDoc aPackageName = aClass.getPackage().getName() + ".*";
142
143       /*if (_theLogger.isDebugEnabled()) {
144         _theLogger.debug(" >>> Looking for the package name " + aPackageName);
145       }*/

146       aNamespace = (Namespace) _theNamespaces.get(aPackageName);
147     }
148
149     return aNamespace;
150   }
151
152   /**
153    * Returns the namespace that should be associated with the XML representation
154    * of the passed in method. If no association is found <code>null</code> is
155    * return.
156    *
157    * @param aMethod The method for which to retrieve a namespace.
158    * @return The associated namespace or <code>null</code> is none is found.
159    * @see #DEFAULT_NAMESPACE
160    */

161   public Namespace getNamespaceFor(Method JavaDoc aMethod) {
162     Namespace aNamespace = null;
163
164     // First look for the full qualified name of the method
165
String JavaDoc aName = aMethod.getDeclaringClass().getName() + "#" +
166       aMethod.getName();
167
168     /*if (_theLogger.isDebugEnabled()) {
169       _theLogger.debug("Getting a namespace for the method: " + aMethod);
170       _theLogger.debug(" >>> Looking for the method name " + aName);
171     }*/

172     aNamespace = (Namespace) _theNamespaces.get(aName);
173
174     if (aNamespace == null) {
175       // If not found search using the class name
176

177       /*if (_theLogger.isDebugEnabled()) {
178         _theLogger.debug(" >>> Looking for the class name " + aMethod.getDeclaringClass().getName());
179       }*/

180       aNamespace = (Namespace) _theNamespaces.get(aMethod.getDeclaringClass()
181                                                          .getName());
182
183       if (aNamespace == null) {
184         // If not found search using the package name
185
String JavaDoc aPackageName = aMethod.getDeclaringClass().getPackage().getName() +
186           ".*";
187
188         /*if (_theLogger.isDebugEnabled()) {
189           _theLogger.debug(" >>> Looking for the package name " + aPackageName);
190         }*/

191         aNamespace = (Namespace) _theNamespaces.get(aPackageName);
192       }
193     }
194
195     return aNamespace;
196   }
197 }
198
Popular Tags