001    /*
002     * Copyright (C) 2006-2007 the original author or authors.
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.codehaus.gmaven.runtime.support.stubgen.parser;
018    
019    import java.net.URL;
020    
021    /**
022     * Pseudo-enum for Groovy/Java source type.
023     *
024     * @version $Id: SourceType.java 18 2009-07-16 09:39:40Z user57 $
025     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
026     */
027    public final class SourceType
028    {
029        public static final SourceType GROOVY = new SourceType("GROOVY", 0);
030    
031        public static final String GROOVY_EXT = ".groovy";
032    
033        public static final String GROOVY_NAME = "GROOVY";
034    
035        public static final int GROOVY_CODE = 0;
036    
037        public static final SourceType JAVA = new SourceType("JAVA", 1);
038    
039        public static final String JAVA_EXT = ".java";
040    
041        public static final String JAVA_NAME = "JAVA";
042    
043        public static final int JAVA_CODE = 1;
044    
045        public static final String SOURCE_TYPE_TAG = "source-type";
046    
047        public final String name;
048    
049        public final int code;
050    
051        private SourceType(final String name, final int code) {
052            this.name = name;
053            this.code = code;
054        }
055    
056        public String toString() {
057            return name;
058        }
059    
060        public static SourceType forURL(final URL url) {
061            String name = url.getFile().toLowerCase();
062    
063            if (name.endsWith(GROOVY_EXT)) {
064                return GROOVY;
065            }
066            if (name.endsWith(JAVA_EXT)) {
067                return JAVA;
068            }
069            else {
070                throw new IllegalArgumentException("Unable to determine source type from URL: " + url);
071            }
072        }
073    
074        public static SourceType forName(final String name) {
075            if (name.equals(GROOVY_NAME)) {
076                return GROOVY;
077            }
078            if (name.equals(JAVA_NAME)) {
079                return JAVA;
080            }
081            else {
082                throw new IllegalArgumentException("Unable to determine source type from name: " + name);
083            }
084        }
085    
086        public static SourceType forCode(final int code) {
087            if (code == GROOVY_CODE) {
088                return GROOVY;
089            }
090            if (code == JAVA_CODE) {
091                return JAVA;
092            }
093            else {
094                throw new IllegalArgumentException("Unable to determine source type from code: " + code);
095            }
096        }
097    }