001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.jci.utils;
019    
020    import java.io.File;
021    import java.util.Locale;
022    
023    /**
024     * Mainly common path manipultation helper methods
025     * NOT FOR USE OUTSIDE OF JCI
026     * 
027     * @author tcurdt
028     */
029    public final class ConversionUtils {
030    
031        /**
032         * Please do not use - internal
033         * org/my/Class.xxx -> org.my.Class
034         */
035        public static String convertResourceToClassName( final String pResourceName ) {
036            return ConversionUtils.stripExtension(pResourceName).replace('/', '.');
037        }
038    
039        /**
040         * Please do not use - internal
041         * org.my.Class -> org/my/Class.class
042         */
043        public static String convertClassToResourcePath( final String pName ) {
044            return pName.replace('.', '/') + ".class";
045        }
046    
047        /**
048         * Please do not use - internal
049         * org/my/Class.xxx -> org/my/Class
050         */
051        public static String stripExtension( final String pResourceName ) {
052            final int i = pResourceName.lastIndexOf('.');
053            if (i < 0) {
054                return pResourceName;
055            }
056            final String withoutExtension = pResourceName.substring(0, i);
057            return withoutExtension;
058        }
059    
060        public static String toJavaCasing(final String pName) {
061            final char[] name = pName.toLowerCase(Locale.US).toCharArray();
062            name[0] = Character.toUpperCase(name[0]);
063            return new String(name);
064        }
065    
066    /*
067        public static String clazzName( final File base, final File file ) {
068            final int rootLength = base.getAbsolutePath().length();
069            final String absFileName = file.getAbsolutePath();
070            final int p = absFileName.lastIndexOf('.');
071            final String relFileName = absFileName.substring(rootLength + 1, p);
072            final String clazzName = relFileName.replace(File.separatorChar, '.');
073            return clazzName;
074        }
075    */
076        public static String relative( final File base, final File file ) {
077            final int rootLength = base.getAbsolutePath().length();
078            final String absFileName = file.getAbsolutePath();
079            final String relFileName = absFileName.substring(rootLength + 1);
080            return relFileName;
081        }
082    
083        /**
084         * a/b/c.java -> a/b/c.java
085         * a\b\c.java -> a/b/c.java
086         * @param pFileName
087         * @return the converted name
088         */
089        public static String getResourceNameFromFileName( final String pFileName ) {
090            if ('/' == File.separatorChar) {
091                return pFileName;
092            }
093    
094            return pFileName.replace(File.separatorChar, '/');
095        }
096    
097    }