View Javadoc
1   /*
2    * Copyright 2007 Joachim Grueneis
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.xml.util.resolvers;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  
20  /**
21   * Some helpers used by the resolver commands. This is a utility class which is NOT meant to be
22   * instantiated.
23   * 
24   * @author <a href="mailto:jgrueneis AT gmail DOT com">Joachim Grueneis</a>
25   * @version $Revision$ $Date$
26   * @since 1.2
27   */
28  public final class ResolveHelpers {
29    private static final Log LOG = LogFactory.getLog(ResolveHelpers.class);
30  
31    /**
32     * A private constructor as this is a utility class without an own state.
33     */
34    private ResolveHelpers() {}
35  
36    /**
37     * Qualifies the given <code>fileName</code> with the given <code>packageName</code> and returns
38     * the resulting file path.<br>
39     * If <code>packageName</code> is <code>null</code> or a zero-length String, this method will
40     * return <code>fileName</code>.<br>
41     *
42     * @param fileName The file name to be qualified.
43     * @param packageName The package name to be used for qualifying.
44     * @return The qualified file path.
45     */
46    public static String getQualifiedFileName(String fileName, String packageName) {
47      if (packageName == null || packageName.isEmpty()) {
48        return fileName;
49      }
50  
51      return packageName.replace('.', '/') + '/' + fileName;
52    } // -- getQualifiedFileName
53  
54    /**
55     * Gets the package name of the given class name.
56     *
57     * @param className The class name to retrieve the package name from.
58     * @return The package name or the empty String if <code>className</code> is <code>null</code> or
59     *         does not contain a package.
60     */
61    public static String getPackageName(String className) {
62      if (className == null) {
63        return "";
64      }
65  
66      int idx = className.lastIndexOf('.');
67      if (idx >= 0) {
68        return className.substring(0, idx);
69      }
70      return "";
71    } // -- getPackageName
72  
73    /**
74     * Compares the two strings for equality. A Null and empty strings are considered equal.
75     *
76     * @return true if the two strings are considered equal.
77     */
78    public static boolean namespaceEquals(String ns1, String ns2) {
79      if (ns1 == null) {
80        return ns2 == null || ns2.length() == 0;
81      }
82  
83      if (ns2 == null) {
84        return ns1.length() == 0;
85      }
86  
87      return ns1.equals(ns2);
88    } // -- namespaceEquals
89  
90    /**
91     * Gets the <code>ClassLoader</code> that's actually to be used (e.g. for loading resources).<br>
92     * The actual <code>ClassLoader</code> is determined in the following way: <lu>
93     * <li>If the passed in "preferred" loader is not <code>null</code>, it is used.
94     * <li>If the loader of this XMLClassDescriptor is not <code>null</code>, it is used.
95     * <li>The context class loader of the current thread is used. </lu>
96     *
97     * @param loader The "preferred" <code>ClassLoader</code>.
98     * @return The loader to be used.
99     */
100   public static ClassLoader getClassLoader(ClassLoader loader) {
101     if (loader != null) {
102       return loader;
103     }
104 
105     return Thread.currentThread().getContextClassLoader();
106   } // -- getClassLoader
107 
108   /**
109    * Capsulates the ClassLoader.loadClass method to throw no exceptions but return null instead. Any
110    * exception caught are logged with info severity.
111    * 
112    * @param classLoader the class loader to use
113    * @param className the class to load
114    * @return the loaded Class or null
115    */
116   public static Class loadClass(ClassLoader classLoader, String className) {
117     if (classLoader == null) {
118       String message = "Argument class loader must not be null";
119       LOG.warn(message);
120       throw new IllegalArgumentException(message);
121     }
122 
123     if ((className == null) || (className.length() == 0)) {
124       LOG.debug("Name of class to load is null or empty -> ignored!");
125       return null;
126     }
127 
128     try {
129       return classLoader.loadClass(className);
130     } catch (ClassNotFoundException e) {
131       if (LOG.isDebugEnabled()) {
132         LOG.debug("Ignored problem at loading class: " + className + " through class loader: "
133             + classLoader + ", exception: " + e);
134       }
135       return null;
136     }
137   } // -- loadClass
138 }