01: package org.drools.util;
02:
03: import java.io.File;
04:
05: public final class ClassUtils {
06:
07: /**
08: * Please do not use - internal
09: * org/my/Class.xxx -> org.my.Class
10: */
11: public static String convertResourceToClassName(
12: final String pResourceName) {
13: return ClassUtils.stripExtension(pResourceName).replace('/',
14: '.');
15: }
16:
17: /**
18: * Please do not use - internal
19: * org.my.Class -> org/my/Class.class
20: */
21: public static String convertClassToResourcePath(final String pName) {
22: return pName.replace('.', '/') + ".class";
23: }
24:
25: /**
26: * Please do not use - internal
27: * org/my/Class.xxx -> org/my/Class
28: */
29: public static String stripExtension(final String pResourceName) {
30: final int i = pResourceName.lastIndexOf('.');
31: final String withoutExtension = pResourceName.substring(0, i);
32: return withoutExtension;
33: }
34:
35: public static String toJavaCasing(final String pName) {
36: final char[] name = pName.toLowerCase().toCharArray();
37: name[0] = Character.toUpperCase(name[0]);
38: return new String(name);
39: }
40:
41: public static String clazzName(final File base, final File file) {
42: final int rootLength = base.getAbsolutePath().length();
43: final String absFileName = file.getAbsolutePath();
44: final int p = absFileName.lastIndexOf('.');
45: final String relFileName = absFileName.substring(
46: rootLength + 1, p);
47: final String clazzName = relFileName.replace(
48: File.separatorChar, '.');
49: return clazzName;
50: }
51:
52: public static String relative(final File base, final File file) {
53: final int rootLength = base.getAbsolutePath().length();
54: final String absFileName = file.getAbsolutePath();
55: final String relFileName = absFileName
56: .substring(rootLength + 1);
57: return relFileName;
58: }
59:
60: public static String canonicalName(Class clazz) {
61: StringBuffer name = new StringBuffer();
62:
63: if (clazz.isArray()) {
64: name.append(canonicalName(clazz.getComponentType()));
65: name.append("[]");
66: } else if (clazz.getDeclaringClass() == null) {
67: name.append(clazz.getName());
68: } else {
69: name.append(canonicalName(clazz.getDeclaringClass()));
70: name.append(".");
71: name.append(clazz.getName().substring(
72: clazz.getDeclaringClass().getName().length() + 1));
73: }
74:
75: return name.toString();
76: }
77:
78: }
|