001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.resolve;
018    
019    import org.jetbrains.jet.lang.parsing.JetScriptDefinition;
020    import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider;
021    import org.jetbrains.jet.lang.psi.JetFile;
022    import org.jetbrains.jet.lang.psi.JetPackageDirective;
023    
024    public class ScriptNameUtil {
025        private ScriptNameUtil() {
026        }
027    
028        public static String classNameForScript(JetFile file) {
029            JetScriptDefinition scriptDefinition = JetScriptDefinitionProvider.getInstance(file.getProject()).findScriptDefinition(file);
030    
031            String name = file.getName();
032            int index = name.lastIndexOf('/');
033            if(index != -1)
034                name = name.substring(index+1);
035            if(name.endsWith(scriptDefinition.getExtension()))
036                name = name.substring(0, name.length()-scriptDefinition.getExtension().length());
037            else {
038                index = name.indexOf('.');
039                if(index != -1)
040                    name = name.substring(0,index);
041            }
042            name = Character.toUpperCase(name.charAt(0)) + (name.length() == 0 ? "" : name.substring(1));
043            JetPackageDirective directive = file.getPackageDirective();
044            if(directive != null && directive.getName().length() > 0) {
045                name = directive.getName().replace('.','/') + "/" + name;
046            }
047            return name;
048        }
049    }