001    // Copyright 2005-2006 Ferdinand Prantl <prantl@users.sourceforge.net>
002    // Copyright 2001-2004 The Apache Software Foundation
003    // All rights reserved.
004    //
005    // Licensed under the Apache License, Version 2.0 (the "License");
006    // you may not use this file except in compliance with the License.
007    // 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    // See http://ant-eclipse.sourceforge.net for the most recent version
018    // and more information.
019    
020    package prantl.ant.eclipse;
021    
022    import java.util.HashSet;
023    import java.util.Hashtable;
024    
025    import org.apache.tools.ant.BuildException;
026    
027    /**
028     * Configures the component preferences file <tt>.settings/org.eclipse.jdt.core.prefs</tt>
029     * on the high level using attributes for the typical constellations of variable values.
030     * 
031     * @since Ant-Eclipse 1.0
032     * @author Ferdinand Prantl &lt;prantl@users.sourceforge.net&gt;
033     */
034    public class OrgEclipseJdtCorePreferencesElement extends PreferencesElement {
035    
036        private static final String ELEMENT = "jdtcore";
037    
038        private static final String COMPILERCOMPLIANCE_ATTRIBUTE = "compilercompliance";
039    
040        private static final String COMPILERCOMPLIANCE_NAME = "org.eclipse.jdt.core.compiler.compliance";
041    
042        private static final HashSet COMPILERCOMPLIANCE_VALUES = new HashSet();
043    
044        private static final String[] ORGECLIPSEJDTCORE_NAMES = {
045                "org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode",
046                "org.eclipse.jdt.core.compiler.codegen.targetPlatform",
047                "org.eclipse.jdt.core.compiler.codegen.unusedLocal",
048                "org.eclipse.jdt.core.compiler.compliance",
049                "org.eclipse.jdt.core.compiler.debug.lineNumber",
050                "org.eclipse.jdt.core.compiler.debug.localVariable",
051                "org.eclipse.jdt.core.compiler.debug.sourceFile",
052                "org.eclipse.jdt.core.compiler.problem.assertIdentifier",
053                "org.eclipse.jdt.core.compiler.problem.enumIdentifier",
054                "org.eclipse.jdt.core.compiler.source" };
055    
056        private static final Hashtable ORGECLIPSEJDTCORE_DEFAULTS = new Hashtable();
057    
058        /**
059         * Returns the name of the package these preferences belong to.
060         * 
061         * @return The name of the package these preferences belong to.
062         */
063        static final String getPackageName() {
064            return "org.eclipse.jdt.core";
065        }
066    
067        /**
068         * Creates a new instance of the element for the file with preferences for
069         * org.eclipse.jdt.core.
070         * 
071         * @param parent
072         *        The parent settings element of this preferences one.
073         * @since Ant-Eclipse 1.0
074         */
075        public OrgEclipseJdtCorePreferencesElement(SettingsElement parent) {
076            super(parent);
077            internalSetName(getPackageName());
078            COMPILERCOMPLIANCE_VALUES.add("1.3");
079            COMPILERCOMPLIANCE_VALUES.add("1.4");
080            COMPILERCOMPLIANCE_VALUES.add("5.0");
081            COMPILERCOMPLIANCE_VALUES.add("6.0");
082            ORGECLIPSEJDTCORE_DEFAULTS.put("1.3", new String[] { "enabled", // org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode
083                    "1.1", // org.eclipse.jdt.core.compiler.codegen.targetPlatform
084                    "preserve", // org.eclipse.jdt.core.compiler.codegen.unusedLocal
085                    "1.3", // org.eclipse.jdt.core.compiler.compliance
086                    "generate", // org.eclipse.jdt.core.compiler.debug.lineNumber
087                    "generate", // org.eclipse.jdt.core.compiler.debug.localVariable
088                    "generate", // org.eclipse.jdt.core.compiler.debug.sourceFile
089                    "ignore", // org.eclipse.jdt.core.compiler.problem.assertIdentifier
090                    "ignore", // org.eclipse.jdt.core.compiler.problem.enumIdentifier
091                    "1.3" // org.eclipse.jdt.core.compiler.source
092            });
093            ORGECLIPSEJDTCORE_DEFAULTS.put("1.4", new String[] { "enabled", // org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode
094                    "1.2", // org.eclipse.jdt.core.compiler.codegen.targetPlatform
095                    "preserve", // org.eclipse.jdt.core.compiler.codegen.unusedLocal
096                    "1.4", // org.eclipse.jdt.core.compiler.compliance
097                    "generate", // org.eclipse.jdt.core.compiler.debug.lineNumber
098                    "generate", // org.eclipse.jdt.core.compiler.debug.localVariable
099                    "generate", // org.eclipse.jdt.core.compiler.debug.sourceFile
100                    "warning", // org.eclipse.jdt.core.compiler.problem.assertIdentifier
101                    "warning", // org.eclipse.jdt.core.compiler.problem.enumIdentifier
102                    "1.3" // org.eclipse.jdt.core.compiler.source
103            });
104            ORGECLIPSEJDTCORE_DEFAULTS.put("5.0", new String[] { "enabled", // org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode
105                    "1.5", // org.eclipse.jdt.core.compiler.codegen.targetPlatform
106                    "preserve", // org.eclipse.jdt.core.compiler.codegen.unusedLocal
107                    "1.5", // org.eclipse.jdt.core.compiler.compliance
108                    "generate", // org.eclipse.jdt.core.compiler.debug.lineNumber
109                    "generate", // org.eclipse.jdt.core.compiler.debug.localVariable
110                    "generate", // org.eclipse.jdt.core.compiler.debug.sourceFile
111                    "error", // org.eclipse.jdt.core.compiler.problem.assertIdentifier
112                    "error", // org.eclipse.jdt.core.compiler.problem.enumIdentifier
113                    "1.5" // org.eclipse.jdt.core.compiler.source
114            });
115            ORGECLIPSEJDTCORE_DEFAULTS.put("6.0", new String[] { "enabled", // org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode
116                    "1.6", // org.eclipse.jdt.core.compiler.codegen.targetPlatform
117                    "preserve", // org.eclipse.jdt.core.compiler.codegen.unusedLocal
118                    "1.6", // org.eclipse.jdt.core.compiler.compliance
119                    "generate", // org.eclipse.jdt.core.compiler.debug.lineNumber
120                    "generate", // org.eclipse.jdt.core.compiler.debug.localVariable
121                    "generate", // org.eclipse.jdt.core.compiler.debug.sourceFile
122                    "error", // org.eclipse.jdt.core.compiler.problem.assertIdentifier
123                    "error", // org.eclipse.jdt.core.compiler.problem.enumIdentifier
124                    "1.6" // org.eclipse.jdt.core.compiler.source
125            });
126        }
127    
128        /**
129         * Returns the line separator (default is inherited from the workspace settings and
130         * not set here in the file).
131         * 
132         * @return The line separator (default is inherited from the workspace settings and
133         *         not set here in the file).
134         */
135        public String getCompilerCompliance() {
136            VariableElement variable = getVariable(COMPILERCOMPLIANCE_NAME);
137            return variable == null ? null : variable.getValue();
138        }
139    
140        /**
141         * Sets the version of the Eclipse preferences. The default value should be left and
142         * not set explicitely.
143         * 
144         * @param value
145         *        A valid line separator.
146         * @since Ant-Eclipse 1.0
147         */
148        public void setCompilerCompliance(String value) {
149            if (!COMPILERCOMPLIANCE_VALUES.contains(value))
150                throw new BuildException("The attribute \"" + COMPILERCOMPLIANCE_ATTRIBUTE
151                        + "\" (variable \"" + COMPILERCOMPLIANCE_NAME
152                        + "\") has an invalid value \"" + value + "\". Valid values are "
153                        + getValidCompilerComplianceValues() + ".");
154            internalCreateVariable(COMPILERCOMPLIANCE_NAME, value);
155        }
156    
157        /**
158         * Returns allowed values for the variable org.eclipse.jdt.core.compiler.compliance.
159         * 
160         * @return A new string with allowed values for the variable
161         *         org.eclipse.jdt.core.compiler.compliance.
162         * @since Ant-Eclipse 1.0
163         */
164        String getValidCompilerComplianceValues() {
165            return getValidValues(COMPILERCOMPLIANCE_VALUES);
166        }
167    
168        /**
169         * Performs the validation of the element at the time when the whole build file was
170         * parsed checking the content of the element and possibly adding mandatory variables
171         * with default settings.
172         * 
173         * @since Ant-Eclipse 1.0
174         */
175        public void validate() {
176            VariableElement variable = getVariable(COMPILERCOMPLIANCE_NAME);
177            if (variable == null)
178                throw new BuildException("The attribute \"" + COMPILERCOMPLIANCE_ATTRIBUTE
179                        + "\" (variable \"" + COMPILERCOMPLIANCE_NAME
180                        + "\") was missing in the element \"" + ELEMENT + "\".");
181            String[] defaults = (String[]) ORGECLIPSEJDTCORE_DEFAULTS
182                    .get(variable.getValue());
183            for (int i = 0; i < ORGECLIPSEJDTCORE_NAMES.length; ++i)
184                internalAddVariable(ORGECLIPSEJDTCORE_NAMES[i], defaults[i]);
185    
186            super.validate();
187        }
188    
189    }