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.JetNamespaceHeader;
023    
024    public class ScriptNameUtil {
025        public static final String LAST_EXPRESSION_VALUE_FIELD_NAME = "rv";
026    
027        private ScriptNameUtil() {
028        }
029    
030        public static String classNameForScript(JetFile file) {
031            JetScriptDefinition scriptDefinition = JetScriptDefinitionProvider.getInstance(file.getProject()).findScriptDefinition(file);
032    
033            String name = file.getName();
034            int index = name.lastIndexOf('/');
035            if(index != -1)
036                name = name.substring(index+1);
037            if(name.endsWith(scriptDefinition.getExtension()))
038                name = name.substring(0, name.length()-scriptDefinition.getExtension().length());
039            else {
040                index = name.indexOf('.');
041                if(index != -1)
042                    name = name.substring(0,index);
043            }
044            name = Character.toUpperCase(name.charAt(0)) + (name.length() == 0 ? "" : name.substring(1));
045            JetNamespaceHeader header = file.getNamespaceHeader();
046            if(header != null && header.getName().length() > 0) {
047                name = header.getName().replace('.','/') + "/" + name;
048            }
049            return name;
050        }
051    }