1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.jci.utils;
19
20 import java.io.File;
21 import java.util.Locale;
22
23 /**
24 * Mainly common path manipultation helper methods
25 * NOT FOR USE OUTSIDE OF JCI
26 *
27 * @author tcurdt
28 */
29 public final class ConversionUtils {
30
31 /**
32 * Please do not use - internal
33 * org/my/Class.xxx -> org.my.Class
34 */
35 public static String convertResourceToClassName( final String pResourceName ) {
36 return ConversionUtils.stripExtension(pResourceName).replace('/', '.');
37 }
38
39 /**
40 * Please do not use - internal
41 * org.my.Class -> org/my/Class.class
42 */
43 public static String convertClassToResourcePath( final String pName ) {
44 return pName.replace('.', '/') + ".class";
45 }
46
47 /**
48 * Please do not use - internal
49 * org/my/Class.xxx -> org/my/Class
50 */
51 public static String stripExtension( final String pResourceName ) {
52 final int i = pResourceName.lastIndexOf('.');
53 if (i < 0) {
54 return pResourceName;
55 }
56 final String withoutExtension = pResourceName.substring(0, i);
57 return withoutExtension;
58 }
59
60 public static String toJavaCasing(final String pName) {
61 final char[] name = pName.toLowerCase(Locale.US).toCharArray();
62 name[0] = Character.toUpperCase(name[0]);
63 return new String(name);
64 }
65
66 /*
67 public static String clazzName( final File base, final File file ) {
68 final int rootLength = base.getAbsolutePath().length();
69 final String absFileName = file.getAbsolutePath();
70 final int p = absFileName.lastIndexOf('.');
71 final String relFileName = absFileName.substring(rootLength + 1, p);
72 final String clazzName = relFileName.replace(File.separatorChar, '.');
73 return clazzName;
74 }
75 */
76 public static String relative( final File base, final File file ) {
77 final int rootLength = base.getAbsolutePath().length();
78 final String absFileName = file.getAbsolutePath();
79 final String relFileName = absFileName.substring(rootLength + 1);
80 return relFileName;
81 }
82
83 /**
84 * a/b/c.java -> a/b/c.java
85 * a\b\c.java -> a/b/c.java
86 * @param pFileName
87 * @return the converted name
88 */
89 public static String getResourceNameFromFileName( final String pFileName ) {
90 if ('/' == File.separatorChar) {
91 return pFileName;
92 }
93
94 return pFileName.replace(File.separatorChar, '/');
95 }
96
97 }