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