    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.compilers;
    019
    020    import java.util.HashMap;
    021    import java.util.Map;
    022
    023    import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
    024
    025    /**
    026     * Native Eclipse compiler settings
    027     *
    028     * @author tcurdt
    029     */
    030    public final class EclipseJavaCompilerSettings extends JavaCompilerSettings {
    031
    032        final private Map<String, String> defaultEclipseSettings = new HashMap<String, String>();
    033
    034        public EclipseJavaCompilerSettings() {
    035            defaultEclipseSettings.put(CompilerOptions.OPTION_LineNumberAttribute, CompilerOptions.GENERATE);
    036            defaultEclipseSettings.put(CompilerOptions.OPTION_SourceFileAttribute, CompilerOptions.GENERATE);
    037            defaultEclipseSettings.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE);
    038            defaultEclipseSettings.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.GENERATE);
    039        }
    040
    041        public EclipseJavaCompilerSettings( final JavaCompilerSettings pSettings ) {
    042            super(pSettings);
    043
    044            if (pSettings instanceof EclipseJavaCompilerSettings) {
    045                    defaultEclipseSettings.putAll(((EclipseJavaCompilerSettings)pSettings).toNativeSettings());
    046            }
    047        }
    048
    049        public EclipseJavaCompilerSettings( final Map<String, String> pMap ) {
    050            defaultEclipseSettings.putAll(pMap);
    051        }
    052
    053        private static Map<String, String> nativeVersions = new HashMap<String, String>() {
    054                    private static final long serialVersionUID = 1L;
    055            {
    056            put("1.1", CompilerOptions.VERSION_1_1);
    057            put("1.2", CompilerOptions.VERSION_1_2);
    058            put("1.3", CompilerOptions.VERSION_1_3);
    059            put("1.4", CompilerOptions.VERSION_1_4);
    060            put("1.5", CompilerOptions.VERSION_1_5);
    061            put("1.6", CompilerOptions.VERSION_1_6);
    062            put("1.7", CompilerOptions.VERSION_1_7);
    063        }};
    064
    065        private String toNativeVersion( final String pVersion ) {
    066            final String nativeVersion = nativeVersions.get(pVersion);
    067
    068            if (nativeVersion == null) {
    069                    throw new RuntimeException("unknown version " + pVersion);
    070            }
    071
    072            return nativeVersion;
    073        }
    074
    075        Map<String, String> toNativeSettings() {
    076            final Map<String, String> map = new HashMap<String, String>(defaultEclipseSettings);
    077
    078            map.put(CompilerOptions.OPTION_SuppressWarnings, isWarnings()?CompilerOptions.GENERATE:CompilerOptions.DO_NOT_GENERATE);
    079            map.put(CompilerOptions.OPTION_ReportDeprecation, isDeprecations()?CompilerOptions.GENERATE:CompilerOptions.DO_NOT_GENERATE);
    080            map.put(CompilerOptions.OPTION_TargetPlatform, toNativeVersion(getTargetVersion()));
    081            map.put(CompilerOptions.OPTION_Source, toNativeVersion(getSourceVersion()));
    082            map.put(CompilerOptions.OPTION_Encoding, getSourceEncoding());
    083
    084            return map;
    085        }
    086
    087        @Override
    088        public String toString() {
    089            return toNativeSettings().toString();
    090        }
    091    }