org.exolab.castor.xml.util.resolvers.ResolveHelpers Maven / Gradle / Ivy
/*
* Copyright 2007 Joachim Grueneis
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.exolab.castor.xml.util.resolvers;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Some helpers used by the resolver commands. This is a utility class which is NOT meant to be
* instantiated.
*
* @author Joachim Grueneis
* @version $Revision$ $Date$
* @since 1.2
*/
public final class ResolveHelpers {
private static final Log LOG = LogFactory.getLog(ResolveHelpers.class);
/**
* A private constructor as this is a utility class without an own state.
*/
private ResolveHelpers() {}
/**
* Qualifies the given fileName
with the given packageName
and returns
* the resulting file path.
* If packageName
is null
or a zero-length String, this method will
* return fileName
.
*
* @param fileName The file name to be qualified.
* @param packageName The package name to be used for qualifying.
* @return The qualified file path.
*/
public static String getQualifiedFileName(String fileName, String packageName) {
if (packageName == null || packageName.isEmpty()) {
return fileName;
}
return packageName.replace('.', '/') + '/' + fileName;
} // -- getQualifiedFileName
/**
* Gets the package name of the given class name.
*
* @param className The class name to retrieve the package name from.
* @return The package name or the empty String if className
is null
or
* does not contain a package.
*/
public static String getPackageName(String className) {
if (className == null) {
return "";
}
int idx = className.lastIndexOf('.');
if (idx >= 0) {
return className.substring(0, idx);
}
return "";
} // -- getPackageName
/**
* Compares the two strings for equality. A Null and empty strings are considered equal.
*
* @return true if the two strings are considered equal.
*/
public static boolean namespaceEquals(String ns1, String ns2) {
if (ns1 == null) {
return ns2 == null || ns2.length() == 0;
}
if (ns2 == null) {
return ns1.length() == 0;
}
return ns1.equals(ns2);
} // -- namespaceEquals
/**
* Gets the ClassLoader
that's actually to be used (e.g. for loading resources).
* The actual ClassLoader
is determined in the following way:
* If the passed in "preferred" loader is not null
, it is used.
* If the loader of this XMLClassDescriptor is not null
, it is used.
* The context class loader of the current thread is used.
*
* @param loader The "preferred" ClassLoader
.
* @return The loader to be used.
*/
public static ClassLoader getClassLoader(ClassLoader loader) {
if (loader != null) {
return loader;
}
return Thread.currentThread().getContextClassLoader();
} // -- getClassLoader
/**
* Capsulates the ClassLoader.loadClass method to throw no exceptions but return null instead. Any
* exception caught are logged with info severity.
*
* @param classLoader the class loader to use
* @param className the class to load
* @return the loaded Class or null
*/
public static Class loadClass(ClassLoader classLoader, String className) {
if (classLoader == null) {
String message = "Argument class loader must not be null";
LOG.warn(message);
throw new IllegalArgumentException(message);
}
if ((className == null) || (className.length() == 0)) {
LOG.debug("Name of class to load is null or empty -> ignored!");
return null;
}
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Ignored problem at loading class: " + className + " through class loader: "
+ classLoader + ", exception: " + e);
}
return null;
}
} // -- loadClass
}