001    /*--------------------------------------------------------------------------+
002       $Id: ELanguage.java 11100 2007-07-24 11:52:41Z juergens $
003     |                                                                          |
004     | Copyright 2005-2007 Technische Universitaet Muenchen                     |
005     |                                                                          |
006     | Licensed under the Apache License, Version 2.0 (the "License");          |
007     | you may not use this file except in compliance with the License.         |
008     | You may obtain a copy of the License at                                  |
009     |                                                                          |
010     |    http://www.apache.org/licenses/LICENSE-2.0                            |
011     |                                                                          |
012     | Unless required by applicable law or agreed to in writing, software      |
013     | distributed under the License is distributed on an "AS IS" BASIS,        |
014     | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015     | See the License for the specific language governing permissions and      |
016     | limitations under the License.                                           |
017     +--------------------------------------------------------------------------*/
018    package edu.tum.cs.scanner;
019    
020    /**
021     * Enumeration class for the languages support by the scanner framework.
022     * 
023     * @author Florian Deissenboeck
024     * @author Elmar Juergens
025     * @author Tobias Simon
026     * @author Jonathan Streit
027     * @author $Author: juergens $
028     * @version $Revision: 11100 $
029     * @levd.rating GREEN Rev: 11100
030     */
031    public enum ELanguage {
032            /** Java */
033            JAVA,
034    
035            /** C/C++ */
036            CPP,
037    
038            /** Visual Basic */
039            VB,
040    
041            /** PL/I */
042            PL1,
043    
044            /** COBOL */
045            COBOL,
046    
047            /** C# */
048            CS;
049    
050            /** Gets the {@link ELanguage} value corresonding to a file extension */
051            public static ELanguage fromFileExtension(String path) {
052                    if (path == null) {
053                            throw new IllegalArgumentException("Path must not be null");
054                    }
055    
056                    int pointPos = path.lastIndexOf('.');
057                    if (pointPos == -1) {
058                            throw new IllegalArgumentException("Filename has no extension");
059                    }
060                    String extension = path.substring(pointPos + 1);
061    
062                    if (extension.equalsIgnoreCase("java")) {
063                            return JAVA;
064                    } else if (extension.equalsIgnoreCase("cpp")
065                                    || extension.equalsIgnoreCase("c")) {
066                            return CPP;
067                    }
068                    if (extension.equalsIgnoreCase("vb")) {
069                            return VB;
070                    }
071                    if (extension.equalsIgnoreCase("pl1")
072                                    || extension.equalsIgnoreCase("pli")) {
073                            return PL1;
074                    }
075                    if (extension.equalsIgnoreCase("cbl")
076                                    || extension.equalsIgnoreCase("cob")
077                                    || extension.equalsIgnoreCase("cobol")) {
078                            return COBOL;
079                    }
080                    if (extension.equalsIgnoreCase("cs")) {
081                            return CS;
082                    }
083    
084                    throw new IllegalStateException("Extension '" + extension + "' unknown");
085            }
086    }