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
22 /**
23 * Mainly common path manipultation helper methods
24 * NOT FOR USE OUTSIDE OF JCI
25 *
26 * @author tcurdt
27 */
28 public final class ConversionUtils {
29
30 /**
31 * Please do not use - internal
32 * org/my/Class.xxx -> org.my.Class
33 */
34 public static String convertResourceToClassName( final String pResourceName ) {
35 return ConversionUtils.stripExtension(pResourceName).replace('/', '.');
36 }
37
38 /**
39 * Please do not use - internal
40 * org.my.Class -> org/my/Class.class
41 */
42 public static String convertClassToResourcePath( final String pName ) {
43 return pName.replace('.', '/') + ".class";
44 }
45
46 /**
47 * Please do not use - internal
48 * org/my/Class.xxx -> org/my/Class
49 */
50 public static String stripExtension( final String pResourceName ) {
51 final int i = pResourceName.lastIndexOf('.');
52 if (i < 0) {
53 return pResourceName;
54 }
55 final String withoutExtension = pResourceName.substring(0, i);
56 return withoutExtension;
57 }
58
59 public static String toJavaCasing(final String pName) {
60 final char[] name = pName.toLowerCase().toCharArray();
61 name[0] = Character.toUpperCase(name[0]);
62 return new String(name);
63 }
64
65 /*
66 public static String clazzName( final File base, final File file ) {
67 final int rootLength = base.getAbsolutePath().length();
68 final String absFileName = file.getAbsolutePath();
69 final int p = absFileName.lastIndexOf('.');
70 final String relFileName = absFileName.substring(rootLength + 1, p);
71 final String clazzName = relFileName.replace(File.separatorChar, '.');
72 return clazzName;
73 }
74 */
75 public static String relative( final File base, final File file ) {
76 final int rootLength = base.getAbsolutePath().length();
77 final String absFileName = file.getAbsolutePath();
78 final String relFileName = absFileName.substring(rootLength + 1);
79 return relFileName;
80 }
81
82 /**
83 * a/b/c.java -> a/b/c.java
84 * a\b\c.java -> a/b/c.java
85 * @param pFileName
86 * @return
87 */
88 public static String getResourceNameFromFileName( final String pFileName ) {
89 if ('/' == File.separatorChar) {
90 return pFileName;
91 }
92
93 return pFileName.replace(File.separatorChar, '/');
94 }
95
96 }